From b28f6f4bb2f138cdf1ad95658fe651a09ca0345f Mon Sep 17 00:00:00 2001 From: Shubham Palriwala Date: Mon, 27 Nov 2023 19:10:51 +0530 Subject: [PATCH] feat: support for make and n8n integration states (#1568) Co-authored-by: Matthias Nannt --- .../[environmentId]/integrations/page.tsx | 49 ++++++++++++++++--- .../migration.sql | 18 +++++++ packages/database/schema.prisma | 6 ++- packages/lib/webhook/service.ts | 2 +- packages/types/webhooks.ts | 6 ++- 5 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 packages/database/migrations/20231116131301_add_types_to_wehbhook_source/migration.sql diff --git a/apps/web/app/(app)/environments/[environmentId]/integrations/page.tsx b/apps/web/app/(app)/environments/[environmentId]/integrations/page.tsx index 5b0ac9d859..df9103d7e7 100644 --- a/apps/web/app/(app)/environments/[environmentId]/integrations/page.tsx +++ b/apps/web/app/(app)/environments/[environmentId]/integrations/page.tsx @@ -7,7 +7,7 @@ import WebhookLogo from "@/images/webhook.png"; import ZapierLogo from "@/images/zapier-small.png"; import { Card } from "@formbricks/ui/Card"; import Image from "next/image"; -import { getCountOfWebhooksBasedOnSource } from "@formbricks/lib/webhook/service"; +import { getWebhookCountBySource } from "@formbricks/lib/webhook/service"; import { getEnvironment } from "@formbricks/lib/environment/service"; import { getIntegrations } from "@formbricks/lib/integration/service"; import { getTeamByEnvironmentId } from "@formbricks/lib/team/service"; @@ -20,13 +20,24 @@ import { ErrorComponent } from "@formbricks/ui/ErrorComponent"; export default async function IntegrationsPage({ params }) { const environmentId = params.environmentId; - const [environment, integrations, userWebhooks, zapierWebhooks, team, session] = await Promise.all([ + const [ + environment, + integrations, + team, + session, + userWebhookCount, + zapierWebhookCount, + makeWebhookCount, + n8nwebhookCount, + ] = await Promise.all([ getEnvironment(environmentId), getIntegrations(environmentId), - getCountOfWebhooksBasedOnSource(environmentId, "user"), - getCountOfWebhooksBasedOnSource(environmentId, "zapier"), getTeamByEnvironmentId(params.environmentId), getServerSession(authOptions), + getWebhookCountBySource(environmentId, "user"), + getWebhookCountBySource(environmentId, "zapier"), + getWebhookCountBySource(environmentId, "make"), + getWebhookCountBySource(environmentId, "n8n"), ]); if (!session) { @@ -67,9 +78,13 @@ export default async function IntegrationsPage({ params }) { label: "Zapier", description: "Integrate Formbricks with 5000+ apps via Zapier", icon: Zapier Logo, - connected: zapierWebhooks > 0, + connected: zapierWebhookCount > 0, statusText: - zapierWebhooks === 1 ? "1 zap" : zapierWebhooks === 0 ? "Not Connected" : `${zapierWebhooks} zaps`, + zapierWebhookCount === 1 + ? "1 zap" + : zapierWebhookCount === 0 + ? "Not Connected" + : `${zapierWebhookCount} zaps`, }, { connectHref: `/environments/${params.environmentId}/integrations/webhooks`, @@ -81,9 +96,13 @@ export default async function IntegrationsPage({ params }) { label: "Webhooks", description: "Trigger Webhooks based on actions in your surveys", icon: Webhook Logo, - connected: userWebhooks > 0, + connected: userWebhookCount > 0, statusText: - userWebhooks === 1 ? "1 webhook" : userWebhooks === 0 ? "Not Connected" : `${userWebhooks} webhooks`, + userWebhookCount === 1 + ? "1 webhook" + : userWebhookCount === 0 + ? "Not Connected" + : `${userWebhookCount} webhooks`, }, { connectHref: `/environments/${params.environmentId}/integrations/google-sheets`, @@ -121,6 +140,13 @@ export default async function IntegrationsPage({ params }) { label: "n8n", description: "Integrate Formbricks with 350+ apps via n8n", icon: n8n Logo, + connected: n8nwebhookCount > 0, + statusText: + n8nwebhookCount === 1 + ? "1 integration" + : n8nwebhookCount === 0 + ? "Not Connected" + : `${n8nwebhookCount} integrations`, }, { docsHref: "https://formbricks.com/docs/integrations/make", @@ -132,6 +158,13 @@ export default async function IntegrationsPage({ params }) { label: "Make.com", description: "Integrate Formbricks with 1000+ apps via Make", icon: Make Logo, + connected: makeWebhookCount > 0, + statusText: + makeWebhookCount === 1 + ? "1 integration" + : makeWebhookCount === 0 + ? "Not Connected" + : `${makeWebhookCount} integration`, }, ]; diff --git a/packages/database/migrations/20231116131301_add_types_to_wehbhook_source/migration.sql b/packages/database/migrations/20231116131301_add_types_to_wehbhook_source/migration.sql new file mode 100644 index 0000000000..a6a8932678 --- /dev/null +++ b/packages/database/migrations/20231116131301_add_types_to_wehbhook_source/migration.sql @@ -0,0 +1,18 @@ +-- 1. Rename the existing ENUM type. +ALTER TYPE "WehbhookSource" RENAME TO "TempWebhookSource"; + +-- 2. Create the new ENUM type. +CREATE TYPE "WebhookSource" AS ENUM ('user', 'zapier', 'make', 'n8n'); + +-- 3. Remove the default. +ALTER TABLE "Webhook" ALTER COLUMN "source" DROP DEFAULT; + +-- 4. Change the column type using the USING clause for casting. +ALTER TABLE "Webhook" +ALTER COLUMN "source" TYPE "WebhookSource" USING "source"::text::"WebhookSource"; + +-- 5. Add the default back. +ALTER TABLE "Webhook" ALTER COLUMN "source" SET DEFAULT 'user'; + +-- Optionally, if you want to drop the old ENUM type after verifying everything works: +DROP TYPE "TempWebhookSource"; diff --git a/packages/database/schema.prisma b/packages/database/schema.prisma index 7f7199bd61..6f92affcd0 100644 --- a/packages/database/schema.prisma +++ b/packages/database/schema.prisma @@ -31,9 +31,11 @@ enum PipelineTriggers { responseFinished } -enum WehbhookSource { +enum WebhookSource { user zapier + make + n8n } model Webhook { @@ -42,7 +44,7 @@ model Webhook { createdAt DateTime @default(now()) @map(name: "created_at") updatedAt DateTime @updatedAt @map(name: "updated_at") url String - source WehbhookSource @default(user) + source WebhookSource @default(user) environment Environment @relation(fields: [environmentId], references: [id], onDelete: Cascade) environmentId String triggers PipelineTriggers[] diff --git a/packages/lib/webhook/service.ts b/packages/lib/webhook/service.ts index 44eb8f40b2..cde984d29a 100644 --- a/packages/lib/webhook/service.ts +++ b/packages/lib/webhook/service.ts @@ -36,7 +36,7 @@ export const getWebhooks = async (environmentId: string, page?: number): Promise } )(); -export const getCountOfWebhooksBasedOnSource = async ( +export const getWebhookCountBySource = async ( environmentId: string, source: TWebhookInput["source"] ): Promise => diff --git a/packages/types/webhooks.ts b/packages/types/webhooks.ts index 055f8d61d1..ed97c90d90 100644 --- a/packages/types/webhooks.ts +++ b/packages/types/webhooks.ts @@ -1,13 +1,15 @@ import { z } from "zod"; import { ZPipelineTrigger } from "./pipelines"; +export const ZWebhookSource = z.enum(["user", "zapier", "make", "n8n"]); + export const ZWebhook = z.object({ id: z.string().cuid2(), name: z.string().nullish(), createdAt: z.date(), updatedAt: z.date(), url: z.string().url(), - source: z.enum(["user", "zapier"]), + source: ZWebhookSource, environmentId: z.string().cuid2(), triggers: z.array(ZPipelineTrigger), surveyIds: z.array(z.string().cuid2()), @@ -19,7 +21,7 @@ export const ZWebhookInput = z.object({ url: z.string().url(), name: z.string().nullish(), triggers: z.array(ZPipelineTrigger), - source: z.enum(["user", "zapier"]).optional(), + source: ZWebhookSource.optional(), surveyIds: z.array(z.string().cuid2()).optional(), });