diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js index 4984c9f078027f840f262f68a5fd22b914b87cbf..4267f01c179aae6399ca1f3fea8d8f78ed508cc6 100644 --- a/packages/component-fixture-manager/src/fixtures/collections.js +++ b/packages/component-fixture-manager/src/fixtures/collections.js @@ -10,6 +10,7 @@ const chance = new Chance() const collections = { collection: { id: standardCollID, + delete: jest.fn(), title: chance.sentence(), type: 'collection', fragments: [fragment.id], @@ -52,6 +53,7 @@ const collections = { }, collection1: { id: standardCollID, + delete: jest.fn(), title: chance.sentence(), type: 'collection', fragments: [fragment.id], @@ -94,6 +96,7 @@ const collections = { }, collectionReviewCompleted: { id: collectionReviewCompletedID, + delete: jest.fn(), type: 'collection', owners: [user.id], status: 'reviewCompleted', diff --git a/packages/component-fixture-manager/src/fixtures/fragments.js b/packages/component-fixture-manager/src/fixtures/fragments.js index c8931dd9bb71a0259f0817a5a0a448ea53e37e42..5696e17019b802e9a131c762b184f058f2bc799e 100644 --- a/packages/component-fixture-manager/src/fixtures/fragments.js +++ b/packages/component-fixture-manager/src/fixtures/fragments.js @@ -18,6 +18,12 @@ const chance = new Chance() const fragments = { fragment: { id: chance.guid(), + delete: jest.fn(), + files: { + manuscripts: [{ id: chance.guid() }], + coverLetter: [], + supplementary: [], + }, collectionId: standardCollID, metadata: { title: chance.sentence(), @@ -180,6 +186,12 @@ const fragments = { }, noParentFragment: { id: chance.guid(), + delete: jest.fn(), + files: { + manuscripts: [{ id: chance.guid() }], + coverLetter: [], + supplementary: [], + }, collectionId: '', metadata: { title: chance.sentence(), @@ -199,9 +211,9 @@ const fragments = { }, reviewCompletedFragment: { id: chance.guid(), + delete: jest.fn(), type: 'fragment', files: { - coverLetter: [], manuscripts: [ { id: @@ -211,6 +223,7 @@ const fragments = { originalName: 'LinkedInProfileDemystified.pdf', }, ], + coverLetter: [], supplementary: [], }, owners: [user.id], diff --git a/packages/component-fixture-manager/src/fixtures/teams.js b/packages/component-fixture-manager/src/fixtures/teams.js index 5191421c22e3cd9f0f4c5256c18becd8c6d89166..c100c2a3798fc3bc4e635a98077806f11ad86bcc 100644 --- a/packages/component-fixture-manager/src/fixtures/teams.js +++ b/packages/component-fixture-manager/src/fixtures/teams.js @@ -33,6 +33,7 @@ const teams = { }, members: [handlingEditor.id], save: jest.fn(() => teams.heTeam), + delete: jest.fn(), updateProperties: jest.fn(() => teams.heTeam), id: heTeamID, }, @@ -49,6 +50,7 @@ const teams = { }, members: [reviewer.id, inactiveReviewer.id, answerReviewer.id], save: jest.fn(() => teams.revTeam), + delete: jest.fn(), updateProperties: jest.fn(() => teams.revTeam), id: revTeamID, }, @@ -65,6 +67,7 @@ const teams = { }, members: [reviewer.id, answerReviewer.id, recReviewer.id], save: jest.fn(() => teams.revRecommendationTeam), + delete: jest.fn(), updateProperties: jest.fn(() => teams.revRecommendationTeam), id: revRecommendationTeamID, }, @@ -81,6 +84,7 @@ const teams = { }, members: [submittingAuthor.id], save: jest.fn(() => teams.authorTeam), + delete: jest.fn(), updateProperties: jest.fn(() => teams.authorTeam), id: authorTeamID, }, diff --git a/packages/component-manuscript-manager/src/routes/collections/delete.js b/packages/component-manuscript-manager/src/routes/collections/delete.js index 05b9e042f9c08c24f9367aea7f6db546e08e8b08..62ed1fc8c760f3f1c2aa0385e3a0db337dcb8be5 100644 --- a/packages/component-manuscript-manager/src/routes/collections/delete.js +++ b/packages/component-manuscript-manager/src/routes/collections/delete.js @@ -1,4 +1,4 @@ -const { get, remove, concat } = require('lodash') +const { get, remove, concat, has } = require('lodash') const config = require('config') const { @@ -19,6 +19,12 @@ module.exports = models => async (req, res) => { try { collection = await models.Collection.find(collectionId) + if (has(collection, 'status')) { + return res.status(400).json({ + error: 'You can only delete manuscripts while in draft.', + }) + } + const fragmentId = collection.fragments[0] fragment = await models.Fragment.find(fragmentId) @@ -48,17 +54,18 @@ module.exports = models => async (req, res) => { return user.save() }), ) - return team.delete() }), ) - const fileKeys = concat( + let fileKeys = concat( fragment.files.manuscripts, fragment.files.coverLetter, fragment.files.supplementary, fragmentId, - ).map(file => file.id) + ) + + fileKeys = fileKeys.map(file => file.id) if (fileKeys.length > 1) { await deleteFilesS3({ fileKeys, s3Config }) diff --git a/packages/component-manuscript-manager/src/tests/collections/delete.test.js b/packages/component-manuscript-manager/src/tests/collections/delete.test.js new file mode 100644 index 0000000000000000000000000000000000000000..cee28293a8b2143164d5697c09d49d50762200f5 --- /dev/null +++ b/packages/component-manuscript-manager/src/tests/collections/delete.test.js @@ -0,0 +1,65 @@ +process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' +process.env.SUPPRESS_NO_CONFIG_WARNING = true + +const { cloneDeep } = require('lodash') +const fixturesService = require('pubsweet-component-fixture-service') +const requests = require('../requests') + +const { Model, fixtures } = fixturesService + +jest.mock('pubsweet-component-mts-package/src/PackageManager', () => ({ + deleteFilesS3: jest.fn(), +})) +jest.mock('@pubsweet/component-send-email', () => ({ + send: jest.fn(), +})) + +const path = '../routes/collections/delete' +const route = { + path: '/api/collections/:collectionId/', +} +describe('Delete collections route handler', () => { + let testFixtures = {} + let models + beforeEach(() => { + testFixtures = cloneDeep(fixtures) + models = Model.build(testFixtures) + }) + + it('should return an error when deleting a collection which is not in draft', async () => { + const { admin } = testFixtures.users + const { collection } = testFixtures.collections + + const res = await requests.sendRequest({ + userId: admin.id, + route, + models, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + expect(data.error).toBe('You can only delete manuscripts while in draft.') + }) + + it('should return success when deleting a draft collection', async () => { + const { admin } = testFixtures.users + const { collection } = testFixtures.collections + delete collection.status + + const res = await requests.sendRequest({ + userId: admin.id, + route, + models, + path, + params: { + collectionId: collection.id, + }, + }) + + expect(res.statusCode).toBe(204) + }) +})