Scheduling infrastructure for absolutely everyone.
fix: event-type validations move to pick not omit and separate create/edit
Agusti Fernandez Pardo9afbb3d12789
diff --git a/lib/validations/event-type.ts b/lib/validations/event-type.tsindex e8665a9e3ba..40411a72229 100644--- a/lib/validations/event-type.ts+++ b/lib/validations/event-type.ts@@ -2,14 +2,49 @@ import { z } from "zod";import { _EventTypeModel as EventType } from "@calcom/prisma/zod";-export const schemaEventTypeBaseBodyParams = EventType.omit({ id: true }).partial();+export const schemaEventTypeBaseBodyParams = EventType.pick({+ title: true,+ slug: true,+ length: true,+}).partial();-const schemaEventTypeRequiredParams = z.object({- title: z.string(),- slug: z.string(),- length: z.number(),+const schemaEventTypeCreateParams = z+ .object({+ title: z.string(),+ slug: z.string(),+ length: z.number(),+ })+ .strict();++export const schemaEventTypeCreateBodyParams =+ schemaEventTypeBaseBodyParams.merge(schemaEventTypeCreateParams);++const schemaEventTypeEditParams = z+ .object({+ token: z.string().optional(),+ url: z.string().optional(),+ })+ .strict();++export const schemaEventTypeEditBodyParams = schemaEventTypeBaseBodyParams.merge(schemaEventTypeEditParams);+export const schemaEventTypeReadPublic = EventType.pick({+ title: true,+ slug: true,+ length: true,});-export const schemaEventTypeBodyParams = schemaEventTypeBaseBodyParams.merge(schemaEventTypeRequiredParams);-// @NOTE: Removing locations and metadata properties before validation, add them later if required-export const schemaEventTypePublic = EventType.omit({ locations: true, metadata: true });+// import { z } from "zod";++// import { _EventTypeModel as EventType } from "@calcom/prisma/zod";++// export const schemaEventTypeBaseBodyParams = EventType.omit({ id: true }).partial();++// const schemaEventTypeRequiredParams = z.object({+// title: z.string(),+// slug: z.string(),+// length: z.number(),+// });++// export const schemaEventTypeBodyParams = schemaEventTypeBaseBodyParams.merge(schemaEventTypeRequiredParams);+// // @NOTE: Removing locations and metadata properties before validation, add them later if required+// export const schemaEventTypePublic = EventType.omit({ locations: true, metadata: true });diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].tsindex a6900b1d27e..328e101b280 100644--- a/pages/api/event-types/[id].ts+++ b/pages/api/event-types/[id].ts@@ -4,7 +4,7 @@ import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";import type { EventTypeResponse } from "@lib/types";-import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";+import { schemaEventTypeEditBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";import {schemaQueryIdParseInt,withValidQueryIdTransformParseInt,@@ -52,7 +52,7 @@ export async function eventTypeById(case "GET":await prisma.eventType.findUnique({ where: { id: safeQuery.data.id } })- .then((data) => schemaEventTypePublic.parse(data))+ .then((data) => schemaEventTypeReadPublic.parse(data)).then((event_type) => res.status(200).json({ event_type })).catch((error: Error) =>res.status(404).json({@@ -89,13 +89,13 @@ export async function eventTypeById(* description: Authorization information is missing or invalid.*/case "PATCH":- const safeBody = schemaEventTypeBodyParams.safeParse(body);+ const safeBody = schemaEventTypeEditBodyParams.safeParse(body);if (!safeBody.success) {throw new Error("Invalid request body");}await prisma.eventType.update({ where: { id: safeQuery.data.id }, data: safeBody.data })- .then((data) => schemaEventTypePublic.parse(data))+ .then((data) => schemaEventTypeReadPublic.parse(data)).then((event_type) => res.status(200).json({ event_type })).catch((error: Error) =>res.status(404).json({diff --git a/pages/api/event-types/index.ts b/pages/api/event-types/index.tsindex dad45eec42a..1229785f501 100644--- a/pages/api/event-types/index.ts+++ b/pages/api/event-types/index.ts@@ -4,7 +4,7 @@ import prisma from "@calcom/prisma";import { withMiddleware } from "@lib/helpers/withMiddleware";import { EventTypeResponse, EventTypesResponse } from "@lib/types";-import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";+import { schemaEventTypeCreateBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";async function createOrlistAllEventTypes({ method, body, userId }: NextApiRequest,@@ -29,7 +29,7 @@ async function createOrlistAllEventTypes(* description: No event types were found*/const data = await prisma.eventType.findMany({ where: { userId } });- const event_types = data.map((eventType) => schemaEventTypePublic.parse(eventType));+ const event_types = data.map((eventType) => schemaEventTypeReadPublic.parse(eventType));if (event_types) res.status(200).json({ event_types });else(error: Error) =>@@ -55,11 +55,11 @@ async function createOrlistAllEventTypes(* 401:* description: Authorization information is missing or invalid.*/- const safe = schemaEventTypeBodyParams.safeParse(body);+ const safe = schemaEventTypeCreateBodyParams.safeParse(body);if (!safe.success) throw new Error("Invalid request body");const data = await prisma.eventType.create({ data: { ...safe.data, userId } });- const event_type = schemaEventTypePublic.parse(data);+ const event_type = schemaEventTypeReadPublic.parse(data);if (data) res.status(201).json({ event_type, message: "EventType created successfully" });else