Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix: Azure endpoint return all users from ioet
  • Loading branch information
cxcarvaj committed Feb 16, 2022
commit 504a0f376cb25bdbd360340123b9ef798cb6ad3d
46 changes: 22 additions & 24 deletions nodejs-functions/src/handlers/automatic-clock-outs/clock_out.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const SlackClient = require('./slack_client');
const { CLOCK_OUT_MESSAGE, CLOCK_OUT_MESSAGE_MIDNIGHT } = require('./constants');

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

Expand All @@ -23,52 +23,50 @@ const doClockOut = async (context) => {
const slackUsers = await SlackClient.findUsersInSlack();

const { resources: entries } = await timeEntryDao.getEntriesWithNoEndDate();
console.log(`Checking for time-entries that need to be clocked out`);
context.log(`Checking for time-entries that need to be clocked out`);

let totalClockOutsExecuted = 0;

await Promise.all(
entries.map(async (timeEntryAsJson) => {
const timeEntry = new TimeEntry(timeEntryAsJson);
const { userName, userEmail } = findUserData(users, timeEntry.timeEntry.owner_id);
const { userName, userEmail } = findUserData( users, timeEntry.timeEntry.owner_id);
const userId = findSlackUserId(slackUsers, userEmail);
if( userEmail === '[email protected]'){
if (timeEntry.needsToBeClockedOut()) {
if (userId) {
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE.replace('%user_name%', userName));
}
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut();
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
totalClockOutsExecuted++;

if (timeEntry.needsToBeClockedOut()) {
if (userId) {
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE.replace('%user_name%', userName));
}

else if (timeEntry.needsToBeClockedOutMidnight()) {
if (userId) {
SlackClient.sendMessageToUser(userId, CLOCK_OUT_MESSAGE_MIDNIGHT.replace('%user_name%', userName));
}
timeEntryAsJson.end_date = timeEntry.getTimeToClockOutMidnight();
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
totalClockOutsExecuted++;
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut();
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
totalClockOutsExecuted++;

} else if (timeEntry.needsToBeClockedOutMidnight()) {
if (userId) {
SlackClient.sendMessageToUser(userId,CLOCK_OUT_MESSAGE_MIDNIGHT.replace('%user_name%', userName));
}
timeEntryAsJson.end_date = timeEntry.getTimeToClockOutMidnight();
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson);
totalClockOutsExecuted++;
}

})
);

console.log(
context.log(
`I just clocked out ${totalClockOutsExecuted} entries, thanks are not needed...`
);
};


const findUserData = (users, id) => {
const user = users.find((user) => user.id === id);
return user ? { userName: user.name.split(' ')[0], userEmail: (user.email) } : {};
return user
? { userName: user.name.split(' ')[0], userEmail: user.email }
: {};
};

const findSlackUserId = (slackUsers, email) => {
const user = slackUsers.find((slackUser) => slackUser.email === email);
return user ? user.id : null;
};
doClockOut()

module.exports = { doClockOut };