Skip to content

Commit 6bbfc1b

Browse files
authored
feat: TT-184 Slack nick name send a personal message to users (#44)
* feat: TT-184 Slack nick name send a personal message to users * feat: TT-184 Slack nick name send a personal message to users
1 parent 810dfdb commit 6bbfc1b

File tree

10 files changed

+72
-48
lines changed

10 files changed

+72
-48
lines changed

AutomaticClockOuts/clock_out.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ 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 { CLOCK_OUT_MESSAGE } = 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;
14-
const client = new CosmosClient({endpoint, key});
13+
const { endpoint, key, databaseId } = config;
14+
const client = new CosmosClient({ endpoint, key });
1515
const database = client.database(databaseId);
1616
const container = database.container('time_entry');
1717
const timeEntryDao = new TimeEntryDao(database);
@@ -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);
32-
const userId = findSlackUserId(slackUsers,user_email);
33+
const userId = findSlackUserId(slackUsers, user_email);
3334
if(userId){
3435
usersWithClockOut.push("<@"+userId+">");
36+
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE);
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=`${CLOCK_OUT_MESSAGE} \n- ${usersWithClockOut.join('\n- ')}`;
45+
SlackClient.sendMessageToChannel(ClockOutMessageChannel);
5346
}
5447
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
5548
}
@@ -59,9 +52,11 @@ const findUserEmail = (users, id) => {
5952
return _.first(user.otherMails)
6053
}
6154

62-
const findSlackUserId = (users,email)=>{
55+
const findSlackUserId = (users, email) => {
6356
const user = users.find(user => user.email === email);
64-
return user? user.id:null
57+
return user ? user.id : null
6558
}
6659

67-
module.exports = {doClockOut};
60+
doClockOut(console)
61+
62+
module.exports = { doClockOut };

AutomaticClockOuts/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +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_NOTIFY"],
109
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"],
10+
channelId: process.env["ID_CHANNEL_NOTIFY"]
1111
};
1212

1313
module.exports = config;

AutomaticClockOuts/constants.js

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

AutomaticClockOuts/readme.md

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

AutomaticClockOuts/slack_client.js

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

65
const findUsersInSlack = async () => {
76
const response = await client.users.list();
@@ -15,4 +14,18 @@ const findUsersInSlack = async () => {
1514
return usersIdAndEmails;
1615
};
1716

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

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ 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 { CLOCK_OUT_MESSAGE } = 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;
14-
const client = new CosmosClient({endpoint, key});
13+
const { endpoint, key, databaseId } = config;
14+
const client = new CosmosClient({ endpoint, key });
1515
const database = client.database(databaseId);
1616
const container = database.container('time_entry');
1717
const timeEntryDao = new TimeEntryDao(database);
@@ -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);
32-
const userId = findSlackUserId(slackUsers,user_email);
33+
const userId = findSlackUserId(slackUsers, user_email);
3334
if(userId){
3435
usersWithClockOut.push("<@"+userId+">");
36+
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE);
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=`${CLOCK_OUT_MESSAGE} \n- ${usersWithClockOut.join('\n- ')}`;
45+
SlackClient.sendMessageToChannel(ClockOutMessageChannel);
5346
}
5447
context.log(`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`);
5548
}
@@ -59,9 +52,9 @@ const findUserEmail = (users, id) => {
5952
return _.first(user.otherMails)
6053
}
6154

62-
const findSlackUserId = (users,email)=>{
55+
const findSlackUserId = (users, email) => {
6356
const user = users.find(user => user.email === email);
64-
return user? user.id:null
57+
return user ? user.id : null
6558
}
6659

67-
module.exports = {doClockOut};
60+
module.exports = { doClockOut };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +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_NOTIFY"],
109
slackApiToken: process.env["SLACK_TOKEN_NOTIFY"],
10+
channelId: process.env["ID_CHANNEL_NOTIFY"]
1111
};
1212

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ KEY='XXX'
3737
CLIENT_ID='XXX'
3838
AUTHORITY='XXX'
3939
CLIENT_SECRET='XXX'
40-
SLACK_WEBHOOK_NOTIFY='XXX'
4140
SLACK_TOKEN_NOTIFY='XXX'
41+
ID_CHANNEL_NOTIFY='XXX'
4242
```
4343
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 & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { WebClient, LogLevel } = require("@slack/web-api");
2-
const { slackApiToken } = require("./config");
3-
4-
const client = new WebClient(slackApiToken,{logLevel: LogLevel.DEBUG});
2+
const { slackApiToken, channelId } = require("./config");
3+
const client = new WebClient(slackApiToken, { logLevel: LogLevel.DEBUG });
54

65
const findUsersInSlack = async () => {
76
const response = await client.users.list();
@@ -15,4 +14,18 @@ const findUsersInSlack = async () => {
1514
return usersIdAndEmails;
1615
};
1716

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

0 commit comments

Comments
 (0)