The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Consolidate link click webhook sync and centralize WebhookTrigger type
Kiran K59bfd5af2317
diff --git a/apps/web/app/api/webhooks/[webhookId]/route.ts b/apps/web/app/api/webhooks/[webhookId]/route.tsindex a8b72271a6..45a146f3db 100644--- a/apps/web/app/api/webhooks/[webhookId]/route.ts+++ b/apps/web/app/api/webhooks/[webhookId]/route.ts@@ -3,11 +3,9 @@ import { parseRequestBody } from "@/lib/api/utils";import { withWorkspace } from "@/lib/auth";import { prisma } from "@/lib/prisma";import { NewWebhook } from "@/lib/types";-import {- LINK_CLICK_WEBHOOK_TRIGGER,- WebhookTrigger,-} from "@/lib/webhook/constants";-import { syncWorkspaceWebhookStatus } from "@/lib/webhook/sync-webhook";+import { syncWorkspaceWebhookStatus } from "@/lib/webhook/click-webhook-workspaces";+import { LINK_CLICK_WEBHOOK_TRIGGER } from "@/lib/webhook/constants";+import type { WebhookTrigger } from "@/lib/webhook/types";import { validateWebhook } from "@/lib/webhook/validate-webhook";import { updateWebhookSchema, WebhookSchema } from "@/lib/zod/schemas/webhooks";import { arrayEqual } from "@dub/utils";diff --git a/apps/web/lib/actions/enable-disable-webhook.ts b/apps/web/lib/actions/enable-disable-webhook.tsindex 7b8b5b7fb4..6358806043 100644--- a/apps/web/lib/actions/enable-disable-webhook.ts+++ b/apps/web/lib/actions/enable-disable-webhook.ts@@ -3,7 +3,7 @@import { prisma } from "@/lib/prisma";import { waitUntil } from "@vercel/functions";import * as z from "zod/v4";-import { syncWorkspaceWebhookStatus } from "../webhook/sync-webhook";+import { syncWorkspaceWebhookStatus } from "../webhook/click-webhook-workspaces";import { authActionClient } from "./safe-action";import { throwIfNoPermission } from "./throw-if-no-permission";diff --git a/apps/web/lib/integrations/common/ui/configure-webhook.tsx b/apps/web/lib/integrations/common/ui/configure-webhook.tsxindex 339b079107..e7d53d2dd8 100644--- a/apps/web/lib/integrations/common/ui/configure-webhook.tsx+++ b/apps/web/lib/integrations/common/ui/configure-webhook.tsx@@ -6,9 +6,9 @@ import { WebhookProps } from "@/lib/types";import {LINK_CLICK_WEBHOOK_TRIGGER,PROGRAM_LEVEL_WEBHOOK_TRIGGERS,- WebhookTrigger,WORKSPACE_LEVEL_WEBHOOK_TRIGGERS,} from "@/lib/webhook/constants";+import type { WebhookTrigger } from "@/lib/webhook/types";import { Link } from "@/ui/shared/icons";import {isWebhookTriggerSelectionInvalid,diff --git a/apps/web/lib/integrations/slack/transform.ts b/apps/web/lib/integrations/slack/transform.tsindex 227fbeeb7d..c6dc3fa23a 100644--- a/apps/web/lib/integrations/slack/transform.ts+++ b/apps/web/lib/integrations/slack/transform.ts@@ -3,8 +3,8 @@ import { getBountyRewardDescription } from "@/lib/bounty/rewards";import { APP_DOMAIN, COUNTRIES, currencyFormatter, truncate } from "@dub/utils";import { LinkWebhookEvent } from "dub/models/components";import * as z from "zod/v4";-import { WebhookTrigger } from "../../types";import { webhookPayloadSchema } from "../../webhook/schemas";+import type { WebhookTrigger } from "../../webhook/types";import {BountyEventWebhookPayload,ClickEventWebhookPayload,diff --git a/apps/web/lib/webhook/cache.ts b/apps/web/lib/webhook/cache.tsdeleted file mode 100644index d0573865b7..0000000000--- a/apps/web/lib/webhook/cache.ts+++ /dev/null@@ -1,72 +0,0 @@-// import { redis } from "@/lib/upstash";-// import { WebhookCacheProps } from "../types";-// import { hasLinkClickTrigger } from "./utils";--// const WEBHOOK_CACHE_KEY_PREFIX = "webhook";--// class WebhookCache {-// async mset(webhooks: WebhookCacheProps[]) {-// if (webhooks.length === 0) {-// return;-// }--// const pipeline = redis.pipeline();--// webhooks.map((webhook) => {-// pipeline.set(this._createKey(webhook.id), this._format(webhook));-// });--// return await pipeline.exec();-// }--// async set(webhook: WebhookCacheProps) {-// // We only cache the link level webhooks for now-// if (!hasLinkClickTrigger(webhook)) {-// return;-// }--// return await redis.set(this._createKey(webhook.id), this._format(webhook));-// }--// async mget(webhookIds: string[]) {-// const webhooks = await redis.mget<WebhookCacheProps[]>(-// webhookIds.map(this._createKey),-// );--// return webhooks.filter(Boolean);-// }--// async delete(webhookId: string) {-// return await redis.del(this._createKey(webhookId));-// }--// async deleteMany(webhookIds: string[]) {-// if (webhookIds.length === 0) {-// return;-// }--// const pipeline = redis.pipeline();--// webhookIds.map((webhookId) => {-// pipeline.del(this._createKey(webhookId));-// });--// return await pipeline.exec();-// }--// _format(webhook: WebhookCacheProps) {-// return {-// id: webhook.id,-// url: webhook.url,-// secret: webhook.secret,-// triggers: webhook.triggers,-// ...(webhook.disabledAt ? { disabledAt: webhook.disabledAt } : {}),-// };-// }--// _createKey(webhookId: string) {-// return `${WEBHOOK_CACHE_KEY_PREFIX}:${webhookId}`;-// }-// }--// export const webhookCache = new WebhookCache();diff --git a/apps/web/lib/webhook/click-webhook-workspaces.ts b/apps/web/lib/webhook/click-webhook-workspaces.tsindex 82ba78a00d..514186486f 100644--- a/apps/web/lib/webhook/click-webhook-workspaces.ts+++ b/apps/web/lib/webhook/click-webhook-workspaces.ts@@ -1,12 +1,13 @@import { prisma } from "@/lib/prisma";import { redis } from "@/lib/upstash";import { LINK_CLICK_WEBHOOK_TRIGGER } from "./constants";+import type { WebhookTrigger } from "./types";export const REDIS_KEY = "webhookClickWorkspaces";const TMP_REDIS_KEY = `${REDIS_KEY}:tmp`;class ClickWebhookWorkspaces {- async set(workspaceId: string) {+ async add(workspaceId: string) {return await redis.sadd(REDIS_KEY, workspaceId);}@@ -54,3 +55,37 @@ export const rebuildClickWebhookWorkspaces = async () => {};export const clickWebhookWorkspaces = new ClickWebhookWorkspaces();++// Synchronize the workspace's webhook status with its active webhooks.+export const syncWorkspaceWebhookStatus = async (workspaceId: string) => {+ const activeWebhooks = await prisma.webhook.findMany({+ where: {+ projectId: workspaceId,+ disabledAt: null,+ },+ select: {+ triggers: true,+ },+ });++ await prisma.project.update({+ where: {+ id: workspaceId,+ },+ data: {+ webhookEnabled: activeWebhooks.length > 0,+ },+ });++ const linkClickWebhooks = activeWebhooks.filter((webhook) =>+ (webhook.triggers as WebhookTrigger[])?.includes(+ LINK_CLICK_WEBHOOK_TRIGGER,+ ),+ );++ if (linkClickWebhooks.length > 0) {+ await clickWebhookWorkspaces.add(workspaceId);+ } else {+ await clickWebhookWorkspaces.remove(workspaceId);+ }+};diff --git a/apps/web/lib/webhook/constants.ts b/apps/web/lib/webhook/constants.tsindex 9d1d03bffc..1cf99251ae 100644--- a/apps/web/lib/webhook/constants.ts+++ b/apps/web/lib/webhook/constants.ts@@ -1,4 +1,5 @@import { WebhookScope } from "@prisma/client";+import type { WebhookTrigger } from "./types";export const WEBHOOK_SECRET_LENGTH = 16;@@ -27,7 +28,7 @@ export const PROGRAM_LEVEL_WEBHOOK_TRIGGERS = ["payout.confirmed",] as const;-export const WEBHOOK_SCOPE_OPTIONS = [+export const CLICK_WEBHOOK_SCOPE_OPTIONS = [{value: "all",label: "All links",@@ -55,8 +56,6 @@ export const WEBHOOK_TRIGGERS = [LINK_CLICK_WEBHOOK_TRIGGER,] as const;-export type WebhookTrigger = (typeof WEBHOOK_TRIGGERS)[number];-export const WEBHOOK_TRIGGER_DESCRIPTIONS: Record<WebhookTrigger, string> = {"link.created": "Link created","link.updated": "Link updated",diff --git a/apps/web/lib/webhook/create-webhook.ts b/apps/web/lib/webhook/create-webhook.tsindex bf682bbf49..050d1bcc45 100644--- a/apps/web/lib/webhook/create-webhook.ts+++ b/apps/web/lib/webhook/create-webhook.ts@@ -6,7 +6,7 @@ import { waitUntil } from "@vercel/functions";import * as z from "zod/v4";import { createWebhookSchema } from "../zod/schemas/webhooks";import { createWebhookSecret } from "./secret";-import { syncWorkspaceWebhookStatus } from "./sync-webhook";+import { syncWorkspaceWebhookStatus } from "./click-webhook-workspaces";export async function createWebhook({name,diff --git a/apps/web/lib/webhook/failure.ts b/apps/web/lib/webhook/failure.tsindex 073266c33b..feb4a88faa 100644--- a/apps/web/lib/webhook/failure.ts+++ b/apps/web/lib/webhook/failure.ts@@ -7,7 +7,7 @@ import {WEBHOOK_FAILURE_DISABLE_THRESHOLD,WEBHOOK_FAILURE_NOTIFY_THRESHOLDS,} from "./constants";-import { syncWorkspaceWebhookStatus } from "./sync-webhook";+import { syncWorkspaceWebhookStatus } from "./click-webhook-workspaces";export const handleWebhookFailure = async (webhookId: string) => {const webhook = await prisma.webhook.update({diff --git a/apps/web/lib/webhook/get-webhooks.ts b/apps/web/lib/webhook/get-webhooks.tsindex 50bbb8bd20..590cb0dbe6 100644--- a/apps/web/lib/webhook/get-webhooks.ts+++ b/apps/web/lib/webhook/get-webhooks.ts@@ -1,5 +1,5 @@import { prisma } from "@/lib/prisma";-import { WebhookTrigger } from "../types";+import type { WebhookTrigger } from "./types";interface GetWebhooksProps {workspaceId: string;diff --git a/apps/web/lib/webhook/publish.ts b/apps/web/lib/webhook/publish.tsindex c19978cd9b..a11659a1da 100644--- a/apps/web/lib/webhook/publish.ts+++ b/apps/web/lib/webhook/publish.ts@@ -1,8 +1,8 @@import { prisma } from "@/lib/prisma";import { Webhook } from "@prisma/client";-import { WebhookTrigger, WorkspaceProps } from "../types";+import { WorkspaceProps } from "../types";import { sendWebhooks } from "./qstash";-import { WebhookEventPayload } from "./types";+import type { WebhookEventPayload, WebhookTrigger } from "./types";// Send workspace level webhookexport const sendWorkspaceWebhook = async ({diff --git a/apps/web/lib/webhook/qstash.ts b/apps/web/lib/webhook/qstash.tsindex 0759aae408..f246ac75f1 100644--- a/apps/web/lib/webhook/qstash.ts+++ b/apps/web/lib/webhook/qstash.ts@@ -6,7 +6,7 @@ import * as z from "zod/v4";import { formatEventForSegment } from "../integrations/segment/transform";import { createSegmentBasicAuthHeader } from "../integrations/segment/utils";import { formatEventForSlack } from "../integrations/slack/transform";-import { WebhookTrigger } from "../types";+import type { WebhookTrigger } from "./types";import { createWebhookSignature } from "./signature";import { prepareWebhookPayload } from "./transform";import { WebhookEventPayload } from "./types";diff --git a/apps/web/lib/webhook/sample-events/payload.ts b/apps/web/lib/webhook/sample-events/payload.tsindex 114ba72ba7..546af93656 100644--- a/apps/web/lib/webhook/sample-events/payload.ts+++ b/apps/web/lib/webhook/sample-events/payload.ts@@ -1,4 +1,4 @@-import { WebhookTrigger } from "@/lib/types";+import type { WebhookTrigger } from "@/lib/webhook/types";import bountyCreated from "./bounty-created.json";import bountyUpdated from "./bounty-updated.json";import commissionCreated from "./commission-created.json";diff --git a/apps/web/lib/webhook/sync-webhook.ts b/apps/web/lib/webhook/sync-webhook.tsdeleted file mode 100644index 03f6fd4466..0000000000--- a/apps/web/lib/webhook/sync-webhook.ts+++ /dev/null@@ -1,37 +0,0 @@-import { prisma } from "@/lib/prisma";-import { clickWebhookWorkspaces } from "./click-webhook-workspaces";-import { LINK_CLICK_WEBHOOK_TRIGGER, WebhookTrigger } from "./constants";--// Synchronize the workspace's webhook status with its active webhooks.-export const syncWorkspaceWebhookStatus = async (workspaceId: string) => {- const activeWebhooks = await prisma.webhook.findMany({- where: {- projectId: workspaceId,- disabledAt: null,- },- select: {- triggers: true,- },- });-- await prisma.project.update({- where: {- id: workspaceId,- },- data: {- webhookEnabled: activeWebhooks.length > 0,- },- });-- const linkClickWebhooks = activeWebhooks.filter((webhook) =>- (webhook.triggers as WebhookTrigger[])?.includes(- LINK_CLICK_WEBHOOK_TRIGGER,- ),- );-- if (linkClickWebhooks.length > 0) {- await clickWebhookWorkspaces.set(workspaceId);- } else {- await clickWebhookWorkspaces.remove(workspaceId);- }-};diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.tsindex 3f948a6346..55e75ec44b 100644--- a/apps/web/lib/webhook/transform.ts+++ b/apps/web/lib/webhook/transform.ts@@ -5,7 +5,8 @@ import {import { nanoid, toCamelCase } from "@dub/utils";import { ExpandedLink, transformLink } from "../api/links/utils/transform-link";import { generateRandomName } from "../names";-import { ClickEventTB, WebhookTrigger } from "../types";+import { ClickEventTB } from "../types";+import type { WebhookTrigger } from "./types";import { clickEventSchema } from "../zod/schemas/clicks";import { WEBHOOK_EVENT_ID_PREFIX } from "./constants";import { leadWebhookEventSchema, saleWebhookEventSchema } from "./schemas";diff --git a/apps/web/lib/webhook/types.ts b/apps/web/lib/webhook/types.tsindex 3f08404e7c..b25707c8a0 100644--- a/apps/web/lib/webhook/types.ts+++ b/apps/web/lib/webhook/types.ts@@ -5,12 +5,15 @@ import { linkEventSchema } from "../zod/schemas/links";import { EnrolledPartnerSchema } from "../zod/schemas/partners";import { payoutWebhookEventSchema } from "../zod/schemas/payouts";import { partnerApplicationWebhookSchema } from "../zod/schemas/program-application";+import { WEBHOOK_TRIGGERS } from "./constants";import {clickWebhookEventSchema,leadWebhookEventSchema,saleWebhookEventSchema,} from "./schemas";+export type WebhookTrigger = (typeof WEBHOOK_TRIGGERS)[number];+export type ClickEventWebhookPayload = z.infer<typeof clickWebhookEventSchema>;export type LeadEventWebhookPayload = z.infer<typeof leadWebhookEventSchema>;diff --git a/apps/web/lib/webhook/utils.ts b/apps/web/lib/webhook/utils.tsindex 55fe7684da..556f362f4e 100644--- a/apps/web/lib/webhook/utils.ts+++ b/apps/web/lib/webhook/utils.ts@@ -1,5 +1,6 @@import { Webhook, WebhookReceiver } from "@prisma/client";-import { LINK_CLICK_WEBHOOK_TRIGGER, WebhookTrigger } from "./constants";+import { LINK_CLICK_WEBHOOK_TRIGGER } from "./constants";+import type { WebhookTrigger } from "./types";const webhookReceivers: Record<string, WebhookReceiver> = {"zapier.com": "zapier",diff --git a/apps/web/lib/zod/schemas/webhooks.ts b/apps/web/lib/zod/schemas/webhooks.tsindex 6afa740801..b1a486aa47 100644--- a/apps/web/lib/zod/schemas/webhooks.ts+++ b/apps/web/lib/zod/schemas/webhooks.ts@@ -1,4 +1,5 @@-import { WEBHOOK_TRIGGERS, WebhookTrigger } from "@/lib/webhook/constants";+import { WEBHOOK_TRIGGERS } from "@/lib/webhook/constants";+import { WebhookTrigger } from "@/lib/webhook/types";import { WebhookScope } from "@prisma/client";import * as z from "zod/v4";import { parseUrlSchema } from "./utils";diff --git a/apps/web/tests/webhooks/index.test.ts b/apps/web/tests/webhooks/index.test.tsindex 905868c6a3..da49eba255 100644--- a/apps/web/tests/webhooks/index.test.ts+++ b/apps/web/tests/webhooks/index.test.ts@@ -1,5 +1,5 @@import { qstash } from "@/lib/cron";-import { WebhookTrigger } from "@/lib/types";+import type { WebhookTrigger } from "@/lib/webhook/types";import { WEBHOOK_TRIGGERS } from "@/lib/webhook/constants";import { sendWebhooks } from "@/lib/webhook/qstash";import { samplePayload } from "@/lib/webhook/sample-events/payload";diff --git a/apps/web/ui/modals/send-test-webhook-modal.tsx b/apps/web/ui/modals/send-test-webhook-modal.tsxindex cbc259b715..b8281e25f3 100644--- a/apps/web/ui/modals/send-test-webhook-modal.tsx+++ b/apps/web/ui/modals/send-test-webhook-modal.tsx@@ -1,6 +1,7 @@import { sendTestWebhookEvent } from "@/lib/actions/send-test-webhook";import useWorkspace from "@/lib/swr/use-workspace";-import { WebhookProps, WebhookTrigger } from "@/lib/types";+import { WebhookProps } from "@/lib/types";+import type { WebhookTrigger } from "@/lib/webhook/types";import { WEBHOOK_TRIGGER_DESCRIPTIONS } from "@/lib/webhook/constants";import { Button, Combobox, ComboboxOption, Modal } from "@dub/ui";import { useAction } from "next-safe-action/hooks";diff --git a/apps/web/ui/webhooks/webhook-trigger-selector.tsx b/apps/web/ui/webhooks/webhook-trigger-selector.tsxindex 959c049c82..f8cdec53a8 100644--- a/apps/web/ui/webhooks/webhook-trigger-selector.tsx+++ b/apps/web/ui/webhooks/webhook-trigger-selector.tsx@@ -2,11 +2,11 @@import useWorkspace from "@/lib/swr/use-workspace";import {+ CLICK_WEBHOOK_SCOPE_OPTIONS,LINK_CLICK_WEBHOOK_TRIGGER,- WEBHOOK_SCOPE_OPTIONS,WEBHOOK_TRIGGER_DESCRIPTIONS,- WebhookTrigger,} from "@/lib/webhook/constants";+import type { WebhookTrigger } from "@/lib/webhook/types";import { Checkbox, RadioGroup, RadioGroupItem } from "@dub/ui";import { arrayEqual, cn, fetcher } from "@dub/utils";import { WebhookScope } from "@prisma/client";@@ -170,7 +170,7 @@ export function WebhookTriggerSelector({className="flex flex-col gap-2"disabled={disabled}>- {WEBHOOK_SCOPE_OPTIONS.map((option) => {+ {CLICK_WEBHOOK_SCOPE_OPTIONS.map((option) => {const isSelected = scope === option.value;return (