diff --git a/packages/component-invite/src/routes/postHandleInvitation.js b/packages/component-invite/src/routes/postHandleInvitation.js index 893908b6642ff41fa55fbabd0f837d91535a62f8..5be9650861cc50aed18fffd6ed959da624936792 100644 --- a/packages/component-invite/src/routes/postHandleInvitation.js +++ b/packages/component-invite/src/routes/postHandleInvitation.js @@ -12,7 +12,7 @@ module.exports = models => async (req, res) => { return } - const user = await models.User.find(req.user) + let user = await models.User.find(req.user) if (!user.invitations) { res.status(400).json({ error: 'The user has no invitation' }) logger.error('The request user does not have any invitation') @@ -22,12 +22,12 @@ module.exports = models => async (req, res) => { try { const collection = await models.Collection.find(collectionId) - const matchingInvitation = user.invitations.find( + const matchingInvitations = user.invitations.filter( invitation => invitation.collectionId === collectionId && invitation.type === type, ) - if (matchingInvitation === undefined) { + if (matchingInvitations.length === 0) { res.status(400).json({ error: `Request data does not match any user invitation`, }) @@ -37,10 +37,10 @@ module.exports = models => async (req, res) => { return } + const matchingInvitation = matchingInvitations[0] matchingInvitation.hasAnswer = true if (accept === true) { matchingInvitation.isAccepted = true - await user.save() try { const users = await models.User.all() @@ -57,14 +57,19 @@ module.exports = models => async (req, res) => { return res.status(500).json({ error: 'Mail could not be sent.' }) } } else { + matchingInvitation.isAccepted = false await teamHelper.removeTeamMember( matchingInvitation.teamId, user.id, models.Team, ) + const { reason } = req.body + if (reason !== undefined) { + matchingInvitation.reason = reason + } } - - res.status(204).json() + user = await user.save() + res.status(200).json(user) return } catch (e) { const notFoundError = await helpers.handleNotFoundError(e, 'collection')