From 7deb5f8e1f09f22f39c59aec23981b031cfd662c Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Wed, 11 May 2022 15:57:00 +0200 Subject: [PATCH] fix: webhooks upgraded to new version, need to update templates --- lib/validations/booking.ts | 5 +-- ...-event-reference.ts => event-reference.ts} | 0 lib/validations/webhook.ts | 42 ++++++++++++++++++- pages/api/bookings/index.ts | 5 ++- pages/api/event-references/[id].ts | 5 ++- pages/api/event-references/index.ts | 14 ++++--- pages/api/hooks/[id].ts | 21 ++++------ pages/api/hooks/index.ts | 10 ++--- 8 files changed, 71 insertions(+), 31 deletions(-) rename lib/validations/{daily-event-reference.ts => event-reference.ts} (100%) diff --git a/lib/validations/booking.ts b/lib/validations/booking.ts index 60c231efa5..67b4f1b35a 100644 --- a/lib/validations/booking.ts +++ b/lib/validations/booking.ts @@ -14,11 +14,10 @@ const schemaBookingBaseBodyParams = Booking.pick({ const schemaBookingCreateParams = z .object({ uid: z.string(), - userId: z.number(), eventTypeId: z.number(), title: z.string(), - startTime: z.date(), - endTime: z.date(), + startTime: z.date().or(z.string()), + endTime: z.date().or(z.string()), }) .strict(); diff --git a/lib/validations/daily-event-reference.ts b/lib/validations/event-reference.ts similarity index 100% rename from lib/validations/daily-event-reference.ts rename to lib/validations/event-reference.ts diff --git a/lib/validations/webhook.ts b/lib/validations/webhook.ts index 40b0731507..0340f8d707 100644 --- a/lib/validations/webhook.ts +++ b/lib/validations/webhook.ts @@ -1,5 +1,43 @@ +import { z } from "zod"; + import { _WebhookModel as Webhook } from "@calcom/prisma/zod"; -export const schemaWebhookBodyParams = Webhook.omit({ id: true }).partial(); +const schemaWebhookBaseBodyParams = Webhook.pick({ + id: true, + userId: true, + eventTypeId: true, + eventTriggers: true, + active: true, + subscriberUrl: true, + payloadTemplate: true, +}).partial(); -export const schemaWebhookPublic = Webhook.omit({}); +const schemaWebhookCreateParams = z + .object({ + id: z.string(), + subscriberUrl: z.string(), + }) + .strict(); + +export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge(schemaWebhookCreateParams); + +const schemaWebhookEditParams = z + .object({ + uid: z.string().optional(), + title: z.string().optional(), + startTime: z.date().optional(), + endTime: z.date().optional(), + }) + .strict(); + +export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge(schemaWebhookEditParams); + +export const schemaWebhookReadPublic = Webhook.pick({ + id: true, + userId: true, + eventTypeId: true, + uid: true, + title: true, + startTime: true, + endTime: true, +}); diff --git a/pages/api/bookings/index.ts b/pages/api/bookings/index.ts index 1ee689b195..00b92932a7 100644 --- a/pages/api/bookings/index.ts +++ b/pages/api/bookings/index.ts @@ -10,6 +10,7 @@ async function createOrlistAllBookings( { method, body, userId }: NextApiRequest, res: NextApiResponse ) { + console.log("userIduserId", userId); if (method === "GET") { /** * @swagger @@ -77,8 +78,8 @@ async function createOrlistAllBookings( return; // throw new Error("Invalid request body"); } - - const data = await prisma.booking.create({ data: { ...safe.data, userId } }); + safe.data.userId = userId; + const data = await prisma.booking.create({ data: { ...safe.data } }); const booking = schemaBookingReadPublic.parse(data); if (booking) res.status(201).json({ booking, message: "Booking created successfully" }); diff --git a/pages/api/event-references/[id].ts b/pages/api/event-references/[id].ts index a6a15b9e19..03ee39f49e 100644 --- a/pages/api/event-references/[id].ts +++ b/pages/api/event-references/[id].ts @@ -7,7 +7,7 @@ import type { DailyEventReferenceResponse } from "@lib/types"; import { schemaDailyEventReferenceEditBodyParams, schemaDailyEventReferenceReadPublic, -} from "@lib/validations/daily-event-reference"; +} from "@lib/validations/event-reference"; import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt, @@ -37,6 +37,7 @@ export async function dailyEventReferenceById( * /event-references/{id}: * get: * summary: Find a event reference + * operationId: getEventReferenceById * parameters: * - in: path * name: id @@ -72,6 +73,7 @@ export async function dailyEventReferenceById( * /event-references/{id}: * patch: * summary: Edit an existing event reference + * operationId: editEventReferenceById * parameters: * - in: path * name: id @@ -110,6 +112,7 @@ export async function dailyEventReferenceById( * /event-references/{id}: * delete: * summary: Remove an existing event reference + * operationId: removeEventReferenceById * parameters: * - in: path * name: id diff --git a/pages/api/event-references/index.ts b/pages/api/event-references/index.ts index bc19171de8..e0b18a7919 100644 --- a/pages/api/event-references/index.ts +++ b/pages/api/event-references/index.ts @@ -7,7 +7,7 @@ import { DailyEventReferenceResponse, DailyEventReferencesResponse } from "@lib/ import { schemaDailyEventReferenceCreateBodyParams, schemaDailyEventReferenceReadPublic, -} from "@lib/validations/daily-event-reference"; +} from "@lib/validations/event-reference"; async function createOrlistAllDailyEventReferences( { method, body, userId }: NextApiRequest, @@ -21,7 +21,8 @@ async function createOrlistAllDailyEventReferences( * @swagger * /event-references: * get: - * summary: Find all daily event reference + * summary: Find all event reference + * operationId: listEventReferences * tags: * - event-references * responses: @@ -30,7 +31,7 @@ async function createOrlistAllDailyEventReferences( * 401: * description: Authorization information is missing or invalid. * 404: - * description: No daily event references were found + * description: No event references were found */ const data = await prisma.dailyEventReference.findMany({ where: { bookingId: { in: userBookingIds } }, @@ -50,12 +51,13 @@ async function createOrlistAllDailyEventReferences( * @swagger * /event-references: * post: - * summary: Creates a new daily event reference + * summary: Creates a new event reference + * operationId: addEventReference * tags: * - event-references * responses: * 201: - * description: OK, daily event reference created + * description: OK, event reference created * 400: * description: Bad request. DailyEventReference body is invalid. * 401: @@ -72,7 +74,7 @@ async function createOrlistAllDailyEventReferences( else (error: Error) => res.status(400).json({ - message: "Could not create new daily event reference", + message: "Could not create new event reference", error, }); } else res.status(405).json({ message: `Method ${method} not allowed` }); diff --git a/pages/api/hooks/[id].ts b/pages/api/hooks/[id].ts index c9fcdf346d..59ae3e3516 100644 --- a/pages/api/hooks/[id].ts +++ b/pages/api/hooks/[id].ts @@ -4,17 +4,14 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import type { WebhookResponse } from "@lib/types"; -import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook"; -import { - schemaQueryIdParseInt, - withValidQueryIdTransformParseInt, -} from "@lib/validations/shared/queryIdTransformParseInt"; +import { schemaQueryIdAsString } from "@lib/validations/shared/queryIdString"; +import { schemaWebhookEditBodyParams, schemaWebhookReadPublic } from "@lib/validations/webhook"; export async function WebhookById( { method, query, body, userId }: NextApiRequest, res: NextApiResponse ) { - const safeQuery = schemaQueryIdParseInt.safeParse(query); + const safeQuery = schemaQueryIdAsString.safeParse(query); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); const data = await prisma.webhook.findMany({ where: { userId } }); const userWebhooks = data.map((webhook) => webhook.id); @@ -50,8 +47,8 @@ export async function WebhookById( case "GET": await prisma.webhook .findUnique({ where: { id: safeQuery.data.id } }) - .then((data) => schemaWebhookPublic.parse(data)) - .then((hook) => res.status(200).json({ hook })) + .then((data) => schemaWebhookReadPublic.parse(data)) + .then((webhook) => res.status(200).json({ webhook })) .catch((error: Error) => res.status(404).json({ message: `Webhook with id: ${safeQuery.data.id} not found`, @@ -86,14 +83,14 @@ export async function WebhookById( * description: Authorization information is missing or invalid. */ case "PATCH": - const safeBody = schemaWebhookBodyParams.safeParse(body); + const safeBody = schemaWebhookEditBodyParams.safeParse(body); if (!safeBody.success) { throw new Error("Invalid request body"); } await prisma.webhook .update({ where: { id: safeQuery.data.id }, data: safeBody.data }) - .then((data) => schemaWebhookPublic.parse(data)) - .then((hook) => res.status(200).json({ hook })) + .then((data) => schemaWebhookReadPublic.parse(data)) + .then((webhook) => res.status(200).json({ webhook })) .catch((error: Error) => res.status(404).json({ message: `Webhook with id: ${safeQuery.data.id} not found`, @@ -150,4 +147,4 @@ export async function WebhookById( } } -export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(WebhookById)); +export default withMiddleware("HTTP_GET_DELETE_PATCH")(WebhookById); diff --git a/pages/api/hooks/index.ts b/pages/api/hooks/index.ts index 9ff4f441af..9ce8615ea7 100644 --- a/pages/api/hooks/index.ts +++ b/pages/api/hooks/index.ts @@ -4,7 +4,7 @@ import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; import { WebhookResponse, WebhooksResponse } from "@lib/types"; -import { schemaWebhookBodyParams, schemaWebhookPublic } from "@lib/validations/webhook"; +import { schemaWebhookCreateBodyParams, schemaWebhookReadPublic } from "@lib/validations/webhook"; async function createOrlistAllWebhooks( { method, body, userId }: NextApiRequest, @@ -28,8 +28,8 @@ async function createOrlistAllWebhooks( * 404: * description: No webhooks were found */ - const data = await prisma.webhooks.findMany({ where: { userId } }); - const webhooks = data.map((webhook) => schemaWebhookPublic.parse(webhook)); + const data = await prisma.webhook.findMany({ where: { userId } }); + const webhooks = data.map((webhook) => schemaWebhookReadPublic.parse(webhook)); if (webhooks) res.status(200).json({ webhooks }); else (error: Error) => @@ -55,11 +55,11 @@ async function createOrlistAllWebhooks( * 401: * description: Authorization information is missing or invalid. */ - const safe = schemaWebhookBodyParams.safeParse(body); + const safe = schemaWebhookCreateBodyParams.safeParse(body); if (!safe.success) throw new Error("Invalid request body"); const data = await prisma.webhook.create({ data: { ...safe.data, userId } }); - const webhook = schemaWebhookPublic.parse(data); + const webhook = schemaWebhookReadPublic.parse(data); if (data) res.status(201).json({ webhook, message: "Webhook created successfully" }); else