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

30 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-05-09 13:57:01 +02:00
import { ObjectId } from 'mongodb'
export default defineEventHandler(async (event) => {
const { name, color } = await readBody(event);
2023-05-09 16:01:19 +02:00
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' })
2023-05-09 13:57:01 +02:00
const user = await getAuth(event)
2023-05-11 19:46:18 +02:00
const config = useRuntimeConfig()
if (!user.team.admin) return createError({ statusCode: 403, statusMessage: 'Admin team rol vereist' })
2023-05-09 13:57:01 +02:00
const teamsColl = db.collection('teams')
const team = await teamsColl.findOne({ _id: new ObjectId(user.team.id) });
2023-05-09 16:01:19 +02:00
if (team.name !== name && await teamsColl.findOne({ name: { $regex: new RegExp(name, "i") } })) return createError({ statusCode: 400, statusMessage: 'Team naam bestaat al' })
2023-05-09 13:57:01 +02:00
2023-05-11 19:46:18 +02:00
await $fetch(config.discordHost + '/team/edit', {
method: 'POST',
body: { voiceChannelId: team.voiceChannelId, textChannelId: team.textChannelId, name: name }
})
2023-05-09 13:57:01 +02:00
await teamsColl.updateOne({ _id: new ObjectId(user.team.id) }, { $set: { name: name, color: color } })
return team
});