From 59e1de6771badc023992ab7dc52bc73a63551c2d Mon Sep 17 00:00:00 2001 From: Keith Holliday Date: Wed, 26 Oct 2016 16:01:43 -0500 Subject: [PATCH] Moved approval to subdoc --- .../POST-group_tasks_id_approve_userId.test.js | 6 +++--- .../POST-group_tasks_id_score_direction.test.js | 4 ++-- website/server/controllers/api-v3/tasks.js | 8 ++++---- website/server/controllers/api-v3/tasks/groups.js | 10 +++++----- website/server/libs/taskManager.js | 2 +- website/server/models/group.js | 2 +- website/server/models/task.js | 14 ++++++++------ 7 files changed, 24 insertions(+), 22 deletions(-) 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 2f63df65d5..0fa9807e07 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_TASK_APPROVAL'); expect(member.notifications[0].data.message).to.equal(t('yourTaskHasBeenApproved')); - 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 + expect(syncedTask.group.approval.approved).to.be.true; + expect(syncedTask.group.approval.approvingUser).to.equal(user._id); + expect(syncedTask.group.approval.dateApproved).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 4a565f2bac..8a42ae9f75 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.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 + expect(updatedTask.group.approval.requested).to.equal(true); + expect(updatedTask.group.approval.requestedDate).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 0a4d161b9c..4e4ea6acf9 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.group.requiresApproval && !task.group.approved) { - if (task.group.approvalRequested) { + if (task.group.approval.required && !task.group.approval.approved) { + if (task.group.approval.requested) { throw new NotAuthorized(res.t('taskRequiresApproval')); } - task.group.approvalRequested = true; - task.group.approvalRequestedDate = new Date(); + task.group.approval.requested = true; + task.group.approval.requestedDate = 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 7aad25ca59..db522e86b1 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.group.approvedDate = new Date(); - task.group.approvingUser = user._id; - task.group.approved = true; + task.group.approval.dateApproved = new Date(); + task.group.approval.approvingUser = user._id; + task.group.approval.approved = true; assignedUser.addNotification('GROUP_TASK_APPROVAL', {message: res.t('yourTaskHasBeenApproved')}); @@ -264,8 +264,8 @@ api.getGroupApprovals = { let approvals = await Tasks.Task.find({ 'group.id': groupId, - 'group.approved': false, - 'group.approvalRequested': true, + 'group.approval.approved': false, + 'group.approval.requested': true, }, 'userId group').exec(); res.respond(200, approvals); diff --git a/website/server/libs/taskManager.js b/website/server/libs/taskManager.js index 877541ad72..0fbd9f2bfc 100644 --- a/website/server/libs/taskManager.js +++ b/website/server/libs/taskManager.js @@ -57,7 +57,7 @@ export async function createTasks (req, res, options = {}) { } else if (group) { newTask.group.id = group._id; if (taskData.requiresApproval) { - newTask.group.requiresApproval = true; + newTask.group.approval.required = true; } } else { newTask.userId = user._id; diff --git a/website/server/models/group.js b/website/server/models/group.js index 57b1492275..c3a52ba5d9 100644 --- a/website/server/models/group.js +++ b/website/server/models/group.js @@ -938,7 +938,7 @@ 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; + matchingTask.group.approval.required = taskToSync.group.approval.required; 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 4ff7c6eded..8de85da31e 100644 --- a/website/server/models/task.js +++ b/website/server/models/task.js @@ -69,12 +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}, + approval: { + required: {type: Boolean, default: false}, + approved: {type: Boolean, default: false}, + dateApproved: {type: Date}, + approvingUser: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.']}, + requested: {type: Boolean, default: false}, + requestedDate: {type: Date}, + }, }, reminders: [{