Skip to content

Commit b54e61a

Browse files
authored
Merge pull request #80 from ioet/TT-537-azure-endpoint-doesnt-return-the-complete-users
Fix: Azure endpoint return all users from ioet
2 parents 430b725 + 462117f commit b54e61a

File tree

4 files changed

+66
-49
lines changed

4 files changed

+66
-49
lines changed

nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
1-
const _ = require("lodash");
2-
const CosmosClient = require("@azure/cosmos").CosmosClient;
3-
const config = require("./config");
4-
const TimeEntry = require("./time_entry");
5-
const MsalClient = require("./msal_client");
6-
const TimeEntryDao = require("./time_entry_dao");
7-
const SlackClient = require("./slack_client");
8-
const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require("./constants");
1+
const _ = require('lodash');
2+
const CosmosClient = require('@azure/cosmos').CosmosClient;
3+
const config = require('./config');
4+
const TimeEntry = require('./time_entry');
5+
const MsalClient = require('./msal_client');
6+
const TimeEntryDao = require('./time_entry_dao');
7+
const SlackClient = require('./slack_client');
8+
const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require('./constants');
99

1010
const doClockOut = async (context) => {
11-
context.log(
12-
`I am going to check how many entries were not clocked out ${new Date()}`
13-
);
11+
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
1412

1513
const { endpoint, key, databaseId } = config;
1614
const client = new CosmosClient({ endpoint, key });
1715
const database = client.database(databaseId);
18-
const container = database.container("time_entry");
16+
const container = database.container('time_entry');
1917
const timeEntryDao = new TimeEntryDao(database);
2018

2119
const response = await MsalClient.findUsersInMS();
22-
const users = response.data.value;
20+
const users = response.data;
2321
const slackUsers = await SlackClient.findUsersInSlack();
2422

2523
const { resources: entries } = await timeEntryDao.getEntriesWithNoEndDate();
@@ -53,19 +51,17 @@ const doClockOut = async (context) => {
5351
})
5452
);
5553

56-
context.log(
57-
`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`
58-
);
54+
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
5955
};
6056

6157
const findUserData = (users, id) => {
62-
const user = users.find((user) => user.objectId === id);
63-
return user ? { userName: user.displayName.split(" ")[0], userEmail: _.first(user.otherMails) } : {};
58+
const targetUser = users.find((user) => user.id === id);
59+
return targetUser ? { userName: targetUser.name.split(' ')[0], userEmail: targetUser.email } : {};
6460
};
6561

6662
const findSlackUserId = (slackUsers, email) => {
67-
const user = slackUsers.find((slackUser) => slackUser.email === email);
68-
return user ? user.id : null;
63+
const slackTargetUser = slackUsers.find((slackUser) => slackUser.email === email);
64+
return slackTargetUser ? slackTargetUser.id : null;
6965
};
7066

7167
module.exports = { doClockOut };

nodejs-functions/src/handlers/automatic-clock-outs/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const config = {
66
clientId: process.env["CLIENT_ID"],
77
authority: process.env["AUTHORITY"],
88
clientSecret: process.env["CLIENT_SECRET"],
9-
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"]
9+
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"],
10+
userNameMS: process.env["USER_NAME_MS"],
11+
userPasswordMS: process.env["USER_PASSWORD_MS"],
12+
b2cLogin = process.env["B2C_LOGIN"]
1013
};
1114

1215
module.exports = config;
Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
1-
const axios = require("axios")
2-
const msal = require('@azure/msal-node');
3-
const config = require("./config");
4-
5-
const findUsersInMS = async() => {
6-
const {clientId, authority, clientSecret} = config;
7-
const endpoint = 'https://graph.windows.net/ioetec.onmicrosoft.com'
8-
const configuration = {
9-
auth: {
10-
clientId: clientId,
11-
authority: authority,
12-
clientSecret: clientSecret
13-
}
14-
};
15-
16-
const cca = new msal.ConfidentialClientApplication(configuration);
17-
const clientCredentialRequest = {
18-
scopes: ['https://graph.windows.net/.default'],
19-
};
20-
const response = await cca.acquireTokenByClientCredential(clientCredentialRequest)
21-
const token = response.accessToken
22-
return axios.get(`${endpoint}/users?api-version=1.6&$select=displayName,otherMails,objectId`,
23-
{ 'headers': { 'Authorization': token } })
24-
}
25-
26-
module.exports = { findUsersInMS };
1+
const axios = require('axios');
2+
const config = require('./config');
273

4+
const getToken = async () => {
5+
const { clientId, userNameMS, userPasswordMS, b2cLogin } = config;
6+
const endpoint = b2cLogin;
7+
8+
const params = new URLSearchParams();
9+
10+
params.append('username', userNameMS);
11+
params.append('password', userPasswordMS);
12+
params.append('grant_type', 'password');
13+
params.append('scope', clientId);
14+
params.append('client_id', clientId);
15+
params.append('response_type', 'token');
16+
17+
const headers = {
18+
headers: {
19+
'Content-Type': 'application/x-www-form-urlencoded',
20+
},
21+
};
22+
23+
return axios.post(endpoint, params, headers)
24+
.then((result) => {
25+
return result.data.access_token;
26+
})
27+
.catch((err) => {
28+
console.log(`Invalid request to: ${endpoint}`);
29+
});
30+
};
31+
32+
const findUsersInMS = async () => {
33+
const endpoint = 'https://timetracker-api.azurewebsites.net/';
34+
const token = await getToken();
35+
36+
const headers = {
37+
headers: {
38+
Authorization: `Bearer ${token}`,
39+
},
40+
};
41+
42+
return axios.get(`${endpoint}/users`, headers);
43+
};
44+
45+
module.exports = { findUsersInMS };

nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const moment = require("moment")
1+
const moment = require('moment')
22

33
class TimeEntry {
44

@@ -15,7 +15,7 @@ class TimeEntry {
1515
}
1616

1717
getMidnightInTimeEntryZone(){
18-
return moment(this.timeEntry.start_date).utcOffset(this.timeEntry.timezone_offset * -1).endOf('day');
18+
return moment(this.timeEntry.start_date).utc().subtract(this.timeEntry.timezone_offset, 'minutes').endOf('day')
1919
}
2020

2121
getTimeToClockOutMidnight(){

0 commit comments

Comments
 (0)