Skip to content
Closed
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
feat: TT-484 ClockedOutMidnight
  • Loading branch information
scastillo-jp committed Jan 20, 2022
commit 4f041c31790af5a0ff39be0d8628aef670f5fcc2
18 changes: 14 additions & 4 deletions nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`);
Expand All @@ -26,18 +26,28 @@ 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);
}
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
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...`);
}
Expand Down
13 changes: 13 additions & 0 deletions nodejs-functions/src/handlers/automatic-clock-outs/time_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down