Skip to content
Snippets Groups Projects
Commit a6a2993d authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

Merge branch 'faraday-server' into 'faraday-master'

Faraday server

See merge request xpub!93
parents 7dcda00f 95978ec3
No related branches found
No related tags found
No related merge requests found
Showing
with 165 additions and 3 deletions
_build/
api/
logs/
node_modules/
uploads/
.env.*
.env
config/local*.*
\ No newline at end of file
## xPub-faraday-server
A server package that adds extra features needed by `xpub-faraday` on top of `pubsweet`
\ No newline at end of file
module.exports = {
backend: () => app => require('./src/AuthorBackend')(app),
}
{
"name": "xpub-faraday-server",
"version": "0.0.1",
"description": "xpub configured for faraday",
"license": "MIT",
"files": [
"src"
],
"repository": {
"type": "git",
"url": "https://gitlab.coko.foundation/xpub/xpub"
},
"dependencies": {
"body-parser": "^1.17.2",
"config": "^1.26.1",
"moment": "^2.18.1",
"nodemailer": "^4.0.1"
},
"peerDependencies": {
"@pubsweet/logger": "^0.0.1",
"pubsweet-server": "^1.0.1",
"pubsweet": "^1.1.1",
"pubsweet-client": "^1.1.1"
}
}
const bodyParser = require('body-parser')
const AuthorBackend = app => {
app.post(
'/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,
)
fragment.authors = fragment.authors ? fragment.authors : []
if (fragment.authors.length > 0) {
const emailAuthors = fragment.authors.filter(
author => author.email === req.body.email,
)
if (emailAuthors.length > 0) {
res.status(400).json({ error: 'Author already exists' })
return
}
const nameAuthors = fragment.authors.filter(
author =>
author.first_name === req.body.first_name &&
author.middle_name === req.body.middle_name &&
author.last_name === req.body.last_name,
)
if (nameAuthors.length > 0) {
res.status(400).json({ error: 'Author already exists' })
return
}
}
fragment.authors.push(req.body)
fragment = await fragment.save()
res.status(200).json(fragment)
} 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' })
}
},
)
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' })
}
},
)
}
module.exports = AuthorBackend
......@@ -2,5 +2,7 @@
"pubsweet-component-xpub-app",
"pubsweet-component-xpub-authentication",
"pubsweet-component-xpub-dashboard",
"xpub-faraday-server",
"pubsweet-component-ink-backend",
"pubsweet-component-wizard"
]
{"pubsweet-server":{"secret":"702e1d23496c143026b634af15af57f5a88df7b7da9e3c24746da152d7068c72b98c692a09e76d5a618f2c2e473a8b6153bc4524a604290d04591eab9e0811e2"}}
......@@ -22,7 +22,7 @@ module.exports = {
abstract: Joi.string(),
articleType: Joi.string(),
articleSection: Joi.array().items(Joi.string()),
authors: Joi.array(),
// authors: Joi.array(),
keywords: Joi.array(),
}),
declarations: Joi.array(),
......@@ -59,6 +59,20 @@ module.exports = {
reviewers: Joi.array(),
lock: Joi.object(),
decision: Joi.object(),
authors: Joi.array().items(
Joi.object({
first_name: Joi.string().required(),
last_name: Joi.string().required(),
middle_name: Joi.string().allow(''),
email: Joi.string()
.email()
.required(),
affiliation: Joi.string().required(),
country: Joi.string().allow(''),
is_submitting: Joi.boolean(),
is_corresponding: Joi.boolean(),
}),
),
},
],
user: {
......
......@@ -41,7 +41,8 @@
"winston": "^2.4.0",
"xpub-journal": "^0.0.2",
"xpub-selectors": "^0.0.2",
"xpub-theme": "^0.0.2"
"xpub-theme": "^0.0.2",
"xpub-faraday-server": "^0.0.1"
},
"devDependencies": {
"babel-core": "^6.26.0",
......
......@@ -1295,7 +1295,7 @@ bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
version "4.11.8"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
body-parser@1.18.2, body-parser@^1.15.2:
body-parser@1.18.2, body-parser@^1.15.2, body-parser@^1.17.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.2.tgz#87678a19d84b47d859b83199bd59bce222b10454"
dependencies:
......@@ -6296,6 +6296,10 @@ node-sass@^4.5.3:
stdout-stream "^1.4.0"
"true-case-path" "^1.0.2"
nodemailer@^4.0.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/nodemailer/-/nodemailer-4.4.1.tgz#ce480eb3db7b949b3366e301b8f0af1c1248025e"
nomnom@~1.6.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/nomnom/-/nomnom-1.6.2.tgz#84a66a260174408fc5b77a18f888eccc44fb6971"
......
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