27 lines
722 B
TypeScript
27 lines
722 B
TypeScript
|
export default defineEventHandler(async (event) => {
|
||
|
const { teamName, teamColor } = await readBody(event)
|
||
|
let teamId: string = event.context.params ? event.context.params.id : '@current'
|
||
|
|
||
|
if (teamId === '@current') {
|
||
|
const user = await getUser('@me', event)
|
||
|
|
||
|
if (!user.team) {
|
||
|
throw createError({ statusCode: 400, statusMessage: 'User must be in a team' })
|
||
|
}
|
||
|
|
||
|
teamId = user.team._id.toString()
|
||
|
}
|
||
|
|
||
|
const team = await TeamModel.findByIdAndUpdate(teamId, {
|
||
|
$set: {
|
||
|
name: teamName, color: teamColor
|
||
|
}
|
||
|
}, { returnDocument: 'after', runValidators: true })
|
||
|
|
||
|
if (!team) {
|
||
|
throw createError({ statusCode: 400, statusMessage: 'Team could not be found' })
|
||
|
}
|
||
|
|
||
|
return team
|
||
|
})
|