Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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
sending slack messages on clock out
  • Loading branch information
enriquezrene committed Sep 20, 2020
commit 4c8f3d6a9710fcb6b0b2604c8c756b787bfaf226
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
AutomaticClockOuts/.env
bin
obj
csx
Expand Down
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions AutomaticClockOuts/clock_out.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const CosmosClient = require("@azure/cosmos").CosmosClient;
const config = require("./config");
const TimeEntry = require('./time_entry');
const axios = require('axios');
const MsalClient = require('./msal_client')

const doClockOut = async (context, timer) => {
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
const {endpoint, key, databaseId, containerId, azureEndpoint} = config;
const client = new CosmosClient({endpoint, key});
const database = client.database(databaseId);
const container = database.container(containerId);
const response = await MsalClient.findUsersInMS();
const users = response.data.value;

const QUERY_WITHOUT_END_DATE =
"SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"

const {resources: entries} = await container.items
.query({query: QUERY_WITHOUT_END_DATE})
.fetchAll();

context.log(`Checking for time-entries that need to be clocked out`);
let totalClockOutsExecuted = 0;
await Promise.all(entries.map(async (timeEntryAsJson) => {
const timeEntry = new TimeEntry(timeEntryAsJson)
if (timeEntry.needsToBeClockedOut()) {
context.log(`I am going to clock out ${JSON.stringify(timeEntryAsJson)}`);
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
console.log(`I am doing it for you ${JSON.stringify(findUser(users, timeEntry.owner_id))}`)
axios.post('https://hooks.slack.com/services/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',
{"text": `I am doing it for you ${JSON.stringify(findUser(users, timeEntry.owner_id))}`}
)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
console.log(JSON.stringify(timeEntry))
totalClockOutsExecuted++
}
}));
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
}

const findUser = (users, id) => {
return users.find( user => user.objectId === id)
}

module.exports = { doClockOut };
5 changes: 4 additions & 1 deletion AutomaticClockOuts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ const config = {
key: process.env["COSMOS_DB_KEY"],
databaseId: "time-tracker-db",
containerId: "time_entry",
partitionKey: { kind: "Hash", paths: ["/category"] }
partitionKey: { kind: "Hash", paths: ["/category"] },
client_id: process.env["MS_CLIENT_ID"],
authority: process.env["MS_AUTHORITY"],
clientSecret: process.env["MS_CLIENT_SECRET"]
};

module.exports = config;
66 changes: 39 additions & 27 deletions AutomaticClockOuts/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
const CosmosClient = require("@azure/cosmos").CosmosClient;
const moment = require("moment")
const config = require("./config");
const TimeEntry = require('./time_entry');
const axios = require('axios');
const MsalClient = require('./msal_client');
const ClockOut = require('./clock_out');

module.exports = async function (context, myTimer) {

context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
const {endpoint, key, databaseId, containerId} = config;
const client = new CosmosClient({endpoint, key});
const database = client.database(databaseId);
const container = database.container(containerId);

const QUERY_WITHOUT_END_DATE =
"SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"

const {resources: entries} = await container.items
.query({query: QUERY_WITHOUT_END_DATE})
.fetchAll();

context.log(`Checking for time-entries that need to be clocked out`);
let totalClockOutsExecuted = 0;
await Promise.all(entries.map(async (timeEntryAsJson) => {
const timeEntry = new TimeEntry(timeEntryAsJson)
if (timeEntry.needsToBeClockedOut()) {
context.log(`I am going to clock out ${JSON.stringify(timeEntryAsJson.id)}`);
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson)
totalClockOutsExecuted++
}
}));
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
context.log('starting.....................')
await ClockOut.doClockOut(context, myTimer)
// context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
// const {endpoint, key, databaseId, containerId, azureEndpoint} = config;
// const client = new CosmosClient({endpoint, key});
// const database = client.database(databaseId);
// const container = database.container(containerId);
// const users = await MsalClient.findUsersInMS();
//
// const QUERY_WITHOUT_END_DATE =
// "SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"
//
// const {resources: entries} = await container.items
// .query({query: QUERY_WITHOUT_END_DATE})
// .fetchAll();
//
// context.log(`Checking for time-entries that need to be clocked out`);
// let totalClockOutsExecuted = 0;
// await Promise.all(entries.map(async (timeEntryAsJson) => {
// const timeEntry = new TimeEntry(timeEntryAsJson)
// if (timeEntry.needsToBeClockedOut()) {
// context.log(`I am going to clock out ${JSON.stringify(timeEntryAsJson)}`);
// timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
// axios.post('https://hooks.slack.com/services/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',
// {"text": `Hey, we clocked-out for you yesterday at midnight, please make sure to update your entered data`}
// )
// .then(function (response) {
// console.log(response);
// })
// .catch(function (error) {
// console.log(error);
// });
// totalClockOutsExecuted++
// }
// }));
// context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
};
28 changes: 28 additions & 0 deletions AutomaticClockOuts/msal_client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const axios = require("axios")
const msal = require('@azure/msal-node');

const findUsersInMS = async() => {
const endpoint = 'https://graph.windows.net/ioetec.onmicrosoft.com'
const config = {
auth: {
clientId: process.env["MS_CLIENT_ID"],
authority: process.env["MS_AUTHORITY"],
clientSecret: process.env["MS_CLIENT_SECRET"]
}
};

console.log('---------------------------------------------')
console.log(JSON.stringify(config))

const cca = new msal.ConfidentialClientApplication(config);
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 };

Loading