From bd85f1085818cad2859530408761f5cd94a80a95 Mon Sep 17 00:00:00 2001 From: Israel Leon Date: Tue, 16 Mar 2021 18:57:00 -0500 Subject: [PATCH] feat: TT-184 Slack nick name send a personal message to users --- AutomaticClockOuts/clock_out.js | 29 +++++++------------ AutomaticClockOuts/config.js | 2 +- AutomaticClockOuts/constants.js | 5 ++++ AutomaticClockOuts/readme.md | 2 +- AutomaticClockOuts/slack_client.js | 21 +++++++++++--- .../automatic-clock-outs/clock_out.js | 29 +++++++------------ .../handlers/automatic-clock-outs/config.js | 2 +- .../automatic-clock-outs/constants.js | 5 ++++ .../handlers/automatic-clock-outs/readme.md | 2 +- .../automatic-clock-outs/slack_client.js | 21 +++++++++++--- 10 files changed, 70 insertions(+), 48 deletions(-) create mode 100644 AutomaticClockOuts/constants.js create mode 100644 nodejs-functions/src/handlers/automatic-clock-outs/constants.js diff --git a/AutomaticClockOuts/clock_out.js b/AutomaticClockOuts/clock_out.js index 52de7d4..235cb96 100644 --- a/AutomaticClockOuts/clock_out.js +++ b/AutomaticClockOuts/clock_out.js @@ -2,16 +2,16 @@ 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 { CLOCK_OUT_MESSAGE } = 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 client = new CosmosClient({endpoint, key}); + const { endpoint, key, databaseId } = config; + const client = new CosmosClient({ endpoint, key}); const database = client.database(databaseId); const container = database.container('time_entry'); const timeEntryDao = new TimeEntryDao(database); @@ -25,13 +25,15 @@ const doClockOut = async (context) => { let totalClockOutsExecuted = 0; const usersWithClockOut = []; + await Promise.all(entries.map(async (timeEntryAsJson) => { const timeEntry = new TimeEntry(timeEntryAsJson) if (timeEntry.needsToBeClockedOut()) { const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id); - const userId = findSlackUserId(slackUsers,user_email); + const userId = findSlackUserId(slackUsers, user_email); if(userId){ usersWithClockOut.push("<@"+userId+">"); + SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE); } 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=`${CLOCK_OUT_MESSAGE} \n- ${usersWithClockOut.join('\n- ')}`; + SlackClient.sendMessageToChannel(ClockOutMessageChannel); } context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`); } @@ -59,9 +52,9 @@ const findUserEmail = (users, id) => { return _.first(user.otherMails) } -const findSlackUserId = (users,email)=>{ +const findSlackUserId = (users, email) => { const user = users.find(user => user.email === email); - return user? user.id:null + return user ? user.id : null } -module.exports = {doClockOut}; +module.exports = { doClockOut }; diff --git a/AutomaticClockOuts/config.js b/AutomaticClockOuts/config.js index 84328ca..b3ba785 100644 --- a/AutomaticClockOuts/config.js +++ b/AutomaticClockOuts/config.js @@ -6,8 +6,8 @@ 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"], + channelId: process.env["ID_CHANNEL_NOTIFY"] }; module.exports = config; diff --git a/AutomaticClockOuts/constants.js b/AutomaticClockOuts/constants.js new file mode 100644 index 0000000..7572aaf --- /dev/null +++ b/AutomaticClockOuts/constants.js @@ -0,0 +1,5 @@ +const constants = { + CLOCK_OUT_MESSAGE : '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:' +}; + +module.exports = constants; diff --git a/AutomaticClockOuts/readme.md b/AutomaticClockOuts/readme.md index 1832f44..4c9d10c 100644 --- a/AutomaticClockOuts/readme.md +++ b/AutomaticClockOuts/readme.md @@ -37,7 +37,7 @@ KEY='XXX' CLIENT_ID='XXX' AUTHORITY='XXX' CLIENT_SECRET='XXX' -SLACK_WEBHOOK_NOTIFY='XXX' SLACK_TOKEN_NOTIFY='XXX' +ID_CHANNEL_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..cbe4e5c 100644 --- a/AutomaticClockOuts/slack_client.js +++ b/AutomaticClockOuts/slack_client.js @@ -1,7 +1,6 @@ const { WebClient, LogLevel } = require("@slack/web-api"); -const { slackApiToken } = require("./config"); - -const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG}); +const { slackApiToken, channelId } = require("./config"); +const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG }); const findUsersInSlack = async () => { const response = await client.users.list(); @@ -15,4 +14,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(channelId, 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..235cb96 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js @@ -2,16 +2,16 @@ 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 { CLOCK_OUT_MESSAGE } = 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 client = new CosmosClient({endpoint, key}); + const { endpoint, key, databaseId } = config; + const client = new CosmosClient({ endpoint, key}); const database = client.database(databaseId); const container = database.container('time_entry'); const timeEntryDao = new TimeEntryDao(database); @@ -25,13 +25,15 @@ const doClockOut = async (context) => { let totalClockOutsExecuted = 0; const usersWithClockOut = []; + await Promise.all(entries.map(async (timeEntryAsJson) => { const timeEntry = new TimeEntry(timeEntryAsJson) if (timeEntry.needsToBeClockedOut()) { const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id); - const userId = findSlackUserId(slackUsers,user_email); + const userId = findSlackUserId(slackUsers, user_email); if(userId){ usersWithClockOut.push("<@"+userId+">"); + SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE); } 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=`${CLOCK_OUT_MESSAGE} \n- ${usersWithClockOut.join('\n- ')}`; + SlackClient.sendMessageToChannel(ClockOutMessageChannel); } context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`); } @@ -59,9 +52,9 @@ const findUserEmail = (users, id) => { return _.first(user.otherMails) } -const findSlackUserId = (users,email)=>{ +const findSlackUserId = (users, email) => { const user = users.find(user => user.email === email); - return user? user.id:null + return user ? user.id : null } -module.exports = {doClockOut}; +module.exports = { doClockOut }; diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/config.js b/nodejs-functions/src/handlers/automatic-clock-outs/config.js index 84328ca..b3ba785 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/config.js +++ b/nodejs-functions/src/handlers/automatic-clock-outs/config.js @@ -6,8 +6,8 @@ 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"], + channelId: process.env["ID_CHANNEL_NOTIFY"] }; module.exports = config; 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..7572aaf --- /dev/null +++ b/nodejs-functions/src/handlers/automatic-clock-outs/constants.js @@ -0,0 +1,5 @@ +const constants = { + CLOCK_OUT_MESSAGE : '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:' +}; + +module.exports = constants; diff --git a/nodejs-functions/src/handlers/automatic-clock-outs/readme.md b/nodejs-functions/src/handlers/automatic-clock-outs/readme.md index 1832f44..4c9d10c 100644 --- a/nodejs-functions/src/handlers/automatic-clock-outs/readme.md +++ b/nodejs-functions/src/handlers/automatic-clock-outs/readme.md @@ -37,7 +37,7 @@ KEY='XXX' CLIENT_ID='XXX' AUTHORITY='XXX' CLIENT_SECRET='XXX' -SLACK_WEBHOOK_NOTIFY='XXX' SLACK_TOKEN_NOTIFY='XXX' +ID_CHANNEL_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..cbe4e5c 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,6 @@ const { WebClient, LogLevel } = require("@slack/web-api"); -const { slackApiToken } = require("./config"); - -const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG}); +const { slackApiToken, channelId } = require("./config"); +const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG }); const findUsersInSlack = async () => { const response = await client.users.list(); @@ -15,4 +14,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(channelId, message); +}; + +module.exports = { findUsersInSlack, sendMessageToUser, sendMessageToChannel };