Skip to content

Commit 4c8f3d6

Browse files
committed
sending slack messages on clock out
1 parent bfe0c91 commit 4c8f3d6

File tree

13 files changed

+340
-38
lines changed

13 files changed

+340
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
AutomaticClockOuts/.env
12
bin
23
obj
34
csx

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AutomaticClockOuts/clock_out.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const CosmosClient = require("@azure/cosmos").CosmosClient;
2+
const config = require("./config");
3+
const TimeEntry = require('./time_entry');
4+
const axios = require('axios');
5+
const MsalClient = require('./msal_client')
6+
7+
const doClockOut = async (context, timer) => {
8+
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
9+
const {endpoint, key, databaseId, containerId, azureEndpoint} = config;
10+
const client = new CosmosClient({endpoint, key});
11+
const database = client.database(databaseId);
12+
const container = database.container(containerId);
13+
const response = await MsalClient.findUsersInMS();
14+
const users = response.data.value;
15+
16+
const QUERY_WITHOUT_END_DATE =
17+
"SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"
18+
19+
const {resources: entries} = await container.items
20+
.query({query: QUERY_WITHOUT_END_DATE})
21+
.fetchAll();
22+
23+
context.log(`Checking for time-entries that need to be clocked out`);
24+
let totalClockOutsExecuted = 0;
25+
await Promise.all(entries.map(async (timeEntryAsJson) => {
26+
const timeEntry = new TimeEntry(timeEntryAsJson)
27+
if (timeEntry.needsToBeClockedOut()) {
28+
context.log(`I am going to clock out ${JSON.stringify(timeEntryAsJson)}`);
29+
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
30+
console.log(`I am doing it for you ${JSON.stringify(findUser(users, timeEntry.owner_id))}`)
31+
axios.post('https://hooks.slack.com/services/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',
32+
{"text": `I am doing it for you ${JSON.stringify(findUser(users, timeEntry.owner_id))}`}
33+
)
34+
.then(function (response) {
35+
console.log(response);
36+
})
37+
.catch(function (error) {
38+
console.log(error);
39+
});
40+
console.log(JSON.stringify(timeEntry))
41+
totalClockOutsExecuted++
42+
}
43+
}));
44+
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
45+
}
46+
47+
const findUser = (users, id) => {
48+
return users.find( user => user.objectId === id)
49+
}
50+
51+
module.exports = { doClockOut };

AutomaticClockOuts/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const config = {
33
key: process.env["COSMOS_DB_KEY"],
44
databaseId: "time-tracker-db",
55
containerId: "time_entry",
6-
partitionKey: { kind: "Hash", paths: ["/category"] }
6+
partitionKey: { kind: "Hash", paths: ["/category"] },
7+
client_id: process.env["MS_CLIENT_ID"],
8+
authority: process.env["MS_AUTHORITY"],
9+
clientSecret: process.env["MS_CLIENT_SECRET"]
710
};
811

912
module.exports = config;

AutomaticClockOuts/index.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,45 @@
11
const CosmosClient = require("@azure/cosmos").CosmosClient;
2-
const moment = require("moment")
32
const config = require("./config");
43
const TimeEntry = require('./time_entry');
4+
const axios = require('axios');
5+
const MsalClient = require('./msal_client');
6+
const ClockOut = require('./clock_out');
57

68
module.exports = async function (context, myTimer) {
7-
8-
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
9-
const {endpoint, key, databaseId, containerId} = config;
10-
const client = new CosmosClient({endpoint, key});
11-
const database = client.database(databaseId);
12-
const container = database.container(containerId);
13-
14-
const QUERY_WITHOUT_END_DATE =
15-
"SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"
16-
17-
const {resources: entries} = await container.items
18-
.query({query: QUERY_WITHOUT_END_DATE})
19-
.fetchAll();
20-
21-
context.log(`Checking for time-entries that need to be clocked out`);
22-
let totalClockOutsExecuted = 0;
23-
await Promise.all(entries.map(async (timeEntryAsJson) => {
24-
const timeEntry = new TimeEntry(timeEntryAsJson)
25-
if (timeEntry.needsToBeClockedOut()) {
26-
context.log(`I am going to clock out ${JSON.stringify(timeEntryAsJson.id)}`);
27-
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
28-
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson)
29-
totalClockOutsExecuted++
30-
}
31-
}));
32-
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
9+
context.log('starting.....................')
10+
await ClockOut.doClockOut(context, myTimer)
11+
// context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
12+
// const {endpoint, key, databaseId, containerId, azureEndpoint} = config;
13+
// const client = new CosmosClient({endpoint, key});
14+
// const database = client.database(databaseId);
15+
// const container = database.container(containerId);
16+
// const users = await MsalClient.findUsersInMS();
17+
//
18+
// const QUERY_WITHOUT_END_DATE =
19+
// "SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"
20+
//
21+
// const {resources: entries} = await container.items
22+
// .query({query: QUERY_WITHOUT_END_DATE})
23+
// .fetchAll();
24+
//
25+
// context.log(`Checking for time-entries that need to be clocked out`);
26+
// let totalClockOutsExecuted = 0;
27+
// await Promise.all(entries.map(async (timeEntryAsJson) => {
28+
// const timeEntry = new TimeEntry(timeEntryAsJson)
29+
// if (timeEntry.needsToBeClockedOut()) {
30+
// context.log(`I am going to clock out ${JSON.stringify(timeEntryAsJson)}`);
31+
// timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
32+
// axios.post('https://hooks.slack.com/services/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',
33+
// {"text": `Hey, we clocked-out for you yesterday at midnight, please make sure to update your entered data`}
34+
// )
35+
// .then(function (response) {
36+
// console.log(response);
37+
// })
38+
// .catch(function (error) {
39+
// console.log(error);
40+
// });
41+
// totalClockOutsExecuted++
42+
// }
43+
// }));
44+
// context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
3345
};

AutomaticClockOuts/msal_client.js

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

0 commit comments

Comments
 (0)