20 lines
695 B
JavaScript
20 lines
695 B
JavaScript
import { ObjectId } from 'mongodb'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const { name, color } = await readBody(event);
|
|
|
|
if (!isHexColor(color)) return createError({ statusCode: 400, statusMessage: 'Team color is not a valid hex code' })
|
|
|
|
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: name })) return createError({ statusCode: 400, statusMessage: 'Team name already exists' })
|
|
|
|
await teamsColl.updateOne({ _id: new ObjectId(user.team.id) }, { $set: { name: name, color: color } })
|
|
|
|
return team
|
|
});
|