Polarcraft/web/server/api/team/edit.js
2023-05-11 19:46:18 +02:00

30 lines
1.3 KiB
JavaScript

import { ObjectId } from 'mongodb'
export default defineEventHandler(async (event) => {
const { name, color } = await readBody(event);
if (!name || !color) return createError({ statusCode: 400, statusMessage: 'Team naam en kleur zijn vereist' })
if (!isHexColor(color)) return createError({ statusCode: 400, statusMessage: 'Team kleur is geen goede kleurencode' })
if (!verifyUsername(name)) return createError({ statusCode: 400, statusMessage: 'Team naam moet alfanumeriek zijn en mag maximaal 20 tekens lang zijn' })
const user = await getAuth(event)
const config = useRuntimeConfig()
if (!user.team.admin) return createError({ statusCode: 403, statusMessage: 'Admin team rol vereist' })
const teamsColl = db.collection('teams')
const team = await teamsColl.findOne({ _id: new ObjectId(user.team.id) });
if (team.name !== name && await teamsColl.findOne({ name: { $regex: new RegExp(name, "i") } })) return createError({ statusCode: 400, statusMessage: 'Team naam bestaat al' })
await $fetch(config.discordHost + '/team/edit', {
method: 'POST',
body: { voiceChannelId: team.voiceChannelId, textChannelId: team.textChannelId, name: name }
})
await teamsColl.updateOne({ _id: new ObjectId(user.team.id) }, { $set: { name: name, color: color } })
return team
});