diff --git a/packages/component-fixture-manager/src/fixtures/collectionIDs.js b/packages/component-fixture-manager/src/fixtures/collectionIDs.js index d4d27580eba01b480a3de9ec5650fc7a0dcc8b01..4f351142bab1ef0de0b4c1d82f767ed391b42b75 100644 --- a/packages/component-fixture-manager/src/fixtures/collectionIDs.js +++ b/packages/component-fixture-manager/src/fixtures/collectionIDs.js @@ -5,4 +5,5 @@ const chance = new Chance() module.exports = { standardCollID: chance.guid(), collectionReviewCompletedID: chance.guid(), + twoVersionsCollectionId: chance.guid(), } diff --git a/packages/component-fixture-manager/src/fixtures/collections.js b/packages/component-fixture-manager/src/fixtures/collections.js index 4984c9f078027f840f262f68a5fd22b914b87cbf..1b07746e940fd74eb94f4a6bb46118ed9a3b514f 100644 --- a/packages/component-fixture-manager/src/fixtures/collections.js +++ b/packages/component-fixture-manager/src/fixtures/collections.js @@ -4,6 +4,7 @@ const { fragment, reviewCompletedFragment } = require('./fragments') const { standardCollID, collectionReviewCompletedID, + twoVersionsCollectionId, } = require('./collectionIDs') const chance = new Chance() @@ -123,6 +124,36 @@ const collections = { }, technicalChecks: {}, }, + twoVersionsCollection: { + id: twoVersionsCollectionId, + title: chance.sentence(), + type: 'collection', + fragments: [fragment.id, reviewCompletedFragment.id], + owners: [user.id], + save: jest.fn(() => collections.collection), + invitations: [ + { + id: chance.guid(), + role: 'handlingEditor', + hasAnswer: true, + isAccepted: false, + userId: handlingEditor.id, + invitedOn: chance.timestamp(), + respondedOn: null, + }, + ], + handlingEditor: { + id: handlingEditor.id, + hasAnswer: false, + isAccepted: false, + email: handlingEditor.email, + invitedOn: chance.timestamp(), + respondedOn: null, + name: `${handlingEditor.firstName} ${handlingEditor.lastName}`, + }, + status: 'revisionRequested', + customId: chance.natural({ min: 999999, max: 9999999 }), + }, } module.exports = collections diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js index 55d54aa4274c9035da2fe92342ccab04929b6220..28ef9ffcd5708f36094bbaddc54c7c37b5730b85 100644 --- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js +++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js @@ -1,5 +1,5 @@ const uuid = require('uuid') -const { pick, get, set, has, isEmpty } = require('lodash') +const { pick, get, set, has, isEmpty, last } = require('lodash') const config = require('config') const { v4 } = require('uuid') @@ -70,17 +70,25 @@ module.exports = models => async (req, res) => { }) if ( - recommendation === recommendations.publish && recommendationType === recommendations.type.editor && collection.handlingEditor && collection.handlingEditor.id === req.user ) { const fragmentHelper = new Fragment({ fragment }) - if (!fragmentHelper.hasReviewReport()) { + if ( + recommendation === recommendations.publish && + !fragmentHelper.hasReviewReport() + ) { return res .status(400) .json({ error: 'Cannot publish without at least one reviewer report.' }) } + + if (last(collection.fragments) !== fragmentId) { + return res + .status(400) + .json({ error: 'Cannot make a recommendation on an older version.' }) + } } fragment.recommendations = fragment.recommendations || [] diff --git a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js index b090a9cb6fc1ae464488560f7032b0ff97388dea..7ea43a34dd81e851e260393abcead2e4dd03462e 100644 --- a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js +++ b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js @@ -357,4 +357,31 @@ describe('Post fragments recommendations route handler', () => { 'Cannot publish without at least one reviewer report.', ) }) + + it('should return an error when a HE makes a recommendation on an older version of a manuscript', async () => { + const { handlingEditor } = testFixtures.users + const { twoVersionsCollection } = testFixtures.collections + const { fragment } = testFixtures.fragments + body.recommendation = 'publish' + body.recommendationType = 'editorRecommendation' + + const res = await requests.sendRequest({ + body, + userId: handlingEditor.id, + models, + route, + path, + params: { + collectionId: twoVersionsCollection.id, + fragmentId: fragment.id, + }, + }) + + expect(res.statusCode).toBe(400) + const data = JSON.parse(res._getData()) + + expect(data.error).toEqual( + 'Cannot make a recommendation on an older version.', + ) + }) })