Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions AutomaticClockOuts/clock_out.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -25,31 +25,24 @@ 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)
totalClockOutsExecuted++
}
}));
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...`);
}
Expand All @@ -59,9 +52,11 @@ 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};
doClockOut(console)

module.exports = { doClockOut };
2 changes: 1 addition & 1 deletion AutomaticClockOuts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 5 additions & 0 deletions AutomaticClockOuts/constants.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion AutomaticClockOuts/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 17 additions & 4 deletions AutomaticClockOuts/slack_client.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 };
29 changes: 11 additions & 18 deletions nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -25,31 +25,24 @@ 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)
totalClockOutsExecuted++
}
}));
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...`);
}
Expand All @@ -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 };
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 17 additions & 4 deletions nodejs-functions/src/handlers/automatic-clock-outs/slack_client.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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 };