Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,34 @@ const taskController = function (Task) {
}
};

const deleteTaskById = async (req, res) => {
try {
const { taskId, userId } = req.params;

if (!taskId || !userId) {
return res.status(400).send({ error: 'taskId and userId are required' });
}

if (!(await hasPermission(req.body.requestor, 'deleteTask'))) {
return res.status(403).send({ error: 'Not authorized to delete tasks.' });
}
const task = await Task.findById(taskId);
if (!task) {
return res.status(404).send({ error: 'Task not found' });
}

task.resources = task.resources.filter((r) => r.userID.toString() !== userId);
await task.save();

return res.status(200).send({
message: 'Task removed from user successfully',
});
} catch (err) {
console.error('Error removing user from task:', err);
return res.status(500).send({ error: 'Internal server error' });
}
};

const swap = async function (req, res) {
if (!(await hasPermission(req.body.requestor, 'swapTask'))) {
res.status(403).send({ error: 'You are not authorized to create new projects.' });
Expand Down Expand Up @@ -1354,6 +1382,7 @@ const taskController = function (Task) {
deleteTask,
getTaskById,
updateTask,
deleteTaskById,
importTask,
fixTasks,
updateAllParents,
Expand Down
2 changes: 2 additions & 0 deletions src/routes/taskRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const routes = function (task, userProfile) {

taskRouter.route('/task/updateAllParents/:wbsId/').put(controller.updateAllParents);

taskRouter.route('/task/deleteTask/:taskId/:userId').delete(controller.deleteTaskById);

taskRouter.route('/tasks/swap/').put(controller.swap);

taskRouter.route('/tasks/update/num').put(controller.updateNum);
Expand Down