From 61210806df5e9dd12324629e5607410476236f3c Mon Sep 17 00:00:00 2001
From: Sebastian <sebastian.mihalache@thinslices.com>
Date: Tue, 24 Apr 2018 10:08:23 +0300
Subject: [PATCH] feat(component-invite): remove refused reviewers

---
 packages/component-invite/src/helpers/Team.js | 10 +++++++++
 .../routes/collectionsInvitations/patch.js    | 22 +++++++++----------
 .../xpub-faraday/config/authsome-helpers.js   |  6 +++++
 packages/xpub-faraday/config/authsome.js      |  3 +++
 4 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/packages/component-invite/src/helpers/Team.js b/packages/component-invite/src/helpers/Team.js
index 8955e107a..8092336a8 100644
--- a/packages/component-invite/src/helpers/Team.js
+++ b/packages/component-invite/src/helpers/Team.js
@@ -106,10 +106,20 @@ const getTeamByGroupAndCollection = async (collectionId, role, TeamModel) => {
   )
 }
 
+const updateHETeam = async (collection, role, TeamModel, user) => {
+  const team = await getTeamByGroupAndCollection(collection.id, role, TeamModel)
+  delete collection.handlingEditor
+  await removeTeamMember(team.id, user.id, TeamModel)
+  user.teams = user.teams.filter(userTeamId => team.id !== userTeamId)
+  await user.save()
+  await collection.save()
+}
+
 module.exports = {
   createNewTeam,
   setupManuscriptTeam,
   removeTeamMember,
   getTeamMembersByCollection,
   getTeamByGroupAndCollection,
+  updateHETeam,
 }
diff --git a/packages/component-invite/src/routes/collectionsInvitations/patch.js b/packages/component-invite/src/routes/collectionsInvitations/patch.js
index c23e245ea..da754a1fc 100644
--- a/packages/component-invite/src/routes/collectionsInvitations/patch.js
+++ b/packages/component-invite/src/routes/collectionsInvitations/patch.js
@@ -19,7 +19,7 @@ module.exports = models => async (req, res) => {
 
   let user = await models.User.find(req.user)
   try {
-    let collection = await models.Collection.find(collectionId)
+    const collection = await models.Collection.find(collectionId)
     const invitation = await collection.invitations.find(
       invitation => invitation.id === invitationId,
     )
@@ -70,19 +70,17 @@ module.exports = models => async (req, res) => {
       }
     } else {
       invitation.isAccepted = false
-      const team = await teamHelper.getTeamByGroupAndCollection(
-        collectionId,
-        invitation.role,
-        models.Team,
-      )
-      if (invitation.role === 'handlingEditor') {
-        delete collection.handlingEditor
-      }
-      await collection.save()
-      await teamHelper.removeTeamMember(team.id, user.id, models.Team)
-      user.teams = user.teams.filter(userTeamId => team.id !== userTeamId)
+
+      if (invitation.role === 'handlingEditor')
+        await teamHelper.updateHETeam(
+          collection,
+          invitation.role,
+          models.Team,
+          user,
+        )
       if (reason !== undefined) {
         invitation.reason = reason
+        await collection.save()
       }
       try {
         if (invitation.role === 'handlingEditor') {
diff --git a/packages/xpub-faraday/config/authsome-helpers.js b/packages/xpub-faraday/config/authsome-helpers.js
index 89f456158..e9eb600cb 100644
--- a/packages/xpub-faraday/config/authsome-helpers.js
+++ b/packages/xpub-faraday/config/authsome-helpers.js
@@ -19,4 +19,10 @@ module.exports = {
       coll.visibleStatus = statuses[coll.status].private
     }
   },
+  filterRefusedReviewers: (coll, user) => {
+    const matchingInv = coll.invitations.find(inv => inv.userId === user.id)
+    if (matchingInv === undefined) return null
+    if (matchingInv.hasAnswer === true && !matchingInv.isAccepted) return null
+    return coll
+  },
 }
diff --git a/packages/xpub-faraday/config/authsome.js b/packages/xpub-faraday/config/authsome.js
index 9418e830b..cd7bed1d0 100644
--- a/packages/xpub-faraday/config/authsome.js
+++ b/packages/xpub-faraday/config/authsome.js
@@ -35,6 +35,9 @@ async function teamPermissions(user, operation, object, context) {
             }
             helpers.setPublicStatuses(coll, matchingCollPerm)
             helpers.parseReviewerAuthors(coll, matchingCollPerm)
+            if (['reviewer'].includes(matchingCollPerm.permission)) {
+              return helpers.filterRefusedReviewers(coll, user)
+            }
             return coll
           })
           .filter(Boolean)
-- 
GitLab