From 934a4e5f3b36d227bf654f47b8343061f6daa18e Mon Sep 17 00:00:00 2001
From: Sebastian Mihalache <sebi@Sebastians-MacBook-Pro.local>
Date: Mon, 15 Jan 2018 17:41:01 +0200
Subject: [PATCH] finished remove author

---
 packages/xpub-faraday-server/README.md        |  3 +
 packages/xpub-faraday-server/package.json     |  3 +
 .../xpub-faraday-server/src/AuthorBackend.js  | 60 ++++++++++++++++++-
 packages/xpub-faraday/config/validations.js   |  2 +-
 4 files changed, 65 insertions(+), 3 deletions(-)
 create mode 100644 packages/xpub-faraday-server/README.md

diff --git a/packages/xpub-faraday-server/README.md b/packages/xpub-faraday-server/README.md
new file mode 100644
index 000000000..79c103c5a
--- /dev/null
+++ b/packages/xpub-faraday-server/README.md
@@ -0,0 +1,3 @@
+## xPub-faraday-server
+
+A server package that adds extra features needed by `xpub-faraday` on top of `pubsweet`
\ No newline at end of file
diff --git a/packages/xpub-faraday-server/package.json b/packages/xpub-faraday-server/package.json
index d51b87dcc..d98f0c016 100644
--- a/packages/xpub-faraday-server/package.json
+++ b/packages/xpub-faraday-server/package.json
@@ -3,6 +3,9 @@
   "version": "0.0.1",
   "description": "xpub configured for faraday",
   "license": "MIT",
+  "files": [
+    "src"
+  ],
   "repository": {
     "type": "git",
     "url": "https://gitlab.coko.foundation/xpub/xpub"
diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js
index 5e68dd472..075df4bc4 100644
--- a/packages/xpub-faraday-server/src/AuthorBackend.js
+++ b/packages/xpub-faraday-server/src/AuthorBackend.js
@@ -2,10 +2,14 @@ const bodyParser = require('body-parser')
 
 const AuthorBackend = app => {
   app.post(
-    '/api/:fragmentId/author',
+    '/api/fragments/:fragmentId/authors',
     bodyParser.json(),
     async (req, res, next) => {
       try {
+        if (!req.params.fragmentId) {
+          res.status(400).json({ error: 'Fragment ID is required' })
+          return
+        }
         let fragment = await app.locals.models.Fragment.find(
           req.params.fragmentId,
         )
@@ -14,7 +18,59 @@ const AuthorBackend = app => {
         fragment = await fragment.save()
         res.status(200).json(fragment)
       } catch (e) {
-        res.status(400).json({ error: e })
+        if (e.name === 'NotFoundError') {
+          res.status(e.status).json({ error: 'Fragment not found' })
+          return
+        }
+
+        if (e.name === 'ValidationError') {
+          res.status(404).json({ error: e.details[0].message })
+          return
+        }
+        res.status(400).json({ error: 'Something went wrong' })
+      }
+    },
+  )
+  app.delete(
+    '/api/fragments/:fragmentId/authors/:authorEmail',
+    async (req, res, next) => {
+      const { fragmentId, authorEmail } = req.params
+      try {
+        let fragment = await app.locals.models.Fragment.find(fragmentId)
+        if (fragment.authors === 'undefined') {
+          res.status(404).json({ error: 'Fragment does not have any authors' })
+          return
+        }
+        // find author in authors list by email
+        if (fragment.authors.length === 0) {
+          res.status(404).json({ error: 'Fragment does not have any authors' })
+          return
+        }
+
+        const newAuthors = fragment.authors.filter(
+          author => author.email !== authorEmail,
+        )
+
+        if (newAuthors.length === fragment.authors.length) {
+          res.status(404).json({ error: 'Author not found' })
+          return
+        }
+
+        fragment.authors = newAuthors
+        fragment = await fragment.save()
+        res.status(204).json({})
+        return
+      } catch (e) {
+        if (e.name === 'NotFoundError') {
+          res.status(e.status).json({ error: 'Fragment not found' })
+          return
+        }
+
+        if (e.name === 'ValidationError') {
+          res.status(404).json({ error: e.details[0].message })
+          return
+        }
+        res.status(400).json({ error: 'Something went wrong' })
       }
     },
   )
diff --git a/packages/xpub-faraday/config/validations.js b/packages/xpub-faraday/config/validations.js
index 453f43806..238686df6 100644
--- a/packages/xpub-faraday/config/validations.js
+++ b/packages/xpub-faraday/config/validations.js
@@ -67,7 +67,7 @@ module.exports = {
             .email()
             .required(),
           affiliation: Joi.string().required(),
-          country: Joi.string().required(),
+          country: Joi.string().allow(''),
           is_submitting: Joi.boolean(),
           is_corresponding: Joi.boolean(),
         }),
-- 
GitLab