@@ -8,7 +8,7 @@ const SlackClient = require('./slack_client');
88const { CLOCK_OUT_MESSAGE , CLOCK_OUT_MESSAGE_MIDNIGHT } = require ( './constants' ) ;
99
1010const doClockOut = async ( context ) => {
11- console . log (
11+ context . log (
1212 `I am going to check how many entries were not clocked out ${ new Date ( ) } `
1313 ) ;
1414
@@ -23,52 +23,50 @@ const doClockOut = async (context) => {
2323 const slackUsers = await SlackClient . findUsersInSlack ( ) ;
2424
2525 const { resources : entries } = await timeEntryDao . getEntriesWithNoEndDate ( ) ;
26- console . log ( `Checking for time-entries that need to be clocked out` ) ;
26+ context . log ( `Checking for time-entries that need to be clocked out` ) ;
2727
2828 let totalClockOutsExecuted = 0 ;
2929
3030 await Promise . all (
3131 entries . map ( async ( timeEntryAsJson ) => {
3232 const timeEntry = new TimeEntry ( timeEntryAsJson ) ;
33- const { userName, userEmail } = findUserData ( users , timeEntry . timeEntry . owner_id ) ;
33+ const { userName, userEmail } = findUserData ( users , timeEntry . timeEntry . owner_id ) ;
3434 const userId = findSlackUserId ( slackUsers , userEmail ) ;
35- if ( userEmail === '[email protected] ' ) { 36- if ( timeEntry . needsToBeClockedOut ( ) ) {
37- if ( userId ) {
38- SlackClient . sendMessageToUser ( userId , CLOCK_OUT_MESSAGE . replace ( '%user_name%' , userName ) ) ;
39- }
40- timeEntryAsJson . end_date = timeEntry . getTimeToClockOut ( ) ;
41- await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
42- totalClockOutsExecuted ++ ;
35+
36+ if ( timeEntry . needsToBeClockedOut ( ) ) {
37+ if ( userId ) {
38+ SlackClient . sendMessageToUser ( userId , CLOCK_OUT_MESSAGE . replace ( '%user_name%' , userName ) ) ;
4339 }
44-
45- else if ( timeEntry . needsToBeClockedOutMidnight ( ) ) {
46- if ( userId ) {
47- SlackClient . sendMessageToUser ( userId , CLOCK_OUT_MESSAGE_MIDNIGHT . replace ( '%user_name%' , userName ) ) ;
48- }
49- timeEntryAsJson . end_date = timeEntry . getTimeToClockOutMidnight ( ) ;
50- await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
51- totalClockOutsExecuted ++ ;
40+ timeEntryAsJson . end_date = timeEntry . getTimeToClockOut ( ) ;
41+ await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
42+ totalClockOutsExecuted ++ ;
43+
44+ } else if ( timeEntry . needsToBeClockedOutMidnight ( ) ) {
45+ if ( userId ) {
46+ SlackClient . sendMessageToUser ( userId , CLOCK_OUT_MESSAGE_MIDNIGHT . replace ( '%user_name%' , userName ) ) ;
5247 }
48+ timeEntryAsJson . end_date = timeEntry . getTimeToClockOutMidnight ( ) ;
49+ await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
50+ totalClockOutsExecuted ++ ;
5351 }
54-
5552 } )
5653 ) ;
5754
58- console . log (
55+ context . log (
5956 `I just clocked out ${ totalClockOutsExecuted } entries, thanks are not needed...`
6057 ) ;
6158} ;
6259
63-
6460const findUserData = ( users , id ) => {
6561 const user = users . find ( ( user ) => user . id === id ) ;
66- return user ? { userName : user . name . split ( ' ' ) [ 0 ] , userEmail : ( user . email ) } : { } ;
62+ return user
63+ ? { userName : user . name . split ( ' ' ) [ 0 ] , userEmail : user . email }
64+ : { } ;
6765} ;
6866
6967const findSlackUserId = ( slackUsers , email ) => {
7068 const user = slackUsers . find ( ( slackUser ) => slackUser . email === email ) ;
7169 return user ? user . id : null ;
7270} ;
73- doClockOut ( )
71+
7472module . exports = { doClockOut } ;
0 commit comments