diff --git a/packages/xpub-collabra/api/.gitkeep b/packages/xpub-collabra/api/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/xpub-collabra/api/db/.gitkeep b/packages/xpub-collabra/api/db/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/xpub-collabra/api/db/dev/.gitkeep b/packages/xpub-collabra/api/db/dev/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/xpub-collabra/api/db/production/.gitkeep b/packages/xpub-collabra/api/db/production/.gitkeep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/packages/xpub-faraday-server/.gitignore b/packages/xpub-faraday-server/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..3614a810088d89d9ccaa28d82401545634874a18 --- /dev/null +++ b/packages/xpub-faraday-server/.gitignore @@ -0,0 +1,8 @@ +_build/ +api/ +logs/ +node_modules/ +uploads/ +.env.* +.env +config/local*.* \ No newline at end of file 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/index.js b/packages/xpub-faraday-server/index.js new file mode 100644 index 0000000000000000000000000000000000000000..e0310e3e92600af029a5869f91ff4b3d48a0f8bd --- /dev/null +++ b/packages/xpub-faraday-server/index.js @@ -0,0 +1,3 @@ +module.exports = { + backend: () => app => require('./src/AuthorBackend')(app), +} diff --git a/packages/xpub-faraday-server/package.json b/packages/xpub-faraday-server/package.json new file mode 100644 index 0000000000000000000000000000000000000000..d98f0c016af9cff551af398acb2f355bd5734e4a --- /dev/null +++ b/packages/xpub-faraday-server/package.json @@ -0,0 +1,25 @@ +{ + "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" + } +} diff --git a/packages/xpub-faraday-server/src/AuthorBackend.js b/packages/xpub-faraday-server/src/AuthorBackend.js new file mode 100644 index 0000000000000000000000000000000000000000..9fdc55ab64ed65866d9d4f37b92978220a985963 --- /dev/null +++ b/packages/xpub-faraday-server/src/AuthorBackend.js @@ -0,0 +1,101 @@ +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 diff --git a/packages/xpub-faraday/config/components.json b/packages/xpub-faraday/config/components.json index 8c02f48c37a60c6896047ee0bd9bbf9e837c7833..d2f94630a129aac037beb019e7f000121cd6c0c1 100644 --- a/packages/xpub-faraday/config/components.json +++ b/packages/xpub-faraday/config/components.json @@ -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" ] diff --git a/packages/xpub-faraday/config/local-development.json b/packages/xpub-faraday/config/local-development.json index 3f2081114b39b4b5320f83221aaff0b820de289b..2b249d74f689ff658c0aa3b7aa908226cc03e18e 100644 --- a/packages/xpub-faraday/config/local-development.json +++ b/packages/xpub-faraday/config/local-development.json @@ -1 +1,2 @@ {"pubsweet-server":{"secret":"702e1d23496c143026b634af15af57f5a88df7b7da9e3c24746da152d7068c72b98c692a09e76d5a618f2c2e473a8b6153bc4524a604290d04591eab9e0811e2"}} + diff --git a/packages/xpub-faraday/config/validations.js b/packages/xpub-faraday/config/validations.js index fad768ca3b83fd9adad1769340742614b6de26a3..d30f26928056ba4df3e15b7977c2ad657249525c 100644 --- a/packages/xpub-faraday/config/validations.js +++ b/packages/xpub-faraday/config/validations.js @@ -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: { diff --git a/packages/xpub-faraday/package.json b/packages/xpub-faraday/package.json index 9131984012af9750269c3ba90d628b8f57504873..d026e0a0ecf49fa1acd90b55d34dd816e2aafb34 100644 --- a/packages/xpub-faraday/package.json +++ b/packages/xpub-faraday/package.json @@ -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", diff --git a/yarn.lock b/yarn.lock index d17e4c6441874c5933d3e266b43bc99653bae1d9..026e42ed5bbd04af10413337e8fcaf8ad86eabf1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"