diff --git a/packages/xpub-faraday-server/README.md b/packages/xpub-faraday-server/README.md new file mode 100644 index 0000000000000000000000000000000000000000..79c103c5a7c0ace162991862d328954d706018a3 --- /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 d51b87dcc2d462da98d372ad24c1486274d1183c..d98f0c016af9cff551af398acb2f355bd5734e4a 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 5e68dd4724fa86bdaa5c881ce9c21b6d74b585db..075df4bc449c4420a7ee5ebc34d478e2ae5cc5dd 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 453f4380636b5a3df186b0013d8317ccbefbb58d..238686df675ccdbf4d0c3d84fea47860d2bb80ed 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(), }),