Files
pichunt/server/api/hunt/[huntId]/team/[teamId]/index.put.ts
T

58 lines
1.2 KiB
TypeScript

import { useValidatedBody, useValidatedParams, z, zh } from 'h3-zod';
import prisma from '~~/lib/prisma';
export default defineEventHandler(async (event) => {
const user = await requireUserSession(event);
const userId = user.user.id;
const { huntId, teamId } = await useValidatedParams(
event,
z.object({
huntId: zh.numAsString,
teamId: zh.numAsString
})
);
const body = await useValidatedBody(
event,
z.object({
name: z.string().min(3).max(256).trim(),
description: z.string().max(256).trim().default(''),
password: z.string().min(1).max(256).trim()
})
);
// Only the team owner may edit the team
const team = await prisma.huntTeam.findFirst({
where: {
id: teamId,
huntId,
ownerId: userId
},
select: {
id: true
}
});
if (!team) {
throw createError({ status: 403, statusText: 'Not allowed!' });
}
return await prisma.huntTeam.update({
where: {
id: teamId
},
data: {
name: body.name,
description: body.description,
password: body.password
},
select: {
id: true,
name: true,
description: true,
password: true
}
});
});