diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js index 73aab63ed1139cc27ae14df932c9c49ce855346e..ead1ed38cabd8e5eaa615a1fa7a9e5b84d6d3052 100644 --- a/packages/component-fixture-manager/src/fixtures/collections.js +++ b/packages/component-fixture-manager/src/fixtures/collections.js @@ -1,6 +1,6 @@ const Chance = require('chance') const { user, handlingEditor, answerHE } = require('./userData') -const { fragment, newVersion } = require('./fragments') +const { fragment } = require('./fragments') const { standardCollID } = require('./collectionIDs') const chance = new Chance() @@ -9,7 +9,7 @@ const collections = { id: standardCollID, title: chance.sentence(), type: 'collection', - fragments: [fragment.id, newVersion.id], + fragments: [fragment.id], owners: [user.id], save: jest.fn(), invitations: [ diff --git a/packages/component-fixture-manager/src/fixtures/fragments.js b/packages/component-fixture-manager/src/fixtures/fragments.js index 10fcf908ed077f7ecda22e87cb745af8b0a539f1..bf52d33fa37f801d591111e077e6fac2f1eedcb8 100644 --- a/packages/component-fixture-manager/src/fixtures/fragments.js +++ b/packages/component-fixture-manager/src/fixtures/fragments.js @@ -115,45 +115,23 @@ const fragments = { save: jest.fn(() => fragments.fragment), owners: [user.id], type: 'fragment', - }, - newVersion: { - id: chance.guid(), - collectionId: standardCollID, - metadata: { - title: chance.sentence(), - abstract: chance.paragraph(), - }, - authors: [ - { - email: chance.email(), - id: submittingAuthor.id, - isSubmitting: true, - isCorresponding: false, - }, - ], - invitations: [ - { - id: chance.guid(), - role: 'reviewer', - hasAnswer: false, - isAccepted: false, - userId: reviewer.id, - invitedOn: chance.timestamp(), - respondedOn: null, + revision: { + collectionId: standardCollID, + metadata: { + title: chance.sentence(), + abstract: chance.paragraph(), }, - { - id: chance.guid(), - role: 'reviewer', - hasAnswer: true, - isAccepted: false, - userId: answerReviewer.id, - invitedOn: chance.timestamp(), - respondedOn: chance.timestamp(), - }, - ], - save: jest.fn(() => fragments.fragment), - owners: [user.id], - type: 'fragment', + authors: [ + { + email: chance.email(), + id: submittingAuthor.id, + isSubmitting: true, + isCorresponding: false, + }, + ], + owners: [user.id], + type: 'fragment', + }, }, noParentFragment: { id: chance.guid(), diff --git a/packages/component-fixture-manager/src/helpers/Model.js b/packages/component-fixture-manager/src/helpers/Model.js index 9ff60517c6f1ce111f22a5a4f1e5369737c0a9c6..2edf2e726a863bc6f4ffd38ca806e4b2ab3ccec4 100644 --- a/packages/component-fixture-manager/src/helpers/Model.js +++ b/packages/component-fixture-manager/src/helpers/Model.js @@ -1,5 +1,6 @@ const UserMock = require('../mocks/User') const TeamMock = require('../mocks/Team') +const FragmentMock = require('../mocks/Fragment') const notFoundError = new Error() notFoundError.name = 'NotFoundError' @@ -12,9 +13,7 @@ const build = fixtures => { find: jest.fn(id => findMock(id, 'collections', fixtures)), }, Team: {}, - Fragment: { - find: jest.fn(id => findMock(id, 'fragments', fixtures)), - }, + Fragment: {}, } UserMock.find = jest.fn(id => findMock(id, 'users', fixtures)) @@ -33,8 +32,12 @@ const build = fixtures => { ) TeamMock.all = jest.fn(() => Object.values(fixtures.teams)) + FragmentMock.find = jest.fn(id => findMock(id, 'fragments', fixtures)) + models.User = UserMock models.Team = TeamMock + models.Fragment = FragmentMock + return models } diff --git a/packages/component-fixture-manager/src/mocks/Fragment.js b/packages/component-fixture-manager/src/mocks/Fragment.js new file mode 100644 index 0000000000000000000000000000000000000000..9c938ae733683de3befff59ae55c344b6a3dadde --- /dev/null +++ b/packages/component-fixture-manager/src/mocks/Fragment.js @@ -0,0 +1,20 @@ +/* eslint-disable func-names-any */ + +function Fragment(properties) { + this.type = 'fragment' + this.id = properties.id + this.collectionId = properties.collectionId + this.metadata = properties.metadata + this.recommendations = properties.recommendations + this.authors = properties.authors + this.invitations = properties.invitations + this.owners = properties.owners + this.revision = properties.revision +} + +Fragment.prototype.save = jest.fn(function saveFragment() { + this.id = '111222' + return Promise.resolve(this) +}) + +module.exports = Fragment diff --git a/packages/component-manuscript-manager/src/routes/fragments/patch.js b/packages/component-manuscript-manager/src/routes/fragments/patch.js index 9dd7cc27321b993b62c3d3d0e66ba15ea8774028..92cbaf906c43c46c6b2ef0804a647c32a9d10e92 100644 --- a/packages/component-manuscript-manager/src/routes/fragments/patch.js +++ b/packages/component-manuscript-manager/src/routes/fragments/patch.js @@ -40,6 +40,12 @@ module.exports = models => async (req, res) => { const collectionHelper = new Collection({ collection }) const fragmentHelper = new Fragment({ fragment }) + const heRecommendation = fragmentHelper.getLatestHERequestToRevision() + if (!heRecommendation) { + return res.status(400).json({ + error: 'No Handling Editor request to revision has been found.', + }) + } const userHelper = new User({ UserModel: models.User }) const newFragmentBody = { @@ -47,6 +53,7 @@ module.exports = models => async (req, res) => { ...fragment.revision, invitations: await fragmentHelper.getInvitationsForSubmittingReviewers(), version: fragment.version + 1, + created: new Date(), } let newFragment = new models.Fragment(newFragmentBody) @@ -59,8 +66,6 @@ module.exports = models => async (req, res) => { delete fragment.revision fragment.save() - const heRecommendation = fragmentHelper.getLatestHERequestToRevision() - if (heRecommendation.recommendation === 'major') { const reviewerIds = newFragment.invitations.map(inv => { const { userId } = inv @@ -112,12 +117,6 @@ module.exports = models => async (req, res) => { }), ) - if (!heRecommendation) { - return res.status(400).json({ - error: 'No Handling Editor request to revision has been found.', - }) - } - collectionHelper.updateStatusByRecommendation({ recommendation: heRecommendation.recommendation, }) diff --git a/packages/component-manuscript-manager/src/tests/fragments/patch.test.js b/packages/component-manuscript-manager/src/tests/fragments/patch.test.js index b5afab0749a5c28f77bee042b30aeee90f179345..36f8edad9c27e972390b0f798fac21305c150252 100644 --- a/packages/component-manuscript-manager/src/tests/fragments/patch.test.js +++ b/packages/component-manuscript-manager/src/tests/fragments/patch.test.js @@ -27,7 +27,7 @@ describe('Patch fragments route handler', () => { it('should return success when the parameters are correct', async () => { const { user } = testFixtures.users const { collection } = testFixtures.collections - const { newVersion } = testFixtures.fragments + const { fragment } = testFixtures.fragments const res = await requests.sendRequest({ body, userId: user.id, @@ -36,7 +36,7 @@ describe('Patch fragments route handler', () => { path, params: { collectionId: collection.id, - fragmentId: newVersion.id, + fragmentId: fragment.id, }, }) @@ -87,10 +87,6 @@ describe('Patch fragments route handler', () => { const { user } = testFixtures.users const { fragment } = testFixtures.fragments const { collection } = testFixtures.collections - // const collection = { - // ...fCollection, - // fragments: [...fCollection.fragments, '123'], - // } fragment.recommendations.length = 0 const res = await requests.sendRequest({ @@ -112,19 +108,19 @@ describe('Patch fragments route handler', () => { ) }) it('should return an error when the request user is not the owner', async () => { - const { author } = testFixtures.users - const { newVersion } = testFixtures.fragments + const { handlingEditor } = testFixtures.users + const { fragment } = testFixtures.fragments const { collection } = testFixtures.collections const res = await requests.sendRequest({ body, - userId: author.id, + userId: handlingEditor.id, models, route, path, params: { collectionId: collection.id, - fragmentId: newVersion.id, + fragmentId: fragment.id, }, }) @@ -132,11 +128,11 @@ describe('Patch fragments route handler', () => { const data = JSON.parse(res._getData()) expect(data.error).toEqual('Unauthorized.') }) - it('should return an error when no previous version exists', async () => { + it('should return an error when no revision exists', async () => { const { user } = testFixtures.users const { fragment } = testFixtures.fragments const { collection } = testFixtures.collections - collection.fragments.length = 1 + delete fragment.revision const res = await requests.sendRequest({ body, @@ -152,6 +148,6 @@ describe('Patch fragments route handler', () => { expect(res.statusCode).toBe(400) const data = JSON.parse(res._getData()) - expect(data.error).toEqual('No previous version has been found.') + expect(data.error).toEqual('No revision has been found.') }) })