Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jatin create a function to send email to user's who completed hours but missed summary #1104

Open
wants to merge 7 commits into
base: development
Choose a base branch
from
17 changes: 16 additions & 1 deletion src/cronjobs/userProfileJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const moment = require('moment-timezone');
const userhelper = require('../helpers/userHelper')();

const userProfileJobs = () => {
var summaryNotSubmitted;
const allUserProfileJobs = new CronJob(
// '* * * * *', // Comment out for testing. Run Every minute.
'1 0 * * 0', // Every Sunday, 1 minute past midnight.

async () => {
const SUNDAY = 0; // will change back to 0 after fix
if (moment().tz('America/Los_Angeles').day() === SUNDAY) {
Expand All @@ -26,6 +26,21 @@ const userProfileJobs = () => {
'America/Los_Angeles',
);

const summaryNotSubmittedJobs=new CronJob(
// '* * * * *',
'0 4 * * 0', // Every Sunday at 4AM
async () => {
const SUNDAY = 0;
if (moment().tz('America/Los_Angeles').day() === SUNDAY) {
await userhelper.completeHoursAndMissedSummary();
}
},
null,
false,
'America/Los_Angeles',
)

allUserProfileJobs.start();
summaryNotSubmittedJobs.start();
};
module.exports = userProfileJobs;
92 changes: 89 additions & 3 deletions src/helpers/userHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ const userHelper = function () {
// save updated records in batch (mongoose updateMany) and do asyc email sending
2. Wrap the operation in one transaction to ensure the atomicity of the operation.
*/
var summaryNotSubmitted = []
for (let i = 0; i < users.length; i += 1) {
const user = users[i];

Expand Down Expand Up @@ -672,6 +673,7 @@ const userHelper = function () {
let requestForTimeOffEndingDate;
let requestForTimeOffreason;
let requestForTimeOffEmailBody;
var checkFlag=false; // variable to check if a user has completed the hours but has not submitted the weekly summary.

if (hasTimeOffRequest) {
// eslint-disable-next-line prefer-destructuring
Expand Down Expand Up @@ -737,10 +739,14 @@ const userHelper = function () {
'dddd M-D-YYYY',
)} and ending ${pdtEndOfLastWeek.format('dddd M-D-YYYY')}.`;
}

} else {
description = `System auto-assigned infringement for not submitting a weekly summary for the week starting ${pdtStartOfLastWeek.format(
'dddd M-D-YYYY',
)} and ending ${pdtEndOfLastWeek.format('dddd M-D-YYYY')}.`;
// add firstname and email for sending
summaryNotSubmitted.push({'firstName':user.firstName,'email':user.email});
console.log(summaryNotSubmitted)
}

const infringement = {
Expand Down Expand Up @@ -809,7 +815,6 @@ const userHelper = function () {
} else {
emailsBCCs = null;
}

emailSender(
status.email,
'New Infringement Assigned',
Expand All @@ -822,7 +827,6 @@ const userHelper = function () {
} else if (isNewUser && !timeNotMet && !hasWeeklySummary) {
usersRequiringBlueSqNotification.push(personId);
}

const categories = await dashboardHelper.laborThisWeekByCategory(
personId,
pdtStartOfLastWeek,
Expand Down Expand Up @@ -896,12 +900,93 @@ const userHelper = function () {
const inactiveUsers = await userProfile.find({ isActive: false }, '_id');
for (let i = 0; i < inactiveUsers.length; i += 1) {
const user = inactiveUsers[i];

await processWeeklySummariesByUserId(mongoose.Types.ObjectId(user._id), false);
}
} catch (err) {
logger.logException(err);
}
return summaryNotSubmitted;
};

const missedSummaryTemplate = (firstname) => {
return (
` <p>Dear ${firstname},</p>

<p>When you read this, please input your summary into the software. When you do, please be sure to put it in using the tab for “Last Week”.</p>

<p>Reply to this email once you’ve done this, so I know to review what you’ve submitted. Do this before tomorrow (Monday) at 3 PM (Pacific Time) and I’ll remove this blue square.</p>

<p>With Gratitude,</p>

<p>Jae Sabol</p>
<p>310.755.4693</p>
<p>Zoom: <a href="https://www.tinyurl.com/zoomoc">www.tinyurl.com/zoomoc</a></p>
<p>Primary Email: <a href="mailto:[email protected]">[email protected]</a></p>
<p>Google Email: <a href="mailto:[email protected]">[email protected]</a></p>
<p>Timezone: Los Angeles, CA - Pacific Time</p>`
)
}
// function to send emails to those users who have completed hours but not submitted their summary
const completeHoursAndMissedSummary= async () => {
try{
const users = await userProfile.find(
{ isActive: true },
'_id weeklycommittedHours weeklySummaries missedHours',
);
const pdtStartOfLastWeek = moment()
.tz('America/Los_Angeles')
.startOf('week')
.subtract(1, 'week');

const pdtEndOfLastWeek = moment().tz('America/Los_Angeles').endOf('week').subtract(1, 'week');
for (let i = 0; i < users.length; i += 1) {
const user = users[i];
const person = await userProfile.findById(user._id);
const personId = mongoose.Types.ObjectId(user._id);
let hasWeeklySummary = false;

if (Array.isArray(user.weeklySummaries) && user.weeklySummaries.length) {
const { summary } = user.weeklySummaries[0];
if (summary) {
hasWeeklySummary = true;
}
}
const results = await dashboardHelper.laborthisweek(
personId,
pdtStartOfLastWeek,
pdtEndOfLastWeek,
);

const { timeSpent_hrs: timeSpent } = results[0];

const weeklycommittedHours = user.weeklycommittedHours + (user.missedHours ?? 0);
const timeNotMet = timeSpent < weeklycommittedHours;

const utcStartMoment = moment(pdtStartOfLastWeek).add(1, 'second');
const utcEndMoment = moment(pdtEndOfLastWeek).subtract(1, 'day').subtract(1, 'second');

const requestsForTimeOff = await timeOffRequest.find({
requestFor: personId,
startingDate: { $lte: utcStartMoment },
endingDate: { $gte: utcEndMoment },
});
const hasTimeOffRequest = requestsForTimeOff.length > 0;
// log values of the below used conditions in if statement to know if the email is being sent is correct conditions
if(hasTimeOffRequest===false && timeNotMet===false && hasWeeklySummary===false){
emailSender(
person.email,
'Re: New Infringement Assigned',
missedSummaryTemplate(person.firstName),
null,
'[email protected]',
'[email protected]',
null,
);
}
}
}catch(err){
console.log(err)
}
};

const applyMissedHourForCoreTeam = async () => {
Expand Down Expand Up @@ -2151,6 +2236,7 @@ const userHelper = function () {
getTangibleHoursReportedThisWeekByUserId,
deleteExpiredTokens,
deleteOldTimeOffRequests,
completeHoursAndMissedSummary
};
};

Expand Down
5 changes: 3 additions & 2 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-disable quotes */
require('dotenv').load();

const { app, logger } = require('./app');
const websockets = require('./websockets').default;

require('./startup/db')();
require('./cronjobs/userProfileJobs')();

// const userHelper=require('./helpers/userHelper')();
// userHelper.completeHoursAndMissedSummary()

const port = process.env.PORT || 4500;

const server = app.listen(port, () => {
Expand Down
Loading