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