From 42345747d5b130701d05643ca8fdf33a0f55b942 Mon Sep 17 00:00:00 2001
From: Tania Fecheta <tania.fecheta@thinslices.com>
Date: Fri, 16 Nov 2018 11:45:29 +0200
Subject: [PATCH] fix(fragmentRecommendation): allow HE to make another
 recommendation on the same version when EiC de

---
 .../src/fixtures/fragments.js                 | 42 +++++++++++++++++++
 .../routes/fragmentsRecommendations/post.js   | 15 +++++--
 .../fragmentsRecommendations/post.test.js     | 21 ++++++++++
 3 files changed, 75 insertions(+), 3 deletions(-)

diff --git a/packages/component-fixture-manager/src/fixtures/fragments.js b/packages/component-fixture-manager/src/fixtures/fragments.js
index 85465027c..0478b2aa9 100644
--- a/packages/component-fixture-manager/src/fixtures/fragments.js
+++ b/packages/component-fixture-manager/src/fixtures/fragments.js
@@ -459,6 +459,48 @@ const fragments = {
         updatedOn: chance.timestamp(),
         submittedOn: chance.timestamp(),
       },
+      {
+        recommendation: 'publish',
+        recommendationType: 'editorRecommendation',
+        comments: [
+          {
+            content: chance.paragraph(),
+            public: true,
+            files: [
+              {
+                id: chance.guid(),
+                name: 'file.pdf',
+                size: chance.natural(),
+              },
+            ],
+          },
+        ],
+        id: chance.guid(),
+        userId: handlingEditor.id,
+        createdOn: 1542361074012,
+        updatedOn: chance.timestamp(),
+      },
+      {
+        recommendation: 'return-to-handling-editor',
+        recommendationType: 'editorRecommendation',
+        comments: [
+          {
+            content: chance.paragraph(),
+            public: true,
+            files: [
+              {
+                id: chance.guid(),
+                name: 'file.pdf',
+                size: chance.natural(),
+              },
+            ],
+          },
+        ],
+        id: chance.guid(),
+        userId: handlingEditor.id,
+        createdOn: 1542361115749,
+        updatedOn: chance.timestamp(),
+      },
     ],
     authors: [
       {
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
index 60cbb6a90..1c24847ae 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js
@@ -45,6 +45,9 @@ module.exports = models => async (req, res) => {
     r => r.userId === req.user,
   )
 
+  const returnToHERecommendations = get(fragment, 'recommendations', []).filter(
+    r => r.recommendation === 'return-to-handling-editor',
+  )
   const authsome = authsomeHelper.getAuthsome(models)
   const target = {
     fragment,
@@ -84,9 +87,15 @@ module.exports = models => async (req, res) => {
         .status(400)
         .json({ error: 'Cannot write another review on this version.' })
     }
-    return res
-      .status(400)
-      .json({ error: 'Cannot make another recommendation on this version.' })
+    if (
+      recommendationType === recommendations.type.editor &&
+      last(returnToHERecommendations).createdOn <
+        last(currentUserRecommendation).createdOn
+    ) {
+      return res
+        .status(400)
+        .json({ error: 'Cannot make another recommendation on this version.' })
+    }
   }
 
   if (
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 9f67766c9..fa1cf243d 100644
--- a/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
+++ b/packages/component-manuscript-manager/src/tests/fragmentsRecommendations/post.test.js
@@ -428,6 +428,27 @@ describe('Post fragments recommendations route handler', () => {
     expect(data.error).toEqual('Cannot write another review on this version.')
   })
 
+  it('should return success when creating another recommendation as a HE on the same version when EiC returned manuscript to He ', async () => {
+    const { noRecommendationHE } = testFixtures.users
+    const { noEditorRecomedationCollection } = testFixtures.collections
+    const { noEditorRecomedationFragment } = testFixtures.fragments
+
+    const res = await requests.sendRequest({
+      body,
+      userId: noRecommendationHE.id,
+      models,
+      route,
+      path,
+      params: {
+        collectionId: noEditorRecomedationCollection.id,
+        fragmentId: noEditorRecomedationFragment.id,
+      },
+    })
+    expect(res.statusCode).toBe(200)
+    const data = JSON.parse(res._getData())
+    expect(data.userId).toEqual(noRecommendationHE.id)
+  })
+
   it('should return an error when an EiC makes a decision on an older version of a manuscript', async () => {
     const { editorInChief } = testFixtures.users
     const { twoVersionsCollection } = testFixtures.collections
-- 
GitLab