@@ -5,7 +5,7 @@ const TimeEntry = require('./time_entry');
55const MsalClient = require ( './msal_client' ) ;
66const TimeEntryDao = require ( './time_entry_dao' ) ;
77const SlackClient = require ( './slack_client' ) ;
8- const { CLOCK_OUT_MESSAGE } = require ( './constants' ) ;
8+ const { CLOCK_OUT_MESSAGE , CLOCK_OUT_MESSAGE_MIDNIGHT } = require ( './constants' ) ;
99
1010const 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}
0 commit comments