Skip to content
Snippets Groups Projects
Commit 3bc72808 authored by Sebastian's avatar Sebastian
Browse files

feat(component-invite): resend invitation

parent c511b3fa
No related branches found
No related tags found
1 merge request!4feat(component-invite): resend invitation
......@@ -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(
......
......@@ -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,
}
......@@ -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')(
......
......@@ -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)
})
})
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