diff --git a/test/api/v3/integration/tasks/groups/POST-group_tasks_id_approve_userId.test.js b/test/api/v3/integration/tasks/groups/POST-group_tasks_id_approve_userId.test.js index daf2a478ed..247553f5f6 100644 --- a/test/api/v3/integration/tasks/groups/POST-group_tasks_id_approve_userId.test.js +++ b/test/api/v3/integration/tasks/groups/POST-group_tasks_id_approve_userId.test.js @@ -63,8 +63,8 @@ describe('POST /tasks/:id/approve/:userId', () => { expect(member.notifications[0].type).to.equal('GROUP'); expect(member.notifications[0].data.message).to.equal(t('yourTaskHasBeenApproved')); - expect(syncedTask.approved).to.be.true; - expect(syncedTask.approvingUser).to.equal(user._id); - expect(syncedTask.approvedDate).to.be.a('string'); // date gets converted to a string as json doesn't have a Date type + expect(syncedTask.group.approved).to.be.true; + expect(syncedTask.group.approvingUser).to.equal(user._id); + expect(syncedTask.group.approvedDate).to.be.a('string'); // date gets converted to a string as json doesn't have a Date type }); }); diff --git a/test/api/v3/integration/tasks/groups/POST-group_tasks_id_score_direction.test.js b/test/api/v3/integration/tasks/groups/POST-group_tasks_id_score_direction.test.js index 89a5407091..82ee1df755 100644 --- a/test/api/v3/integration/tasks/groups/POST-group_tasks_id_score_direction.test.js +++ b/test/api/v3/integration/tasks/groups/POST-group_tasks_id_score_direction.test.js @@ -50,8 +50,8 @@ describe('POST /tasks/:id/score/:direction', () => { })); expect(response.message).to.equal(t('taskApprovalHasBeenRequested')); - expect(updatedTask.approvalRequested).to.equal(true); - expect(updatedTask.approvalRequestedDate).to.be.a('string'); // date gets converted to a string as json doesn't have a Date type + expect(updatedTask.group.approvalRequested).to.equal(true); + expect(updatedTask.group.approvalRequestedDate).to.be.a('string'); // date gets converted to a string as json doesn't have a Date type }); it('errors when approval has already been requested', async () => { diff --git a/website/server/controllers/api-v3/tasks.js b/website/server/controllers/api-v3/tasks.js index bf91c62103..2d102e8be0 100644 --- a/website/server/controllers/api-v3/tasks.js +++ b/website/server/controllers/api-v3/tasks.js @@ -316,13 +316,13 @@ api.scoreTask = { if (!task) throw new NotFound(res.t('taskNotFound')); - if (task.requiresApproval && !task.approved) { - if (task.approvalRequested) { + if (task.group.requiresApproval && !task.group.approved) { + if (task.group.approvalRequested) { throw new NotAuthorized(res.t('taskRequiresApproval')); } - task.approvalRequested = true; - task.approvalRequestedDate = new Date(); + task.group.approvalRequested = true; + task.group.approvalRequestedDate = new Date(); let group = await Group.getGroup({user, groupId: task.group.id, fields: requiredGroupFields}); let groupLeader = await User.findById(group.leader); // Use this method so we can get access to notifications diff --git a/website/server/controllers/api-v3/tasks/groups.js b/website/server/controllers/api-v3/tasks/groups.js index 78d978f77a..2e9bf77114 100644 --- a/website/server/controllers/api-v3/tasks/groups.js +++ b/website/server/controllers/api-v3/tasks/groups.js @@ -221,9 +221,9 @@ api.approveTask = { if (group.leader !== user._id) throw new NotAuthorized(res.t('onlyGroupLeaderCanEditTasks')); - task.approvedDate = new Date(); - task.approvingUser = user._id; - task.approved = true; + task.group.approvedDate = new Date(); + task.group.approvingUser = user._id; + task.group.approved = true; assignedUser.addNotification('GROUP', {message: res.t('yourTaskHasBeenApproved')}); @@ -264,8 +264,8 @@ api.getGroupApprovals = { let approvals = await Tasks.Task.find({ 'group.id': groupId, - approved: false, - approvalRequested: true, + 'group.approved': false, + 'group.approvalRequested': true, }).exec(); res.respond(200, approvals); diff --git a/website/server/libs/taskManager.js b/website/server/libs/taskManager.js index 1ba5ef587c..e5edd43d2d 100644 --- a/website/server/libs/taskManager.js +++ b/website/server/libs/taskManager.js @@ -51,6 +51,10 @@ export async function createTasks (req, res, options = {}) { let taskType = taskData.type; let newTask = new Tasks[taskType](Tasks.Task.sanitize(taskData)); + if (taskData.requiresApproval) { + newTask.group.requiresApproval = true; + } + if (challenge) { newTask.challenge.id = challenge.id; } else if (group) { diff --git a/website/server/models/group.js b/website/server/models/group.js index da25eef480..57b1492275 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -938,6 +938,8 @@ schema.methods.syncTask = async function groupSyncTask (taskToSync, user) { if (orderList.indexOf(matchingTask._id) === -1 && (matchingTask.type !== 'todo' || !matchingTask.completed)) orderList.push(matchingTask._id); } + matchingTask.group.requiresApproval = taskToSync.group.requiresApproval; + if (!matchingTask.notes) matchingTask.notes = taskToSync.notes; // don't override the notes, but provide it if not provided if (matchingTask.tags.indexOf(group._id) === -1) matchingTask.tags.push(group._id); // add tag if missing diff --git a/website/server/models/task.js b/website/server/models/task.js index dfe9bc593f..4ff7c6eded 100644 --- a/website/server/models/task.js +++ b/website/server/models/task.js @@ -69,15 +69,14 @@ export let TaskSchema = new Schema({ broken: {type: String, enum: ['GROUP_DELETED', 'TASK_DELETED', 'UNSUBSCRIBED']}, assignedUsers: [{type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}], taskId: {type: String, ref: 'Task', validate: [validator.isUUID, 'Invalid uuid.']}, + requiresApproval: {type: Boolean, default: false}, + approved: {type: Boolean, default: false}, + approvedDate: {type: Date}, + approvingUser: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}, + approvalRequested: {type: Boolean, default: false}, + approvalRequestedDate: {type: Date}, }, - requiresApproval: {type: Boolean, default: false}, - approved: {type: Boolean, default: false}, - approvedDate: {type: Date}, - approvingUser: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}, - approvalRequested: {type: Boolean, default: false}, - approvalRequestedDate: {type: Date}, - reminders: [{ _id: false, id: {type: String, validate: [validator.isUUID, 'Invalid uuid.'], default: shared.uuid, required: true},