diff --git a/graph/mutators/comment.js b/graph/mutators/comment.js index 42a02609e..c9b3b0ab0 100644 --- a/graph/mutators/comment.js +++ b/graph/mutators/comment.js @@ -189,18 +189,31 @@ const setCommentStatus = ({loaders: {Comments}}, {id, status}) => { /** * Adds a tag to a Comment + * @param {String} id identifier of the comment (uuid) + * @param {String} tag name of the tag */ const addCommentTag = ({user, loaders: {Comments}}, {id, tag}) => { return CommentsService.addTag(id, tag, user.id) .then(() => CommentsService.findById( id )); }; +/** + * Removes a tag from a Comment + * @param {String} id identifier of the comment (uuid) + * @param {String} tag name of the tag + */ +const removeCommentTag = ({user, loaders: {Comments}}, {id, tag}) => { + return CommentsService.removeTag(id, tag) + .then(() => CommentsService.findById( id )); +}; + module.exports = (context) => { let mutators = { Comment: { create: () => Promise.reject(errors.ErrNotAuthorized), setCommentStatus: () => Promise.reject(errors.ErrNotAuthorized), addCommentTag: () => Promise.reject(errors.ErrNotAuthorized), + removeCommentTag: () => Promise.reject(errors.ErrNotAuthorized), } }; @@ -216,5 +229,9 @@ module.exports = (context) => { mutators.Comment.addCommentTag = (action) => addCommentTag(context, action); } + if (context.user && context.user.can('mutation:removeCommentTag')) { + mutators.Comment.removeCommentTag = (action) => removeCommentTag(context, action); + } + return mutators; }; diff --git a/graph/resolvers/root_mutation.js b/graph/resolvers/root_mutation.js index ac862af88..61c4367c1 100644 --- a/graph/resolvers/root_mutation.js +++ b/graph/resolvers/root_mutation.js @@ -51,7 +51,10 @@ const RootMutation = { }, addCommentTag(_, {id, tag}, {mutators: {Comment}}) { return wrapResponse('comment')(Comment.addCommentTag({id, tag})); - } + }, + removeCommentTag(_, {id, tag}, {mutators: {Comment}}) { + return wrapResponse('comment')(Comment.removeCommentTag({id, tag})); + }, }; module.exports = RootMutation; diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index adb349fe2..8bcc39ac5 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -638,6 +638,13 @@ type AddCommentTagResponse implements Response { errors: [UserError] } +# Response to removeCommentTag mutation +type RemoveCommentTagResponse implements Response { + # An array of errors relating to the mutation that occured. + comment: Comment + errors: [UserError] +} + # All mutations for the application are defined on this object. type RootMutation { @@ -664,6 +671,9 @@ type RootMutation { # Add tag to comment. addCommentTag(id: ID!, tag: String!): AddCommentTagResponse + + # Remove tag from comment. + removeCommentTag(id: ID!, tag: String!): RemoveCommentTagResponse } ################################################################################ diff --git a/models/user.js b/models/user.js index f74d63519..67d479471 100644 --- a/models/user.js +++ b/models/user.js @@ -158,6 +158,7 @@ const USER_GRAPH_OPERATIONS = [ 'mutation:setUserStatus', 'mutation:setCommentStatus', 'mutation:addCommentTag', + 'mutation:removeCommentTag', ]; /** diff --git a/test/graph/mutations/removeCommentTag.js b/test/graph/mutations/removeCommentTag.js new file mode 100644 index 000000000..fe60a0f4c --- /dev/null +++ b/test/graph/mutations/removeCommentTag.js @@ -0,0 +1,45 @@ +const expect = require('chai').expect; +const {graphql} = require('graphql'); + +const schema = require('../../../graph/schema'); +const Context = require('../../../graph/context'); +const UserModel = require('../../../models/user'); +const SettingsService = require('../../../services/settings'); +const CommentsService = require('../../../services/comments'); + +describe('graph.mutations.removeCommentTag', () => { + let comment; + beforeEach(async () => { + SettingsService.init(); + comment = await CommentsService.publicCreate({body: `hello there! ${ String(Math.random()).slice(2)}`}); + }); + + const query = ` + mutation RemoveCommentTag ($id: ID!, $tag: String!) { + removeCommentTag(id:$id, tag:$tag) { + comment { + tags { + name + } + } + errors { + translation_key + } + } + } + `; + + it('can add remove tags from comments', async () => { + const user = new UserModel({}); + const context = new Context({user}); + + // add a tag first + await CommentsService.addTag(comment.id, 'BEST'); + const response = await graphql(schema, query, {}, context, {id: comment.id, tag: 'BEST'}); + if (response.errors && response.errors.length) { + console.error(response.errors); + } + expect(response.errors).to.be.empty; + expect(response.data.removeCommentTag.comment.tags).to.deep.equal([]); + }); +});