From 0fb51174c61da902be22747476c7a4b68bf88db9 Mon Sep 17 00:00:00 2001
From: Mihail Hagiu <mihail.hagiu@thinslices.com>
Date: Tue, 11 Dec 2018 11:50:31 +0200
Subject: [PATCH] feat(authsome-mode):Refactor

---
 packages/xpub-faraday/config/authsome-mode.js | 46 ++++++++-----------
 1 file changed, 19 insertions(+), 27 deletions(-)

diff --git a/packages/xpub-faraday/config/authsome-mode.js b/packages/xpub-faraday/config/authsome-mode.js
index 29557c911..fd332e968 100644
--- a/packages/xpub-faraday/config/authsome-mode.js
+++ b/packages/xpub-faraday/config/authsome-mode.js
@@ -1,5 +1,4 @@
 const config = require('config')
-const logger = require('@pubsweet/logger')
 const { get, pickBy, last, has, pick } = require('lodash')
 
 const statuses = config.get('statuses')
@@ -66,6 +65,10 @@ function unauthenticatedUser(operation, object, userId) {
   return false
 }
 
+const filterDraftCollections = c => get(c, 'status', 'draft') !== 'draft'
+
+const filterNoFragmentCollections = c => c.fragments.length !== 0
+
 const createPaths = ['/collections', '/collections/:collectionId/fragments']
 
 async function applyAuthenticatedUserPolicy(user, operation, object, context) {
@@ -309,25 +312,17 @@ async function applyAdminPolicy(user, operation, object, context) {
 
     if (get(object, 'path') === '/api/collections') {
       const collections = await context.models.Collection.all()
-      const modifiedCollections = await Promise.all(
-        collections.map(async coll => {
-          if (coll.fragments.length === 0) {
-            logger.error(`Collection ${coll.id} does not have any fragments!`)
-
-            return null
-          }
+      return Promise.all(
+        collections.filter(filterNoFragmentCollections).map(async coll => {
           const latestFragmentId = coll.fragments[coll.fragments.length - 1]
           coll.currentVersion = await context.models.Fragment.find(
             latestFragmentId,
           )
           const status = get(coll, 'status', 'draft')
           coll.visibleStatus = get(statuses, `${status}.admin.label`)
-
           return coll
         }),
       )
-
-      return modifiedCollections.filter(Boolean)
     }
   }
   return true
@@ -357,23 +352,20 @@ async function applyEditorInChiefPolicy(user, operation, object, context) {
 
     if (get(object, 'path') === '/api/collections') {
       const collections = await context.models.Collection.all()
-      const modifiedCollections = await Promise.all(
-        collections.map(async coll => {
-          if (coll.fragments.length === 0) {
-            logger.error(`Collection ${coll.id} does not have any fragments!`)
-            return null
-          }
-          const latestFragmentId = coll.fragments[coll.fragments.length - 1]
-          coll.currentVersion = await context.models.Fragment.find(
-            latestFragmentId,
-          )
-          const status = get(coll, 'status', 'draft')
-          if (status === 'draft') return null
-          coll.visibleStatus = get(statuses, `${status}.editorInChief.label`)
-          return coll
-        }),
+      return Promise.all(
+        collections
+          .filter(filterDraftCollections)
+          .filter(filterNoFragmentCollections)
+          .map(async coll => {
+            const latestFragmentId = coll.fragments[coll.fragments.length - 1]
+            coll.currentVersion = await context.models.Fragment.find(
+              latestFragmentId,
+            )
+            const status = get(coll, 'status', 'draft')
+            coll.visibleStatus = get(statuses, `${status}.editorInChief.label`)
+            return coll
+          }),
       )
-      return modifiedCollections.filter(Boolean)
     }
   }
   return true
-- 
GitLab