31 lines
729 B
TypeScript
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 ''
|
||
|
})
|