From c9a4bf0615db5e507bb4170981f14eabf3e24fe3 Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebi.mihalache@gmail.com>
Date: Thu, 4 Oct 2018 17:09:45 +0300
Subject: [PATCH] fix(manuscript-manager): handle not found error

---
 .../src/contextualBoxes/AssignHE.js           |  1 +
 .../xpub-faraday/config/authsome-helpers.js   | 39 +++++++++++--------
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/packages/component-faraday-ui/src/contextualBoxes/AssignHE.js b/packages/component-faraday-ui/src/contextualBoxes/AssignHE.js
index 38954576b..c449426ad 100644
--- a/packages/component-faraday-ui/src/contextualBoxes/AssignHE.js
+++ b/packages/component-faraday-ui/src/contextualBoxes/AssignHE.js
@@ -75,6 +75,7 @@ const AssignHE = ({
               <OpenModal
                 confirmText="Invite"
                 isFetching={isFetching}
+                modalKey={`${he.id}-invitehe`}
                 onConfirm={inviteHandlingEditor(he)}
                 subtitle={he.name}
                 title="Confirm Invitation"
diff --git a/packages/xpub-faraday/config/authsome-helpers.js b/packages/xpub-faraday/config/authsome-helpers.js
index 2a154bacf..c028d702d 100644
--- a/packages/xpub-faraday/config/authsome-helpers.js
+++ b/packages/xpub-faraday/config/authsome-helpers.js
@@ -1,6 +1,6 @@
 const { omit, get, last } = require('lodash')
-
 const config = require('config')
+const logger = require('@pubsweet/logger')
 
 const statuses = config.get('statuses')
 
@@ -206,23 +206,30 @@ const parseUser = ({ user }) => omit(user, sensitiveUserProperties)
 const getCollections = async ({ user, models }) => {
   const userPermisssions = await getUserPermissions({ user, Team: models.Team })
 
-  return Promise.all(
+  return (await Promise.all(
     userPermisssions.map(async up => {
       let fragment = {}
       let collection = {}
-      if (up.objectType === 'collection') {
-        collection = await models.Collection.find(up.objectId)
-        const latestFragmentId =
-          collection.fragments[collection.fragments.length - 1]
-        collection.currentVersion = await models.Fragment.find(latestFragmentId)
-      } else {
-        fragment = await models.Fragment.find(up.objectId)
-        collection = await models.Collection.find(fragment.collectionId)
-        collection.currentVersion = stripeFragmentByRole({
-          fragment,
-          role: up.role,
-          user,
-        })
+      try {
+        if (up.objectType === 'collection') {
+          collection = await models.Collection.find(up.objectId)
+          const latestFragmentId =
+            collection.fragments[collection.fragments.length - 1]
+          collection.currentVersion = await models.Fragment.find(
+            latestFragmentId,
+          )
+        } else {
+          fragment = await models.Fragment.find(up.objectId)
+          collection = await models.Collection.find(fragment.collectionId)
+          collection.currentVersion = stripeFragmentByRole({
+            fragment,
+            role: up.role,
+            user,
+          })
+        }
+      } catch (e) {
+        logger.error(`DB ERROR: ${e.message}`)
+        return null
       }
       const status = get(collection, 'status', 'draft')
       collection.visibleStatus = get(statuses, `${status}.${up.role}.label`)
@@ -234,7 +241,7 @@ const getCollections = async ({ user, models }) => {
 
       return stripedColl.coll
     }),
-  )
+  )).filter(Boolean)
 }
 
 module.exports = {
-- 
GitLab