diff --git a/packages/component-invite/src/controllers/assignCollectionRole.js b/packages/component-invite/src/controllers/assignCollectionRole.js index a1489330d600d046fbd9feb747749ce0b755c7d0..cd855f6221ce59106910c090011d0eb1148f0882 100644 --- a/packages/component-invite/src/controllers/assignCollectionRole.js +++ b/packages/component-invite/src/controllers/assignCollectionRole.js @@ -18,14 +18,6 @@ module.exports = async ( url, resend, ) => { - // if (reqUser.admin) { - // logger.error(`admin tried to invite a ${role} to a collection`) - - // return res.status(403).json({ - // error: `admin cannot invite an ${role} to a collection`, - // }) - // } - if (!configRoles.collection.includes(role)) { logger.error(`invitation has been attempted with invalid role: ${role}`) return res @@ -33,11 +25,11 @@ module.exports = async ( .json({ error: `Role ${role} cannot be set on collections` }) } - if (!reqUser.editorInChief && reqUser.teams === undefined) { + if (!reqUser.editorInChief && reqUser.teams === undefined && !reqUser.admin) { return res .status(403) .json({ error: `User ${reqUser.username} is not part of any teams` }) - } else if (reqUser.editorInChief === false) { + } else if (reqUser.editorInChief === false && reqUser.admin === false) { const matchingTeams = await teamHelper.getMatchingTeams( reqUser.teams, models.Team, @@ -67,24 +59,13 @@ module.exports = async ( try { let user = await models.User.findByEmail(email) - let team = await teamHelper.getTeamByGroupAndCollection( + const team = await teamHelper.setupManuscriptTeam( + models, + user, collectionId, role, - models.Team, ) - if (team === undefined) { - team = await teamHelper.setupManuscriptTeam( - models, - user, - collectionId, - role, - ) - user = await models.User.findByEmail(email) - } else { - user.teams = user.teams || [] - user.teams.push(team.id) - user = await user.save() - } + user = await models.User.findByEmail(email) if (user.invitations === undefined) { user = await inviteHelper.setupInvitation( diff --git a/packages/component-invite/src/helpers/Collection.js b/packages/component-invite/src/helpers/Collection.js index d11dc8b7011c1fe50c5b9b7aae4d288a16eae402..da805647f2381691090bc339e83badde97c35073 100644 --- a/packages/component-invite/src/helpers/Collection.js +++ b/packages/component-invite/src/helpers/Collection.js @@ -19,4 +19,11 @@ module.exports = { collection.assignedPeople.push(assignedPerson) await collection.save() }, + removeAssignedPeople: async (collection, email) => { + const assignedPeople = collection.assignedPeople.filter( + person => person.email !== email, + ) + collection.assignedPeople = assignedPeople + await collection.save() + }, } diff --git a/packages/component-invite/src/helpers/Team.js b/packages/component-invite/src/helpers/Team.js index 39bbca5152ab564cbc4e5ac53de18953a962de4a..9a9319e25d942c33b65c0a13053a1e0868871989 100644 --- a/packages/component-invite/src/helpers/Team.js +++ b/packages/component-invite/src/helpers/Team.js @@ -87,7 +87,7 @@ const setupManuscriptTeam = async (models, user, collectionId, role) => { team.members.push(user.id) try { - team = await team.updateProperties(team) + // team = await team.updateProperties(team) team = await team.save() user.teams.push(team.id) await user.save() diff --git a/packages/component-invite/src/routes/deleteInvitation.js b/packages/component-invite/src/routes/deleteInvitation.js index 754e4d9b4108af0073aac88c49cf21733be4ea92..9062262829d0c6ff875bde722f6540feedb84758 100644 --- a/packages/component-invite/src/routes/deleteInvitation.js +++ b/packages/component-invite/src/routes/deleteInvitation.js @@ -4,6 +4,7 @@ const config = require('config') const inviteHelper = require('../helpers/Invitation') const mailService = require('pubsweet-component-mail-service') const logger = require('@pubsweet/logger') +const collectionHelper = require('../helpers/Collection') const configRoles = config.get('roles') module.exports = models => async (req, res) => { @@ -26,7 +27,7 @@ module.exports = models => async (req, res) => { const { collectionId, userId } = req.params try { - await models.Collection.find(collectionId) + const collection = await models.Collection.find(collectionId) let user = await models.User.find(userId) const team = await teamHelper.getTeamByGroupAndCollection( collectionId, @@ -43,6 +44,7 @@ module.exports = models => async (req, res) => { await inviteHelper.revokeInvitation(user, collectionId, role) user = await models.User.find(userId) await teamHelper.removeTeamMember(team.id, userId, models.Team) + await collectionHelper.removeAssignedPeople(collection, user.email) try { await mailService.setupRevokeInvitationEmail( user.email, diff --git a/packages/component-invite/src/tests/deleteInvitation.test.js b/packages/component-invite/src/tests/deleteInvitation.test.js index 30e994b12abf5365195570f6d48892815709e6f7..e20a0773af872f4105b4aa171588124a2415ca1a 100644 --- a/packages/component-invite/src/tests/deleteInvitation.test.js +++ b/packages/component-invite/src/tests/deleteInvitation.test.js @@ -27,7 +27,7 @@ describe('Delete Invitation route handler', () => { const res = httpMocks.createResponse() const initialSize = handlingEditor.invitations.length await require(deleteInvitationPath)(models)(req, res) - expect(res.statusCode).toBe(204) + expect(res.statusCode).toBe(200) expect(heTeam.members).not.toContain(handlingEditor.id) expect(handlingEditor.invitations).toHaveLength(initialSize - 1) }) diff --git a/packages/component-invite/src/tests/fixtures/collections.js b/packages/component-invite/src/tests/fixtures/collections.js index 210aa318228faea749cc790593ec29bb8a5088d4..09b4b6eb797d61b15e529b247b5e76161736859c 100644 --- a/packages/component-invite/src/tests/fixtures/collections.js +++ b/packages/component-invite/src/tests/fixtures/collections.js @@ -1,4 +1,5 @@ const Chance = require('chance') +const { handlingEditor } = require('./userData') const chance = new Chance() module.exports = { @@ -9,6 +10,16 @@ module.exports = { fragments: [], owners: [], save: jest.fn(), + assignedPeople: [ + { + id: handlingEditor.id, + name: `${handlingEditor.firstName} ${handlingEditor.lastName}`, + role: 'handlingEditor', + email: handlingEditor.email, + hasAnswer: false, + isAccepted: false, + }, + ], }, noTeamCollection: { id: chance.guid(), diff --git a/packages/component-invite/src/tests/fixtures/userData.js b/packages/component-invite/src/tests/fixtures/userData.js new file mode 100644 index 0000000000000000000000000000000000000000..a4a2327911e9969621d34315b0b4b0ad9176fdbf --- /dev/null +++ b/packages/component-invite/src/tests/fixtures/userData.js @@ -0,0 +1,12 @@ +const Chance = require('chance') + +const chance = new Chance() + +module.exports = { + handlingEditor: { + id: chance.guid(), + email: chance.email(), + firstName: chance.first(), + lastName: chance.last(), + }, +} diff --git a/packages/component-invite/src/tests/fixtures/users.js b/packages/component-invite/src/tests/fixtures/users.js index 53e00485ec4413381309488acb126c2ccb4b2dfe..c1542563fd1b62c510a94705386cb06be944c341 100644 --- a/packages/component-invite/src/tests/fixtures/users.js +++ b/packages/component-invite/src/tests/fixtures/users.js @@ -1,5 +1,6 @@ const { standardCollection } = require('./collections') const { heTeamID, reviewerTeamID } = require('./teamIDs') +const { handlingEditor } = require('./userData') const users = { admin: { @@ -29,12 +30,12 @@ const users = { handlingEditor: { type: 'user', username: 'handling', - email: 'handling@example.com', + email: handlingEditor.email, password: 'test', admin: false, - id: 'handling123', - firstName: 'Handling', - lastName: 'Editor', + id: handlingEditor.id, + firstName: handlingEditor.firstName, + lastName: handlingEditor.lastName, invitations: [ { type: 'handlingEditor', diff --git a/packages/component-invite/src/tests/postHandleInvitation.test.js b/packages/component-invite/src/tests/postHandleInvitation.test.js index e46e584d6980000aa96c3b145ce23d40d4ff130a..68bec23c38908eec9f2bedf7755b730ea66b96d0 100644 --- a/packages/component-invite/src/tests/postHandleInvitation.test.js +++ b/packages/component-invite/src/tests/postHandleInvitation.test.js @@ -37,7 +37,7 @@ describe('Post handle invitation route handler', () => { req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() await require(postInvitationPath)(models)(req, res) - expect(res.statusCode).toBe(204) + expect(res.statusCode).toBe(200) expect(handlingEditor.invitations[0].hasAnswer).toBeTruthy() expect(handlingEditor.invitations[0].isAccepted).toBeTruthy() }) @@ -54,7 +54,7 @@ describe('Post handle invitation route handler', () => { const res = httpMocks.createResponse() await require(postInvitationPath)(models)(req, res) - expect(res.statusCode).toBe(204) + expect(res.statusCode).toBe(200) expect(invitedHandlingEditor.invitations[0].hasAnswer).toBeTruthy() expect(invitedHandlingEditor.invitations[0].isAccepted).toBeFalsy() }) diff --git a/packages/component-invite/src/tests/postInvite.test.js b/packages/component-invite/src/tests/postInvite.test.js index e350f5c33c485da065b6d4e029e9aa59fd06ad3c..fce9d62a45e5e41f4ccbfd1b7622ab6c9f3e61bf 100644 --- a/packages/component-invite/src/tests/postInvite.test.js +++ b/packages/component-invite/src/tests/postInvite.test.js @@ -39,6 +39,7 @@ const { invitedHandlingEditor, } = fixtures.users const { standardCollection } = fixtures.collections +const { heTeam } = fixtures.teams const postInvitePath = '../routes/postInvite' describe('Post invite route handler', () => { it('should return success when the admin invites a global role', async () => { @@ -55,36 +56,6 @@ describe('Post invite route handler', () => { expect(data.email).toEqual(body.email) expect(data.admin).toEqual(body.admin) }) - it('should return an error when the admin invites a user on a collection', async () => { - const req = httpMocks.createRequest({ - body, - }) - req.user = admin.id - req.params.collectionId = '123' - const res = httpMocks.createResponse() - await require(postInvitePath)(models)(req, res) - expect(res.statusCode).toBe(403) - const data = JSON.parse(res._getData()) - expect(data.error).toEqual( - `admin cannot invite an ${body.role} to a collection`, - ) - }) - it('should return an error when the admin invites a reviewer', async () => { - body.role = 'reviewer' - body.admin = false - const req = httpMocks.createRequest({ - body, - }) - req.user = admin.id - const res = httpMocks.createResponse() - await require(postInvitePath)(models)(req, res) - expect(res.statusCode).toBe(403) - const data = JSON.parse(res._getData()) - expect(data.error).toEqual( - `admin tried to invite an invalid role: ${body.role}`, - ) - body.role = globalRoles[random(0, globalRoles.length - 1)] - }) it('should return an error params are missing', async () => { delete body.email const req = httpMocks.createRequest({ @@ -171,6 +142,7 @@ describe('Post invite route handler', () => { body, }) req.user = editorInChief.id + const initialSize = standardCollection.assignedPeople.length req.params.collectionId = standardCollection.id const res = httpMocks.createResponse() await require(postInvitePath)(models)(req, res) @@ -179,7 +151,11 @@ describe('Post invite route handler', () => { const data = JSON.parse(res._getData()) expect(data.email).toEqual(body.email) expect(data.invitations[0].collectionId).toEqual(req.params.collectionId) - expect(standardCollection.assignedPeople).toHaveLength(1) + expect(standardCollection.assignedPeople.length).toBeGreaterThan( + initialSize, + ) + expect(heTeam.members).toContain(author.id) + expect(author.teams).toContain(heTeam.id) }) it('should return success when the handlingEditor invites a reviewer with a collection', async () => { const body = {