Moved tag creation into mutator

This commit is contained in:
Wyatt Johnson
2017-02-10 12:00:50 -07:00
parent 7325525716
commit c637be364a
3 changed files with 48 additions and 32 deletions
+6
View File
@@ -36,6 +36,12 @@ const createComment = ({user, loaders: {Comments}}, {body, asset_id, parent_id =
}
}
if (user.hasRoles('ADMIN')) {
return CommentsService
.addTag(comment.id, 'STAFF', user.id)
.then(() => comment);
}
return comment;
});
};
+42 -18
View File
@@ -3,7 +3,9 @@ const CommentModel = require('../models/comment');
const ActionModel = require('../models/action');
const ActionsService = require('./actions');
const UsersService = require('./users');
const ALLOWED_TAGS = [
{name: 'STAFF'}
];
module.exports = class CommentsService {
@@ -27,24 +29,46 @@ module.exports = class CommentsService {
author_id
} = comment;
return UsersService.isStaff(author_id).then((isStaff) => {
comment = new CommentModel({
body,
asset_id,
parent_id,
status_history: status ? [{
type: status,
comment = new CommentModel({
body,
asset_id,
parent_id,
status_history: status ? [{
type: status,
created_at: new Date()
}] : [],
tags: [],
status,
author_id
});
return comment.save();
}
/**
* Adds a tag if it doesn't already exist on the comment.
*/
static addTag(id, name, assigned_by) {
if (ALLOWED_TAGS.find((t) => t.name === name) == null) {
return Promise.reject(new Error('tag not allowed'));
}
return CommentModel.update({
id,
tags: {
$ne: {
name
}
}
}, {
$push: {
tags: {
name,
assigned_by,
created_at: new Date()
}] : [],
status,
tags: isStaff ? [{
name: 'STAFF',
assigned_by: null,
created_at: new Date()
}] : [],
author_id
});
return comment.save();
}
}
});
}
-14
View File
@@ -687,18 +687,4 @@ module.exports = class UsersService {
Promise.reject(new Error('You do not have permission to update your username.'));
});
}
/**
* Returns true if the user is staff.
* @param {String} id the id of the user to be enabled.
* @return {Promise}
*/
static isStaff(id) {
return UsersService.findById(id).then((user) => {
if (user) {
return user.hasRoles('ADMIN');
}
return false;
});
}
};