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
+2
View File
@@ -6,6 +6,7 @@ const Assets = require('./assets');
const Comments = require('./comments');
const Metrics = require('./metrics');
const Settings = require('./settings');
const Tags = require('./tags');
const Users = require('./users');
const plugins = require('../../services/plugins');
@@ -18,6 +19,7 @@ let loaders = [
Comments,
Metrics,
Settings,
Tags,
Users,
// Load the plugin loaders from the manager.
+37
View File
@@ -0,0 +1,37 @@
const DataLoader = require('dataloader');
const util = require('./util');
const TagsService = require('../../services/tags');
const TagModel = require('../../models/tag');
/**
* Gets tags based on their item id's.
*/
const genTagsByItemID = (_, item_ids) => {
return TagsService
.findByItemIdArray(item_ids)
.then(util.arrayJoinBy(item_ids, 'item_id'));
};
/**
* Search for tags based on their item_type and ensures that
* the tags returned have unique item id's.
* @param {String} item_type the item id to search by
* @return {Promise} resolves to distinct items tags
*/
const getItemIdsByItemType = (_, item_type) => {
return TagModel.distinct('item_id', {item_type});
};
/**
* Creates a set of loaders based on a GraphQL context.
* @param {Object} context the context of the GraphQL request
* @return {Object} object of loaders
*/
module.exports = (context) => ({
Tags: {
getByID: new DataLoader((ids) => genTagsByItemID(context, ids)),
getByTypes: ({item_type}) => getItemIdsByItemType(context, item_type)
}
});
+7 -7
View File
@@ -5,6 +5,8 @@ const ActionsService = require('../../services/actions');
const CommentsService = require('../../services/comments');
const linkify = require('linkify-it')();
const TagModel = require('../../models/tag');
const Wordlist = require('../../services/wordlist');
/**
@@ -18,20 +20,18 @@ const Wordlist = require('../../services/wordlist');
*/
const createComment = ({user, loaders: {Comments}}, {body, asset_id, parent_id = null}, status = 'NONE') => {
let tags = [];
if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) {
tags = [{name: 'STAFF'}];
}
return CommentsService.publicCreate({
body,
asset_id,
parent_id,
status,
tags,
author_id: user.id
})
.then((comment) => {
.then(async (comment) => {
if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) {
await CommentsService.addTag(comment.id, 'STAFF', user.id);
}
// If the loaders are present, clear the caches for these values because we
// just added a new comment, hence the counts should be updated. We should