From 29666493d6f616fea37cf1be30cc9de4d63fc199 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Tue, 19 Apr 2022 05:19:59 +0200 Subject: [PATCH] make event types return only userId data --- lib/validations/event-type.ts | 4 +- pages/api/event-types/[id].ts | 94 +++++++++++++++++++++------------- pages/api/event-types/index.ts | 11 +++- 3 files changed, 70 insertions(+), 39 deletions(-) diff --git a/lib/validations/event-type.ts b/lib/validations/event-type.ts index 113620157e..ab24a9b0d1 100644 --- a/lib/validations/event-type.ts +++ b/lib/validations/event-type.ts @@ -12,8 +12,8 @@ const schemaEventTypeRequiredParams = z.object({ }); export const schemaEventTypeBodyParams = schemaEventTypeBaseBodyParams.merge(schemaEventTypeRequiredParams); - -export const schemaEventTypePublic = EventType.omit({}); +// Omitting those two (locations/ metadata) because the validations where choking at array and JSON object. add them later if needed +export const schemaEventTypePublic = EventType.omit({ locations: true, metadata: true }); export const withValidEventType = withValidation({ schema: schemaEventTypeBodyParams, diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].ts index 410cbbb5af..91ba0e380a 100644 --- a/pages/api/event-types/[id].ts +++ b/pages/api/event-types/[id].ts @@ -4,6 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { EventTypeResponse } from "@lib/types"; +import { getCalcomUserId } from "@lib/utils/getCalcomUserId"; import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type"; import { schemaQueryIdParseInt, @@ -26,6 +27,8 @@ import { * - ApiKeyAuth: [] * tags: * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types * responses: * 200: * description: OK @@ -55,6 +58,8 @@ import { * - ApiKeyAuth: [] * tags: * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types * responses: * 201: * description: OK, eventType edited successfuly @@ -76,6 +81,8 @@ import { * - ApiKeyAuth: [] * tags: * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types * responses: * 201: * description: OK, eventType removed successfuly @@ -90,44 +97,61 @@ export async function eventTypeById(req: NextApiRequest, res: NextApiResponse eventType.id); + if (userEventTypes.includes(safeQuery.data.id)) { + switch (method) { + case "GET": + await prisma.eventType + .findUnique({ where: { id: safeQuery.data.id } }) + .then((data) => schemaEventTypePublic.parse(data)) + .then((event_type) => res.status(200).json({ event_type })) + .catch((error: Error) => + res.status(404).json({ + message: `EventType with id: ${safeQuery.data.id} not found`, + error, + }) + ); + break; - switch (method) { - case "GET": - await prisma.eventType - .findUnique({ where: { id: safeQuery.data.id } }) - .then((data) => schemaEventTypePublic.parse(data)) - .then((event_type) => res.status(200).json({ event_type })) - .catch((error: Error) => - res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) - ); - break; + case "PATCH": + 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((event_type) => res.status(200).json({ event_type })) + .catch((error: Error) => + res.status(404).json({ + message: `EventType with id: ${safeQuery.data.id} not found`, + error, + }) + ); + break; - case "PATCH": - 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((event_type) => res.status(200).json({ event_type })) - .catch((error: Error) => - res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) - ); - break; + case "DELETE": + await prisma.eventType + .delete({ where: { id: safeQuery.data.id } }) + .then(() => + res.status(200).json({ + message: `EventType with id: ${safeQuery.data.id} deleted`, + }) + ) + .catch((error: Error) => + res.status(404).json({ + message: `EventType with id: ${safeQuery.data.id} not found`, + error, + }) + ); + break; - case "DELETE": - await prisma.eventType - .delete({ where: { id: safeQuery.data.id } }) - .then(() => res.status(200).json({ message: `EventType with id: ${safeQuery.data.id} deleted` })) - .catch((error: Error) => - res.status(404).json({ message: `EventType with id: ${safeQuery.data.id} not found`, error }) - ); - break; - - default: - res.status(405).json({ message: "Method not allowed" }); - break; + default: + res.status(405).json({ message: "Method not allowed" }); + break; + } } } diff --git a/pages/api/event-types/index.ts b/pages/api/event-types/index.ts index 7f04a6a3de..cd883d719c 100644 --- a/pages/api/event-types/index.ts +++ b/pages/api/event-types/index.ts @@ -4,6 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import { EventTypeResponse, EventTypesResponse } from "@lib/types"; +import { getCalcomUserId } from "@lib/utils/getCalcomUserId"; import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validations/event-type"; /** @@ -15,6 +16,8 @@ import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validatio * - ApiKeyAuth: [] * tags: * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types * responses: * 200: * description: OK @@ -28,6 +31,8 @@ import { schemaEventTypeBodyParams, schemaEventTypePublic } from "@lib/validatio * - ApiKeyAuth: [] * tags: * - event-types + * externalDocs: + * url: https://docs.cal.com/event-types * responses: * 201: * description: OK, event type created @@ -42,8 +47,10 @@ async function createOrlistAllEventTypes( res: NextApiResponse ) { const { method } = req; + const userId = await getCalcomUserId(res); + if (method === "GET") { - const data = await prisma.eventType.findMany(); + const data = await prisma.eventType.findMany({ where: { userId } }); const event_types = data.map((eventType) => schemaEventTypePublic.parse(eventType)); if (event_types) res.status(200).json({ event_types }); else @@ -56,7 +63,7 @@ async function createOrlistAllEventTypes( const safe = schemaEventTypeBodyParams.safeParse(req.body); if (!safe.success) throw new Error("Invalid request body"); - const data = await prisma.eventType.create({ data: safe.data }); + const data = await prisma.eventType.create({ data: { ...safe.data, userId } }); const event_type = schemaEventTypePublic.parse(data); if (data) res.status(201).json({ event_type, message: "EventType created successfully" });