From 40fd5984a98a6f51745aa18f436cdae175cb897b Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 5 Dec 2016 13:49:12 -0500 Subject: [PATCH] Added "assigned_by" to status changes --- models/comment.js | 20 +++++++++++++++----- routes/api/comments/index.js | 2 +- tests/models/comment.js | 23 +++++++++++++++-------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/models/comment.js b/models/comment.js index d587a8f3d..af9aa4cb1 100644 --- a/models/comment.js +++ b/models/comment.js @@ -16,6 +16,13 @@ const StatusSchema = new Schema({ 'premod', ], }, + + // The User ID of the user that assigned the status. + assigned_by: { + type: String, + default: null + }, + created_at: Date }, { _id: false @@ -253,16 +260,19 @@ CommentSchema.statics.moderationQueue = (moderation, asset_id = false) => { }; /** - * Change the status of a comment. - * @param {String} id identifier of the comment (uuid) - * @param {String} status the new status of the comment + * Pushes a new status in for the user. + * @param {String} id identifier of the comment (uuid) + * @param {String} status the new status of the comment + * @param {String} assigned_by the user id for the user who performed the + * moderation action * @return {Promise} */ -CommentSchema.statics.changeStatus = (id, status) => Comment.findOneAndUpdate({id}, { +CommentSchema.statics.pushStatus = (id, status, assigned_by = null) => Comment.update({id}, { $push: { status: { type: status, - created_at: new Date() + created_at: new Date(), + assigned_by } } }); diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index fe1e8840d..406f4c8ab 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -126,7 +126,7 @@ router.put('/:comment_id/status', authorization.needed('admin'), (req, res, next } = req.body; Comment - .changeStatus(req.params.comment_id, status) + .pushStatus(req.params.comment_id, status, req.user.id) .then(() => { res.status(204).end(); }) diff --git a/tests/models/comment.js b/tests/models/comment.js index dc500ddb6..a9c2f4787 100644 --- a/tests/models/comment.js +++ b/tests/models/comment.js @@ -216,28 +216,35 @@ describe('models.Comment', () => { describe('#changeStatus', () => { it('should change the status of a comment from no status', () => { - return Comment.changeStatus(comments[0].id, 'rejected') - .then(() => { + let comment_id = comments[0].id; - return Comment.findById(comments[0].id); + return Comment.findById(comment_id) + .then((c) => { + expect(c).to.have.property('status'); + expect(c.status).to.have.length(0); + + return Comment.pushStatus(comment_id, 'rejected', '123'); }) + .then(() => Comment.findById(comment_id)) .then((c) => { expect(c).to.have.property('status'); expect(c.status).to.have.length(1); expect(c.status[0]).to.have.property('type', 'rejected'); + expect(c.status[0]).to.have.property('assigned_by', '123'); }); }); it('should change the status of a comment from accepted', () => { - return Comment.changeStatus(comments[1].id, 'rejected') - .then(() => { - - return Comment.findById(comments[1].id); - }) + return Comment.pushStatus(comments[1].id, 'rejected', '123') + .then(() => Comment.findById(comments[1].id)) .then((c) => { expect(c).to.have.property('status'); expect(c.status).to.have.length(2); + expect(c.status[0]).to.have.property('type', 'accepted'); + expect(c.status[0]).to.have.property('assigned_by', null); + expect(c.status[1]).to.have.property('type', 'rejected'); + expect(c.status[1]).to.have.property('assigned_by', '123'); }); });