Polarcraft/webv2/server/api/teams/index.post.ts

26 lines
449 B
TypeScript
Raw Normal View History

2023-06-06 14:29:05 +02:00
export default defineEventHandler(async (event) => {
2023-06-06 18:22:36 +02:00
const { name, color, userId } = await readBody(event)
2023-06-06 14:29:05 +02:00
2023-06-06 18:22:36 +02:00
const user = await getUser(userId ?? '@me', event)
if (user.team) {
throw createError({ statusCode: 400, statusMessage: 'User already in a team' })
}
2023-06-06 16:35:25 +02:00
2023-06-06 14:29:05 +02:00
const team = new TeamModel({
name,
2023-06-06 18:22:36 +02:00
color,
members: [
user._id
]
2023-06-06 14:29:05 +02:00
})
await team.save()
2023-06-06 18:22:36 +02:00
user.team = team._id
2023-06-06 16:35:25 +02:00
await user.save()
2023-06-06 14:29:05 +02:00
return team
})