From b6eca169302b4bc00846cb83afe8226525a22c89 Mon Sep 17 00:00:00 2001 From: AriaYu927 Date: Tue, 25 Jul 2023 19:07:39 +0800 Subject: [PATCH 1/3] send email --- src/controllers/taskController.js | 41 +++++++++++++++++++++++++++++++ src/routes/taskRouter.js | 3 +++ 2 files changed, 44 insertions(+) diff --git a/src/controllers/taskController.js b/src/controllers/taskController.js index c7d61c1bd..c6858b213 100644 --- a/src/controllers/taskController.js +++ b/src/controllers/taskController.js @@ -3,6 +3,8 @@ const wbs = require('../models/wbs'); const timeEntryHelper = require('../helpers/timeEntryHelper')(); const taskHelper = require('../helpers/taskHelper')(); const hasPermission = require('../utilities/permissions'); +const emailSender = require('../utilities/emailSender'); +const userProfile = require('../models/userProfile'); const taskController = function (Task) { const getTasks = (req, res) => { @@ -1098,6 +1100,44 @@ const taskController = function (Task) { }); }; + const getReviewReqEmailBody = function (name, taskName) { + const text = `New Task Review Request From ${name}: +

The following task is available to review:

+

${taskName}

+

Thank you,

+

One Community

`; + + return text; + }; + + const getRecipients = async function (myUserId) { + const user = await userProfile.findById(myUserId); + const membership = await userProfile.find({ teams: user.teams, role: ['Administrator', 'Manager', 'Mentor'] }); + const emails = membership.map(a => a.email); + return emails; + }; + + const sendReviewReq = async function (req, res) { + const { + myUserId, name, taskName, + } = req.body; + const emailBody = getReviewReqEmailBody(name, taskName); + const recipients = await getRecipients(myUserId); + + try { + emailSender( + recipients, + `Review Request from ${name}`, + emailBody, + 'highestgoodnetwork@gmail.com', + null, + ); + res.status(200).send('Success'); + } catch (err) { + res.status(500).send('Failed'); + } + }; + return { postTask, getTasks, @@ -1114,6 +1154,7 @@ const taskController = function (Task) { getTasksByUserList, getTasksForTeamsByUser, updateChildrenQty, + sendReviewReq, }; }; diff --git a/src/routes/taskRouter.js b/src/routes/taskRouter.js index 00db03325..e1d460d83 100644 --- a/src/routes/taskRouter.js +++ b/src/routes/taskRouter.js @@ -46,6 +46,9 @@ const routes = function (task, userProfile) { wbsRouter.route('/user/:userId/teams/tasks') .get(controller.getTasksForTeamsByUser); + wbsRouter.route('/tasks/reviewreq/:userId') + .post(controller.sendReviewReq); + return wbsRouter; }; From 24f8faa5d88692bd640d175f0d09659d6aecae6c Mon Sep 17 00:00:00 2001 From: AriaYu927 Date: Wed, 26 Jul 2023 15:15:33 +0800 Subject: [PATCH 2/3] solve the bug occurs when find users by team --- src/controllers/taskController.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/controllers/taskController.js b/src/controllers/taskController.js index c6858b213..26be8a539 100644 --- a/src/controllers/taskController.js +++ b/src/controllers/taskController.js @@ -1111,10 +1111,15 @@ const taskController = function (Task) { }; const getRecipients = async function (myUserId) { + const recipients = []; const user = await userProfile.findById(myUserId); - const membership = await userProfile.find({ teams: user.teams, role: ['Administrator', 'Manager', 'Mentor'] }); - const emails = membership.map(a => a.email); - return emails; + const membership = await userProfile.find({ role: ['Administrator', 'Manager', 'Mentor'] }); + membership.forEach((member) => { + if (member.teams.some(team => user.teams.includes(team))) { + recipients.push(member.email); + } + }); + return recipients; }; const sendReviewReq = async function (req, res) { From f376fa8f049717d07eb12cdfd2eab9bcbf250da2 Mon Sep 17 00:00:00 2001 From: AriaYu927 Date: Thu, 19 Oct 2023 22:37:33 +0800 Subject: [PATCH 3/3] add update task status function --- src/controllers/taskController.js | 15 +++++++++++++-- src/routes/taskRouter.js | 3 +++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/controllers/taskController.js b/src/controllers/taskController.js index 7a19fd853..9bcf071de 100644 --- a/src/controllers/taskController.js +++ b/src/controllers/taskController.js @@ -776,7 +776,6 @@ const taskController = function (Task) { }); res.status(200).send(task); - } catch (error) { // Generic error message, you can adjust as needed res.status(500).send({ error: 'Internal Server Error', details: error.message }); @@ -850,6 +849,17 @@ const taskController = function (Task) { } }; + const updateTaskStatus = async (req, res) => { + const { taskId } = req.params; + + Task.findOneAndUpdate( + { _id: mongoose.Types.ObjectId(taskId) }, + { ...req.body, modifiedDatetime: Date.now() }, + ) + .then(() => res.status(201).send()) + .catch(error => res.status(404).send(error)); + }; + const getReviewReqEmailBody = function (name, taskName) { const text = `New Task Review Request From ${name}:

The following task is available to review:

@@ -863,7 +873,7 @@ const taskController = function (Task) { const getRecipients = async function (myUserId) { const recipients = []; const user = await userProfile.findById(myUserId); - const membership = await userProfile.find({ role: ['Administrator', 'Manager', 'Mentor'] }); + const membership = await userProfile.find({ role: { $in: ['Administrator', 'Manager', 'Mentor'] } }); membership.forEach((member) => { if (member.teams.some(team => user.teams.includes(team))) { recipients.push(member.email); @@ -909,6 +919,7 @@ const taskController = function (Task) { moveTask, getTasksByUserList, getTasksForTeamsByUser, + updateTaskStatus, sendReviewReq, }; }; diff --git a/src/routes/taskRouter.js b/src/routes/taskRouter.js index ef972f2cb..46e467c9d 100644 --- a/src/routes/taskRouter.js +++ b/src/routes/taskRouter.js @@ -28,6 +28,9 @@ const routes = function (task, userProfile) { wbsRouter.route('/task/update/:taskId') .put(controller.updateTask); + wbsRouter.route('/task/updateStatus/:taskId') + .put(controller.updateTaskStatus); + wbsRouter.route('/task/updateAllParents/:wbsId/') .put(controller.updateAllParents);