Polarcraft/web/server/api/team/leave.js
2023-05-09 13:57:01 +02:00

17 lines
537 B
JavaScript

import { ObjectId } from 'mongodb'
export default defineEventHandler(async (event) => {
const user = await getAuth(event)
const teamsColl = db.collection('teams')
const team = await teamsColl.findOneAndUpdate({ _id: new ObjectId(user.team.id) }, { $inc: { count: -1 }})
const usersColl = db.collection('users')
await usersColl.findOneAndUpdate({ _id: new ObjectId(user._id) }, { $unset: { team: "" } })
if (team.value.count <= 1) {
await teamsColl.deleteOne({ _id: new ObjectId(user.team.id )})
}
return team
});