diff --git a/services/comments.js b/services/comments.js index e23e4e486..ca258934f 100644 --- a/services/comments.js +++ b/services/comments.js @@ -31,55 +31,6 @@ module.exports = class CommentsService { return commentModel.save(); } - static addTag(id, tagLink, verifyOwnership = false) { - - // Compose the query to find the comment. - const query = { - id, - 'tags.tag.name': { - $ne: tagLink.tag.name - } - }; - - // If ownership verification is required, ensure that the person that is - // assigning the tag is the same person that owns the comment. - if (verifyOwnership) { - query['author_id'] = tagLink.assigned_by; - } - - return CommentModel.update(query, { - $push: { - tags: tagLink - } - }); - } - - static removeTag(id, {tag: {name}, assigned_by}, verifyOwnership = false) { - - // Compose the query to find the comment that has the id: `id` and a tag - // that has the name: `name`. - const query = { - id, - 'tags.tag.name': { - $eq: name - } - }; - - // If ownership verification is required, ensure that the person that is - // assigning the tag is the same person that owns the comment. - if (verifyOwnership) { - query['author_id'] = assigned_by; - } - - return CommentModel.update(query, { - $pull: { - tags: { - name - } - } - }); - } - /** * Finds a comment by the id. * @param {String} id identifier of comment (uuid) diff --git a/test/server/graph/mutations/removeTag.js b/test/server/graph/mutations/removeTag.js index 1075cab49..a7ee4053b 100644 --- a/test/server/graph/mutations/removeTag.js +++ b/test/server/graph/mutations/removeTag.js @@ -9,6 +9,7 @@ const SettingModel = require('../../../../models/setting'); const AssetModel = require('../../../../models/asset'); const SettingsService = require('../../../../services/settings'); const CommentsService = require('../../../../services/comments'); +const TagsService = require('../../../../services/tags'); describe('graph.mutations.removeTag', () => { let asset, comment; @@ -36,7 +37,7 @@ describe('graph.mutations.removeTag', () => { const context = new Context({user}); // add a tag first - await CommentsService.addTag(comment.id, {tag: {name: 'BEST'}}, false); + await TagsService.add(comment.id, 'COMMENTS', {tag: {name: 'BEST'}}, false); const response = await graphql(schema, query, {}, context, {id: comment.id, asset_id: asset.id, name: 'BEST'}); if (response.errors && response.errors.length) { @@ -70,7 +71,7 @@ describe('graph.mutations.removeTag', () => { const context = new Context({user}); // add a tag first - await CommentsService.addTag(comment.id, {tag: {name: 'BEST'}}, false); + await TagsService.add(comment.id, 'COMMENTS', {tag: {name: 'BEST'}}, false); const response = await graphql(schema, query, {}, context, {id: comment.id, asset_id: asset.id, name: 'BEST'}); if (response.errors && response.errors.length) { diff --git a/test/server/services/comments.js b/test/server/services/comments.js index f5d82d1de..68859077f 100644 --- a/test/server/services/comments.js +++ b/test/server/services/comments.js @@ -219,89 +219,6 @@ describe('services.CommentsService', () => { }); - describe('#addTag', () => { - it('adds a tag', async () => { - const id = comments[0].id; - const name = 'BEST'; - const assigned_by = users[0].id; - - await CommentsService.addTag(id, { - tag: { - name - }, - assigned_by - }); - - const {tags} = await CommentsService.findById(id); - expect(tags.length).to.equal(1); - expect(tags[0].tag.name).to.equal(name); - expect(tags[0].assigned_by).to.equal(assigned_by); - }); - - it('can\'t add same tag.id twice', async () => { - const id = comments[0].id; - const name = 'BEST'; - const assigned_by = users[0].id; - - await CommentsService.addTag(id, { - tag: { - name - }, - assigned_by - }); - - { - let {tags} = await CommentsService.findById(id); - expect(tags.length).to.equal(1); - } - - await CommentsService.addTag(id, { - tag: { - name - }, - assigned_by - }); - - { - let {tags} = await CommentsService.findById(id); - expect(tags.length).to.equal(1); - } - }); - }); - - describe('#removeTag', () => { - it('removes a tag', async () => { - const id = comments[0].id; - const name = 'BEST'; - const assigned_by = users[0].id; - - await CommentsService.addTag(id, { - tag: { - name - }, - assigned_by - }); - - { - const {tags} = await CommentsService.findById(id); - expect(tags.length).to.equal(1); - } - - // ok now to remove it - await CommentsService.removeTag(id, { - tag: { - name - }, - assigned_by - }); - - { - const {tags} = await CommentsService.findById(id); - expect(tags.length).to.equal(0); - } - }); - }); - describe('#changeStatus', () => { it('should change the status of a comment from no status', () => { diff --git a/test/server/services/tags.js b/test/server/services/tags.js new file mode 100644 index 000000000..7cdad829e --- /dev/null +++ b/test/server/services/tags.js @@ -0,0 +1,107 @@ +const CommentsService = require('../../../services/comments'); +const TagsService = require('../../../services/tags'); +const UsersService = require('../../../services/users'); +const SettingsService = require('../../../services/settings'); + +const CommentModel = require('../../../models/comment'); + +const expect = require('chai').use(require('chai-as-promised')).expect; + +describe('services.TagsService', () => { + let comment, user; + beforeEach(async () => { + await SettingsService.init(); + user = await UsersService.createLocalUser('stampi@gmail.com', '1Coral!!', 'Stampi'); + comment = await CommentModel.create({ + id: '1', + body: 'comment 10', + asset_id: '123', + status_history: [], + parent_id: null, + author_id: user.id + }); + }); + + describe('#add', () => { + it('adds a tag', async () => { + const id = comment.id; + const name = 'BEST'; + const assigned_by = user.id; + + await TagsService.add(id, 'COMMENTS', { + tag: { + name + }, + assigned_by + }); + + const {tags} = await CommentsService.findById(id); + expect(tags.length).to.equal(1); + expect(tags[0].tag.name).to.equal(name); + expect(tags[0].assigned_by).to.equal(assigned_by); + }); + + it('can\'t add same tag.id twice', async () => { + const id = comment.id; + const name = 'BEST'; + const assigned_by = user.id; + + await TagsService.add(id, 'COMMENTS', { + tag: { + name + }, + assigned_by + }); + + { + let {tags} = await CommentsService.findById(id); + expect(tags.length).to.equal(1); + } + + await TagsService.add(id, 'COMMENTS', { + tag: { + name + }, + assigned_by + }); + + { + let {tags} = await CommentsService.findById(id); + expect(tags.length).to.equal(1); + } + }); + }); + + describe('#remove', () => { + it('removes a tag', async () => { + const id = comment.id; + const name = 'BEST'; + const assigned_by = user.id; + + await TagsService.add(id, 'COMMENTS', { + tag: { + name + }, + assigned_by + }); + + { + const {tags} = await CommentsService.findById(id); + expect(tags.length).to.equal(1); + } + + // ok now to remove it + await TagsService.remove(id, 'COMMENTS', { + tag: { + name + }, + assigned_by + }); + + { + const {tags} = await CommentsService.findById(id); + expect(tags.length).to.equal(0); + } + }); + }); +});