@@ -2,8 +2,9 @@ const CosmosClient = require("@azure/cosmos").CosmosClient;
22const config = require ( "./config" ) ;
33const TimeEntry = require ( './time_entry' ) ;
44const axios = require ( 'axios' ) ;
5- const MsalClient = require ( './msal_client' )
6- const TimeEntryDao = require ( './time_entry_dao' )
5+ const MsalClient = require ( './msal_client' ) ;
6+ const TimeEntryDao = require ( './time_entry_dao' ) ;
7+ const SlackClient = require ( './slack_client' ) ;
78
89const doClockOut = async ( context ) => {
910 context . log ( `I am going to check how many entries were not clocked out ${ new Date ( ) } ` ) ;
@@ -16,21 +17,27 @@ const doClockOut = async (context) => {
1617
1718 const response = await MsalClient . findUsersInMS ( ) ;
1819 const users = response . data . value ;
20+ const slackUsers = await SlackClient . findUsersInSlack ( ) ;
21+
1922 const { resources : entries } = await timeEntryDao . getEntriesWithNoEndDate ( ) ;
2023 context . log ( `Checking for time-entries that need to be clocked out` ) ;
2124
2225 let totalClockOutsExecuted = 0 ;
23- const usersWithClockOut = [ ]
26+ const usersWithClockOut = [ ] ;
2427 await Promise . all ( entries . map ( async ( timeEntryAsJson ) => {
2528 const timeEntry = new TimeEntry ( timeEntryAsJson )
2629 if ( timeEntry . needsToBeClockedOut ( ) ) {
27- usersWithClockOut . push ( findUser ( users , timeEntry . timeEntry . owner_id ) ) ;
30+ const user_email = findUserEmail ( users , timeEntry . timeEntry . owner_id ) ;
31+ const userId = findSlackUserId ( slackUsers , user_email ) ;
32+ if ( userId ) {
33+ usersWithClockOut . push ( "<@" + userId + ">" ) ;
34+ }
2835 timeEntryAsJson . end_date = timeEntry . getTimeToClockOut ( )
2936 await container . item ( timeEntryAsJson . id , timeEntryAsJson . tenant_id ) . replace ( timeEntryAsJson )
3037 totalClockOutsExecuted ++
3138 }
3239 } ) ) ;
33- if ( totalClockOutsExecuted > 0 ) {
40+ if ( usersWithClockOut . length ) {
3441 axios . post ( slackWebHook ,
3542 {
3643 "text" : `OMG, you have been working more than 12 hours in a row. \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- ' ) } `
@@ -46,9 +53,14 @@ const doClockOut = async (context) => {
4653 context . log ( `I just clocked out ${ totalClockOutsExecuted } entries, thanks are not needed...` ) ;
4754}
4855
49- const findUser = ( users , id ) => {
56+ const findUserEmail = ( users , id ) => {
5057 const user = users . find ( user => user . objectId === id )
51- return user . displayName
58+ return user . otherMails [ 0 ]
59+ }
60+
61+ const findSlackUserId = ( users , email ) => {
62+ const user = users . find ( user => user . email === email ) ;
63+ return user ? user . id :null
5264}
5365
5466module . exports = { doClockOut} ;
0 commit comments