From f9e98228c763ca4ed525596259c076d989d6c6e4 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Fri, 20 May 2022 21:48:24 +0200 Subject: [PATCH 1/2] merge directly --- lib/validations/webhook.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/validations/webhook.ts b/lib/validations/webhook.ts index e3ea709ec9..fd95d5ed8c 100644 --- a/lib/validations/webhook.ts +++ b/lib/validations/webhook.ts @@ -12,25 +12,37 @@ const schemaWebhookBaseBodyParams = Webhook.pick({ payloadTemplate: true, }).partial(); -const schemaWebhookCreateParams = z - .object({ +// const schemaWebhookCreateParams = z +// .object({ +// id: z.string(), +// subscriberUrl: z.string(), +// }) +// .strict(); + +export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge( + z.object({ id: z.string(), subscriberUrl: z.string(), }) - .strict(); +); -export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge(schemaWebhookCreateParams); +// const schemaWebhookEditParams = z +// .object({ +// payloadTemplate: z.string().optional(), +// /** @todo: don't use any here and validate eventTriggers proper */ +// eventTriggers: z.any(), +// subscriberUrl: z.string().optional(), +// }) +// .strict(); -const schemaWebhookEditParams = z - .object({ +export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge( + z.object({ payloadTemplate: z.string().optional(), /** @todo: don't use any here and validate eventTriggers proper */ eventTriggers: z.any(), subscriberUrl: z.string().optional(), }) - .strict(); - -export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge(schemaWebhookEditParams); +); export const schemaWebhookReadPublic = Webhook.pick({ id: true, From 1f8c8959000ceec4dd2c1f0b176a12a352d82430 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Fri, 20 May 2022 21:52:41 +0200 Subject: [PATCH 2/2] fix: hooks --- lib/types.ts | 2 +- lib/validations/webhook.ts | 1 - pages/api/hooks/[id].ts | 2 +- pages/api/hooks/index.ts | 16 +++++++--------- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/lib/types.ts b/lib/types.ts index 681e88de65..257568d551 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -177,7 +177,7 @@ export type SchedulesResponse = BaseResponse & { // Webhook export type WebhookResponse = BaseResponse & { - webhook?: Partial; + webhook?: Partial | null; }; export type WebhooksResponse = BaseResponse & { webhooks?: Partial[]; diff --git a/lib/validations/webhook.ts b/lib/validations/webhook.ts index fd95d5ed8c..aab40dc502 100644 --- a/lib/validations/webhook.ts +++ b/lib/validations/webhook.ts @@ -50,7 +50,6 @@ export const schemaWebhookReadPublic = Webhook.pick({ eventTypeId: true, payloadTemplate: true, eventTriggers: true, - /** @todo: find out why this breaks the api */ // eventType: true, // app: true, appId: true, diff --git a/pages/api/hooks/[id].ts b/pages/api/hooks/[id].ts index 715c58cb73..dcf82aaa2d 100644 --- a/pages/api/hooks/[id].ts +++ b/pages/api/hooks/[id].ts @@ -51,7 +51,7 @@ export async function WebhookById( case "GET": await prisma.webhook .findUnique({ where: { id: safeQuery.data.id } }) - .then((data) => schemaWebhookReadPublic.parse(data)) + // .then((data) => schemaWebhookReadPublic.parse(data)) .then((webhook) => res.status(200).json({ webhook })) .catch((error: Error) => res.status(404).json({ diff --git a/pages/api/hooks/index.ts b/pages/api/hooks/index.ts index 12efc93a74..3dc0076c9e 100644 --- a/pages/api/hooks/index.ts +++ b/pages/api/hooks/index.ts @@ -29,15 +29,13 @@ async function createOrlistAllWebhooks( * 404: * description: No webhooks were found */ - const data = await prisma.webhook.findMany({ where: { userId } }); - // const webhooks = data.map((webhook) => schemaWebhookReadPublic.parse(webhook)); - if (data) res.status(200).json({ webhooks: data }); - else - (error: Error) => - res.status(404).json({ - message: "No Webhooks were found", - error, - }); + await prisma.webhook + .findMany({ where: { userId } }) + .then((data) => res.status(200).json({ webhooks: data })) + .catch((error) => { + console.log(error); + res.status(404).json({ message: "No webhooks were found", error }); + }); } else if (method === "POST") { /** * @swagger