Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
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
Original file line number Diff line number Diff line change
@@ -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;
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