From 3b906546cf90edf9c68414c5a857fe12fddc7661 Mon Sep 17 00:00:00 2001
From: Tania Fecheta <tania.fecheta@thinslices.com>
Date: Thu, 13 Dec 2018 10:58:46 +0200
Subject: [PATCH] refactor(collections/patch): change code to add consistency

---
 .../src/routes/collections/patch.js           | 20 +++++++++----------
 .../src/tests/collections/patch.test.js       |  2 --
 .../src/components/Dashboard/DashboardPage.js |  3 ++-
 3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/packages/component-manuscript-manager/src/routes/collections/patch.js b/packages/component-manuscript-manager/src/routes/collections/patch.js
index fd8dba25c..ce752b64e 100644
--- a/packages/component-manuscript-manager/src/routes/collections/patch.js
+++ b/packages/component-manuscript-manager/src/routes/collections/patch.js
@@ -1,6 +1,7 @@
-const { has, get } = require('lodash')
+const { get } = require('lodash')
 const {
   services,
+  Collection,
   authsome: authsomeHelper,
 } = require('pubsweet-component-helper-service')
 
@@ -9,37 +10,36 @@ module.exports = models => async (req, res) => {
 
   try {
     const collection = await models.Collection.find(collectionId)
+    const collectionStatus = get(collection, 'status')
     const authsome = authsomeHelper.getAuthsome(models)
     const target = {
       path: req.route.path,
     }
     const canArchive = await authsome.can(req.user, 'PATCH', target)
 
-    if (!collection)
-      return res.status(404).json({
-        error: `Collection not found`,
-      })
-
-    if (!canArchive)
+    if (!canArchive) {
       return res.status(403).json({
         error: 'Unauthorized',
       })
+    }
 
-    if (!has(collection, 'status')) {
+    if (!collectionStatus) {
       return res.status(400).json({
         error: 'You cannot delete manuscripts while in draft.',
       })
     }
 
-    if (get(collection, 'status') === 'deleted') {
+    if (collectionStatus === 'deleted') {
       return res.status(400).json({
         error: 'Manuscript already deleted',
       })
     }
 
-    if (has(collection, 'status')) {
+    if (collectionStatus) {
       collection.status = 'deleted'
       await collection.save()
+      const collectionHelper = new Collection({ collection })
+      collectionHelper.updateStatus({ newStatus: 'deleted' })
     }
     return res.status(200).json(collection)
   } catch (e) {
diff --git a/packages/component-manuscript-manager/src/tests/collections/patch.test.js b/packages/component-manuscript-manager/src/tests/collections/patch.test.js
index 7a0ed8ad3..77767d8f9 100644
--- a/packages/component-manuscript-manager/src/tests/collections/patch.test.js
+++ b/packages/component-manuscript-manager/src/tests/collections/patch.test.js
@@ -28,8 +28,6 @@ describe('Patch colection route handler', () => {
 
   it('should return an error if the manuscript does not exist', async () => {
     const { admin } = testFixtures.users
-    const { collection } = testFixtures.collections
-    collection.status = 'underReview'
     const res = await requests.sendRequest({
       userId: admin.id,
       body,
diff --git a/packages/components-faraday/src/components/Dashboard/DashboardPage.js b/packages/components-faraday/src/components/Dashboard/DashboardPage.js
index e103791cd..689623146 100644
--- a/packages/components-faraday/src/components/Dashboard/DashboardPage.js
+++ b/packages/components-faraday/src/components/Dashboard/DashboardPage.js
@@ -3,6 +3,7 @@ import { connect } from 'react-redux'
 import { actions } from 'pubsweet-client'
 import { withJournal } from 'xpub-journal'
 import { ConnectPage } from 'xpub-connect'
+import { get } from 'lodash'
 import { withRouter } from 'react-router-dom'
 import { selectCurrentUser } from 'xpub-selectors'
 import { handleError, withFetching } from 'pubsweet-component-faraday-ui'
@@ -38,7 +39,7 @@ export default compose(
         collections,
         currentUser,
         userPermissions,
-        isAdmin: state.currentUser.user.admin,
+        isAdmin: get(state, 'currentUser.user.admin', 'false'),
       }
     },
     {
-- 
GitLab