The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Move `lead.created` and `sale.created` to workspace level webhook
Kiran K1960a16da494
diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex d299903c1d..dd49a61ef0 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -5,7 +5,7 @@ import { generateRandomName } from "@/lib/names";import { prismaEdge } from "@/lib/prisma/edge";import { getClickEvent, recordCustomer, recordLead } from "@/lib/tinybird";import { ratelimit } from "@/lib/upstash";-import { sendLinkWebhookOnEdge } from "@/lib/webhook/publish-edge";+import { sendWorkspaceWebhookOnEdge } from "@/lib/webhook/publish-edge";import { transformLeadEventData } from "@/lib/webhook/transform";import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import {@@ -53,15 +53,15 @@ export const POST = withWorkspaceEdge(});}+ const finalCustomerName =+ customerName || customerEmail || generateRandomName();+waitUntil((async () => {const clickData = clickEventSchemaTB.omit({ timestamp: true }).parse(clickEvent.data[0]);- const finalCustomerName =- customerName || customerEmail || generateRandomName();-// Find customer or create if not existsconst customer = await prismaEdge.customer.upsert({where: {@@ -135,10 +135,10 @@ export const POST = withWorkspaceEdge(customerAvatar: customer.avatar,});- await sendLinkWebhookOnEdge({+ await sendWorkspaceWebhookOnEdge({trigger: "lead.created",- linkId: link.id,data: lead,+ workspace,});})(),);@@ -149,7 +149,7 @@ export const POST = withWorkspaceEdge(},customer: {id: externalId,- name: customerName,+ name: finalCustomerName,email: customerEmail,avatar: customerAvatar,},@@ -161,7 +161,7 @@ export const POST = withWorkspaceEdge(// for backwards compatibility – will remove soonclickId,customerId: externalId,- customerName: customerName,+ customerName: finalCustomerName,customerEmail: customerEmail,customerAvatar: customerAvatar,});diff --git a/apps/web/app/api/track/sale/route.ts b/apps/web/app/api/track/sale/route.tsindex f77a213272..0374ec4c0a 100644--- a/apps/web/app/api/track/sale/route.ts+++ b/apps/web/app/api/track/sale/route.ts@@ -3,7 +3,7 @@ import { parseRequestBody } from "@/lib/api/utils";import { withWorkspaceEdge } from "@/lib/auth/workspace-edge";import { prismaEdge } from "@/lib/prisma/edge";import { getLeadEvent, recordSale } from "@/lib/tinybird";-import { sendLinkWebhookOnEdge } from "@/lib/webhook/publish-edge";+import { sendWorkspaceWebhookOnEdge } from "@/lib/webhook/publish-edge";import { transformSaleEventData } from "@/lib/webhook/transform";import { clickEventSchemaTB } from "@/lib/zod/schemas/clicks";import {@@ -118,10 +118,10 @@ export const POST = withWorkspaceEdge(customerAvatar: customer.avatar,});- await sendLinkWebhookOnEdge({+ await sendWorkspaceWebhookOnEdge({trigger: "sale.created",- linkId: link.id,data: sale,+ workspace,});})(),);diff --git a/apps/web/lib/webhook/constants.ts b/apps/web/lib/webhook/constants.tsindex 1c4789230b..b249eeca60 100644--- a/apps/web/lib/webhook/constants.ts+++ b/apps/web/lib/webhook/constants.ts@@ -12,14 +12,12 @@ export const WORKSPACE_LEVEL_WEBHOOK_TRIGGERS = ["link.created","link.updated","link.deleted",-] as const;--export const LINK_LEVEL_WEBHOOK_TRIGGERS = [- "link.clicked","lead.created","sale.created",] as const;+export const LINK_LEVEL_WEBHOOK_TRIGGERS = ["link.clicked"] as const;+export const WEBHOOK_TRIGGERS = [...WORKSPACE_LEVEL_WEBHOOK_TRIGGERS,...LINK_LEVEL_WEBHOOK_TRIGGERS,diff --git a/apps/web/lib/webhook/publish-edge.ts b/apps/web/lib/webhook/publish-edge.tsindex c30bfbd6d7..5f443c08fb 100644--- a/apps/web/lib/webhook/publish-edge.ts+++ b/apps/web/lib/webhook/publish-edge.ts@@ -1,40 +1,37 @@import { prismaEdge } from "../prisma/edge";-import { WebhookTrigger } from "../types";+import { WebhookTrigger, WorkspaceProps } from "../types";import { sendWebhooks } from "./qstash";import { LeadEventDataProps, SaleEventDataProps } from "./types";-export const sendLinkWebhookOnEdge = async ({+// Send workspace level webhook+export const sendWorkspaceWebhookOnEdge = async ({trigger,- linkId,+ workspace,data,}: {trigger: WebhookTrigger;- linkId: string;+ workspace: Pick<WorkspaceProps, "id" | "webhookEnabled">;data: LeadEventDataProps | SaleEventDataProps;}) => {- const linkWebhooks = await prismaEdge.linkWebhook.findMany({+ console.log(workspace)++ if (!workspace.webhookEnabled) {+ return;+ }++ const webhooks = await prismaEdge.webhook.findMany({where: {- linkId,- webhook: {- triggers: {- array_contains: [trigger],- },+ projectId: workspace.id,+ triggers: {+ array_contains: [trigger],},},- include: {- webhook: {- select: {- id: true,- url: true,- secret: true,- },- },+ select: {+ id: true,+ url: true,+ secret: true,},});- return sendWebhooks({- trigger,- webhooks: linkWebhooks.map(({ webhook }) => webhook),- data,- });+ return sendWebhooks({ trigger, webhooks, data });};diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.tsindex 951e020d78..57b552d641 100644--- a/apps/web/lib/zod/schemas/leads.ts+++ b/apps/web/lib/zod/schemas/leads.ts@@ -31,21 +31,25 @@ export const trackLeadRequestSchema = z.object({.string().max(100).nullish()+ .default(null).describe("Name of the customer in the client's app."),customerEmail: z.string().email().max(100).nullish()+ .default(null).describe("Email of the customer in the client's app."),customerAvatar: z.string().max(100).nullish()+ .default(null).describe("Avatar of the customer in the client's app."),metadata: z.record(z.unknown()).nullish()+ .default(null).describe("Additional metadata to be stored with the lead event"),});