From 7d34ecf9b6ee48360a9f0933e004c752b6363f8f Mon Sep 17 00:00:00 2001
From: Mihail Hagiu <mihail.hagiu@thinslices.com>
Date: Fri, 16 Nov 2018 15:43:20 +0200
Subject: [PATCH] refactor(ReviewerNumbering): Fixed CR issues

---
 .../src/services/Collection.js                |  7 +++--
 .../routes/fragmentsRecommendations/patch.js  | 30 ++++++++-----------
 2 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/packages/component-helper-service/src/services/Collection.js b/packages/component-helper-service/src/services/Collection.js
index f68f5efc7..48331a3c0 100644
--- a/packages/component-helper-service/src/services/Collection.js
+++ b/packages/component-helper-service/src/services/Collection.js
@@ -1,4 +1,4 @@
-const { flatten, isEmpty, maxBy, get } = require('lodash')
+const { isEmpty, maxBy, get, flatMap } = require('lodash')
 
 class Collection {
   constructor({ collection = {} }) {
@@ -120,8 +120,9 @@ class Collection {
     const allCollectionFragments = await this.getAllCollectionFragments({
       FragmentModel,
     })
-    const allCollectionInvitations = flatten(
-      allCollectionFragments.map(fragment => fragment.invitations),
+    const allCollectionInvitations = flatMap(
+      allCollectionFragments,
+      fragment => fragment.invitations,
     )
     const allNumberedInvitationsForUser = allCollectionInvitations
       .filter(invite => invite.userId === userId)
diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js
index 7e1e10feb..7b03107b5 100644
--- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js
+++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/patch.js
@@ -1,4 +1,4 @@
-const { findIndex } = require('lodash')
+const { find } = require('lodash')
 const {
   services,
   authsome: authsomeHelper,
@@ -9,6 +9,7 @@ const Notification = require('../../notifications/notification')
 
 module.exports = models => async (req, res) => {
   const { collectionId, fragmentId, recommendationId } = req.params
+  const userId = req.user
   let collection, fragment
   try {
     collection = await models.Collection.find(collectionId)
@@ -26,7 +27,7 @@ module.exports = models => async (req, res) => {
     if (!recommendation)
       return res.status(404).json({ error: 'Recommendation not found.' })
 
-    if (recommendation.userId !== req.user)
+    if (recommendation.userId !== userId)
       return res.status(403).json({
         error: 'Unauthorized.',
       })
@@ -36,14 +37,14 @@ module.exports = models => async (req, res) => {
       fragment,
       path: req.route.path,
     }
-    const canPatch = await authsome.can(req.user, 'PATCH', target)
+    const canPatch = await authsome.can(userId, 'PATCH', target)
     if (!canPatch)
       return res.status(403).json({
         error: 'Unauthorized.',
       })
 
     const UserModel = models.User
-    const reviewer = await UserModel.find(req.user)
+    const reviewer = await UserModel.find(userId)
 
     Object.assign(recommendation, req.body)
     recommendation.updatedOn = Date.now()
@@ -63,22 +64,17 @@ module.exports = models => async (req, res) => {
         const collectionHelper = new Collection({ collection })
         collectionHelper.updateStatus({ newStatus: 'reviewCompleted' })
       }
-    }
 
-    if (req.body.submittedOn) {
       const collectionHelper = new Collection({ collection })
-      const invitationIndex = findIndex(fragment.invitations, [
+      const FragmentModel = models.Fragment
+      const reviewerNumber = await collectionHelper.getReviewerNumber({
+        userId: req.authInfo.id,
+        FragmentModel,
+      })
+      find(fragment.invitations, [
         'userId',
-        req.authInfo.id,
-      ])
-      if (invitationIndex >= 0) {
-        const FragmentModel = models.Fragment
-        const reviewerNumber = await collectionHelper.getReviewerNumber({
-          userId: req.authInfo.id,
-          FragmentModel,
-        })
-        fragment.invitations[invitationIndex].reviewerNumber = reviewerNumber
-      }
+        userId,
+      ]).reviewerNumber = reviewerNumber
     }
 
     fragment.save()
-- 
GitLab