22 lines
992 B
JavaScript
22 lines
992 B
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 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 teamsColl.updateOne({ _id: new ObjectId(user.team.id) }, { $set: { name: name, color: color } })
|
|
|
|
return team
|
|
});
|