Fix tests related to tags on comments.

This commit is contained in:
gaba
2017-04-20 12:24:08 -07:00
parent 1137fca3b4
commit bd72bfc6ec
10 changed files with 139 additions and 81 deletions
+18 -9
View File
@@ -3,8 +3,8 @@ const CommentModel = require('../models/comment');
const ActionModel = require('../models/action');
const ActionsService = require('./actions');
const TagModel = require('../models/tag');
const TagsService = require('./tags');
const TagModel = require('../models/tag');
const STATUSES = [
'ACCEPTED',
@@ -51,14 +51,18 @@ module.exports = class CommentsService {
*/
static addTag(id, name, assigned_by) {
return TagsService.insertCommentTag({
name,
item_id: id,
item_type: 'COMMENTS',
user_id: assigned_by,
return CommentModel.findOne({id})
.then((comment) => {
if (comment == null) {
return Promise.reject(new Error('tag not allowed'));
}
return TagsService.insertCommentTag({
name,
item_id: id,
item_type: 'COMMENTS',
user_id: assigned_by,
});
});
// Add the ID to the comment
}
/**
@@ -68,10 +72,15 @@ module.exports = class CommentsService {
* @param {String} name the name of the tag to add
*/
static removeTag(id, name) {
return TagModel.remove({
return TagModel.findOneAndRemove({
item_type: 'COMMENTS',
item_id: id,
name
})
.then((tag) => {
if (tag == null) {
return Promise.reject(new Error('tag does not exist'));
}
});
}
+27 -32
View File
@@ -8,13 +8,26 @@ const ALLOWED_COMMENT_TAGS = [
module.exports = class TagsService {
/**
* Finds an action by the id.
* Finds a tag by the id.
* @param {String} id identifier of the tag (uuid)
*/
static findById(id) {
return TagModel.findOne({id});
}
/**
* Finds atag by the item_id and name.
* @param {String} item_id identifier of the item that the tag was applied into(uuid)
* @param {string} name name of the tag
*/
static findByItemIdAndName(item_id, name, item_type) {
return TagModel.find({
item_id,
item_type,
name
});
}
/**
* Add a tag.
* @param {string} name the actual tag
@@ -30,43 +43,25 @@ module.exports = class TagsService {
return Promise.reject(new Error('tag not allowed'));
}
// Tags are made unique by using a query that can be reproducable, i.e.,
// not containing user inputable values.
let query = {
// // Tags are made unique by using a query that can be reproducable, i.e.,
// // not containing user inputable values.
// let query = {
// name: tag.name,
// item_id: tag.item_id,
// item_type: tag.item_type,
// assigned_by: tag.user_id,
// privacy_type: tag.privacy_type
// };
// Create/Update the tag.
let newtag = new TagModel({
name: tag.name,
item_id: tag.item_id,
item_type: tag.item_type,
assigned_by: tag.user_id,
privacy_type: tag.privacy_type
};
// Create/Update the tag.
return TagModel.findOneAndUpdate(query, tag, {
// 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
})
.then(({nModified}) => {
switch (nModified) {
case 0:
// either the tag was already there, or the comment doesn't exist with that id...
throw new Error('Could not add tag to comment. Either the comment doesn\'t exist or the tag is already present.');
case 1:
// tag added
return;
default:
// this should never happen because no multi parameter and unique index on id
}
});
return newtag.save();
}
};