1- const _ = require ( " lodash" ) ;
2- const CosmosClient = require ( " @azure/cosmos" ) . CosmosClient ;
3- const config = require ( " ./config" ) ;
4- const TimeEntry = require ( " ./time_entry" ) ;
5- const MsalClient = require ( " ./msal_client" ) ;
6- const TimeEntryDao = require ( " ./time_entry_dao" ) ;
7- const SlackClient = require ( " ./slack_client" ) ;
8- const { CLOCK_OUT_MESSAGE , CLOCK_OUT_MESSAGE_MIDNIGHT } = require ( " ./constants" ) ;
1+ const _ = require ( ' lodash' ) ;
2+ const CosmosClient = require ( ' @azure/cosmos' ) . CosmosClient ;
3+ const config = require ( ' ./config' ) ;
4+ const TimeEntry = require ( ' ./time_entry' ) ;
5+ const MsalClient = require ( ' ./msal_client' ) ;
6+ const TimeEntryDao = require ( ' ./time_entry_dao' ) ;
7+ const SlackClient = require ( ' ./slack_client' ) ;
8+ const { CLOCK_OUT_MESSAGE , CLOCK_OUT_MESSAGE_MIDNIGHT } = require ( ' ./constants' ) ;
99
1010const doClockOut = async ( context ) => {
11- context . log (
11+ console . log (
1212 `I am going to check how many entries were not clocked out ${ new Date ( ) } `
1313 ) ;
1414
1515 const { endpoint, key, databaseId } = config ;
1616 const client = new CosmosClient ( { endpoint, key } ) ;
1717 const database = client . database ( databaseId ) ;
18- const container = database . container ( " time_entry" ) ;
18+ const container = database . container ( ' time_entry' ) ;
1919 const timeEntryDao = new TimeEntryDao ( database ) ;
2020
2121 const response = await MsalClient . findUsersInMS ( ) ;
22- const users = response . data . value ;
22+ const users = response . data ;
2323 const slackUsers = await SlackClient . findUsersInSlack ( ) ;
2424
2525 const { resources : entries } = await timeEntryDao . getEntriesWithNoEndDate ( ) ;
26- context . log ( `Checking for time-entries that need to be clocked out` ) ;
26+ console . log ( `Checking for time-entries that need to be clocked out` ) ;
2727
2828 let totalClockOutsExecuted = 0 ;
2929
@@ -32,40 +32,43 @@ const doClockOut = async (context) => {
3232 const timeEntry = new TimeEntry ( timeEntryAsJson ) ;
3333 const { userName, userEmail } = findUserData ( users , timeEntry . timeEntry . owner_id ) ;
3434 const userId = findSlackUserId ( slackUsers , userEmail ) ;
35-
36- if ( timeEntry . needsToBeClockedOut ( ) ) {
37- if ( userId ) {
38- SlackClient . sendMessageToUser ( userId , CLOCK_OUT_MESSAGE . replace ( '%user_name%' , userName ) ) ;
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 ++ ;
3943 }
40- timeEntryAsJson . end_date = timeEntry . getTimeToClockOut ( ) ;
41- await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
42- totalClockOutsExecuted ++ ;
43- }
44-
45- else if ( timeEntry . needsToBeClockedOutMidnight ( ) ) {
46- if ( userId ) {
47- SlackClient . sendMessageToUser ( userId , CLOCK_OUT_MESSAGE_MIDNIGHT . replace ( '%user_name%' , userName ) ) ;
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 ++ ;
4852 }
49- timeEntryAsJson . end_date = timeEntry . getTimeToClockOutMidnight ( ) ;
50- await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
51- totalClockOutsExecuted ++ ;
5253 }
54+
5355 } )
5456 ) ;
5557
56- context . log (
58+ console . log (
5759 `I just clocked out ${ totalClockOutsExecuted } entries, thanks are not needed...`
5860 ) ;
5961} ;
6062
63+
6164const findUserData = ( users , id ) => {
62- const user = users . find ( ( user ) => user . objectId === id ) ;
63- return user ? { userName : user . displayName . split ( " " ) [ 0 ] , userEmail : _ . first ( user . otherMails ) } : { } ;
65+ const user = users . find ( ( user ) => user . id === id ) ;
66+ return user ? { userName : user . name . split ( ' ' ) [ 0 ] , userEmail : ( user . email ) } : { } ;
6467} ;
6568
6669const findSlackUserId = ( slackUsers , email ) => {
6770 const user = slackUsers . find ( ( slackUser ) => slackUser . email === email ) ;
6871 return user ? user . id : null ;
6972} ;
70-
73+ doClockOut ( )
7174module . exports = { doClockOut } ;
0 commit comments