Skip to content

Commit 67c6f70

Browse files
committed
Fix: Azure endpoint return all users from ioet
1 parent 430b725 commit 67c6f70

File tree

4 files changed

+86
-60
lines changed

4 files changed

+86
-60
lines changed
Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
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(
11+
console.log(
1212
`I am going to check how many entries were not clocked out ${new Date()}`
1313
);
1414

1515
const { endpoint, key, databaseId } = config;
1616
const client = new CosmosClient({ endpoint, key });
1717
const database = client.database(databaseId);
18-
const container = database.container("time_entry");
18+
const container = database.container('time_entry');
1919
const timeEntryDao = new TimeEntryDao(database);
2020

2121
const response = await MsalClient.findUsersInMS();
22-
const users = response.data.value;
22+
const users = response.data;
2323
const slackUsers = await SlackClient.findUsersInSlack();
2424

2525
const { resources: entries } = await timeEntryDao.getEntriesWithNoEndDate();
26-
context.log(`Checking for time-entries that need to be clocked out`);
26+
console.log(`Checking for time-entries that need to be clocked out`);
2727

2828
let totalClockOutsExecuted = 0;
2929

@@ -32,40 +32,43 @@ const doClockOut = async (context) => {
3232
const timeEntry = new TimeEntry(timeEntryAsJson);
3333
const { userName, userEmail } = findUserData(users, timeEntry.timeEntry.owner_id);
3434
const userId = findSlackUserId(slackUsers, userEmail);
35-
36-
if (timeEntry.needsToBeClockedOut()) {
37-
if (userId) {
38-
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE.replace('%user_name%', userName));
35+
if( userEmail === '[email protected]'){
36+
if (timeEntry.needsToBeClockedOut()) {
37+
if (userId) {
38+
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE.replace('%user_name%', userName));
39+
}
40+
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut();
41+
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
42+
totalClockOutsExecuted++;
3943
}
40-
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut();
41-
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
42-
totalClockOutsExecuted++;
43-
}
44-
45-
else if (timeEntry.needsToBeClockedOutMidnight()) {
46-
if (userId) {
47-
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT.replace('%user_name%', userName));
44+
45+
else if (timeEntry.needsToBeClockedOutMidnight()) {
46+
if (userId) {
47+
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT.replace('%user_name%', userName));
48+
}
49+
timeEntryAsJson.end_date = timeEntry.getTimeToClockOutMidnight();
50+
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
51+
totalClockOutsExecuted++;
4852
}
49-
timeEntryAsJson.end_date = timeEntry.getTimeToClockOutMidnight();
50-
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
51-
totalClockOutsExecuted++;
5253
}
54+
5355
})
5456
);
5557

56-
context.log(
58+
console.log(
5759
`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`
5860
);
5961
};
6062

63+
6164
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) } : {};
65+
const user = users.find((user) => user.id === id);
66+
return user ? { userName: user.name.split(' ')[0], userEmail: (user.email) } : {};
6467
};
6568

6669
const findSlackUserId = (slackUsers, email) => {
6770
const user = slackUsers.find((slackUser) => slackUser.email === email);
6871
return user ? user.id : null;
6972
};
70-
73+
doClockOut()
7174
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+
1013
};
1114

1215
module.exports = config;
Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,47 @@
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 } = config;
6+
const endpoint =
7+
'https://ioetec.b2clogin.com/ioetec.onmicrosoft.com/B2C_1_accesstoken/oauth2/v2.0/token';
8+
9+
const params = new URLSearchParams();
10+
11+
params.append('username', userNameMS);
12+
params.append('password', userPasswordMS);
13+
params.append('grant_type', 'password');
14+
params.append('scope', clientId);
15+
params.append('client_id', clientId);
16+
params.append('response_type', 'token');
17+
18+
const headers = {
19+
headers: {
20+
'Content-Type': 'application/x-www-form-urlencoded',
21+
},
22+
};
23+
24+
return await axios
25+
.post(endpoint, params, headers)
26+
.then((result) => {
27+
return result.data.access_token;
28+
})
29+
.catch((err) => {
30+
console.log(`Invalid request to: ${endpoint}`);
31+
});
32+
};
33+
34+
const findUsersInMS = async () => {
35+
const endpoint = 'https://timetracker-api.azurewebsites.net/';
36+
const token = await getToken();
37+
38+
const headers = {
39+
headers: {
40+
Authorization: `Bearer ${token}`,
41+
},
42+
};
43+
44+
return await axios.get(`${endpoint}/users`, headers);
45+
};
46+
47+
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)