2023-06-07 22:29:18 +02:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const { userId } = await readBody(event)
|
|
|
|
const teamId: string = event.context.params ? event.context.params.id : '@current'
|
|
|
|
|
|
|
|
if (teamId === '@current') {
|
|
|
|
throw createError({ statusCode: 400, statusMessage: 'Cannot join your own team' })
|
|
|
|
}
|
|
|
|
|
|
|
|
const user = await getUser(userId, event)
|
|
|
|
|
|
|
|
if (user.team) {
|
|
|
|
throw createError({ statusCode: 400, statusMessage: 'User already is in a team' })
|
|
|
|
}
|
|
|
|
|
2023-06-24 13:27:33 +02:00
|
|
|
const team = await TeamModel.findByIdAndUpdate(teamId, {
|
|
|
|
$addToSet: {
|
|
|
|
members: user._id
|
|
|
|
}
|
|
|
|
}).populate('members')
|
2023-06-07 22:29:18 +02:00
|
|
|
|
|
|
|
if (!team) {
|
|
|
|
throw createError({ statusCode: 500, statusMessage: 'Team does not exist' })
|
|
|
|
}
|
|
|
|
|
|
|
|
user.team = team._id
|
2023-06-24 13:27:33 +02:00
|
|
|
user.teamInvites = []
|
2023-06-07 22:29:18 +02:00
|
|
|
|
2023-06-24 13:27:33 +02:00
|
|
|
await user.save()
|
2023-06-07 22:29:18 +02:00
|
|
|
|
|
|
|
return team
|
|
|
|
})
|