Skip to content
Snippets Groups Projects
Commit 934a4e5f authored by Sebastian Mihalache's avatar Sebastian Mihalache
Browse files

finished remove author

parent f4468a21
No related branches found
No related tags found
No related merge requests found
## xPub-faraday-server
A server package that adds extra features needed by `xpub-faraday` on top of `pubsweet`
\ No newline at end of file
......@@ -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"
......
......@@ -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' })
}
},
)
......
......@@ -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(),
}),
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment