From dbcad2c838944841b7df870713752ce4068ea2d4 Mon Sep 17 00:00:00 2001 From: Rene Enriquez Date: Thu, 19 Nov 2020 22:10:57 -0500 Subject: [PATCH 1/3] WIP avoid mentioning same person twice --- AutomaticClockOuts/clock_out.js | 24 ++++++++++++------------ AutomaticClockOuts/config.js | 1 - AutomaticClockOuts/index.js | 5 ----- AutomaticClockOuts/time_entry_dao.js | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 AutomaticClockOuts/time_entry_dao.js diff --git a/AutomaticClockOuts/clock_out.js b/AutomaticClockOuts/clock_out.js index 9807267..4c9a782 100644 --- a/AutomaticClockOuts/clock_out.js +++ b/AutomaticClockOuts/clock_out.js @@ -3,30 +3,30 @@ const config = require("./config"); const TimeEntry = require('./time_entry'); const axios = require('axios'); const MsalClient = require('./msal_client') +const TimeEntryDao = require('./time_entry_dao') -const doClockOut = async (context, timer) => { +const doClockOut = async (context) => { context.log(`I am going to check how many entries were not clocked out ${new Date()}`); - const {endpoint, key, databaseId, containerId, slackWebHook} = config; + + const { endpoint, key, databaseId, slackWebHook } = config; const client = new CosmosClient({endpoint, key}); const database = client.database(databaseId); - const container = database.container(containerId); + const timeEntryDao = new TimeEntryDao(database); + 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(); - + const {resources: entries} = await timeEntryDao.getEntriesWithNoEndDate(); context.log(`Checking for time-entries that need to be clocked out`); + let totalClockOutsExecuted = 0; const usersWithClockOut = [] await Promise.all(entries.map(async (timeEntryAsJson) => { const timeEntry = new TimeEntry(timeEntryAsJson) if (timeEntry.needsToBeClockedOut()) { - usersWithClockOut.push(findUser(users, timeEntry.timeEntry.owner_id)) + const userToClockOut = findUser(users, timeEntry.timeEntry.owner_id); + if(!userToClockOut.includes(userToClockOut)){ + usersWithClockOut.push(userToClockOut) + } timeEntryAsJson.end_date = timeEntry.getTimeToClockOut() await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson) totalClockOutsExecuted++ diff --git a/AutomaticClockOuts/config.js b/AutomaticClockOuts/config.js index bb7a893..c2028d3 100644 --- a/AutomaticClockOuts/config.js +++ b/AutomaticClockOuts/config.js @@ -2,7 +2,6 @@ const config = { endpoint: process.env["ENDPOINT"], key: process.env["KEY"], databaseId: "time-tracker-db", - containerId: "time_entry", partitionKey: { kind: "Hash", paths: ["/category"] }, clientId: process.env["CLIENT_ID"], authority: process.env["AUTHORITY"], diff --git a/AutomaticClockOuts/index.js b/AutomaticClockOuts/index.js index 22de912..5cab026 100644 --- a/AutomaticClockOuts/index.js +++ b/AutomaticClockOuts/index.js @@ -1,8 +1,3 @@ -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 ClockOut = require('./clock_out'); module.exports = async function (context, myTimer) { diff --git a/AutomaticClockOuts/time_entry_dao.js b/AutomaticClockOuts/time_entry_dao.js new file mode 100644 index 0000000..e450dea --- /dev/null +++ b/AutomaticClockOuts/time_entry_dao.js @@ -0,0 +1,19 @@ +class TimeEntryDao { + + CONTAINER_ID = 'time_entry'; + + constructor(database) { + this.container = database.container(this.CONTAINER_ID); + } + + async getEntriesWithNoEndDate () { + 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)"; + return this.container.items + .query({query: QUERY_WITHOUT_END_DATE}) + .fetchAll(); + } + +} + +module.exports = TimeEntryDao From ec57b654e9c74fff2cf2558614c2465a372ad433 Mon Sep 17 00:00:00 2001 From: Rene Enriquez Date: Thu, 19 Nov 2020 22:35:34 -0500 Subject: [PATCH 2/3] fix: updating the query to ignore deleted entries --- AutomaticClockOuts/clock_out.js | 5 +---- AutomaticClockOuts/time_entry_dao.js | 7 +++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/AutomaticClockOuts/clock_out.js b/AutomaticClockOuts/clock_out.js index 4c9a782..8c18e70 100644 --- a/AutomaticClockOuts/clock_out.js +++ b/AutomaticClockOuts/clock_out.js @@ -23,10 +23,7 @@ const doClockOut = async (context) => { await Promise.all(entries.map(async (timeEntryAsJson) => { const timeEntry = new TimeEntry(timeEntryAsJson) if (timeEntry.needsToBeClockedOut()) { - const userToClockOut = findUser(users, timeEntry.timeEntry.owner_id); - if(!userToClockOut.includes(userToClockOut)){ - usersWithClockOut.push(userToClockOut) - } + usersWithClockOut.push(findUser(users, timeEntry.timeEntry.owner_id)); timeEntryAsJson.end_date = timeEntry.getTimeToClockOut() await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson) totalClockOutsExecuted++ diff --git a/AutomaticClockOuts/time_entry_dao.js b/AutomaticClockOuts/time_entry_dao.js index e450dea..3ac7956 100644 --- a/AutomaticClockOuts/time_entry_dao.js +++ b/AutomaticClockOuts/time_entry_dao.js @@ -1,14 +1,13 @@ class TimeEntryDao { - CONTAINER_ID = 'time_entry'; - constructor(database) { - this.container = database.container(this.CONTAINER_ID); + const CONTAINER_ID = 'time_entry'; + this.container = database.container(CONTAINER_ID); } async getEntriesWithNoEndDate () { 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)"; + "SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date) AND (NOT IS_DEFINED(c.deleted) OR IS_NULL(c.deleted) = true)"; return this.container.items .query({query: QUERY_WITHOUT_END_DATE}) .fetchAll(); From b8be91c3a2ceef32463fb31fe35aff43b04c40e3 Mon Sep 17 00:00:00 2001 From: Rene Enriquez Date: Thu, 19 Nov 2020 22:39:12 -0500 Subject: [PATCH 3/3] fix: adding env vars info as part of the readme file --- AutomaticClockOuts/readme.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/AutomaticClockOuts/readme.md b/AutomaticClockOuts/readme.md index ae88d19..a5f3103 100644 --- a/AutomaticClockOuts/readme.md +++ b/AutomaticClockOuts/readme.md @@ -32,9 +32,11 @@ NOTE: Don't forget to set the following environment variables to make this app work: ```sh -COSMOS_DB_KEY -MS_CLIENT_ID -MS_AUTHORITY -MS_CLIENT_SECRET +ENDPOINT='XXX' +KEY='XXX' +CLIENT_ID='XXX' +AUTHORITY='XXX' +CLIENT_SECRET='XXX' +SLACK_WEBHOOK='XXX' ``` Check the pinned message in our slack channel to get these values