diff --git a/packages/component-invite/src/controllers/assignCollectionRole.js b/packages/component-invite/src/controllers/assignCollectionRole.js index 2dc68984601b5ab8ddf8a60ea8ae01db49434cb0..98b44fd47bfcf57fa032ff33ee1b4f7eb143046a 100644 --- a/packages/component-invite/src/controllers/assignCollectionRole.js +++ b/packages/component-invite/src/controllers/assignCollectionRole.js @@ -14,6 +14,7 @@ module.exports = async ( collectionId, models, url, + resend, ) => { if (reqUser.admin) { logger.error(`admin tried to invite a ${role} to a collection`) @@ -62,17 +63,36 @@ module.exports = async ( try { let user = await models.User.findByEmail(email) - - const team = await teamHelper.setupManuscriptTeam( - models, - user, - collectionId, - role, - ) + let team + if (resend === undefined) { + team = await teamHelper.setupManuscriptTeam( + models, + user, + collectionId, + role, + ) + } // getting the updated user from the DB - creating a team also updates the user user = await models.User.findByEmail(email) - user = await teamHelper.setupInvitation(user, role, collectionId, team.id) + + if (user.invitations === undefined) { + user = await teamHelper.setupInvitation(user, role, collectionId, team.id) + } else { + const matchingInvitation = teamHelper.getMatchingInvitation( + user.invitations, + collectionId, + role, + ) + if (matchingInvitation === undefined) { + user = await teamHelper.setupInvitation( + user, + role, + collectionId, + team.id, + ) + } + } try { await mailService.setupAssignEmail( diff --git a/packages/component-invite/src/helpers/Team.js b/packages/component-invite/src/helpers/Team.js index 553b4cd75f9d84eceecca548b54a851a94c77331..c2d1924a6b88a6b671d4a9b5edf8c996090bb7e9 100644 --- a/packages/component-invite/src/helpers/Team.js +++ b/packages/component-invite/src/helpers/Team.js @@ -166,6 +166,11 @@ const getInviteData = (invitations, collectionId, role) => { return { timestamp, status } } +const getMatchingInvitation = (invitations, collectionId, role) => + invitations.find( + invite => invite.type === role && invite.collectionId === collectionId, + ) + module.exports = { createNewTeam, setupEiCTeams, @@ -175,4 +180,5 @@ module.exports = { removeTeamMember, getTeamMembersByCollection, getInviteData, + getMatchingInvitation, } diff --git a/packages/component-invite/src/routes/postInvite.js b/packages/component-invite/src/routes/postInvite.js index 6c8d4de3a92bccf49ad4f3a4a5de8335796dafb1..d588b7eb297db2f8715ee022d656560d3c9b4aa4 100644 --- a/packages/component-invite/src/routes/postInvite.js +++ b/packages/component-invite/src/routes/postInvite.js @@ -28,7 +28,6 @@ module.exports = models => async (req, res) => { return } const collectionId = get(req, 'params.collectionId') - const url = `${req.protocol}://${req.get('host')}` if (collectionId) { return require('../controllers/assignCollectionRole')( diff --git a/packages/component-invite/src/tests/postInvite.test.js b/packages/component-invite/src/tests/postInvite.test.js index 74aff28e5e9e68ecda6dd0eba2ae819d8d78f7aa..202c13615e9f0334d4a953598d597db951c3dd63 100644 --- a/packages/component-invite/src/tests/postInvite.test.js +++ b/packages/component-invite/src/tests/postInvite.test.js @@ -30,7 +30,13 @@ const notFoundError = new Error() notFoundError.name = 'NotFoundError' notFoundError.status = 404 -const { admin, editorInChief, handlingEditor, author } = fixtures.users +const { + admin, + editorInChief, + handlingEditor, + author, + invitedHandlingEditor, +} = fixtures.users const { standardCollection } = fixtures.collections const postInvitePath = '../routes/postInvite' describe('Post invite route handler', () => { @@ -216,4 +222,23 @@ describe('Post invite route handler', () => { const data = JSON.parse(res._getData()) expect(data.error).toEqual(`Role ${body.role} is invalid`) }) + it('should return success when the EiC resends an invitation to a handlingEditor with a collection', async () => { + const body = { + email: invitedHandlingEditor.email, + role: 'handlingEditor', + } + const req = httpMocks.createRequest({ + body, + }) + req.user = editorInChief.id + req.params.collectionId = standardCollection.id + const res = httpMocks.createResponse() + await require(postInvitePath)(models)(req, res) + + expect(res.statusCode).toBe(200) + const data = JSON.parse(res._getData()) + expect(data.email).toEqual(body.email) + expect(data.invitations[0].collectionId).toEqual(req.params.collectionId) + expect(data.invitations).toHaveLength(1) + }) })