mirror of
https://github.com/wassname/talk.git
synced 2026-07-01 05:08:43 +08:00
Merge pull request #99 from coralproject/upsertting-actions
Corrected action upserting functionality
This commit is contained in:
@@ -27,6 +27,35 @@ ActionSchema.statics.findById = function(id) {
|
||||
return Action.findOne({id});
|
||||
};
|
||||
|
||||
/**
|
||||
* Add an action.
|
||||
* @param {String} item_id identifier of the comment (uuid)
|
||||
* @param {String} user_id user id of the action (uuid)
|
||||
* @param {String} action the new action to the comment
|
||||
* @return {Promise}
|
||||
*/
|
||||
ActionSchema.statics.insertUserAction = ({item_id, item_type, user_id, action_type}) => {
|
||||
const action = {
|
||||
item_id,
|
||||
item_type,
|
||||
user_id,
|
||||
action_type
|
||||
};
|
||||
|
||||
// Create/Update the action.
|
||||
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
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds actions in an array of ids.
|
||||
* @param {String} ids array of user identifiers (uuid)
|
||||
|
||||
+8
-11
@@ -157,20 +157,17 @@ CommentSchema.statics.changeStatus = function(id, status) {
|
||||
|
||||
/**
|
||||
* Add an action to the comment.
|
||||
* @param {String} id identifier of the comment (uuid)
|
||||
* @param {String} item_id identifier of the comment (uuid)
|
||||
* @param {String} user_id user id of the action (uuid)
|
||||
* @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,
|
||||
item_type: 'comment',
|
||||
item_id: id,
|
||||
user_id: user_id
|
||||
});
|
||||
return action.save();
|
||||
};
|
||||
CommentSchema.statics.addAction = (item_id, user_id, action_type) => Action.insertUserAction({
|
||||
item_id,
|
||||
item_type: 'comment',
|
||||
user_id,
|
||||
action_type
|
||||
});
|
||||
|
||||
//==============================================================================
|
||||
// Remove Statics
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user