diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index 3413c6f4e..427ab4c63 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -28,7 +28,7 @@ const createComment = ({user, loaders: {Comments}, pubsub}, {body, asset_id, par .then(async (comment) => { if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) { - await CommentsService.addTag(comment.id, 'STAFF', user.id); + await CommentsService.addTag(comment.id, 'STAFF', user); } // If the loaders are present, clear the caches for these values because we @@ -209,7 +209,7 @@ const setCommentStatus = ({user, loaders: {Comments}}, {id, status}) => { * @param {String} tag name of the tag */ const addCommentTag = ({user, loaders: {Comments}}, {id, tag}) => { - return CommentsService.addTag(id, tag, user.id); + return CommentsService.addTag(id, tag, user); }; /** diff --git a/services/comments.js b/services/comments.js index 4c8f423c6..41c383725 100644 --- a/services/comments.js +++ b/services/comments.js @@ -2,7 +2,7 @@ const CommentModel = require('../models/comment'); const ActionModel = require('../models/action'); const ActionsService = require('./actions'); - +const SettingModel = require('../models/setting'); const SettingsService = require('./settings'); const STATUSES = [ @@ -25,8 +25,6 @@ module.exports = class CommentsService { */ static publicCreate(comment) { - console.log('-----------------> debug publicCreate'); - // Check to see if this is an array of comments, if so map it out. if (Array.isArray(comment)) { return Promise.all(comment.map(CommentsService.publicCreate)); @@ -56,22 +54,33 @@ module.exports = class CommentsService { */ static addTag(id, name, added_by) { - console.log('-----------------> debug addtag', name); - - SettingsService.retrieve() - .then(({tags}) => { - if (!ALLOWED_TAGS.includes(name) || tags.findIndex((t) => {return t.id === name & t.models.include('COMMENTS');}) === -1) { + // Check that the tag is allowed by the system OR in our setting.tags. + return SettingsService.retrieve() + .then((settings) => { + + // Moderators or ADMIN can add any tag automatically. + if (added_by != null && (added_by.hasRoles('ADMIN') || added_by.hasRoles('MODERATOR'))) { + SettingModel.findOneAndUpdate({id: settings.id}, { + $push: { + tags: { + id: name, + added_by: added_by.id + } + } + }); + } + else if (!ALLOWED_TAGS.includes(name) || settings.tags.findIndex((t) => {return t.id === name & t.models.include('COMMENTS');}) === -1) { return Promise.reject(new Error('tag not allowed')); } - }); - return CommentModel.findOneAndUpdate({id}, { - $push: { - tags: { - id: name, - added_by: added_by + return CommentModel.findOneAndUpdate({id}, { + $push: { + tags: { + id: name, + added_by: added_by.id + } } - } + }); }); } diff --git a/test/server/graph/loaders/metrics.js b/test/server/graph/loaders/metrics.js index 5d74f154d..0d2741ef8 100644 --- a/test/server/graph/loaders/metrics.js +++ b/test/server/graph/loaders/metrics.js @@ -23,27 +23,11 @@ describe('graph.loaders.Metrics', () => { describe('different comment states', () => { - beforeEach(() =>{ - CommentModel.create( {id: '1', body: 'a new comment!'}) - .then((comment) => { - console.log('*************** wtf comment', comment); - }) - .catch((error) => { - console.log('1 debug the rror create ', error); - }); - console.log('debug 1'); - CommentModel.create({id: '2', body: 'a new comment!'}) - .catch((error) => { - console.log('2 debug the rror create ', error); - }); - console.log('debug 2'); + beforeEach(() =>[ + CommentModel.create( {id: '1', body: 'a new comment!'}), + CommentModel.create({id: '2', body: 'a new comment!'}), CommentModel.create({id: '3', body: 'a new comment!'}) - .catch((error) => { - console.log('3 debug the rror create ', error); - }); - console.log('debug 3'); - } - ); + ]); [ {flagged: 0, actions: []}, @@ -56,7 +40,6 @@ describe('graph.loaders.Metrics', () => { ]} ].forEach(({flagged, actions}) => { - console.log('-----------------> debug forEach'); describe(`with actions=${actions.length}`, () => { beforeEach(() => ActionModel.create(actions)); diff --git a/test/server/graph/mutations/createComment.js b/test/server/graph/mutations/createComment.js index 31cb9872f..41822fcdb 100644 --- a/test/server/graph/mutations/createComment.js +++ b/test/server/graph/mutations/createComment.js @@ -21,7 +21,7 @@ describe('graph.mutations.createComment', () => { id status tags { - name + id } } errors { @@ -228,7 +228,7 @@ describe('graph.mutations.createComment', () => { .then(({tags}) => { if (tag) { expect(tags).to.have.length(1); - expect(tags[0]).to.have.property('name', tag); + expect(tags[0]).to.have.property('id', tag); } else { expect(tags).length(0); }