From 61b609aff1e7a80c111b225bc382df87c9488822 Mon Sep 17 00:00:00 2001
From: Bogdan Cochior <bogdan.cochior@thinslices.com>
Date: Thu, 26 Jul 2018 10:41:09 +0300
Subject: [PATCH] fix(authsome): temporary fix for collection userPermissions

---
 .../src/components/SubmissionModal.js         |  2 +-
 .../component-wizard/src/redux/conversion.js  | 44 ++++++++++++++-----
 packages/xpub-faraday/config/authsome-mode.js |  5 ++-
 3 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/packages/component-modal/src/components/SubmissionModal.js b/packages/component-modal/src/components/SubmissionModal.js
index 62060fb6b..f092ac3c3 100644
--- a/packages/component-modal/src/components/SubmissionModal.js
+++ b/packages/component-modal/src/components/SubmissionModal.js
@@ -22,7 +22,7 @@ const SubmissionModal = ({
     <Title>{title}</Title>
     <Text>
       The corresponding author confirms that all co-authors are included, and
-      taht everyone listed as co-author agrees to that role and all the
+      that everyone listed as co-author agrees to that role and all the
       following requirements and acknowledgements. The submission represents
       original work and that sources are given proper attribution.
     </Text>
diff --git a/packages/component-wizard/src/redux/conversion.js b/packages/component-wizard/src/redux/conversion.js
index 5bdabda09..94cb6eb3e 100644
--- a/packages/component-wizard/src/redux/conversion.js
+++ b/packages/component-wizard/src/redux/conversion.js
@@ -23,16 +23,35 @@ export const isRevisionFlow = (state, collection, fragment = {}) =>
   collection.fragments.length > 1 && !fragment.submitted
 
 /* actions */
-const addSubmittingAuthor = (user, collectionId, fragmentId) => {
-  const author = {
-    ...pick(user, ['id', 'email', 'affiliation', 'firstName', 'lastName']),
-    isSubmitting: true,
-    isCorresponding: true,
+// const addSubmittingAuthor = (user, collectionId, fragmentId) => {
+//   const author = {
+//     ...pick(user, ['id', 'email', 'affiliation', 'firstName', 'lastName']),
+//     isSubmitting: true,
+//     isCorresponding: true,
+//   }
+//   create(`/collections/${collectionId}/fragments/${fragmentId}/users`, {
+//     role: 'author',
+//     ...author,
+//   })
+// }
+
+const getAuthors = currentUser => {
+  if (!currentUser.admin) {
+    return [
+      {
+        ...pick(currentUser, [
+          'id',
+          'email',
+          'affiliation',
+          'firstName',
+          'lastName',
+        ]),
+        isSubmitting: true,
+        isCorresponding: true,
+      },
+    ]
   }
-  create(`/collections/${collectionId}/fragments/${fragmentId}/users`, {
-    role: 'author',
-    ...author,
-  })
+  return []
 }
 
 export const createDraftSubmission = history => (dispatch, getState) => {
@@ -48,6 +67,7 @@ export const createDraftSubmission = history => (dispatch, getState) => {
     return dispatch(
       actions.createFragment(collection, {
         created: new Date(), // TODO: set on server
+        authors: getAuthors(currentUser),
         collectionId: collection.id,
         files: {
           manuscripts: [],
@@ -63,9 +83,9 @@ export const createDraftSubmission = history => (dispatch, getState) => {
         throw new Error('Failed to create a project')
       }
       const route = `/projects/${collection.id}/versions/${fragment.id}/submit`
-      if (!currentUser.admin) {
-        addSubmittingAuthor(currentUser, collection.id, fragment.id)
-      }
+      // if (!currentUser.admin) {
+      //   addSubmittingAuthor(currentUser, collection.id, fragment.id)
+      // }
 
       // redirect after a short delay
       window.setTimeout(() => {
diff --git a/packages/xpub-faraday/config/authsome-mode.js b/packages/xpub-faraday/config/authsome-mode.js
index 2a6968bf4..b30cc31d3 100644
--- a/packages/xpub-faraday/config/authsome-mode.js
+++ b/packages/xpub-faraday/config/authsome-mode.js
@@ -96,12 +96,13 @@ async function applyAuthenticatedUserPolicy(user, operation, object, context) {
             user,
             Team: context.models.Team,
           })
-
-          const { role } = userPermissions.find(
+          // TODO: check why collPermission can be undefined
+          const collPermission = userPermissions.find(
             p =>
               p.objectId === collection.id ||
               collection.fragments.includes(p.objectId),
           )
+          const role = get(collPermission, 'role', 'author')
           const visibleStatus = get(statuses, `${status}.${role}.label`)
           const parsedCollection = helpers.stripeCollectionByRole(
             collection,
-- 
GitLab