From 43b94d3e9662bb2be8128c7546cc58148c2fbf8f Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Wed, 1 Jun 2022 17:05:33 +0200 Subject: [PATCH 1/3] feat: adds automatic generation of uuid id for webhooks create on API --- lib/helpers/verifyApiKey.ts | 1 - lib/utils/isAdmin.ts | 3 ++- lib/validations/webhook.ts | 2 +- pages/api/hooks/[id].ts | 4 ++-- pages/api/hooks/index.ts | 3 ++- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/helpers/verifyApiKey.ts b/lib/helpers/verifyApiKey.ts index 38fa6b44c3..c45f3d3c3f 100644 --- a/lib/helpers/verifyApiKey.ts +++ b/lib/helpers/verifyApiKey.ts @@ -11,7 +11,6 @@ declare module "next" { userId: number; method: string; query: { [key: string]: string | string[] }; - body: any; } } diff --git a/lib/utils/isAdmin.ts b/lib/utils/isAdmin.ts index c97628b46d..64af37367f 100644 --- a/lib/utils/isAdmin.ts +++ b/lib/utils/isAdmin.ts @@ -1,5 +1,6 @@ +import { UserPermissionRole } from "@prisma/client"; + import prisma from "@calcom/prisma"; -import { UserPermissionRole } from "@calcom/prisma/client"; export const isAdminGuard = async (userId: number) => { const user = await prisma.user.findUnique({ where: { id: userId } }); diff --git a/lib/validations/webhook.ts b/lib/validations/webhook.ts index ee42052204..c157a6e367 100644 --- a/lib/validations/webhook.ts +++ b/lib/validations/webhook.ts @@ -26,7 +26,7 @@ const schemaWebhookBaseBodyParams = Webhook.pick({ export const schemaWebhookCreateParams = z .object({ - id: z.string(), + // id: z.string(), subscriberUrl: z.string().url(), eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array(), active: z.boolean(), diff --git a/pages/api/hooks/[id].ts b/pages/api/hooks/[id].ts index dcf82aaa2d..32a81e7b12 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({ @@ -97,7 +97,7 @@ export async function WebhookById( } await prisma.webhook .update({ where: { id: safeQuery.data.id }, data: safeBody.data }) - // .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 05b6e90d57..422a30e419 100644 --- a/pages/api/hooks/index.ts +++ b/pages/api/hooks/index.ts @@ -1,4 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; +import { v4 as uuidv4 } from "uuid"; import prisma from "@calcom/prisma"; @@ -60,7 +61,7 @@ async function createOrlistAllWebhooks( res.status(400).json({ message: "Invalid request body" }); return; } - const data = await prisma.webhook.create({ data: { ...safe.data, userId } }); + const data = await prisma.webhook.create({ data: { id: uuidv4(), ...safe.data, userId } }); if (data) res.status(201).json({ webhook: data, message: "Webhook created successfully" }); else (error: Error) => From 3fc22a8eff2cd9327ebd5e2c19af58925be5df16 Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Wed, 1 Jun 2022 17:08:01 +0200 Subject: [PATCH 2/3] fix: remove comments --- lib/validations/webhook.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/validations/webhook.ts b/lib/validations/webhook.ts index c157a6e367..f5de69c45a 100644 --- a/lib/validations/webhook.ts +++ b/lib/validations/webhook.ts @@ -26,7 +26,6 @@ const schemaWebhookBaseBodyParams = Webhook.pick({ export const schemaWebhookCreateParams = z .object({ - // id: z.string(), subscriberUrl: z.string().url(), eventTriggers: z.enum(WEBHOOK_TRIGGER_EVENTS).array(), active: z.boolean(), @@ -52,6 +51,7 @@ export const schemaWebhookReadPublic = Webhook.pick({ eventTypeId: true, payloadTemplate: true, eventTriggers: true, + /** @todo: find out how to properly add back and validate those. */ // eventType: true, // app: true, appId: true, From fe73973b7a9bf9869174a1d5e6b8ed1ed24da3ab Mon Sep 17 00:00:00 2001 From: Agusti Fernandez Pardo Date: Thu, 2 Jun 2022 15:55:52 +0200 Subject: [PATCH 3/3] fix: add uuid to api/package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 16c51d9d6b..57da05e5e6 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "next-validations": "^0.2.0", "typescript": "^4.6.4", "tzdata": "^1.0.30", + "uuid": "^8.3.2", "zod": "^3.16.0" } }