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