-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: Azure endpoint return all users from ioet #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cxcarvaj
merged 6 commits into
master
from
TT-537-azure-endpoint-doesnt-return-the-complete-users
Feb 16, 2022
+66
−49
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
67c6f70
Fix: Azure endpoint return all users from ioet
cxcarvaj 504a0f3
Fix: Azure endpoint return all users from ioet
cxcarvaj 55a06c2
refactor: Refactor of code smells
cxcarvaj bbf68f2
style: Changes requested by review
cxcarvaj 2a1dc12
style: Changes requested by reviews
cxcarvaj 462117f
style: Changes requested by Sandros review
cxcarvaj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Fix: Azure endpoint return all users from ioet
- Loading branch information
commit 67c6f701a31d322f44c0dd09ec08bb45d2f708e9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,29 @@ | ||
| const _ = require("lodash"); | ||
| const CosmosClient = require("@azure/cosmos").CosmosClient; | ||
| const config = require("./config"); | ||
| const TimeEntry = require("./time_entry"); | ||
| const MsalClient = require("./msal_client"); | ||
| const TimeEntryDao = require("./time_entry_dao"); | ||
| const SlackClient = require("./slack_client"); | ||
| const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require("./constants"); | ||
| const _ = require('lodash'); | ||
| const CosmosClient = require('@azure/cosmos').CosmosClient; | ||
| const config = require('./config'); | ||
| const TimeEntry = require('./time_entry'); | ||
| const MsalClient = require('./msal_client'); | ||
| const TimeEntryDao = require('./time_entry_dao'); | ||
| const SlackClient = require('./slack_client'); | ||
| const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require('./constants'); | ||
|
|
||
| const doClockOut = async (context) => { | ||
| context.log( | ||
| console.log( | ||
| `I am going to check how many entries were not clocked out ${new Date()}` | ||
| ); | ||
|
|
||
| const { endpoint, key, databaseId } = config; | ||
| const client = new CosmosClient({ endpoint, key }); | ||
| const database = client.database(databaseId); | ||
| const container = database.container("time_entry"); | ||
| const container = database.container('time_entry'); | ||
cxcarvaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const timeEntryDao = new TimeEntryDao(database); | ||
|
|
||
| const response = await MsalClient.findUsersInMS(); | ||
| const users = response.data.value; | ||
| const users = response.data; | ||
| const slackUsers = await SlackClient.findUsersInSlack(); | ||
|
|
||
| const { resources: entries } = await timeEntryDao.getEntriesWithNoEndDate(); | ||
| context.log(`Checking for time-entries that need to be clocked out`); | ||
| console.log(`Checking for time-entries that need to be clocked out`); | ||
|
|
||
| let totalClockOutsExecuted = 0; | ||
|
|
||
|
|
@@ -32,40 +32,43 @@ const doClockOut = async (context) => { | |
| const timeEntry = new TimeEntry(timeEntryAsJson); | ||
| const { userName, userEmail } = findUserData(users, timeEntry.timeEntry.owner_id); | ||
| const userId = findSlackUserId(slackUsers, userEmail); | ||
|
|
||
| if (timeEntry.needsToBeClockedOut()) { | ||
| if (userId) { | ||
| SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE.replace('%user_name%', userName)); | ||
| if( userEmail === '[email protected]'){ | ||
| if (timeEntry.needsToBeClockedOut()) { | ||
| if (userId) { | ||
| SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE.replace('%user_name%', userName)); | ||
| } | ||
| timeEntryAsJson.end_date = timeEntry.getTimeToClockOut(); | ||
| await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson); | ||
| totalClockOutsExecuted++; | ||
| } | ||
| timeEntryAsJson.end_date = timeEntry.getTimeToClockOut(); | ||
| await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson); | ||
| totalClockOutsExecuted++; | ||
| } | ||
|
|
||
| else if (timeEntry.needsToBeClockedOutMidnight()) { | ||
| if (userId) { | ||
| SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT.replace('%user_name%', userName)); | ||
|
|
||
| else if (timeEntry.needsToBeClockedOutMidnight()) { | ||
| if (userId) { | ||
| SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT.replace('%user_name%', userName)); | ||
| } | ||
| timeEntryAsJson.end_date = timeEntry.getTimeToClockOutMidnight(); | ||
| await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson); | ||
| totalClockOutsExecuted++; | ||
| } | ||
| timeEntryAsJson.end_date = timeEntry.getTimeToClockOutMidnight(); | ||
| await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson); | ||
| totalClockOutsExecuted++; | ||
| } | ||
|
|
||
| }) | ||
| ); | ||
|
|
||
| context.log( | ||
| console.log( | ||
| `I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...` | ||
| ); | ||
| }; | ||
|
|
||
|
|
||
| const findUserData = (users, id) => { | ||
| const user = users.find((user) => user.objectId === id); | ||
| return user ? { userName: user.displayName.split(" ")[0], userEmail: _.first(user.otherMails) } : {}; | ||
| const user = users.find((user) => user.id === id); | ||
| return user ? { userName: user.name.split(' ')[0], userEmail: (user.email) } : {}; | ||
| }; | ||
|
|
||
| const findSlackUserId = (slackUsers, email) => { | ||
| const user = slackUsers.find((slackUser) => slackUser.email === email); | ||
| return user ? user.id : null; | ||
| }; | ||
|
|
||
| doClockOut() | ||
| module.exports = { doClockOut }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 46 additions & 26 deletions
72
nodejs-functions/src/handlers/automatic-clock-outs/msal_client.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,47 @@ | ||
| const axios = require("axios") | ||
| const msal = require('@azure/msal-node'); | ||
| const config = require("./config"); | ||
|
|
||
| const findUsersInMS = async() => { | ||
| const {clientId, authority, clientSecret} = config; | ||
| const endpoint = 'https://graph.windows.net/ioetec.onmicrosoft.com' | ||
| const configuration = { | ||
| auth: { | ||
| clientId: clientId, | ||
| authority: authority, | ||
| clientSecret: clientSecret | ||
| } | ||
| }; | ||
|
|
||
| const cca = new msal.ConfidentialClientApplication(configuration); | ||
| const clientCredentialRequest = { | ||
| scopes: ['https://graph.windows.net/.default'], | ||
| }; | ||
| const response = await cca.acquireTokenByClientCredential(clientCredentialRequest) | ||
| const token = response.accessToken | ||
| return axios.get(`${endpoint}/users?api-version=1.6&$select=displayName,otherMails,objectId`, | ||
| { 'headers': { 'Authorization': token } }) | ||
| } | ||
|
|
||
| module.exports = { findUsersInMS }; | ||
| const axios = require('axios'); | ||
| const config = require('./config'); | ||
|
|
||
| const getToken = async () => { | ||
| const { clientId, userNameMS, userPasswordMS } = config; | ||
| const endpoint = | ||
| 'https://ioetec.b2clogin.com/ioetec.onmicrosoft.com/B2C_1_accesstoken/oauth2/v2.0/token'; | ||
cxcarvaj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const params = new URLSearchParams(); | ||
|
|
||
| params.append('username', userNameMS); | ||
| params.append('password', userPasswordMS); | ||
| params.append('grant_type', 'password'); | ||
| params.append('scope', clientId); | ||
| params.append('client_id', clientId); | ||
| params.append('response_type', 'token'); | ||
|
|
||
| const headers = { | ||
| headers: { | ||
| 'Content-Type': 'application/x-www-form-urlencoded', | ||
| }, | ||
| }; | ||
|
|
||
| return await axios | ||
| .post(endpoint, params, headers) | ||
| .then((result) => { | ||
| return result.data.access_token; | ||
| }) | ||
| .catch((err) => { | ||
| console.log(`Invalid request to: ${endpoint}`); | ||
| }); | ||
| }; | ||
|
|
||
| const findUsersInMS = async () => { | ||
| const endpoint = 'https://timetracker-api.azurewebsites.net/'; | ||
| const token = await getToken(); | ||
|
|
||
| const headers = { | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| }, | ||
| }; | ||
|
|
||
| return await axios.get(`${endpoint}/users`, headers); | ||
| }; | ||
|
|
||
| module.exports = { findUsersInMS }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.