From 0e95a1d3e8c80136aa0674233b7982f125b66601 Mon Sep 17 00:00:00 2001 From: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Date: Wed, 25 May 2022 14:13:52 +0530 Subject: [PATCH 1/2] Enabled webhook create call --- pages/api/hooks/index.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pages/api/hooks/index.ts b/pages/api/hooks/index.ts index 983fe3ecce..05b6e90d57 100644 --- a/pages/api/hooks/index.ts +++ b/pages/api/hooks/index.ts @@ -55,19 +55,19 @@ async function createOrlistAllWebhooks( * 401: * description: Authorization information is missing or invalid. */ - // const safe = schemaWebhookCreateBodyParams.safeParse(body); - // if (!safe.success) { - // res.status(400).json({ message: "Invalid request body" }); - // return; - // } - // const data = await prisma.webhook.create({ data: { ...safe.data, userId } }); - // if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" }); - // else - // (error: Error) => - // res.status(400).json({ - // message: "Could not create new webhook", - // error, - // }); + const safe = schemaWebhookCreateBodyParams.safeParse(body); + if (!safe.success) { + res.status(400).json({ message: "Invalid request body" }); + return; + } + const data = await prisma.webhook.create({ data: { ...safe.data, userId } }); + if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new webhook", + error, + }); } else res.status(405).json({ message: `Method ${method} not allowed` }); } From 2e22978838b6ce16bb038a0acefbcf94a9910089 Mon Sep 17 00:00:00 2001 From: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Date: Wed, 25 May 2022 14:23:43 +0530 Subject: [PATCH 2/2] Added schema for create --- lib/validations/webhook.ts | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/lib/validations/webhook.ts b/lib/validations/webhook.ts index aab40dc502..a655e5f11c 100644 --- a/lib/validations/webhook.ts +++ b/lib/validations/webhook.ts @@ -12,29 +12,21 @@ const schemaWebhookBaseBodyParams = Webhook.pick({ payloadTemplate: true, }).partial(); -// const schemaWebhookCreateParams = z -// .object({ -// id: z.string(), -// subscriberUrl: z.string(), -// }) -// .strict(); +export const schemaWebhookCreateParams = z + .object({ + userId: z.number().or(z.string()).optional(), + eventTypeId: z.number().or(z.string()).optional(), + eventTriggers: z.any.optional(), + active: z.boolean().optional(), + subscriberUrl: z.string(), + payloadTemplate: z.string().optional(), + }) + .strict(); export const schemaWebhookCreateBodyParams = schemaWebhookBaseBodyParams.merge( - z.object({ - id: z.string(), - subscriberUrl: z.string(), - }) + 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(); - export const schemaWebhookEditBodyParams = schemaWebhookBaseBodyParams.merge( z.object({ payloadTemplate: z.string().optional(),