diff --git a/src/controllers/taskController.js b/src/controllers/taskController.js index d5e048537..9bcf071de 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) => { @@ -774,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 }); @@ -848,6 +849,60 @@ 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:

+

${taskName}

+

Thank you,

+

One Community

`; + + return text; + }; + + const getRecipients = async function (myUserId) { + const recipients = []; + const user = await userProfile.findById(myUserId); + 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); + } + }); + return recipients; + }; + + 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, @@ -864,6 +919,8 @@ const taskController = function (Task) { moveTask, getTasksByUserList, getTasksForTeamsByUser, + updateTaskStatus, + sendReviewReq, }; }; diff --git a/src/routes/taskRouter.js b/src/routes/taskRouter.js index 66e0520e3..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); @@ -46,6 +49,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; };