Skip to content
Snippets Groups Projects
Commit 587d4bf1 authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files
parents 490b2cec 5477b591
No related branches found
No related tags found
No related merge requests found
......@@ -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')
......
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