Implement cannot ignore staff in users service

`UsersService.ignoreUsers` now checks if any user provided is a staff
member. In case it is it throws a ErrCannotIgnoreStaff error.
This commit is contained in:
Daniel Janeiro
2017-10-07 23:02:25 +01:00
parent 0c7b35ffde
commit 7eaad1541b
2 changed files with 20 additions and 1 deletions
+7 -1
View File
@@ -2,6 +2,7 @@ const assert = require('assert');
const uuid = require('uuid');
const bcrypt = require('bcryptjs');
const errors = require('../errors');
const some = require('lodash/some');
const {
ROOT_URL
@@ -879,13 +880,18 @@ module.exports = class UsersService {
* @param {String} id the id of the user that is ignoring another users
* @param {Array<String>} usersToIgnore Array of user IDs to ignore
*/
static ignoreUsers(id, usersToIgnore) {
static async ignoreUsers(id, usersToIgnore) {
assert(Array.isArray(usersToIgnore), 'usersToIgnore is an array');
assert(usersToIgnore.every((u) => typeof u === 'string'), 'usersToIgnore is an array of string user IDs');
if (usersToIgnore.includes(id)) {
throw new Error('Users cannot ignore themselves');
}
const users = await UsersService.findByIdArray(usersToIgnore);
if (some(users, (user) => user.isStaff())) {
throw errors.ErrCannotIgnoreStaff;
}
// TODO: For each usersToIgnore, make sure they exist?
return UserModel.update({id}, {
$addToSet: {