Polarcraft/webv2/server/api/teams/[id]/members/index.post.ts
Xeovalyte fbcd0d1bb9
Some checks failed
Build and Deploy / Deploy Web (push) Failing after 1s
Build and Deploy / Deploy Discord Bot (push) Failing after 1s
feat: Added team page
2023-06-24 13:27:33 +02:00

32 lines
780 B
TypeScript

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' })
}
const team = await TeamModel.findByIdAndUpdate(teamId, {
$addToSet: {
members: user._id
}
}).populate('members')
if (!team) {
throw createError({ statusCode: 500, statusMessage: 'Team does not exist' })
}
user.team = team._id
user.teamInvites = []
await user.save()
return team
})