diff --git a/client/coral-admin/src/actions/users.js b/client/coral-admin/src/actions/users.js index 5fe882166..8747293c4 100644 --- a/client/coral-admin/src/actions/users.js +++ b/client/coral-admin/src/actions/users.js @@ -2,7 +2,6 @@ * Action disptacher related to users */ -export const updateUserStatus = (status, id) => (dispatch, getState) => { - console.log('DEBUG ', getState); +export const updateUserStatus = (status, id) => (dispatch) => { dispatch({type: 'USER_STATUS_UPDATE', id, status}); }; diff --git a/models/comment.js b/models/comment.js index d17e98860..bf13eff82 100644 --- a/models/comment.js +++ b/models/comment.js @@ -151,11 +151,11 @@ CommentSchema.statics.moderationQueue = function(moderation) { //============================================================================== /** - * Change the status of a comment. + * Set the status of a comment. * @param {String} id identifier of the comment (uuid) * @param {String} status the new status of the comment */ -CommentSchema.statics.changeStatus = function(id, status) { +CommentSchema.statics.setStatus = function(id, status) { return Comment.findOneAndUpdate({'id': id}, {$set: {'status': status}}, {upsert: false, new: true}); }; diff --git a/models/user.js b/models/user.js index cb56a8e55..d2adf6867 100644 --- a/models/user.js +++ b/models/user.js @@ -3,6 +3,8 @@ const uuid = require('uuid'); const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); +const Comment = require('./comment'); + // SALT_ROUNDS is the number of rounds that the bcrypt algorithm will run // through during the salting process. const SALT_ROUNDS = 10; @@ -418,9 +420,6 @@ UserService.setStatus = (id, status) => { return Promise.reject(new Error(`status ${status} is not supported`)); } - // If we are banning the user - // disable their account - return UserModel.update({ id: id }, { @@ -430,6 +429,24 @@ UserService.setStatus = (id, status) => { }); }; +/** + * Ban a user. + * @param {String} id id of a user + * @param {String} comment_id id of the comment that the user was ban for. + * @param {Function} done callback after the operation is complete + */ +UserService.ban = (id, comment_id) => { + // Disable their account + return UserService.disableUser(id) + .then(() => { + // Set status of the user to banned + return UserService.setStatus(id, 'banned').then(() => { + // Reject the comment that the user was ban for. + return Comment.setStatus(comment_id, 'rejected'); + }); + }); +}; + /** * Finds a user with the id. * @param {String} id user id (uuid) diff --git a/routes/api/comments/index.js b/routes/api/comments/index.js index 99957e987..f1f05382f 100644 --- a/routes/api/comments/index.js +++ b/routes/api/comments/index.js @@ -101,7 +101,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) + .setStatus(req.params.comment_id, req.body.status) .then(comment => res.status(200).send(comment)) .catch(error => next(error)); diff --git a/tests/models/user.js b/tests/models/user.js index dc3c5e960..cd3b67e7c 100644 --- a/tests/models/user.js +++ b/tests/models/user.js @@ -1,6 +1,7 @@ require('../utils/mongoose'); const User = require('../../models/user'); +const Comment = require('../../models/comment'); const expect = require('chai').expect; describe('User: models', () => { @@ -65,9 +66,43 @@ describe('User: models', () => { }); describe('#setStatus', () => { + it('should set the status to active', () => { + return User + .setStatus(mockUsers[0].id, 'active') + .then(() => { + User.findById(mockUsers[0].id) + .then((user) => { + expect(user).to.have.property('status') + .and.to.equal('active'); + }); + }); + }); + }); + + describe('#ban', () => { + let mockComment; + beforeEach(() => { + return Comment.new('testing the comment for that user if it is rejected.', mockUsers[0].id) + .then((comment) => { + mockComment = comment; + }); + }); + + it('should disable the user', () => { + return User + .ban(mockUsers[0].id, mockComment.id) + .then(() => { + User.findById(mockUsers[0].id) + .then((user) => { + expect(user).to.have.property('disabled') + .and.to.equal(true); + }); + }); + }); + it('should set the status to banned', () => { return User - .setStatus(mockUsers[0].id, 'banned') + .ban(mockUsers[0].id, mockComment.id) .then(() => { User.findById(mockUsers[0].id) .then((user) => { @@ -76,5 +111,17 @@ describe('User: models', () => { }); }); }); + + it('should set the comment to rejected', () => { + return User + .ban(mockUsers[0].id, mockComment.id) + .then(() => { + Comment.findById(mockComment.id) + .then((comment) => { + expect(comment).to.have.property('status') + .and.to.equal('rejected'); + }); + }); + }); }); });