From 9d28577e9b3a0dd36076fbe35fe8f2b1e7b50b59 Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 8 Dec 2016 15:27:49 -0500 Subject: [PATCH] Removing extra commas and collapsing actions into an object in the backend. --- client/coral-plugin-flags/FlagButton.js | 2 +- models/action.js | 17 +++++------------ models/comment.js | 6 ++---- models/user.js | 6 ++---- routes/api/comments/index.js | 9 +-------- routes/api/user/index.js | 8 +------- tests/routes/api/comments/index.js | 2 +- 7 files changed, 13 insertions(+), 37 deletions(-) diff --git a/client/coral-plugin-flags/FlagButton.js b/client/coral-plugin-flags/FlagButton.js index 920b6b71e..9a19e6113 100644 --- a/client/coral-plugin-flags/FlagButton.js +++ b/client/coral-plugin-flags/FlagButton.js @@ -57,7 +57,7 @@ class FlagButton extends Component { header: lang.t('step-1-header'), options: [ {val: 'user', text: lang.t('flag-username')}, - {val: 'comments', text: lang.t('flag-comment')}, + {val: 'comments', text: lang.t('flag-comment')} ], button: lang.t('continue'), sets: 'itemType' diff --git a/models/action.js b/models/action.js index 13b1c5494..820d8e9ca 100644 --- a/models/action.js +++ b/models/action.js @@ -32,23 +32,16 @@ ActionSchema.statics.findById = function(id) { /** * Add an action. - * @param {String} item_id identifier of the comment (uuid) + * @param {String} item_id identifier of the item (uuid) * @param {String} user_id user id of the action (uuid) - * @param {String} action the new action to the comment + * @param {String} action the new action to the item * @return {Promise} */ -ActionSchema.statics.insertUserAction = ({item_id, item_type, user_id, action_type, field, detail}) => { - const action = { - item_id, - item_type, - user_id, - action_type, - field, - detail - }; +ActionSchema.statics.insertUserAction = ({item_id, item_type, user_id, action}) => { + let action_obj = Object.assign({}, action, {item_id, item_type, user_id}); // Create/Update the action. - return Action.findOneAndUpdate(action, action, { + return Action.findOneAndUpdate(action_obj, action_obj, { // Ensure that if it's new, we return the new object created. new: true, diff --git a/models/comment.js b/models/comment.js index 80bfd67ea..25616c65b 100644 --- a/models/comment.js +++ b/models/comment.js @@ -284,13 +284,11 @@ CommentSchema.statics.pushStatus = (id, status, assigned_by = null) => Comment.u * @param {String} action the new action to the comment * @return {Promise} */ -CommentSchema.statics.addAction = (item_id, user_id, action_type, field, detail) => Action.insertUserAction({ +CommentSchema.statics.addAction = (item_id, user_id, action) => Action.insertUserAction({ item_id, item_type: 'comment', user_id, - action_type, - field, - detail + action }); /** diff --git a/models/user.js b/models/user.js index 626b10e06..c34c87cff 100644 --- a/models/user.js +++ b/models/user.js @@ -555,11 +555,9 @@ UserService.addBio = (id, bio) => ( * @param {String} action the new action to the user * @return {Promise} */ -UserService.addAction = (item_id, user_id, action_type, field, detail) => Action.insertUserAction({ +UserService.addAction = (item_id, user_id, action) => Action.insertUserAction({ item_id, item_type: 'comment', user_id, - action_type, - field, - detail + action }); diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 25984405e..c3aa3efc8 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -148,15 +148,8 @@ router.put('/:comment_id/status', authorization.needed('admin'), (req, res, next }); router.post('/:comment_id/actions', (req, res, next) => { - - const { - action_type, - field, - detail - } = req.body; - Comment - .addAction(req.params.comment_id, req.user.id, action_type, field, detail) + .addAction(req.params.comment_id, req.user.id, req.body) .then((action) => { res.status(201).json(action); }) diff --git a/routes/api/user/index.js b/routes/api/user/index.js index c5cf3d336..3c52be5ca 100644 --- a/routes/api/user/index.js +++ b/routes/api/user/index.js @@ -158,15 +158,9 @@ router.put('/:user_id/bio', (req, res, next) => { }); router.post('/:user_id/actions', authorization.needed(), (req, res, next) => { - console.log('Hit action endpoint'); - const { - action_type, - field, - detail - } = req.body; User - .addAction(req.params.comment_id, req.user.id, action_type, field, detail) + .addAction(req.params.comment_id, req.user.id, req.body) .then((action) => { res.status(201).json(action); }) diff --git a/tests/routes/api/comments/index.js b/tests/routes/api/comments/index.js index bf7a06ff6..06a5bc789 100644 --- a/tests/routes/api/comments/index.js +++ b/tests/routes/api/comments/index.js @@ -350,7 +350,7 @@ describe('/api/v1/comments/:comment_id/actions', () => { return chai.request(app) .post('/api/v1/comments/abc/actions') .set(passport.inject({id: '456', roles: ['admin']})) - .send({'user_id': '456', 'action_type': 'flag'}) + .send({'action_type': 'flag'}) .then((res) => { expect(res).to.have.status(201); expect(res).to.have.body;