Skip to content

Commit

Permalink
Merge pull request #589 from OneCommunityGlobal/development
Browse files Browse the repository at this point in the history
Backend Release to Main [1.14]
  • Loading branch information
one-community authored Oct 27, 2023
2 parents 8b51775 + c57e2ad commit 539c618
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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 <b>${name}</b>:
<p>The following task is available to review:</p>
<p><b>${taskName}</b></p>
<p>Thank you,</p>
<p>One Community</p>`;

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,
'[email protected]',
null,
);
res.status(200).send('Success');
} catch (err) {
res.status(500).send('Failed');
}
};

return {
postTask,
getTasks,
Expand All @@ -864,6 +919,8 @@ const taskController = function (Task) {
moveTask,
getTasksByUserList,
getTasksForTeamsByUser,
updateTaskStatus,
sendReviewReq,
};
};

Expand Down
6 changes: 6 additions & 0 deletions src/routes/taskRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
};

Expand Down

0 comments on commit 539c618

Please sign in to comment.