diff --git a/AutomaticClockOuts/clock_out.js b/AutomaticClockOuts/clock_out.js index 52de7d4..4fbf742 100644 --- a/AutomaticClockOuts/clock_out.js +++ b/AutomaticClockOuts/clock_out.js @@ -2,15 +2,15 @@ const _ = require('lodash'); const CosmosClient = require("@azure/cosmos").CosmosClient; const config = require("./config"); const TimeEntry = require('./time_entry'); -const axios = require('axios'); const MsalClient = require('./msal_client'); const TimeEntryDao = require('./time_entry_dao'); const SlackClient = require('./slack_client'); +const { ClockOutMessage } = require('./constants'); const doClockOut = async (context) => { context.log(`I am going to check how many entries were not clocked out ${new Date()}`); - const {endpoint, key, databaseId, slackWebHook} = config; + const {endpoint, key, databaseId } = config; const client = new CosmosClient({endpoint, key}); const database = client.database(databaseId); const container = database.container('time_entry'); @@ -25,6 +25,7 @@ const doClockOut = async (context) => { let totalClockOutsExecuted = 0; const usersWithClockOut = []; + await Promise.all(entries.map(async (timeEntryAsJson) => { const timeEntry = new TimeEntry(timeEntryAsJson) if (timeEntry.needsToBeClockedOut()) { @@ -32,6 +33,7 @@ const doClockOut = async (context) => { const userId = findSlackUserId(slackUsers,user_email); if(userId){ usersWithClockOut.push("<@"+userId+">"); + SlackClient.sendMessageToUser(userId,ClockOutMessage); } timeEntryAsJson.end_date = timeEntry.getTimeToClockOut() await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson) @@ -39,17 +41,8 @@ const doClockOut = async (context) => { } })); if (usersWithClockOut.length) { - axios.post(slackWebHook, - { - "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- ')}` - } - ) - .then(function (response) { - // console.log(response); - }) - .catch(function (error) { - context.log(error); - }); + const ClockOutMessageChannel=`${ClockOutMessage} \n- ${usersWithClockOut.join('\n- ')}`; + SlackClient.sendMessageToChannel(ClockOutMessageChannel); } context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`); } diff --git a/AutomaticClockOuts/config.js b/AutomaticClockOuts/config.js index 84328ca..13a5075 100644 --- a/AutomaticClockOuts/config.js +++ b/AutomaticClockOuts/config.js @@ -6,7 +6,6 @@ const config = { clientId: process.env["CLIENT_ID"], authority: process.env["AUTHORITY"], clientSecret: process.env["CLIENT_SECRET"], - slackWebHook: process.env["SLACK_WEBHOOK_NOTIFY"], slackApiToken: process.env["SLACK_TOKEN_NOTIFY"], }; diff --git a/AutomaticClockOuts/constants.js b/AutomaticClockOuts/constants.js new file mode 100644 index 0000000..30c6cf9 --- /dev/null +++ b/AutomaticClockOuts/constants.js @@ -0,0 +1,6 @@ +const constants = { + ClockOutMessage : '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:', + time_tracker_channel: 'C01694949JR' +}; + +module.exports = constants; \ No newline at end of file diff --git a/AutomaticClockOuts/readme.md b/AutomaticClockOuts/readme.md index 1832f44..326b208 100644 --- a/AutomaticClockOuts/readme.md +++ b/AutomaticClockOuts/readme.md @@ -37,7 +37,6 @@ KEY='XXX' CLIENT_ID='XXX' AUTHORITY='XXX' CLIENT_SECRET='XXX' -SLACK_WEBHOOK_NOTIFY='XXX' SLACK_TOKEN_NOTIFY='XXX' ``` Check the pinned message in our slack channel to get these values diff --git a/AutomaticClockOuts/slack_client.js b/AutomaticClockOuts/slack_client.js index fcd49a6..ec465d7 100644 --- a/AutomaticClockOuts/slack_client.js +++ b/AutomaticClockOuts/slack_client.js @@ -1,7 +1,7 @@ const { WebClient, LogLevel } = require("@slack/web-api"); const { slackApiToken } = require("./config"); - -const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG}); +const { time_tracker_channel } = require("./constants"); +const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG }); const findUsersInSlack = async () => { const response = await client.users.list(); @@ -15,4 +15,18 @@ const findUsersInSlack = async () => { return usersIdAndEmails; }; -module.exports = { findUsersInSlack }; +const sendMessage = (channel, message) => { + const params = { channel: channel, text: message }; + client.chat.postMessage(params); +}; + +const sendMessageToUser = (userId, message) => { + sendMessage(userId, message); +}; + +// message to public channel +const sendMessageToChannel = (message) => { + sendMessage(time_tracker_channel, message); +}; + +module.exports = { findUsersInSlack, sendMessageToUser, sendMessageToChannel }; diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js b/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js index 52de7d4..4fbf742 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js @@ -2,15 +2,15 @@ const _ = require('lodash'); const CosmosClient = require("@azure/cosmos").CosmosClient; const config = require("./config"); const TimeEntry = require('./time_entry'); -const axios = require('axios'); const MsalClient = require('./msal_client'); const TimeEntryDao = require('./time_entry_dao'); const SlackClient = require('./slack_client'); +const { ClockOutMessage } = require('./constants'); const doClockOut = async (context) => { context.log(`I am going to check how many entries were not clocked out ${new Date()}`); - const {endpoint, key, databaseId, slackWebHook} = config; + const {endpoint, key, databaseId } = config; const client = new CosmosClient({endpoint, key}); const database = client.database(databaseId); const container = database.container('time_entry'); @@ -25,6 +25,7 @@ const doClockOut = async (context) => { let totalClockOutsExecuted = 0; const usersWithClockOut = []; + await Promise.all(entries.map(async (timeEntryAsJson) => { const timeEntry = new TimeEntry(timeEntryAsJson) if (timeEntry.needsToBeClockedOut()) { @@ -32,6 +33,7 @@ const doClockOut = async (context) => { const userId = findSlackUserId(slackUsers,user_email); if(userId){ usersWithClockOut.push("<@"+userId+">"); + SlackClient.sendMessageToUser(userId,ClockOutMessage); } timeEntryAsJson.end_date = timeEntry.getTimeToClockOut() await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson) @@ -39,17 +41,8 @@ const doClockOut = async (context) => { } })); if (usersWithClockOut.length) { - axios.post(slackWebHook, - { - "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- ')}` - } - ) - .then(function (response) { - // console.log(response); - }) - .catch(function (error) { - context.log(error); - }); + const ClockOutMessageChannel=`${ClockOutMessage} \n- ${usersWithClockOut.join('\n- ')}`; + SlackClient.sendMessageToChannel(ClockOutMessageChannel); } context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`); } diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/config.js b/nodejs-functions/src/handlers/automatic-clock-outs/config.js index 84328ca..13a5075 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/config.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/config.js @@ -6,7 +6,6 @@ const config = { clientId: process.env["CLIENT_ID"], authority: process.env["AUTHORITY"], clientSecret: process.env["CLIENT_SECRET"], - slackWebHook: process.env["SLACK_WEBHOOK_NOTIFY"], slackApiToken: process.env["SLACK_TOKEN_NOTIFY"], }; diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/constants.js b/nodejs-functions/src/handlers/automatic-clock-outs/constants.js new file mode 100644 index 0000000..30c6cf9 --- /dev/null +++ b/nodejs-functions/src/handlers/automatic-clock-outs/constants.js @@ -0,0 +1,6 @@ +const constants = { + ClockOutMessage : '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:', + time_tracker_channel: 'C01694949JR' +}; + +module.exports = constants; \ No newline at end of file diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/readme.md b/nodejs-functions/src/handlers/automatic-clock-outs/readme.md index 1832f44..326b208 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/readme.md +++ b/nodejs-functions/src/handlers/automatic-clock-outs/readme.md @@ -37,7 +37,6 @@ KEY='XXX' CLIENT_ID='XXX' AUTHORITY='XXX' CLIENT_SECRET='XXX' -SLACK_WEBHOOK_NOTIFY='XXX' SLACK_TOKEN_NOTIFY='XXX' ``` Check the pinned message in our slack channel to get these values diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/slack_client.js b/nodejs-functions/src/handlers/automatic-clock-outs/slack_client.js index fcd49a6..ec465d7 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/slack_client.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/slack_client.js @@ -1,7 +1,7 @@ const { WebClient, LogLevel } = require("@slack/web-api"); const { slackApiToken } = require("./config"); - -const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG}); +const { time_tracker_channel } = require("./constants"); +const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG }); const findUsersInSlack = async () => { const response = await client.users.list(); @@ -15,4 +15,18 @@ const findUsersInSlack = async () => { return usersIdAndEmails; }; -module.exports = { findUsersInSlack }; +const sendMessage = (channel, message) => { + const params = { channel: channel, text: message }; + client.chat.postMessage(params); +}; + +const sendMessageToUser = (userId, message) => { + sendMessage(userId, message); +}; + +// message to public channel +const sendMessageToChannel = (message) => { + sendMessage(time_tracker_channel, message); +}; + +module.exports = { findUsersInSlack, sendMessageToUser, sendMessageToChannel };