import { useValidatedBody, useValidatedParams, z, zh } from 'h3-zod'; import prisma from '~~/lib/prisma'; import { randomBytes } from 'node:crypto'; export default defineEventHandler(async (event) => { const { huntId } = await useValidatedParams( event, z.object({ huntId: zh.numAsString }) ); const user = await requireUserSession(event); const userId = user.user.id; console.log(user); const existingTeam = await prisma.huntTeam.findFirst({ select: { id: true }, where: { huntId, OR: [ { ownerId: userId }, { members: { some: { memberId: userId } } } ] } }); if (existingTeam) { throw createError({ status: 409, statusText: 'You already have a team!' }); } const { name, description } = await useValidatedBody( event, z.object({ name: z.string().max(256).min(3).trim(), description: z.string().max(256).trim().optional() }) ); const password = randomBytes(3).toString('hex'); const team = await prisma.huntTeam.create({ data: { name, description, ownerId: userId, password, huntId } }); return { team }; });