Polarcraft/webv2/server/api/teams/[id]/members/index.post.ts

28 lines
715 B
TypeScript
Raw Normal View History

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