mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Added task approve route
This commit is contained in:
@@ -29,27 +29,35 @@ describe('POST /tasks/:id/approve/:userId', () => {
|
|||||||
type: 'todo',
|
type: 'todo',
|
||||||
requiresApproval: true,
|
requiresApproval: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
await user.post(`/tasks/${task._id}/assign/${member._id}`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('approves an assigned user', async () => {
|
it('errors when user is not assigned', async () => {
|
||||||
let memberTasks = await member.get('/tasks/user');
|
|
||||||
let syncedTask = find(memberTasks, findAssignedTask);
|
|
||||||
|
|
||||||
await expect(user.post(`/tasks/${task._id}/approve/${member._id}`))
|
await expect(user.post(`/tasks/${task._id}/approve/${member._id}`))
|
||||||
.to.eventually.be.rejected.and.eql({
|
.to.eventually.be.rejected.and.to.eql({
|
||||||
code: 401,
|
code: 404,
|
||||||
error: 'NotAuthorized',
|
error: 'NotFound',
|
||||||
message: t('taskRequiresApproval'),
|
message: t('taskNotFound'),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// it('prevents user from scoring a task that needs to be approved', async () => {
|
it('errors when user is not the group leader', async () => {
|
||||||
// await user.post(`/tasks/${task._id}/score/up`);
|
await user.post(`/tasks/${task._id}/assign/${member._id}`);
|
||||||
// let task = await user.get(`/tasks/${todo._id}`);
|
await expect(member.post(`/tasks/${task._id}/approve/${member._id}`))
|
||||||
//
|
.to.eventually.be.rejected.and.to.eql({
|
||||||
// expect(task.completed).to.equal(true);
|
code: 401,
|
||||||
// expect(task.dateCompleted).to.be.a('string'); // date gets converted to a string as json doesn't have a Date type
|
error: 'NotAuthorized',
|
||||||
// });
|
message: t('onlyGroupLeaderCanEditTasks'),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('approves an assigned user', async () => {
|
||||||
|
await user.post(`/tasks/${task._id}/assign/${member._id}`);
|
||||||
|
await user.post(`/tasks/${task._id}/approve/${member._id}`);
|
||||||
|
|
||||||
|
let memberTasks = await member.get('/tasks/user');
|
||||||
|
let syncedTask = find(memberTasks, findAssignedTask);
|
||||||
|
|
||||||
|
expect(syncedTask.approved).to.be.true;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -196,32 +196,31 @@ api.approveTask = {
|
|||||||
middlewares: [ensureDevelpmentMode, authWithHeaders()],
|
middlewares: [ensureDevelpmentMode, authWithHeaders()],
|
||||||
async handler (req, res) {
|
async handler (req, res) {
|
||||||
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
req.checkParams('taskId', res.t('taskIdRequired')).notEmpty().isUUID();
|
||||||
req.checkParams('assignedUserId', res.t('userIdRequired')).notEmpty().isUUID();
|
req.checkParams('userId', res.t('userIdRequired')).notEmpty().isUUID();
|
||||||
|
|
||||||
let reqValidationErrors = req.validationErrors();
|
let reqValidationErrors = req.validationErrors();
|
||||||
if (reqValidationErrors) throw reqValidationErrors;
|
if (reqValidationErrors) throw reqValidationErrors;
|
||||||
|
|
||||||
let user = res.locals.user;
|
let user = res.locals.user;
|
||||||
let assignedUserId = req.params.assignedUserId;
|
let assignedUserId = req.params.userId;
|
||||||
let assignedUser = await User.findById(assignedUserId);
|
|
||||||
|
|
||||||
let taskId = req.params.taskId;
|
let taskId = req.params.taskId;
|
||||||
let task = await Tasks.Task.findByIdOrAlias(taskId, user._id);
|
let task = await Tasks.Task.findOne({
|
||||||
|
'group.taskId': taskId,
|
||||||
|
'userId': assignedUserId,
|
||||||
|
});
|
||||||
|
|
||||||
if (!task) {
|
if (!task) {
|
||||||
throw new NotFound(res.t('taskNotFound'));
|
throw new NotFound(res.t('taskNotFound'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!task.group.id) {
|
|
||||||
throw new NotAuthorized(res.t('onlyGroupTasksCanBeAssigned'));
|
|
||||||
}
|
|
||||||
|
|
||||||
let group = await Group.getGroup({user, groupId: task.group.id, fields: requiredGroupFields});
|
let group = await Group.getGroup({user, groupId: task.group.id, fields: requiredGroupFields});
|
||||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||||
|
|
||||||
if (group.leader !== user._id) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
if (group.leader !== user._id) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks'));
|
||||||
|
|
||||||
await group.unlinkTask(task, assignedUser);
|
task.approved = true;
|
||||||
|
await task.save();
|
||||||
|
|
||||||
res.respond(200, task);
|
res.respond(200, task);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user