fix: event-type validations move to pick not omit and separate create/edit
parent
8f05c3e991
commit
9afbb3d127
|
@ -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({
|
||||
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 });
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue