Polarcraft/web/server/api/team/leave.js

29 lines
970 B
JavaScript
Raw Normal View History

2023-05-09 13:57:01 +02:00
import { ObjectId } from 'mongodb'
export default defineEventHandler(async (event) => {
const user = await getAuth(event)
2023-05-11 19:46:18 +02:00
const config = useRuntimeConfig()
2023-05-09 13:57:01 +02:00
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: "" } })
2023-05-11 19:46:18 +02:00
await $fetch(config.discordHost + '/team/removeteammember', {
method: 'POST',
body: { voiceChannelId: team.value.voiceChannelId, textChannelId: team.value.textChannelId, discordId: user.discord.id }
})
2023-05-09 13:57:01 +02:00
if (team.value.count <= 1) {
await teamsColl.deleteOne({ _id: new ObjectId(user.team.id )})
2023-05-11 19:46:18 +02:00
await $fetch(config.discordHost + '/team/deletechannels', {
method: 'POST',
body: { voiceChannelId: team.voiceChannelId, textChannelId: team.textChannelId }
})
2023-05-09 13:57:01 +02:00
}
return team
});