From 4c330e22db7121e3afad7853a70c16a9e87bbd7b Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebastian.mihalache@gmail.con>
Date: Mon, 7 May 2018 16:04:28 +0300
Subject: [PATCH] feat(authsome): remove coverletter from file when reviewer

---
 .../xpub-faraday/config/authsome-helpers.js   | 26 ++++++++++++++-----
 packages/xpub-faraday/config/authsome-mode.js | 15 ++++++++---
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/packages/xpub-faraday/config/authsome-helpers.js b/packages/xpub-faraday/config/authsome-helpers.js
index 69f01bc6b..ff76ff3e9 100644
--- a/packages/xpub-faraday/config/authsome-helpers.js
+++ b/packages/xpub-faraday/config/authsome-helpers.js
@@ -27,22 +27,34 @@ const filterRefusedInvitations = (coll, user) => {
   return coll
 }
 
-const filterCollectionData = (collectionsPermissions, collection, user) => {
+const filterObjectData = (collectionsPermissions, object, user) => {
+  if (object.type === 'fragment') {
+    const matchingCollPerm = collectionsPermissions.find(
+      collPerm => object.id === collPerm.fragmentId,
+    )
+    if (matchingCollPerm === undefined) return {}
+    if (['reviewer'].includes(matchingCollPerm.permission)) {
+      object.files = omit(object.files, ['coverLetter'])
+    }
+
+    return object
+  }
   const matchingCollPerm = collectionsPermissions.find(
-    collPerm => collection.id === collPerm.id,
+    collPerm => object.id === collPerm.id,
   )
-  setPublicStatuses(collection, matchingCollPerm)
-  parseAuthorsData(collection, matchingCollPerm)
+  if (matchingCollPerm === undefined) return {}
+  setPublicStatuses(object, matchingCollPerm)
+  parseAuthorsData(object, matchingCollPerm)
   if (['reviewer', 'handlingEditor'].includes(matchingCollPerm.permission)) {
-    return filterRefusedInvitations(collection, user)
+    return filterRefusedInvitations(object, user)
   }
 
-  return collection
+  return object
 }
 
 module.exports = {
   parseAuthorsData,
   setPublicStatuses,
   filterRefusedInvitations,
-  filterCollectionData,
+  filterObjectData,
 }
diff --git a/packages/xpub-faraday/config/authsome-mode.js b/packages/xpub-faraday/config/authsome-mode.js
index 878c15519..ce1b73c53 100644
--- a/packages/xpub-faraday/config/authsome-mode.js
+++ b/packages/xpub-faraday/config/authsome-mode.js
@@ -4,7 +4,7 @@ const omit = require('lodash/omit')
 const helpers = require('./authsome-helpers')
 
 async function teamPermissions(user, operation, object, context) {
-  if (object.type !== 'collection') return true
+  // if (object.type !== 'collection') return true
 
   const permissions = ['handlingEditor', 'author', 'reviewer']
   const teams = await Promise.all(
@@ -16,10 +16,17 @@ async function teamPermissions(user, operation, object, context) {
       return null
     }),
   )
-
+  const objectType = get(object, 'type')
+  let fragmentId = null
+  if (objectType !== undefined) {
+    if (objectType === 'fragment') {
+      fragmentId = object.id
+    }
+  }
   const collectionsPermissions = teams.filter(Boolean).map(team => ({
     id: team.object.id,
     permission: team.teamType.permissions,
+    fragmentId,
   }))
 
   if (collectionsPermissions.length === 0) return {}
@@ -27,7 +34,7 @@ async function teamPermissions(user, operation, object, context) {
   return {
     filter: filterParam => {
       if (!filterParam.length) {
-        return helpers.filterCollectionData(
+        return helpers.filterObjectData(
           collectionsPermissions,
           filterParam,
           user,
@@ -36,7 +43,7 @@ async function teamPermissions(user, operation, object, context) {
 
       const collections = filterParam
         .map(coll =>
-          helpers.filterCollectionData(collectionsPermissions, coll, user),
+          helpers.filterObjectData(collectionsPermissions, coll, user),
         )
         .filter(Boolean)
       return collections
-- 
GitLab