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' ) ;
9
9
10
10
const doClockOut = async ( context ) => {
11
- context . log (
11
+ console . log (
12
12
`I am going to check how many entries were not clocked out ${ new Date ( ) } `
13
13
) ;
14
14
15
15
const { endpoint, key, databaseId } = config ;
16
16
const client = new CosmosClient ( { endpoint, key } ) ;
17
17
const database = client . database ( databaseId ) ;
18
- const container = database . container ( " time_entry" ) ;
18
+ const container = database . container ( ' time_entry' ) ;
19
19
const timeEntryDao = new TimeEntryDao ( database ) ;
20
20
21
21
const response = await MsalClient . findUsersInMS ( ) ;
22
- const users = response . data . value ;
22
+ const users = response . data ;
23
23
const slackUsers = await SlackClient . findUsersInSlack ( ) ;
24
24
25
25
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` ) ;
27
27
28
28
let totalClockOutsExecuted = 0 ;
29
29
@@ -32,40 +32,43 @@ const doClockOut = async (context) => {
32
32
const timeEntry = new TimeEntry ( timeEntryAsJson ) ;
33
33
const { userName, userEmail } = findUserData ( users , timeEntry . timeEntry . owner_id ) ;
34
34
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 ++ ;
39
43
}
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 ++ ;
48
52
}
49
- timeEntryAsJson . end_date = timeEntry . getTimeToClockOutMidnight ( ) ;
50
- await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson ) ;
51
- totalClockOutsExecuted ++ ;
52
53
}
54
+
53
55
} )
54
56
) ;
55
57
56
- context . log (
58
+ console . log (
57
59
`I just clocked out ${ totalClockOutsExecuted } entries, thanks are not needed...`
58
60
) ;
59
61
} ;
60
62
63
+
61
64
const 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 ) } : { } ;
64
67
} ;
65
68
66
69
const findSlackUserId = ( slackUsers , email ) => {
67
70
const user = slackUsers . find ( ( slackUser ) => slackUser . email === email ) ;
68
71
return user ? user . id : null ;
69
72
} ;
70
-
73
+ doClockOut ( )
71
74
module . exports = { doClockOut } ;
0 commit comments