The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add receiver endpoint
Kiran K068598374234
diff --git a/apps/web/app/api/stripe/connect/webhook/customer-created.ts b/apps/web/app/api/stripe/connect/webhook/customer-created.tsindex 29bf97e6bc..370e45afaa 100644--- a/apps/web/app/api/stripe/connect/webhook/customer-created.ts+++ b/apps/web/app/api/stripe/connect/webhook/customer-created.ts@@ -24,6 +24,18 @@ export async function customerCreated(event: Stripe.Event) {return `Click event with ID ${clickId} not found, skipping...`;}+ // Find link+ const linkId = clickEvent.data[0].link_id;+ const link = await prisma.link.findUnique({+ where: {+ id: linkId,+ },+ });++ if (!link) {+ return `Link with ID ${linkId} not found, skipping...`;+ }+// Check the customer is not already created// Find customer using projectConnectId and externalId (the customer's ID in the client app)const customerFound = await prisma.customer.findFirst({@@ -82,7 +94,7 @@ export async function customerCreated(event: Stripe.Event) {// update link leads countprisma.link.update({where: {- id: clickData.link_id,+ id: linkId,},data: {leads: {@@ -95,9 +107,10 @@ export async function customerCreated(event: Stripe.Event) {waitUntil((async () => {sendLinkWebhook("lead.created", {- linkId: clickData.link_id,+ linkId,data: {...leadData,+ ...link,customerId: customer.id,customerName: customer.name,customerEmail: customer.email,diff --git a/apps/web/app/api/track/lead/route.ts b/apps/web/app/api/track/lead/route.tsindex f334bf6a09..5192b37623 100644--- a/apps/web/app/api/track/lead/route.ts+++ b/apps/web/app/api/track/lead/route.ts@@ -52,6 +52,20 @@ export const POST = withWorkspaceEdge(});}+ const linkId = clickEvent.data[0].link_id;+ const link = await prismaEdge.link.findUnique({+ where: {+ id: linkId,+ },+ });++ if (!link) {+ throw new DubApiError({+ code: "not_found",+ message: `Link not found for linkId: ${linkId}`,+ });+ }+const clickData = clickEventSchemaTB.omit({ timestamp: true }).parse(clickEvent.data[0]);@@ -141,10 +155,11 @@ export const POST = withWorkspaceEdge(waitUntil(sendLinkWebhookOnEdge("lead.created", {- linkId: clickData.link_id,+ linkId,data: {...response,...clickData,+ ...link,},}),);diff --git a/apps/web/app/api/webhooks/receive/route.ts b/apps/web/app/api/webhooks/receive/route.tsnew file mode 100644index 0000000000..e0c7244f79--- /dev/null+++ b/apps/web/app/api/webhooks/receive/route.ts@@ -0,0 +1,41 @@+// TODO:+// Move this to proper route path+// Verify the Dub-Signature header++import { prisma } from "@/lib/prisma";+import { LeadEventData } from "@/lib/webhook/types";+import { webhookPayloadSchema } from "@/lib/zod/schemas/webhooks";++// POST /api/webhooks/receive - receive link-level webhooks from Dub+export const POST = async (req: Request) => {+ const body = await req.json();+ const { event, data } = webhookPayloadSchema.parse(body);++ // A new lead was created on Dub+ if (event === "lead.created") {+ const lead = data as LeadEventData;+ const workspaceId = lead.link.externalId?.replace("ws_", "");++ if (!workspaceId) {+ console.error("Workspace ID not found", { lead });+ return new Response("OK");+ }++ // Update the referrer's workspace+ await prisma.project.update({+ where: {+ id: workspaceId,+ },+ data: {+ referredSignups: {+ increment: 1,+ },+ usageLimit: {+ increment: 500,+ },+ },+ });+ }++ return new Response("OK");+};diff --git a/apps/web/lib/webhook/schemas.ts b/apps/web/lib/webhook/schemas.tsindex a1a420713c..b75867866a 100644--- a/apps/web/lib/webhook/schemas.ts+++ b/apps/web/lib/webhook/schemas.ts@@ -45,7 +45,12 @@ export const leadEventSchema = z.object({customerEmail: z.string().nullable(),customerAvatar: z.string().nullable(),click: clickEventSchema.partial(),- link: z.object({ id: z.string() }),+ link: z.object({+ id: z.string(),+ externalId: z.string().nullable(),+ domain: z.string(),+ key: z.string(),+ }),});export const saleEventSchema = z.object({diff --git a/apps/web/lib/webhook/signature.ts b/apps/web/lib/webhook/signature.tsindex bae93260ca..40e6505d5d 100644--- a/apps/web/lib/webhook/signature.ts+++ b/apps/web/lib/webhook/signature.ts@@ -1,11 +1,11 @@// Create a signature for a webhook request-export const createWebhookSignature = async (secret: string, rawBody: any) => {+export const createWebhookSignature = async (secret: string, body: any) => {if (!secret) {throw new Error("A secret must be provided to create a webhook signature.");}const keyData = new TextEncoder().encode(secret);- const messageData = new TextEncoder().encode(rawBody);+ const messageData = new TextEncoder().encode(body);const cryptoKey = await crypto.subtle.importKey("raw",diff --git a/apps/web/lib/webhook/transform.ts b/apps/web/lib/webhook/transform.tsindex 6a2cb851d6..cbe6feb813 100644--- a/apps/web/lib/webhook/transform.ts+++ b/apps/web/lib/webhook/transform.ts@@ -63,6 +63,9 @@ export const transformLead = (data: any) => {},link: {id: lead.linkId,+ externalId: lead.externalId,+ domain: lead.domain,+ key: lead.key,},});};