2023-05-09 13:57:01 +02:00
|
|
|
export default defineEventHandler(async (event) => {
|
|
|
|
const { teamName, teamColor } = await readBody(event);
|
|
|
|
|
2023-05-09 16:01:19 +02:00
|
|
|
if (!teamName || !teamColor) return createError({ statusCode: 400, statusMessage: 'Team naam en kleur zijn vereist' })
|
|
|
|
if (!isHexColor(teamColor)) return createError({ statusCode: 400, statusMessage: 'Team kleur is geen gelidige kleurencode' })
|
|
|
|
if (!verifyUsername(teamName)) 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()
|
2023-05-09 13:57:01 +02:00
|
|
|
|
2023-05-09 16:01:19 +02:00
|
|
|
if (user.team) return createError({ statusCode: 400, statusMessage: 'Gebruiker is al in een team' })
|
2023-05-09 13:57:01 +02:00
|
|
|
|
|
|
|
const teamsColl = db.collection('teams')
|
|
|
|
const usersColl = db.collection('users')
|
|
|
|
|
2023-05-09 16:01:19 +02:00
|
|
|
if (await teamsColl.findOne({ name: { $regex: new RegExp(teamName, "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
|
|
|
const discordResponse = await $fetch(config.discordHost + '/team/createchannels', {
|
|
|
|
method: 'POST',
|
|
|
|
body: { name: teamName, discordId: user.discord.id }
|
|
|
|
})
|
|
|
|
|
|
|
|
const response = await teamsColl.insertOne({ name: teamName, color: teamColor, count: 1, textChannelId: discordResponse.textChannel.id, voiceChannelId: discordResponse.voiceChannel.id })
|
2023-05-09 13:57:01 +02:00
|
|
|
|
|
|
|
await usersColl.findOneAndUpdate({ 'discord.id': user.discord.id }, { $set: { 'team.id': response.insertedId.toString(), 'team.admin': true } })
|
|
|
|
|
|
|
|
return response;
|
|
|
|
});
|