Polarcraft/web/server/api/team/edit.js

20 lines
695 B
JavaScript
Raw Normal View History

2023-05-09 13:57:01 +02:00
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
});