Skip to content

Commit 4f041c3

Browse files
committed
feat: TT-484 ClockedOutMidnight
1 parent 0bdb17a commit 4f041c3

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const TimeEntry = require('./time_entry');
55
const MsalClient = require('./msal_client');
66
const TimeEntryDao = require('./time_entry_dao');
77
const SlackClient = require('./slack_client');
8-
const { CLOCK_OUT_MESSAGE } = require('./constants');
8+
const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require('./constants');
99

1010
const doClockOut = async (context) => {
1111
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
@@ -26,18 +26,28 @@ const doClockOut = async (context) => {
2626
let totalClockOutsExecuted = 0;
2727

2828
await Promise.all(entries.map(async (timeEntryAsJson) => {
29-
const timeEntry = new TimeEntry(timeEntryAsJson)
29+
const timeEntry = new TimeEntry(timeEntryAsJson);
30+
const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id);
31+
const userId = findSlackUserId(slackUsers, user_email);
32+
3033
if (timeEntry.needsToBeClockedOut()) {
31-
const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id);
32-
const userId = findSlackUserId(slackUsers, user_email);
3334
if(userId){
3435
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE);
3536
}
3637
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
3738
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson)
3839
totalClockOutsExecuted++
3940
}
41+
42+
if(timeEntry.needsToBeClockedOutMidnight() && totalClockOutsExecuted > 0) {
43+
if(userId){
44+
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT);
45+
}
46+
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
47+
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
48+
}
4049
}));
50+
4151

4252
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
4353
}

nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@ class TimeEntry {
1414
return moment().utc().toISOString();
1515
}
1616

17+
getMidnightInTimeEntryZone(){
18+
return moment(this.timeEntry.start_date).utc()
19+
.subtract(this.timeEntry.timezone_offset, 'minutes').endOf('day')
20+
}
21+
22+
getCurrentTimeInTimeEntryZone(){
23+
return moment().utc().subtract(this.timeEntry.timezone_offset, 'minutes')
24+
}
25+
1726
needsToBeClockedOut() {
1827
const currentTimeInUTC = moment().utc()
1928
const minutesRunning = moment.duration(currentTimeInUTC.diff(this.getStartTimeInUTC())).asMinutes()
2029
return minutesRunning > 720;
2130
}
31+
32+
needsToBeClockedOutMidnight(){
33+
return this.getMidnightInTimeEntryZone().isBefore(this.getCurrentTimeInTimeEntryZone())
34+
}
2235
}
2336

2437
module.exports = TimeEntry

0 commit comments

Comments
 (0)