From e5dac251f9b079c7f6f9e31b4e0d452d7f93f80e Mon Sep 17 00:00:00 2001 From: Sebastian Mihalache <sebastian.mihalache@gmail.con> Date: Tue, 19 Jun 2018 17:02:34 +0300 Subject: [PATCH] feat(component-user-manager): add author to fragment --- .../src/routes/fragmentsUsers/post.js | 65 +++++++++++-------- packages/xpub-faraday/config/authsome-mode.js | 4 +- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/packages/component-user-manager/src/routes/fragmentsUsers/post.js b/packages/component-user-manager/src/routes/fragmentsUsers/post.js index 7be2f60ea..06740101e 100644 --- a/packages/component-user-manager/src/routes/fragmentsUsers/post.js +++ b/packages/component-user-manager/src/routes/fragmentsUsers/post.js @@ -1,7 +1,12 @@ const { pick } = require('lodash') const mailService = require('pubsweet-component-mail-service') -const { User, Team, services } = require('pubsweet-component-helper-service') +const { + User, + Team, + services, + Fragment, +} = require('pubsweet-component-helper-service') const authorKeys = [ 'id', @@ -48,27 +53,28 @@ module.exports = models => async (req, res) => { try { let user = await UserModel.findByEmail(email) - if (role === 'author') { - await teamHelper.setupTeam({ user, role, objectType: 'fragment' }) - user = await UserModel.find(user.id) - - if (fragment.authors !== undefined) { - const match = fragment.authors.find(author => author.userId === user.id) - if (match) { - return res.status(400).json({ - error: `User ${user.email} is already an author`, - }) - } - } + if (role !== 'author') { + return res.status(400).json({ + error: `${role} is not defined`, + }) + } - return res.status(200).json({ - ...pick(user, authorKeys), - isSubmitting, - isCorresponding, + await teamHelper.setupTeam({ user, role, objectType: 'fragment' }) + user = await UserModel.find(user.id) + + fragment.authors = fragment.authors || [] + const match = fragment.authors.find(author => author.userId === user.id) + + if (match) { + return res.status(400).json({ + error: `User ${user.email} is already an author`, }) } - return res.status(400).json({ - error: `${role} is not defined`, + + return res.status(200).json({ + ...pick(user, authorKeys), + isSubmitting, + isCorresponding, }) } catch (e) { if (role !== 'author') @@ -96,17 +102,22 @@ module.exports = models => async (req, res) => { objectType: 'fragment', }) - if (collection.owners.includes(newUser.id)) { - return res.status(200).json(newUser) - } - - mailService.sendSimpleEmail({ - toEmail: newUser.email, + const fragmentHelper = new Fragment({ fragment }) + await fragmentHelper.addAuthor({ user: newUser, - emailType: 'add-author', - dashboardUrl: baseUrl, + isSubmitting, + isCorresponding, }) + if (!collection.owners.includes(newUser.id)) { + mailService.sendSimpleEmail({ + toEmail: newUser.email, + user: newUser, + emailType: 'add-author', + dashboardUrl: baseUrl, + }) + } + return res.status(200).json({ ...pick(newUser, authorKeys), isSubmitting, diff --git a/packages/xpub-faraday/config/authsome-mode.js b/packages/xpub-faraday/config/authsome-mode.js index 8da1d852c..37e4ff7da 100644 --- a/packages/xpub-faraday/config/authsome-mode.js +++ b/packages/xpub-faraday/config/authsome-mode.js @@ -157,7 +157,7 @@ async function authenticatedUser(user, operation, object, context) { } } - if (get(object, 'type') === 'user' && get(object, 'id') === user.id) { + if (get(object, 'type') === 'user') { return true } } @@ -201,7 +201,7 @@ async function authenticatedUser(user, operation, object, context) { // allow reviewer to patch his recommendation if ( get(object, 'path') === - '/api/collections/:collectionId/fragments/:fragmentId/recommendations' + '/api/collections/:collectionId/fragments/:fragmentId/recommendations/:recommendationId' ) { return helpers.hasPermissionForObject({ user, -- GitLab