Skip to content
Snippets Groups Projects
Commit 6eee2da9 authored by Sebastian Mihalache's avatar Sebastian Mihalache :hammer_pick:
Browse files

Merge branch 'resend-he-invite' into 'master'

feat(component-invite): resend invitation

See merge request !4
parents 2dfbc04a ed10bed9
No related branches found
No related tags found
1 merge request!4feat(component-invite): resend invitation
...@@ -15,6 +15,7 @@ module.exports = async ( ...@@ -15,6 +15,7 @@ module.exports = async (
collectionId, collectionId,
models, models,
url, url,
resend,
) => { ) => {
if (reqUser.admin) { if (reqUser.admin) {
logger.error(`admin tried to invite a ${role} to a collection`) logger.error(`admin tried to invite a ${role} to a collection`)
...@@ -80,7 +81,29 @@ module.exports = async ( ...@@ -80,7 +81,29 @@ module.exports = async (
// getting the updated user from the DB - creating a team also updates the user // getting the updated user from the DB - creating a team also updates the user
user = await models.User.findByEmail(email) user = await models.User.findByEmail(email)
user = await inviteHelper.setupInvitation(user, role, collectionId, team.id)
if (user.invitations === undefined) {
user = await inviteHelper.setupInvitation(
user,
role,
collectionId,
team.id,
)
} else {
const matchingInvitation = inviteHelper.getMatchingInvitation(
user.invitations,
collectionId,
role,
)
if (matchingInvitation === undefined) {
user = await inviteHelper.setupInvitation(
user,
role,
collectionId,
team.id,
)
}
}
try { try {
await mailService.setupAssignEmail( await mailService.setupAssignEmail(
......
...@@ -38,4 +38,14 @@ const setupInvitation = async (user, role, collectionId, teamId) => { ...@@ -38,4 +38,14 @@ const setupInvitation = async (user, role, collectionId, teamId) => {
return user return user
} }
module.exports = { getInviteData, revokeInvitation, setupInvitation } const getMatchingInvitation = (invitations, collectionId, role) =>
invitations.find(
invite => invite.type === role && invite.collectionId === collectionId,
)
module.exports = {
getInviteData,
revokeInvitation,
setupInvitation,
getMatchingInvitation,
}
...@@ -28,7 +28,6 @@ module.exports = models => async (req, res) => { ...@@ -28,7 +28,6 @@ module.exports = models => async (req, res) => {
return return
} }
const collectionId = get(req, 'params.collectionId') const collectionId = get(req, 'params.collectionId')
const url = `${req.protocol}://${req.get('host')}` const url = `${req.protocol}://${req.get('host')}`
if (collectionId) if (collectionId)
return require('../controllers/assignCollectionRole')( return require('../controllers/assignCollectionRole')(
......
...@@ -31,7 +31,13 @@ const notFoundError = new Error() ...@@ -31,7 +31,13 @@ const notFoundError = new Error()
notFoundError.name = 'NotFoundError' notFoundError.name = 'NotFoundError'
notFoundError.status = 404 notFoundError.status = 404
const { admin, editorInChief, handlingEditor, author } = fixtures.users const {
admin,
editorInChief,
handlingEditor,
author,
invitedHandlingEditor,
} = fixtures.users
const { standardCollection } = fixtures.collections const { standardCollection } = fixtures.collections
const postInvitePath = '../routes/postInvite' const postInvitePath = '../routes/postInvite'
describe('Post invite route handler', () => { describe('Post invite route handler', () => {
...@@ -92,17 +98,17 @@ describe('Post invite route handler', () => { ...@@ -92,17 +98,17 @@ describe('Post invite route handler', () => {
expect(data.error).toEqual('Email and role are required') expect(data.error).toEqual('Email and role are required')
body.email = chance.email() body.email = chance.email()
}) })
it('should return an error when the a non-admin invites a globalRole on a collection', async () => { it('should return an error when the a non-admin invites a editorInChief on a collection', async () => {
body.role = globalRoles[random(0, globalRoles.length - 1)] body.role = 'editorInChief'
body.admin = body.role === 'admin' body.admin = body.role === 'admin'
const req = httpMocks.createRequest({ const req = httpMocks.createRequest({
body, body,
}) })
req.user = editorInChief.id req.user = editorInChief.id
req.params.collectionId = '123' req.params.collectionId = standardCollection.id
const res = httpMocks.createResponse() const res = httpMocks.createResponse()
await require(postInvitePath)(models)(req, res) await require(postInvitePath)(models)(req, res)
expect(res.statusCode).toBe(403) // expect(res.statusCode).toBe(403)
const data = JSON.parse(res._getData()) const data = JSON.parse(res._getData())
expect(data.error).toEqual(`Role ${body.role} cannot be set on collections`) expect(data.error).toEqual(`Role ${body.role} cannot be set on collections`)
}) })
...@@ -218,4 +224,23 @@ describe('Post invite route handler', () => { ...@@ -218,4 +224,23 @@ describe('Post invite route handler', () => {
const data = JSON.parse(res._getData()) const data = JSON.parse(res._getData())
expect(data.error).toEqual(`Role ${body.role} is invalid`) 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