Skip to content

Commit f2773f2

Browse files
committed
feat: TT-184 Slack nick name send a personal message to users
1 parent 810dfdb commit f2773f2

File tree

10 files changed

+58
-36
lines changed

10 files changed

+58
-36
lines changed

AutomaticClockOuts/clock_out.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ const _ = require('lodash');
22
const CosmosClient = require("@azure/cosmos").CosmosClient;
33
const config = require("./config");
44
const TimeEntry = require('./time_entry');
5-
const axios = require('axios');
65
const MsalClient = require('./msal_client');
76
const TimeEntryDao = require('./time_entry_dao');
87
const SlackClient = require('./slack_client');
8+
const { ClockOutMessage } = require('./constants');
99

1010
const doClockOut = async (context) => {
1111
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
1212

13-
const {endpoint, key, databaseId, slackWebHook} = config;
13+
const {endpoint, key, databaseId } = config;
1414
const client = new CosmosClient({endpoint, key});
1515
const database = client.database(databaseId);
1616
const container = database.container('time_entry');
@@ -25,31 +25,24 @@ const doClockOut = async (context) => {
2525

2626
let totalClockOutsExecuted = 0;
2727
const usersWithClockOut = [];
28+
2829
await Promise.all(entries.map(async (timeEntryAsJson) => {
2930
const timeEntry = new TimeEntry(timeEntryAsJson)
3031
if (timeEntry.needsToBeClockedOut()) {
3132
const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id);
3233
const userId = findSlackUserId(slackUsers,user_email);
3334
if(userId){
3435
usersWithClockOut.push("<@"+userId+">");
36+
SlackClient.sendMessageToUser(userId,ClockOutMessage);
3537
}
3638
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
3739
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson)
3840
totalClockOutsExecuted++
3941
}
4042
}));
4143
if (usersWithClockOut.length) {
42-
axios.post(slackWebHook,
43-
{
44-
"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- ')}`
45-
}
46-
)
47-
.then(function (response) {
48-
// console.log(response);
49-
})
50-
.catch(function (error) {
51-
context.log(error);
52-
});
44+
const ClockOutMessageChannel=`${ClockOutMessage} \n- ${usersWithClockOut.join('\n- ')}`;
45+
SlackClient.sendMessageToChannel(ClockOutMessageChannel);
5346
}
5447
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
5548
}

AutomaticClockOuts/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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_NOTIFY"],
109
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"],
1110
};
1211

AutomaticClockOuts/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const constants = {
2+
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:',
3+
time_tracker_channel: 'C01694949JR'
4+
};
5+
6+
module.exports = constants;

AutomaticClockOuts/readme.md

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

AutomaticClockOuts/slack_client.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { WebClient, LogLevel } = require("@slack/web-api");
22
const { slackApiToken } = require("./config");
3-
4-
const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG});
3+
const { time_tracker_channel } = require("./constants");
4+
const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG });
55

66
const findUsersInSlack = async () => {
77
const response = await client.users.list();
@@ -15,4 +15,18 @@ const findUsersInSlack = async () => {
1515
return usersIdAndEmails;
1616
};
1717

18-
module.exports = { findUsersInSlack };
18+
const sendMessage = (channel, message) => {
19+
const params = { channel: channel, text: message };
20+
client.chat.postMessage(params);
21+
};
22+
23+
const sendMessageToUser = (userId, message) => {
24+
sendMessage(userId, message);
25+
};
26+
27+
// message to public channel
28+
const sendMessageToChannel = (message) => {
29+
sendMessage(time_tracker_channel, message);
30+
};
31+
32+
module.exports = { findUsersInSlack, sendMessageToUser, sendMessageToChannel };

nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ const _ = require('lodash');
22
const CosmosClient = require("@azure/cosmos").CosmosClient;
33
const config = require("./config");
44
const TimeEntry = require('./time_entry');
5-
const axios = require('axios');
65
const MsalClient = require('./msal_client');
76
const TimeEntryDao = require('./time_entry_dao');
87
const SlackClient = require('./slack_client');
8+
const { ClockOutMessage } = require('./constants');
99

1010
const doClockOut = async (context) => {
1111
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
1212

13-
const {endpoint, key, databaseId, slackWebHook} = config;
13+
const {endpoint, key, databaseId } = config;
1414
const client = new CosmosClient({endpoint, key});
1515
const database = client.database(databaseId);
1616
const container = database.container('time_entry');
@@ -25,31 +25,24 @@ const doClockOut = async (context) => {
2525

2626
let totalClockOutsExecuted = 0;
2727
const usersWithClockOut = [];
28+
2829
await Promise.all(entries.map(async (timeEntryAsJson) => {
2930
const timeEntry = new TimeEntry(timeEntryAsJson)
3031
if (timeEntry.needsToBeClockedOut()) {
3132
const user_email = findUserEmail(users, timeEntry.timeEntry.owner_id);
3233
const userId = findSlackUserId(slackUsers,user_email);
3334
if(userId){
3435
usersWithClockOut.push("<@"+userId+">");
36+
SlackClient.sendMessageToUser(userId,ClockOutMessage);
3537
}
3638
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
3739
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson)
3840
totalClockOutsExecuted++
3941
}
4042
}));
4143
if (usersWithClockOut.length) {
42-
axios.post(slackWebHook,
43-
{
44-
"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- ')}`
45-
}
46-
)
47-
.then(function (response) {
48-
// console.log(response);
49-
})
50-
.catch(function (error) {
51-
context.log(error);
52-
});
44+
const ClockOutMessageChannel=`${ClockOutMessage} \n- ${usersWithClockOut.join('\n- ')}`;
45+
SlackClient.sendMessageToChannel(ClockOutMessageChannel);
5346
}
5447
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
5548
}

nodejs-functions/src/handlers/automatic-clock-outs/config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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_NOTIFY"],
109
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"],
1110
};
1211

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const constants = {
2+
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:',
3+
time_tracker_channel: 'C01694949JR'
4+
};
5+
6+
module.exports = constants;

nodejs-functions/src/handlers/automatic-clock-outs/readme.md

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

nodejs-functions/src/handlers/automatic-clock-outs/slack_client.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { WebClient, LogLevel } = require("@slack/web-api");
22
const { slackApiToken } = require("./config");
3-
4-
const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG});
3+
const { time_tracker_channel } = require("./constants");
4+
const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG });
55

66
const findUsersInSlack = async () => {
77
const response = await client.users.list();
@@ -15,4 +15,18 @@ const findUsersInSlack = async () => {
1515
return usersIdAndEmails;
1616
};
1717

18-
module.exports = { findUsersInSlack };
18+
const sendMessage = (channel, message) => {
19+
const params = { channel: channel, text: message };
20+
client.chat.postMessage(params);
21+
};
22+
23+
const sendMessageToUser = (userId, message) => {
24+
sendMessage(userId, message);
25+
};
26+
27+
// message to public channel
28+
const sendMessageToChannel = (message) => {
29+
sendMessage(time_tracker_channel, message);
30+
};
31+
32+
module.exports = { findUsersInSlack, sendMessageToUser, sendMessageToChannel };

0 commit comments

Comments
 (0)