diff --git a/packages/component-invite/src/HandleInvitation.js b/packages/component-invite/src/HandleInvitation.js index d3c5c37336388cfd5779fdd0c5f6a1fc812cdbe0..7ecdd0cafec79703d13c52d1157c766205128286 100644 --- a/packages/component-invite/src/HandleInvitation.js +++ b/packages/component-invite/src/HandleInvitation.js @@ -15,6 +15,11 @@ const HandleInvitation = app => { authBearer, require('./routes/getCollectionUsers')(app.locals.models), ) + app.delete( + '/api/collections/:collectionId/users/:userId', + authBearer, + require('./routes/deleteInvitation')(app.locals.models), + ) } module.exports = HandleInvitation diff --git a/packages/component-invite/src/controllers/assignCollectionRole.js b/packages/component-invite/src/controllers/assignCollectionRole.js index 2dc68984601b5ab8ddf8a60ea8ae01db49434cb0..0b7a2dd2b7ba9eb8928eff8bf4b2d9821a8b3d0b 100644 --- a/packages/component-invite/src/controllers/assignCollectionRole.js +++ b/packages/component-invite/src/controllers/assignCollectionRole.js @@ -3,6 +3,7 @@ const config = require('config') const helpers = require('../helpers/helpers') const teamHelper = require('../helpers/Team') const mailService = require('pubsweet-component-mail-service') +const inviteHelper = require('../helpers/Invitation') const configRoles = config.get('roles') @@ -72,7 +73,7 @@ module.exports = async ( // 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) + user = await inviteHelper.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..18684f5666a1a8710ac3b7a3beaa19d35ef2f329 100644 --- a/packages/component-invite/src/helpers/Team.js +++ b/packages/component-invite/src/helpers/Team.js @@ -113,26 +113,12 @@ const getMatchingTeams = (teams, TeamModel, collectionId, role) => }) .filter(Boolean) -const setupInvitation = async (user, role, collectionId, teamId) => { - const invitation = { - type: role, - hasAnswer: false, - isAccepted: false, - collectionId, - timestamp: Date.now(), - teamId, - } - user.invitations = user.invitations || [] - user.invitations.push(invitation) - user = await user.save() - return user -} - const removeTeamMember = async (teamId, userId, TeamModel) => { const team = await TeamModel.find(teamId) const members = team.members.filter(member => member !== userId) team.members = members await TeamModel.updateProperties(team) + await team.save() } @@ -151,19 +137,14 @@ const getTeamMembersByCollection = async (collectionId, role, TeamModel) => { return members } -const getInviteData = (invitations, collectionId, role) => { - const matchingInvitation = invitations.find( - invite => invite.type === role && invite.collectionId === collectionId, +const getTeamByGroupAndCollection = async (collectionId, role, TeamModel) => { + const teams = await TeamModel.all() + return teams.find( + team => + team.group === role && + team.object.type === 'collection' && + team.object.id === collectionId, ) - let status = 'pending' - if (matchingInvitation.isAccepted) { - status = 'accepted' - } else if (matchingInvitation.hasAnswer) { - status = 'refused' - } - - const { timestamp } = matchingInvitation - return { timestamp, status } } module.exports = { @@ -171,8 +152,7 @@ module.exports = { setupEiCTeams, setupManuscriptTeam, getMatchingTeams, - setupInvitation, removeTeamMember, getTeamMembersByCollection, - getInviteData, + getTeamByGroupAndCollection, } diff --git a/packages/component-invite/src/routes/getCollectionUsers.js b/packages/component-invite/src/routes/getCollectionUsers.js index 16903452e5d2a81805b118dd5f3886dbe549d987..94570732b4731e170f6e9d6a076258631d1e2de7 100644 --- a/packages/component-invite/src/routes/getCollectionUsers.js +++ b/packages/component-invite/src/routes/getCollectionUsers.js @@ -1,6 +1,7 @@ const helpers = require('../helpers/helpers') const teamHelper = require('../helpers/Team') const config = require('config') +const inviteHelper = require('../helpers/Invitation') const configRoles = config.get('roles') module.exports = models => async (req, res) => { @@ -39,7 +40,7 @@ module.exports = models => async (req, res) => { const membersData = members.map(async member => { const user = await models.User.find(member) - const { timestamp, status } = teamHelper.getInviteData( + const { timestamp, status } = inviteHelper.getInviteData( user.invitations, collectionId, role, diff --git a/packages/component-invite/src/tests/fixtures/teamIDs.js b/packages/component-invite/src/tests/fixtures/teamIDs.js index f8e1c9f2285384a39ccb39de082abf773c7cfb02..74f1842bb26490263c8f58d52cee2b662c72b6cd 100644 --- a/packages/component-invite/src/tests/fixtures/teamIDs.js +++ b/packages/component-invite/src/tests/fixtures/teamIDs.js @@ -1,10 +1,10 @@ const Chance = require('chance') const chance = new Chance() +const heID = chance.guid() +const revID = chance.guid() -const teamIDs = { - heTeam: chance.guid(), - reviewerTeam: chance.guid(), +module.exports = { + heTeamID: heID, + reviewerTeamID: revID, } - -module.exports = { teamIDs } diff --git a/packages/component-invite/src/tests/fixtures/teams.js b/packages/component-invite/src/tests/fixtures/teams.js index 84de7cd6b6284772d1516308692709f0031a8f6d..460e42f67d5bfa95c4c5470180f26a5df5f1ef12 100644 --- a/packages/component-invite/src/tests/fixtures/teams.js +++ b/packages/component-invite/src/tests/fixtures/teams.js @@ -1,7 +1,6 @@ const users = require('./users') const collections = require('./collections') -const { heTeam, reviewerTeam } = require('./teamIDs') - +const { heTeamID, reviewerTeamID } = require('./teamIDs') const { standardCollection } = collections const { editorInChief, handlingEditor, reviewer, invitedHandlingEditor } = users const teams = { @@ -34,7 +33,7 @@ const teams = { members: [handlingEditor.id, invitedHandlingEditor.id], save: jest.fn(() => teams.heTeam), updateProperties: jest.fn(() => teams.heTeam), - id: heTeam, + id: heTeamID, }, reviewerTeam: { teamType: { @@ -50,7 +49,7 @@ const teams = { members: [reviewer.id], save: jest.fn(() => teams.reviewerTeam), updateProperties: jest.fn(() => teams.reviewerTeam), - id: reviewerTeam, + id: reviewerTeamID, }, } module.exports = teams diff --git a/packages/component-invite/src/tests/fixtures/users.js b/packages/component-invite/src/tests/fixtures/users.js index a04b7ff4c5e65b0b444f1a085b2c6fcc1254ed4e..53e00485ec4413381309488acb126c2ccb4b2dfe 100644 --- a/packages/component-invite/src/tests/fixtures/users.js +++ b/packages/component-invite/src/tests/fixtures/users.js @@ -1,5 +1,5 @@ const { standardCollection } = require('./collections') -const { heTeam, reviewerTeam } = require('./teamIDs') +const { heTeamID, reviewerTeamID } = require('./teamIDs') const users = { admin: { @@ -42,10 +42,10 @@ const users = { isAccepted: false, collectionId: standardCollection.id, timestamp: Date.now(), - teamId: heTeam, + teamId: heTeamID, }, ], - teams: [heTeam], + teams: [heTeamID], save: jest.fn(() => users.handlingEditor), editorInChief: false, }, @@ -78,7 +78,7 @@ const users = { title: 'Ms', save: jest.fn(() => users.reviewer), isConfirmed: false, - teams: [reviewerTeam], + teams: [reviewerTeamID], invitations: [ { type: 'reviewer', @@ -86,7 +86,7 @@ const users = { isAccepted: false, collectionId: '123', timestamp: Date.now(), - teamId: reviewerTeam, + teamId: reviewerTeamID, }, ], }, @@ -106,10 +106,10 @@ const users = { isAccepted: false, collectionId: standardCollection.id, timestamp: Date.now(), - teamId: heTeam, + teamId: heTeamID, }, ], - teams: [heTeam], + teams: [heTeamID], save: jest.fn(() => users.handlingEditor), editorInChief: false, },