diff --git a/AutomaticClockOuts/clock_out.js b/AutomaticClockOuts/clock_out.js index 9074d7b..afe4773 100644 --- a/AutomaticClockOuts/clock_out.js +++ b/AutomaticClockOuts/clock_out.js @@ -8,7 +8,7 @@ const TimeEntryDao = require('./time_entry_dao') const doClockOut = async (context) => { context.log(`I am going to check how many entries were not clocked out ${new Date()}`); - const { endpoint, key, databaseId, slackWebHook } = config; + const {endpoint, key, databaseId, slackWebHook} = config; const client = new CosmosClient({endpoint, key}); const database = client.database(databaseId); const container = database.container('time_entry'); @@ -30,10 +30,10 @@ const doClockOut = async (context) => { totalClockOutsExecuted++ } })); - if(totalClockOutsExecuted > 0){ + if (totalClockOutsExecuted > 0) { axios.post(slackWebHook, { - "text": `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: \n- ${usersWithClockOut.join('\n- ')}` + "text": `OMG, you have been working more than 12 hours in a raw. \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: \n- ${usersWithClockOut.join('\n- ')}` } ) .then(function (response) { @@ -47,8 +47,8 @@ const doClockOut = async (context) => { } const findUser = (users, id) => { - const user = users.find( user => user.objectId === id) + const user = users.find(user => user.objectId === id) return user.displayName } -module.exports = { doClockOut }; +module.exports = {doClockOut}; diff --git a/AutomaticClockOuts/test/time_entry.spec.js b/AutomaticClockOuts/test/time_entry.spec.js index e38a57c..789388e 100644 --- a/AutomaticClockOuts/test/time_entry.spec.js +++ b/AutomaticClockOuts/test/time_entry.spec.js @@ -3,38 +3,30 @@ const TimeEntry = require('../time_entry'); const moment = require("moment") const timeEntryAsJson = { - "project_id": "f3630e59-9408-497e-945b-848112bd5a44", - "start_date": "2020-08-19T03:45:12.935Z", - "timezone_offset": 300, - "technologies": ["github"], - "id": "7b849fcc-78aa-468e-aa46-4bff2a0dcee9", - "tenant_id": "cc925a5d-9644-4a4f-8d99-0bee49aadd05" + "project_id": "f3630e59-9408-497e-945b-848112bd5a44", + "start_date": "2020-08-19T13:45:12.935Z", + "timezone_offset": 300, + "technologies": ["github"], + "id": "7b849fcc-78aa-468e-aa46-4bff2a0dcee9", + "tenant_id": "cc925a5d-9644-4a4f-8d99-0bee49aadd05" } -test('date is the day before with 23:59:59.999Z', t => { - const timeEntry = new TimeEntry(timeEntryAsJson) - t.is(timeEntry.getMidnightInTimeEntryZone().toISOString(), '2020-08-18T23:59:59.999Z'); +test('If entry has been running for at least 12h, it needs to be clock out', t => { + const twelveHoursAgo = moment().subtract(721, 'minutes'); + const entryTwelveHoursAgo = {...timeEntryAsJson, start_date: twelveHoursAgo.toISOString()} + const timeEntry = new TimeEntry(entryTwelveHoursAgo) + t.true(timeEntry.needsToBeClockedOut()); }) -test('Current time subtracts the offset in minutes', t => { - const timeEntry = new TimeEntry(timeEntryAsJson) - t.is(timeEntry.getCurrentTimeInTimeEntryZone().toISOString(), moment().utc() - .subtract(timeEntryAsJson.timezone_offset, 'minutes').toISOString()); -}) - -test('If entry starts in the past, then it needs to be clocked', t => { - const timeEntry = new TimeEntry(timeEntryAsJson) - t.truthy(timeEntry.needsToBeClockedOut()); -}) - -test('Time to clock out is midnight + time offset (5 hours)', t => { - const timeEntry = new TimeEntry(timeEntryAsJson) - t.is(timeEntry.getTimeToClockOut(), '2020-08-20T04:59:59.999Z'); +test('If entry has been running for at less than 12h, it does not need to be clock out', t => { + const lessThanTwelveHoursAgo = moment().subtract(719, 'minutes'); + const entryLessThanTwelveHoursAgo = {...timeEntryAsJson, start_date: lessThanTwelveHoursAgo.toISOString()} + const timeEntry = new TimeEntry(entryLessThanTwelveHoursAgo) + t.false(timeEntry.needsToBeClockedOut()); }) test('If entry starts in the future, then it does not need to be clocked', t => { const tomorrow = moment().add(1, 'day').toISOString() - timeEntryAsJson.start_date = tomorrow; - const timeEntry = new TimeEntry(timeEntryAsJson) - t.falsy(timeEntry.needsToBeClockedOut()); + const timeEntry = new TimeEntry({...timeEntryAsJson, start_date: tomorrow}) + t.false(timeEntry.needsToBeClockedOut()); }) diff --git a/AutomaticClockOuts/time_entry.js b/AutomaticClockOuts/time_entry.js index d6edf29..202deef 100644 --- a/AutomaticClockOuts/time_entry.js +++ b/AutomaticClockOuts/time_entry.js @@ -2,27 +2,23 @@ const moment = require("moment") class TimeEntry { - constructor(timeEntry) { - this.timeEntry = timeEntry; - } - - 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') - } - - getTimeToClockOut(){ - return moment(this.timeEntry.start_date).utc().endOf('day') - .add(this.timeEntry.timezone_offset, 'minutes').toISOString() - } - - needsToBeClockedOut(){ - return this.getMidnightInTimeEntryZone().isBefore(this.getCurrentTimeInTimeEntryZone()) - } + constructor(timeEntry) { + this.timeEntry = timeEntry; + } + + getStartTimeInUTC() { + return moment(this.timeEntry.start_date).utcOffset(this.timeEntry.timezone_offset); + } + + getTimeToClockOut() { + return moment().utc().subtract(this.timeEntry.timezone_offset, 'minutes').toISOString() + } + + needsToBeClockedOut() { + const currentTimeInUTC = moment().utc() + const minutesRunning = moment.duration(currentTimeInUTC.diff(this.getStartTimeInUTC())).asMinutes() + return minutesRunning > 720; + } } module.exports = TimeEntry