Polarcraft/webv2/server/api/teams/[id]/members/[userId].delete.ts

31 lines
729 B
TypeScript
Raw Normal View History

2023-06-06 18:22:36 +02:00
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 ''
})