Adding route to delete actions.

This commit is contained in:
David Jay
2016-11-11 17:06:29 -05:00
parent 92869e529a
commit 4bea5e5914
3 changed files with 44 additions and 7 deletions
+18 -3
View File
@@ -104,14 +104,14 @@ CommentSchema.statics.findByStatusByActionType = function(status, action_type) {
return Action
.findCommentsIdByActionType(action_type, 'comment')
.then((actions) => {
return Comment.find({
'status': status,
'status': status,
'id': {
'$in': actions.map(a => {
return a.item_id;
})
}
}
});
});
@@ -188,6 +188,21 @@ CommentSchema.statics.removeById = function(id) {
return Comment.remove({'id': id});
};
/**
* Remove an action from the comment.
* @param {String} id identifier of the comment (uuid)
* @param {String} action_type the type of the action to be removed
* @param {String} user_id the id of the user performing the action
*/
CommentSchema.statics.removeAction = function(id, user_id, action_type) {
return Action.remove({
action_type: action_type,
item_type: 'comment',
item_id: id,
user_id: user_id
});
};
const Comment = mongoose.model('Comment', CommentSchema);
module.exports = Comment;
+15 -4
View File
@@ -54,7 +54,7 @@ router.get('/status/rejected', (req, res, next) => {
// Pre-moderation: New comments are shown in the moderator queues immediately.
// Post-moderation: New comments do not appear in moderation queues unless they are flagged by other users.
router.get('/status/pending', (req, res, next) => {
Setting
.getModerationSetting()
.then(({moderation}) => {
@@ -76,9 +76,9 @@ router.get('/status/pending', (req, res, next) => {
//==============================================================================
router.post('/', (req, res, next) => {
const {body, author_id, asset_id, parent_id, status, username} = req.body;
Comment
.new(body, author_id, asset_id, parent_id, status, username)
.then((comment) => {
@@ -109,7 +109,7 @@ router.post('/:comment_id', (req, res, next) => {
});
router.post('/:comment_id/status', (req, res, next) => {
Comment
.changeStatus(req.params.comment_id, req.body.status)
.then(comment => res.status(200).send(comment))
@@ -143,4 +143,15 @@ router.delete('/:comment_id', (req, res, next) => {
});
});
router.delete('/:comment_id/actions', (req, res, next) => {
Comment
.removeAction(req.params.comment_id, req.body.user_id, req.body.action_type)
.then(() => {
res.status(201).send('OK. Removed');
})
.catch(error => {
next(error);
});
});
module.exports = router;
+11
View File
@@ -134,4 +134,15 @@ describe('Comment: models', () => {
// });
// });
});
describe('#removeAction', () => {
it('should remove an action', () => {
return Comment.removeAction('3', '123', 'flag').then(() => {
return Action.findByItemIdArray(['123']);
})
.then((actions) => {
expect(actions.length).to.equal(0);
});
});
});
});