The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Prevent deleting a or updating (name, url, secrets) of webhook that is managed by an integration.
Kiran K3e269e4d985b
diff --git a/apps/web/app/api/integrations/uninstall/route.ts b/apps/web/app/api/integrations/uninstall/route.tsindex d9c5a531af..d1ad21850d 100644--- a/apps/web/app/api/integrations/uninstall/route.ts+++ b/apps/web/app/api/integrations/uninstall/route.ts@@ -2,7 +2,6 @@ import { DubApiError } from "@/lib/api/errors";import { withWorkspace } from "@/lib/auth";import { uninstallSlackIntegration } from "@/lib/integrations/slack/uninstall";import { prisma } from "@/lib/prisma";-import { deleteWebhook } from "@/lib/webhook/delete-webhook";import { NextResponse } from "next/server";// DELETE /api/integrations/uninstall - uninstall an installation by id@@ -31,15 +30,16 @@ export const DELETE = withWorkspace(});}- await prisma.installedIntegration.delete({+ const { integration } = await prisma.installedIntegration.delete({where: {id: installationId,},- });-- const integration = await prisma.integration.findUniqueOrThrow({- where: {- id: installation.integrationId,+ select: {+ integration: {+ select: {+ slug: true,+ },+ },},});@@ -49,16 +49,6 @@ export const DELETE = withWorkspace(});}- const credentials = installation?.credentials as any;-- // if the integration has added a webhook, delete it- if ("webhookId" in credentials) {- await deleteWebhook({- webhookId: credentials.webhookId,- workspaceId: workspace.id,- });- }-return NextResponse.json({ id: installationId });},{diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex 6c1037a31a..5b5f1a8236 100644--- a/apps/web/app/api/webhooks/[webhookId]/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts@@ -57,6 +57,22 @@ export const PATCH = withWorkspace(await parseRequestBody(req),);+ const existingWebhook = await prisma.webhook.findUniqueOrThrow({+ where: {+ id: webhookId,+ projectId: workspace.id,+ },+ });++ // If the webhook is managed by an integration, only the linkIds & triggers can be updated manually.+ if (existingWebhook.installationId && (name || url)) {+ throw new DubApiError({+ code: "bad_request",+ message:+ "This webhook is managed by an integration. Not all fields can be updated manually.",+ });+ }+if (url) {const webhookUrlExists = await prisma.webhook.findFirst({where: {@@ -105,13 +121,6 @@ export const PATCH = withWorkspace(},});- const existingWebhook = await prisma.webhook.findUniqueOrThrow({- where: {- id: webhookId,- projectId: workspace.id,- },- });-const webhook = await prisma.webhook.update({where: {id: webhookId,diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsxindex 1c7f4560e8..3f8f4d54c0 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/integrations/[integrationSlug]/page.tsx@@ -33,6 +33,11 @@ export default async function IntegrationPage({image: true,},},+ webhook: {+ select: {+ id: true,+ },+ },},},},@@ -48,6 +53,10 @@ export default async function IntegrationPage({? integration.installations[0]?.credentials: undefined;+ const webhookId = installed+ ? integration.installations[0]?.webhook?.id+ : undefined;+return (<IntegrationPageClientintegration={{@@ -66,6 +75,7 @@ export default async function IntegrationPage({}: null,credentials,+ webhookId,}}/>);diff --git a/apps/web/lib/integrations/segment/components/configure-webhook.tsx b/apps/web/lib/integrations/segment/components/configure-webhook.tsxindex 98898d3f71..d981b7b5d8 100644--- a/apps/web/lib/integrations/segment/components/configure-webhook.tsx+++ b/apps/web/lib/integrations/segment/components/configure-webhook.tsx@@ -15,21 +15,11 @@ import { fetcher } from "@dub/utils";import { FormEvent, useEffect, useState } from "react";import { toast } from "sonner";import useSWR, { mutate } from "swr";-import { SegmentSettingsProps } from "./types";--export function ConfigureWebhook({- credentials,-}: {- credentials: SegmentSettingsProps["credentials"];-}) {- const webhookId = credentials?.webhookId;++export function ConfigureWebhook({ webhookId }: { webhookId: string }) {const [saving, setSaving] = useState(false);const { id: workspaceId, role } = useWorkspace();- // if (!flags?.webhooks) {- // redirect(`/${workspaceSlug}`);- // }-const { data: webhook, isLoading } = useSWR<WebhookProps>(`/api/webhooks/${webhookId}?workspaceId=${workspaceId}`,fetcher,@@ -42,7 +32,10 @@ export function ConfigureWebhook({useEffect(() => {if (webhook) {- setData(webhook);+ setData({+ linkIds: webhook.linkIds,+ triggers: webhook.triggers,+ });}}, [webhook]);diff --git a/apps/web/lib/integrations/segment/components/set-write-key.tsx b/apps/web/lib/integrations/segment/components/set-write-key.tsxindex a494f1850b..c6b8b31bc7 100644--- a/apps/web/lib/integrations/segment/components/set-write-key.tsx+++ b/apps/web/lib/integrations/segment/components/set-write-key.tsx@@ -1,15 +1,21 @@"use client";import useWorkspace from "@/lib/swr/use-workspace";+import { SegmentIntegrationCredentials } from "@/lib/types";import { Lock } from "@/ui/shared/icons";import { Button } from "@dub/ui";import { useAction } from "next-safe-action/hooks";import { useState } from "react";import { toast } from "sonner";import { installSegmentAction } from "../install";-import { SegmentSettingsProps } from "./types";-export function SetWriteKey({ installed, credentials }: SegmentSettingsProps) {+export function SetWriteKey({+ credentials,+ installed,+}: {+ credentials: SegmentIntegrationCredentials;+ installed: boolean;+}) {const { id: workspaceId } = useWorkspace();const [writeKey, setWriteKey] = useState(credentials?.writeKey);@@ -73,7 +79,7 @@ export function SetWriteKey({ installed, credentials }: SegmentSettingsProps) {name="writeKey"value={writeKey}onChange={(e) => setWriteKey(e.target.value)}- readOnly={installed?.id ? true : false}+ readOnly={installed}/></div></div>@@ -86,7 +92,7 @@ export function SetWriteKey({ installed, credentials }: SegmentSettingsProps) {text="Enable Segment"className="w-fit"loading={isExecuting}- disabled={installed?.id ? true : false || !writeKey}+ disabled={installed || !writeKey}/></div></div>diff --git a/apps/web/lib/integrations/segment/components/settings.tsx b/apps/web/lib/integrations/segment/components/settings.tsxindex 3f0e02902d..ba470c5af0 100644--- a/apps/web/lib/integrations/segment/components/settings.tsx+++ b/apps/web/lib/integrations/segment/components/settings.tsx@@ -1,17 +1,22 @@"use client";+import {+ InstalledIntegrationInfoProps,+ SegmentIntegrationCredentials,+} from "@/lib/types";import { ConfigureWebhook } from "./configure-webhook";import { SetWriteKey } from "./set-write-key";-import { SegmentSettingsProps } from "./types";-export const SegmentSettings = ({- installed,- credentials,-}: SegmentSettingsProps) => {+export const SegmentSettings = (props: InstalledIntegrationInfoProps) => {+ const { installed, credentials, webhookId } = props;+return (<>- <SetWriteKey installed={installed} credentials={credentials} />- {installed && <ConfigureWebhook credentials={credentials} />}+ <SetWriteKey+ installed={!!installed?.id}+ credentials={credentials as SegmentIntegrationCredentials}+ />+ {installed && webhookId && <ConfigureWebhook webhookId={webhookId} />}</>);};diff --git a/apps/web/lib/integrations/segment/components/types.ts b/apps/web/lib/integrations/segment/components/types.tsindex 80e42d9acd..e915dafdee 100644--- a/apps/web/lib/integrations/segment/components/types.ts+++ b/apps/web/lib/integrations/segment/components/types.ts@@ -4,6 +4,6 @@ export interface SegmentSettingsProps {};credentials: {writeKey?: string;- webhookId?: string;};+ webhookId?: string;}diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.tsindex e2c68704b7..e15984babc 100644--- a/apps/web/lib/types.ts+++ b/apps/web/lib/types.ts@@ -288,6 +288,7 @@ export type InstalledIntegrationInfoProps = Pick<};} | null;credentials?: Prisma.JsonValue;+ webhookId?: string; // Only if the webhook is managed by an integration};export type WebhookTrigger = keyof typeof WEBHOOK_TRIGGER_DESCRIPTIONS;@@ -342,3 +343,7 @@ export type PayoutProps = z.infer<typeof PayoutSchema>;export type PayoutResponse = z.infer<typeof PayoutResponseSchema>;export type PartnerPayoutResponse = z.infer<typeof PartnerPayoutResponseSchema>;++export type SegmentIntegrationCredentials = {+ writeKey?: string;+};diff --git a/apps/web/lib/webhook/delete-webhook.ts b/apps/web/lib/webhook/delete-webhook.tsindex fcf092d1b9..8fa9df99c4 100644--- a/apps/web/lib/webhook/delete-webhook.ts+++ b/apps/web/lib/webhook/delete-webhook.ts@@ -2,6 +2,7 @@ import { linkCache } from "@/lib/api/links/cache";import { prisma } from "@/lib/prisma";import { webhookCache } from "@/lib/webhook/cache";import { waitUntil } from "@vercel/functions";+import { DubApiError } from "../api/errors";import { updateWebhookStatusForWorkspace } from "./update-webhook";export const deleteWebhook = async ({@@ -11,6 +12,24 @@ export const deleteWebhook = async ({webhookId: string;workspaceId: string;}) => {+ const webhook = await prisma.webhook.findUniqueOrThrow({+ where: {+ id: webhookId,+ projectId: workspaceId,+ },+ select: {+ installationId: true,+ },+ });++ if (webhook.installationId) {+ throw new DubApiError({+ code: "bad_request",+ message:+ "This webhook is managed by an integration, hence cannot be deleted manually.",+ });+ }+const linkWebhooks = await prisma.linkWebhook.findMany({where: {webhookId,@@ -23,7 +42,6 @@ export const deleteWebhook = async ({await prisma.webhook.delete({where: {id: webhookId,- projectId: workspaceId,},});