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";
|
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(),
|
title: z.string(),
|
||||||
slug: z.string(),
|
slug: z.string(),
|
||||||
length: z.number(),
|
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);
|
// import { z } from "zod";
|
||||||
// @NOTE: Removing locations and metadata properties before validation, add them later if required
|
|
||||||
export const schemaEventTypePublic = EventType.omit({ locations: true, metadata: true });
|
// 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 { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||||
import type { EventTypeResponse } from "@lib/types";
|
import type { EventTypeResponse } from "@lib/types";
|
||||||
import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type";
|
import { schemaEventTypeEditBodyParams, schemaEventTypeReadPublic } from "@lib/validations/event-type";
|
||||||
import {
|
import {
|
||||||
schemaQueryIdParseInt,
|
schemaQueryIdParseInt,
|
||||||
withValidQueryIdTransformParseInt,
|
withValidQueryIdTransformParseInt,
|
||||||
|
@ -52,7 +52,7 @@ export async function eventTypeById(
|
||||||
case "GET":
|
case "GET":
|
||||||
await prisma.eventType
|
await prisma.eventType
|
||||||
.findUnique({ where: { id: safeQuery.data.id } })
|
.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 }))
|
.then((event_type) => res.status(200).json({ event_type }))
|
||||||
.catch((error: Error) =>
|
.catch((error: Error) =>
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
|
@ -89,13 +89,13 @@ export async function eventTypeById(
|
||||||
* description: Authorization information is missing or invalid.
|
* description: Authorization information is missing or invalid.
|
||||||
*/
|
*/
|
||||||
case "PATCH":
|
case "PATCH":
|
||||||
const safeBody = schemaEventTypeBodyParams.safeParse(body);
|
const safeBody = schemaEventTypeEditBodyParams.safeParse(body);
|
||||||
if (!safeBody.success) {
|
if (!safeBody.success) {
|
||||||
throw new Error("Invalid request body");
|
throw new Error("Invalid request body");
|
||||||
}
|
}
|
||||||
await prisma.eventType
|
await prisma.eventType
|
||||||
.update({ where: { id: safeQuery.data.id }, data: safeBody.data })
|
.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 }))
|
.then((event_type) => res.status(200).json({ event_type }))
|
||||||
.catch((error: Error) =>
|
.catch((error: Error) =>
|
||||||
res.status(404).json({
|
res.status(404).json({
|
||||||
|
|
|
@ -4,7 +4,7 @@ import prisma from "@calcom/prisma";
|
||||||
|
|
||||||
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
||||||
import { EventTypeResponse, EventTypesResponse } from "@lib/types";
|
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(
|
async function createOrlistAllEventTypes(
|
||||||
{ method, body, userId }: NextApiRequest,
|
{ method, body, userId }: NextApiRequest,
|
||||||
|
@ -29,7 +29,7 @@ async function createOrlistAllEventTypes(
|
||||||
* description: No event types were found
|
* description: No event types were found
|
||||||
*/
|
*/
|
||||||
const data = await prisma.eventType.findMany({ where: { userId } });
|
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 });
|
if (event_types) res.status(200).json({ event_types });
|
||||||
else
|
else
|
||||||
(error: Error) =>
|
(error: Error) =>
|
||||||
|
@ -55,11 +55,11 @@ async function createOrlistAllEventTypes(
|
||||||
* 401:
|
* 401:
|
||||||
* description: Authorization information is missing or invalid.
|
* 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");
|
if (!safe.success) throw new Error("Invalid request body");
|
||||||
|
|
||||||
const data = await prisma.eventType.create({ data: { ...safe.data, userId } });
|
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" });
|
if (data) res.status(201).json({ event_type, message: "EventType created successfully" });
|
||||||
else
|
else
|
||||||
|
|
Loading…
Reference in New Issue