Polarcraft/webv2/server/api/teams/[id]/members/[userId].delete.ts
Xeovalyte f9c13e5ada
All checks were successful
Build and Deploy / Deploy Web (push) Successful in 1m3s
Build and Deploy / Deploy Discord Bot (push) Successful in 43s
feat: Added team leave
2023-06-06 18:22:36 +02:00

31 lines
729 B
TypeScript

export default defineEventHandler(async (event) => {
let teamId: string = event.context.params ? event.context.params.id : '@current'
const userId: string = event.context.params ? event.context.params.userId : '@me'
const user = await getUser(userId, event)
if (!user.team) {
throw createError({ statusCode: 400, statusMessage: 'User must be in a team' })
}
if (teamId === '@current') {
teamId = user.team._id.toString()
}
const team = await TeamModel.findOneAndUpdate({ _id: user.team._id }, {
$pull: {
members: user._id
}
}, { returnDocument: 'after' })
if (team && team.members.length < 1) {
team.deleteOne()
}
user.team = undefined
await user.save()
return ''
})