Corrected action upserting functionality

This commit is contained in:
Wyatt Johnson
2016-11-28 10:14:13 -07:00
parent bb8570a3d4
commit 9d8968f1ce
3 changed files with 22 additions and 13 deletions
+18 -7
View File
@@ -161,15 +161,26 @@ CommentSchema.statics.changeStatus = function(id, status) {
* @param {String} action the new action to the comment
* @return {Promise}
*/
CommentSchema.statics.addAction = function(id, user_id, action_type) {
// check that the comment exist
let action = new Action({
action_type: action_type,
CommentSchema.statics.addAction = function(item_id, user_id, action_type) {
const action = {
item_id,
item_type: 'comment',
item_id: id,
user_id: user_id
user_id,
action_type
};
// Update/Create the action for the user.
return Action.findOneAndUpdate(action, action, {
// Ensure that if it's new, we return the new object created.
new: true,
// Perform an upsert in the event that this doesn't exist.
upsert: true,
// Set the default values if not provided based on the mongoose models.
setDefaultsOnInsert: true
});
return action.save();
};
//==============================================================================
+3 -5
View File
@@ -29,8 +29,7 @@ router.post('/', wordlist.filter('body'), (req, res, next) => {
const {
body,
asset_id,
parent_id,
author_id
parent_id
} = req.body;
Comment
@@ -39,7 +38,7 @@ router.post('/', wordlist.filter('body'), (req, res, next) => {
asset_id,
parent_id,
status: req.wordlist.matched ? 'rejected' : '',
author_id
author_id: req.user.id
})
.then((comment) => {
@@ -96,12 +95,11 @@ router.put('/:comment_id/status', authorization.needed('admin'), (req, res, next
router.post('/:comment_id/actions', (req, res, next) => {
const {
user_id,
action_type
} = req.body;
Comment
.addAction(req.params.comment_id, user_id, action_type)
.addAction(req.params.comment_id, req.user.id, action_type)
.then((action) => {
res.status(201).json(action);
})
+1 -1
View File
@@ -461,7 +461,7 @@ describe('Post /:comment_id/actions', () => {
it('it should update actions', () => {
return chai.request(app)
.post('/api/v1/comments/abc/actions')
.set(passport.inject({roles: ['admin']}))
.set(passport.inject({id: '456', roles: ['admin']}))
.send({'user_id': '456', 'action_type': 'flag'})
.then((res) => {
expect(res).to.have.status(201);