diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js b/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js index 840deb1..b798ed5 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js @@ -5,7 +5,7 @@ const TimeEntry = require('./time_entry'); const MsalClient = require('./msal_client'); const TimeEntryDao = require('./time_entry_dao'); const SlackClient = require('./slack_client'); -const { CLOCK_OUT_MESSAGE } = require('./constants'); +const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require('./constants'); const doClockOut = async (context) => { context.log(`I am going to check how many entries were not clocked out ${new Date()}`); @@ -26,10 +26,11 @@ const doClockOut = async (context) => { let totalClockOutsExecuted = 0; await Promise.all(entries.map(async (timeEntryAsJson) => { - const timeEntry = new TimeEntry(timeEntryAsJson) + const timeEntry = new TimeEntry(timeEntryAsJson); + const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id); + const userId = findSlackUserId(slackUsers, user_email); + if (timeEntry.needsToBeClockedOut()) { - const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id); - const userId = findSlackUserId(slackUsers, user_email); if(userId){ SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE); } @@ -37,7 +38,16 @@ const doClockOut = async (context) => { await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson) totalClockOutsExecuted++ } + + if(timeEntry.needsToBeClockedOutMidnight() && totalClockOutsExecuted > 0) { + if(userId){ + SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT); + } + timeEntryAsJson.end_date = timeEntry.getTimeToClockOut() + await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson); + } })); + context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`); } diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/constants.js b/nodejs-functions/src/handlers/automatic-clock-outs/constants.js index 7572aaf..01c2247 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/constants.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/constants.js @@ -1,5 +1,6 @@ const constants = { - CLOCK_OUT_MESSAGE : 'OMG, you have been working more than 12 hours in a row. \nPlease take a break and visit https://timetracker.ioet.com/ to set the right end time for your entries, we just did a clock out for you :wink:' + CLOCK_OUT_MESSAGE : 'OMG, you have been working more than 12 hours in a row. \nPlease take a break and visit https://timetracker.ioet.com/ to set the right end time for your entries, we just did a clock out for you :wink:', + CLOCK_OUT_MESSAGE_MIDNIGHT : 'Hey guys, I did a clock out for you. \nPlease visit https://timetracker.ioet.com/ and set the right end time for your entries :pls:' }; module.exports = constants; diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js b/nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js index 6c4de37..eade4bb 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js @@ -14,11 +14,24 @@ class TimeEntry { return moment().utc().toISOString(); } + getMidnightInTimeEntryZone(){ + return moment(this.timeEntry.start_date).utc() + .subtract(this.timeEntry.timezone_offset, 'minutes').endOf('day') + } + + getCurrentTimeInTimeEntryZone(){ + return moment().utc().subtract(this.timeEntry.timezone_offset, 'minutes') + } + needsToBeClockedOut() { const currentTimeInUTC = moment().utc() const minutesRunning = moment.duration(currentTimeInUTC.diff(this.getStartTimeInUTC())).asMinutes() return minutesRunning > 720; } + + needsToBeClockedOutMidnight(){ + return this.getMidnightInTimeEntryZone().isBefore(this.getCurrentTimeInTimeEntryZone()) + } } module.exports = TimeEntry