Skip to content

Commit 47c1dd7

Browse files
committed
feat: TT-16 Tag people using their Slack nickname on Slack
1 parent 6985bf9 commit 47c1dd7

File tree

6 files changed

+180
-11
lines changed

6 files changed

+180
-11
lines changed

AutomaticClockOuts/clock_out.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const CosmosClient = require("@azure/cosmos").CosmosClient;
22
const config = require("./config");
33
const TimeEntry = require('./time_entry');
44
const 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

89
const 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

5466
module.exports = {doClockOut};

AutomaticClockOuts/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const config = {
66
clientId: process.env["CLIENT_ID"],
77
authority: process.env["AUTHORITY"],
88
clientSecret: process.env["CLIENT_SECRET"],
9-
slackWebHook: process.env["SLACK_WEBHOOK"]
9+
slackWebHook: process.env["SLACK_WEBHOOK_NOTIFY"],
10+
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"],
1011
};
1112

1213
module.exports = config;

AutomaticClockOuts/package-lock.json

Lines changed: 138 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

AutomaticClockOuts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"@azure/cosmos": "3.5.2",
1111
"@azure/msal-node": "^1.0.0-alpha.5",
12+
"@slack/web-api": "^6.0.0",
1213
"axios": "^0.20.0",
1314
"dotenv": "^8.2.0",
1415
"moment": "^2.27.0",

AutomaticClockOuts/readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ KEY='XXX'
3737
CLIENT_ID='XXX'
3838
AUTHORITY='XXX'
3939
CLIENT_SECRET='XXX'
40-
SLACK_WEBHOOK='XXX'
40+
SLACK_WEBHOOK_NOTIFY='XXX'
41+
SLACK_TOKEN_NOTIFY='XXX'
4142
```
4243
Check the pinned message in our slack channel to get these values

AutomaticClockOuts/slack_client.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { WebClient, LogLevel } = require("@slack/web-api");
2+
const { slackApiToken } = require("./config");
3+
4+
const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG,});
5+
6+
const findUsersInSlack = async () => {
7+
const response = await client.users.list();
8+
let usersIdAndEmails = [];
9+
if (response.ok) {
10+
slackUsers = response.members;
11+
usersIdAndEmails = slackUsers
12+
.filter((user) => user.profile.hasOwnProperty("email") && !user.deleted)
13+
.map((user) => ({ id: user.id, email: user.profile.email }));
14+
}
15+
return usersIdAndEmails;
16+
};
17+
18+
module.exports = { findUsersInSlack };

0 commit comments

Comments
 (0)