22 lines
986 B
JavaScript
22 lines
986 B
JavaScript
|
export default defineEventHandler(async (event) => {
|
||
|
const { teamName, teamColor } = await readBody(event);
|
||
|
|
||
|
if (!teamName || !teamColor) return createError({ statusCode: 400, statusMessage: 'teamName and teamColor are required' })
|
||
|
if (!isHexColor(teamColor)) return createError({ statusCode: 400, statusMessage: 'Team color is not a valid hex code' })
|
||
|
|
||
|
const user = await getAuth(event)
|
||
|
|
||
|
if (user.team) return createError({ statusCode: 400, statusMessage: 'User already is in a team' })
|
||
|
|
||
|
const teamsColl = db.collection('teams')
|
||
|
const usersColl = db.collection('users')
|
||
|
|
||
|
if (await teamsColl.findOne({ name: teamName })) return createError({ statusCode: 400, statusMessage: 'Team name already exists' })
|
||
|
|
||
|
const response = await teamsColl.insertOne({ name: teamName, color: teamColor, count: 1 })
|
||
|
|
||
|
await usersColl.findOneAndUpdate({ 'discord.id': user.discord.id }, { $set: { 'team.id': response.insertedId.toString(), 'team.admin': true } })
|
||
|
|
||
|
return response;
|
||
|
});
|