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

Shree create blue square reason scheduler forall #614

Merged
Merged
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://warlord0blog.wordpress.com/2019/09/04/vscode-crlf-vs-lf-battle/

text=lf
text=lf
*.css linguist-vendored eol=lf
*.scss linguist-vendored eol=lf
*.js linguist-vendored eol=lf
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

123 changes: 92 additions & 31 deletions src/controllers/reasonSchedulingController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

const moment = require('moment-timezone');
const UserModel = require('../models/userProfile');
const ReasonModel = require('../models/reason');
const emailSender = require("../utilities/emailSender");


const postReason = async (req, res) => {
Expand All @@ -18,7 +20,7 @@ const postReason = async (req, res) => {
if (moment.tz(reasonData.date, 'America/Los_Angeles').day() !== 0) {
return res.status(400).json({
message:
'The selected day must be a sunday so the code can work properly',
"You must choose the Sunday YOU'LL RETURN as your date. This is so your reason ends up as a note on that blue square.",
errorCode: 0,
});
}
Expand All @@ -37,14 +39,15 @@ const postReason = async (req, res) => {
});
}

//Commented this condition to make reason scheduler available to all the users.
// error case 1
if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
return res.status(403).json({
message:
'You must be an Owner or Administrator to schedule a reason for a Blue Square',
errorCode: 1,
});
}
// if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
// return res.status(403).json({
// message:
// 'You must be an Owner or Administrator to schedule a reason for a Blue Square',
// errorCode: 1,
// });
// }

const foundUser = await UserModel.findById(userId);

Expand Down Expand Up @@ -82,8 +85,38 @@ const postReason = async (req, res) => {
date: savingDate,
userId,
});
await newReason.save();


//await newReason.save();
const savedData = await newReason.save();
if(savedData)
{

//Upon clicking the "Save" button in the Blue Square Reason Scheduler, an email will be automatically sent to the user and Jae.
const subject = `Blue Square Reason for ${foundUser.firstName} ${foundUser.lastName} has been set`;

const emailBody = `<p> Hi ! </p>

<p>This email is to let you know that ${foundUser.firstName} ${foundUser.lastName} has set their Blue Square Reason.</p>

<p>Blue Square Reason : ${newReason.reason} </p>
<p>Scheduled date for the Blue Square Reason: : ${newReason.date} </p>

<p>Thank you,<br />
One Community</p>`;


// 1 hardcoded email- emailSender('@gmail.com', subject, emailBody, null, null);

// 2 user email -
emailSender(`${foundUser.email}`, subject, emailBody, null, null);

//3 - user email and hardcoded email ( After PR approval hardcode Jae's email)
// emailSender(`${foundUser.email},@gmail.com`, subject, emailBody, null, null);
}

return res.sendStatus(200);

} catch (error) {
console.log(error);
return res.status(400).json({
Expand All @@ -98,12 +131,12 @@ const getAllReasons = async (req, res) => {
const { userId } = req.params;

// error case 1
if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
return res.status(403).json({
message:
'You must be an Owner or Administrator to get a reason for a Blue Square',
});
}
// if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
// return res.status(403).json({
// message:
// 'You must be an Owner or Administrator to get a reason for a Blue Square',
// });
// }

const foundUser = await UserModel.findById(userId);

Expand Down Expand Up @@ -136,13 +169,13 @@ const getSingleReason = async (req, res) => {
const { queryDate } = req.query;

// error case 1
if (requestor.role !== 'Administrator' && requestor.role !== 'Owner') {
return res.status(403).json({
message:
"You must be an Administrator or Owner to be able to get a single reason by the user's ID",
errorCode: 1,
});
}
// if (requestor.role !== 'Administrator' && requestor.role !== 'Owner') {
// return res.status(403).json({
// message:
// "You must be an Administrator or Owner to be able to get a single reason by the user's ID",
// errorCode: 1,
// });
// }
const foundUser = await UserModel.findById(userId);

// error case 2
Expand Down Expand Up @@ -185,13 +218,13 @@ const patchReason = async (req, res) => {
const { userId } = req.params;

// error case 1
if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
return res.status(403).json({
message:
'You must be an Owner or Administrator to schedule a reason for a Blue Square',
errorCode: 1,
});
}
// if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
// return res.status(403).json({
// message:
// 'You must be an Owner or Administrator to schedule a reason for a Blue Square',
// errorCode: 1,
// });
// }

if (!reasonData.message) {
return res.status(400).json({
Expand Down Expand Up @@ -226,7 +259,35 @@ const patchReason = async (req, res) => {
}

foundReason.reason = reasonData.message;
await foundReason.save();

const savedData = await foundReason.save();
if(savedData)
{

//Upon clicking the "Save" button in the Blue Square Reason Scheduler, an email will be automatically sent to the user and Jae.
const subject = `Blue Square Reason for ${foundUser.firstName} ${foundUser.lastName} has been updated`;

const emailBody = `<p> Hi ! </p>

<p>This email is to let you know that ${foundUser.firstName} ${foundUser.lastName} has updated their Blue Square Reason.</p>

<p>Updated Blue Square Reason : ${foundReason.reason} </p>
<p>Scheduled date for the Blue Square Reason: : ${foundReason.date} </p>

<p>Thank you,<br />
One Community</p>`;


// 1 hardcoded email- emailSender('@gmail.com', subject, emailBody, null, null);

// 2 user email -
emailSender(`${foundUser.email}`, subject, emailBody, null, null);

//3 - user email and hardcoded email ( After PR approval hardcode Jae's email)
// emailSender(`${foundUser.email},@gmail.com`, subject, emailBody, null, null);


}

return res.status(200).json({
message: 'Reason Updated!',
Expand All @@ -243,7 +304,7 @@ const deleteReason = async (req, res) => {
const { reasonData, requestor } = req.body;
const { userId } = req.params;

// error case 1
//error case 1
if (requestor.role !== 'Owner' && requestor.role !== 'Administrator') {
return res.status(403).json({
message:
Expand Down
Loading