diff --git a/__tests__/event-types.test.ts b/__tests__/event-types.test.ts index 2aea868136..d6eb7fe57f 100644 --- a/__tests__/event-types.test.ts +++ b/__tests__/event-types.test.ts @@ -1,7 +1,6 @@ import { createMocks } from "node-mocks-http"; import prisma from "@calcom/prisma"; -import { EventType } from "@calcom/prisma/client"; import handleEvent from "../pages/api/event-types/[id]"; @@ -15,13 +14,38 @@ describe("/api/event-types/[id]", () => { const { req, res } = createMocks({ method: "GET", query: { - id: 1, + id: "1", }, }); - prisma.eventType.findUnique({ where: { id: 1 } }).then(async (data: EventType) => { - await handleEvent(req, res); - expect(res._getStatusCode()).toBe(200); - expect(JSON.parse(res._getData())).toStrictEqual({ event: data }); - }); + const event = await prisma.eventType.findUnique({ where: { id: 1 } }); + await handleEvent(req, res); + + expect(res._getStatusCode()).toBe(200); + expect(JSON.parse(res._getData())).toStrictEqual({ data: event }); + }); +}); + +// This can never happen under our normal nextjs setup where query is always a string | string[]. +// But seemed a good example for testing an error validation +describe("/api/event-types/[id] errors if query id is number, requires a string", () => { + it("returns a message with the specified events", async () => { + const { req, res } = createMocks({ + method: "GET", + query: { + id: 1, // passing query as a number, which should fail as nextjs will try to parse it as a string + }, + }); + await handleEvent(req, res); + + expect(res._getStatusCode()).toBe(400); + expect(JSON.parse(res._getData())).toStrictEqual([ + { + code: "invalid_type", + expected: "string", + received: "number", + path: ["id"], + message: "Expected string, received number", + }, + ]); }); }); diff --git a/pages/api/event-types/[id].ts b/pages/api/event-types/[id].ts index 112f64937b..7ecc4362c9 100644 --- a/pages/api/event-types/[id].ts +++ b/pages/api/event-types/[id].ts @@ -28,15 +28,20 @@ type ResponseData = { export async function eventType(req: NextApiRequest, res: NextApiResponse) { const { query, method } = req; if (method === "GET") { - const safe = await schema.safeParse(query); - if (safe.success) { - try { + try { + const safe = await schema.safeParse(query); + // if (!safe.success) { + // res.status(500).json({ error: safe.error.message }); + // } + if (safe.success) { const event = await prisma.eventType.findUnique({ where: { id: safe.data.id } }); - res.status(200).json({ data: event }); - } catch (error) { - console.log(error); - res.status(400).json({ error: error }); + + if (event) res.status(200).json({ data: event }); + if (!event) res.status(404).json({ error: "Event type not found" }); } + } catch (error) { + console.log("catched", error); + res.status(500).json({ error: error }); } } else { // Reject any other HTTP method than POST diff --git a/pages/api/event-types/new.ts b/pages/api/event-types/new.ts index a9fc7d770e..f294a259bb 100644 --- a/pages/api/event-types/new.ts +++ b/pages/api/event-types/new.ts @@ -14,32 +14,42 @@ const schema = z }) .strict(); // Adding strict so that we can disallow passing in extra fields +type schema = z.infer; const validate = withValidation({ schema, type: "Zod", mode: "body", }); -interface Body { - title: string; - slug: string; - length: number; -} +// interface Body { +// title: string; +// slug: string; +// length: number; +// } type Data = { data?: EventType; error?: string; }; type NextApiRequestWithBody = NextApiRequest & { - body: Body; + // body: Body; }; async function createEventType(req: NextApiRequestWithBody, res: NextApiResponse) { - const { body, method }: { body: Body; method?: string } = req; + const { body, method } = req; if (method === "POST") { - schema.safeParse(body); - const newEvent = await prisma.eventType.create({ data: body }); - res.status(201).json({ data: newEvent }); + const safe = schema.safeParse(body); + if (safe.success && safe.data) { + await prisma.eventType + .create({ + data: { title: safe.data.title, slug: safe.data.slug, length: safe.data.length }, + }) + .then((event) => res.status(201).json({ data: event })) + .catch((error) => { + console.log(error); + res.status(400).json({ error: "Could not create event type" }); + }); + } } else { // Reject any other HTTP method than POST res.status(405).json({ error: "Only POST Method allowed" }); diff --git a/tsconfig.json b/tsconfig.json index e77f9df418..8581d9636a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,6 +20,6 @@ "jsx": "preserve" }, "include": [ - "./pages/api/*.ts" + "./" ] }