From cb16a0fe3ca2ca0d93a8e1be6cec45028c3e4519 Mon Sep 17 00:00:00 2001
From: Sebastian <sebastian.mihalache@thinslices.com>
Date: Thu, 29 Mar 2018 16:03:56 +0300
Subject: [PATCH] feat(component-invite): get collection users update

---
 packages/component-invite/config/test.js                    | 6 +++---
 packages/component-invite/package.json                      | 2 +-
 .../src/controllers/assignCollectionRole.js                 | 4 ++--
 packages/component-invite/src/routes/deleteInvitation.js    | 6 ++++--
 packages/component-invite/src/routes/getCollectionUsers.js  | 3 +++
 packages/component-invite/src/tests/postInvite.test.js      | 4 ++--
 packages/component-mail-service/src/Mail.js                 | 2 +-
 packages/xpub-faraday/config/authsome.js                    | 4 ++--
 8 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/packages/component-invite/config/test.js b/packages/component-invite/config/test.js
index 98b13174e..a1e52fc0b 100644
--- a/packages/component-invite/config/test.js
+++ b/packages/component-invite/config/test.js
@@ -9,12 +9,12 @@ module.exports = {
   },
   roles: {
     global: ['admin', 'editorInChief', 'author', 'handlingEditor'],
-    collection: ['handlingEditor', 'reviewer', 'coAuthor'],
+    collection: ['handlingEditor', 'reviewer', 'author'],
     inviteRights: {
-      admin: ['admin', 'editorInChief', 'author', 'handlingEditor', 'coAuthor'],
+      admin: ['admin', 'editorInChief', 'author', 'handlingEditor', 'author'],
       editorInChief: ['handlingEditor'],
       handlingEditor: ['reviewer'],
-      author: ['coAuthor'],
+      author: ['author'],
     },
   },
 }
diff --git a/packages/component-invite/package.json b/packages/component-invite/package.json
index 180757c55..898457935 100644
--- a/packages/component-invite/package.json
+++ b/packages/component-invite/package.json
@@ -7,7 +7,7 @@
   "files": ["src"],
   "main": "index.js",
   "scripts": {
-    "test": "echo \"Error: no test specified\" && exit 0"
+    "test": "jest"
   },
   "repository": {
     "type": "git",
diff --git a/packages/component-invite/src/controllers/assignCollectionRole.js b/packages/component-invite/src/controllers/assignCollectionRole.js
index 50c1e31f9..7603c877b 100644
--- a/packages/component-invite/src/controllers/assignCollectionRole.js
+++ b/packages/component-invite/src/controllers/assignCollectionRole.js
@@ -131,7 +131,7 @@ module.exports = async (
       return res.status(500).json({ error: 'Mail could not be sent.' })
     }
   } catch (e) {
-    if (e.name === 'NotFoundError' && role === 'coAuthor') {
+    if (e.name === 'NotFoundError' && role === 'author') {
       return userHelper.setupNewUser(
         body,
         url,
@@ -139,7 +139,7 @@ module.exports = async (
         email,
         role,
         models.User,
-        'invite-coauthor',
+        'invite-author',
       )
     }
     const notFoundError = await helpers.handleNotFoundError(e, 'user')
diff --git a/packages/component-invite/src/routes/deleteInvitation.js b/packages/component-invite/src/routes/deleteInvitation.js
index b85fa6d9e..f4346a3f4 100644
--- a/packages/component-invite/src/routes/deleteInvitation.js
+++ b/packages/component-invite/src/routes/deleteInvitation.js
@@ -20,8 +20,10 @@ module.exports = models => async (req, res) => {
   }
 
   const reqUser = await models.User.find(req.user)
-  if (!reqUser.editorInChief) {
-    res.status(400).json({ error: 'The request user must be Editor in Chief' })
+  if (!reqUser.editorInChief || !reqUser.admin) {
+    res
+      .status(400)
+      .json({ error: 'The request user must be Editor in Chief or Admin' })
     return
   }
 
diff --git a/packages/component-invite/src/routes/getCollectionUsers.js b/packages/component-invite/src/routes/getCollectionUsers.js
index a08cdba07..a6b3f5817 100644
--- a/packages/component-invite/src/routes/getCollectionUsers.js
+++ b/packages/component-invite/src/routes/getCollectionUsers.js
@@ -34,6 +34,9 @@ module.exports = models => async (req, res) => {
 
     const membersData = members.map(async member => {
       const user = await models.User.find(member)
+      if (role === 'author') {
+        return user
+      }
       const { timestamp, status } = inviteHelper.getInviteData(
         user.invitations,
         collectionId,
diff --git a/packages/component-invite/src/tests/postInvite.test.js b/packages/component-invite/src/tests/postInvite.test.js
index e790949de..56c8771c0 100644
--- a/packages/component-invite/src/tests/postInvite.test.js
+++ b/packages/component-invite/src/tests/postInvite.test.js
@@ -245,7 +245,7 @@ describe('Post invite route handler', () => {
   it('should return success when an author adds a new co author to a collection', async () => {
     const body = {
       email: chance.email(),
-      role: 'coAuthor',
+      role: 'author',
     }
     const req = httpMocks.createRequest({
       body,
@@ -263,7 +263,7 @@ describe('Post invite route handler', () => {
   it('should return success when an author adds an existing user as co author to a collection', async () => {
     const body = {
       email: author.email,
-      role: 'coAuthor',
+      role: 'author',
     }
     const req = httpMocks.createRequest({
       body,
diff --git a/packages/component-mail-service/src/Mail.js b/packages/component-mail-service/src/Mail.js
index 1d7e18891..760e6824c 100644
--- a/packages/component-mail-service/src/Mail.js
+++ b/packages/component-mail-service/src/Mail.js
@@ -36,7 +36,7 @@ module.exports = {
           url: dashBoardUrl,
         }
         break
-      case 'assign-coauthor':
+      case 'assign-author':
         subject = 'Manuscript Assignment on Hindawi'
         replacements = {
           url: dashBoardUrl,
diff --git a/packages/xpub-faraday/config/authsome.js b/packages/xpub-faraday/config/authsome.js
index abd7c3491..96417f9df 100644
--- a/packages/xpub-faraday/config/authsome.js
+++ b/packages/xpub-faraday/config/authsome.js
@@ -22,8 +22,8 @@ async function teamPermissions(user, operation, object, context) {
     return {
       filter: filterParam => {
         if (filterParam.length > 0) {
-          const collections = filterParam.filter(coll =>
-            collIDs.includes(coll.id),
+          const collections = filterParam.filter(
+            coll => collIDs.includes(coll.id) || coll.owners.includes(user.id),
           )
           return collections
         }
-- 
GitLab