The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add `link` to trackLeadResponseSchema
Steven Tey256ecf8512b7
diff --git a/apps/web/lib/api/conversions/track-lead.ts b/apps/web/lib/api/conversions/track-lead.tsindex f408c62e7c..8f01d5a847 100644--- a/apps/web/lib/api/conversions/track-lead.ts+++ b/apps/web/lib/api/conversions/track-lead.ts@@ -15,7 +15,7 @@ import {trackLeadResponseSchema,} from "@/lib/zod/schemas/leads";import { prisma } from "@dub/prisma";-import { WorkflowTrigger } from "@dub/prisma/client";+import { Link, WorkflowTrigger } from "@dub/prisma/client";import { nanoid, R2_URL } from "@dub/utils";import { waitUntil } from "@vercel/functions";import { z } from "zod";@@ -48,6 +48,7 @@ export const trackLead = async ({},},});+ let link: Link | null = null;// if clickId is an empty string, use the existing customer's clickId if it exists// otherwise, throw an error (this is for mode="deferred" lead tracking)@@ -136,13 +137,10 @@ export const trackLead = async ({}// get the referral link from the from the clickData- const link = await prisma.link.findUnique({+ link = await prisma.link.findUnique({where: {id: clickData.link_id,},- select: {- projectId: true,- },});if (!link) {@@ -273,7 +271,7 @@ export const trackLead = async ({// - send lead.created webhookif (mode !== "deferred") {- const [link, _project] = await Promise.all([+ const [updatedLink, _project] = await Promise.all([// update link leads countprisma.link.update({where: {@@ -299,6 +297,7 @@ export const trackLead = async ({},}),]);+ link = updatedLink; // update the link variable to the latest versionif (link.programId && link.partnerId && customer) {await createPartnerCommission({@@ -342,6 +341,7 @@ export const trackLead = async ({click: {id: clickId,},+ link,customer: customer ?? {name: finalCustomerName,email: customerEmail || null,diff --git a/apps/web/lib/api/conversions/track-sale.ts b/apps/web/lib/api/conversions/track-sale.tsindex bd4a97987b..d903ed2a07 100644--- a/apps/web/lib/api/conversions/track-sale.ts+++ b/apps/web/lib/api/conversions/track-sale.ts@@ -59,9 +59,10 @@ export const trackSale = async ({// Return idempotent response if invoiceId is already processedif (invoiceId) {- const [oldKeyValue, newKeyValue] = await redis.mget([- `dub_sale_events:invoiceId:${invoiceId}`,+ // TODO: remove oldKeyValue stuff after 7 days (on Sep 18)+ const [newKeyValue, oldKeyValue] = await redis.mget([`trackSale:${workspace.id}:invoiceId:${invoiceId}`,+ `dub_sale_events:invoiceId:${invoiceId}`,]);if (newKeyValue) {@@ -562,7 +563,7 @@ const _trackSale = async ({`trackSale:${workspace.id}:invoiceId:${invoiceId}`,trackSaleResponse,{- ex: 60 * 60 * 24 * 7,+ ex: 60 * 60 * 24 * 7, // cache for 1 week},);}diff --git a/apps/web/lib/zod/schemas/leads.ts b/apps/web/lib/zod/schemas/leads.tsindex 0d8d9f9ffd..77fb74c646 100644--- a/apps/web/lib/zod/schemas/leads.ts+++ b/apps/web/lib/zod/schemas/leads.ts@@ -2,7 +2,7 @@ import z from "@/lib/zod";import { clickEventSchema, clickEventSchemaTB } from "./clicks";import { CustomerSchema } from "./customers";import { commonDeprecatedEventFields } from "./deprecated";-import { linkEventSchema } from "./links";+import { linkEventSchema, LinkSchema } from "./links";export const trackLeadRequestSchema = z.object({clickId: z@@ -76,6 +76,17 @@ export const trackLeadResponseSchema = z.object({click: z.object({id: z.string(),}),+ link: LinkSchema.pick({+ id: true,+ domain: true,+ key: true,+ shortLink: true,+ url: true,+ partnerId: true,+ programId: true,+ tenantId: true,+ externalId: true,+ }).nullish(),customer: z.object({name: z.string().nullable(),email: z.string().nullable(),diff --git a/apps/web/tests/tracks/track-lead-client.test.ts b/apps/web/tests/tracks/track-lead-client.test.tsindex 0c0896219f..01a732664b 100644--- a/apps/web/tests/tracks/track-lead-client.test.ts+++ b/apps/web/tests/tracks/track-lead-client.test.ts@@ -23,7 +23,7 @@ describe("POST /track/lead/client", async () => {const clickId = clickResponse.data.clickId;const customer = randomCustomer();- test("track a lead (with clickId from a prior /track/lead/client request)", async () => {+ test("track a lead on the client (with clickId from a prior /track/click request)", async () => {const response = await fetch(`${env.E2E_BASE_URL}/api/track/lead/client`, {method: "POST",headers: {@@ -48,6 +48,7 @@ describe("POST /track/lead/client", async () => {click: {id: clickId,},+ link: leadResponse.link,customer: customer,});});diff --git a/apps/web/tests/tracks/track-lead.test.ts b/apps/web/tests/tracks/track-lead.test.tsindex 40075ab126..a1e7a83c21 100644--- a/apps/web/tests/tracks/track-lead.test.ts+++ b/apps/web/tests/tracks/track-lead.test.ts@@ -19,6 +19,7 @@ const expectValidLeadResponse = ({click: {id: clickId,},+ link: response.data.link,customer,});};@@ -174,43 +175,43 @@ describe("POST /track/lead", async () => {});test("track a lead with `externalId` (backward compatibility)", async () => {- const customer4 = randomCustomer();+ const customer5 = randomCustomer();const response = await http.post<TrackLeadResponse>({path: "/track/lead",body: {clickId: trackedClickId,- externalId: customer4.externalId,+ externalId: customer5.externalId,eventName: "Signup",- customerName: customer4.name,- customerEmail: customer4.email,- customerAvatar: customer4.avatar,+ customerName: customer5.name,+ customerEmail: customer5.email,+ customerAvatar: customer5.avatar,},});expectValidLeadResponse({response,- customer: customer4,+ customer: customer5,clickId: trackedClickId,});});test("track a lead with `customerId` (backward compatibility)", async () => {- const customer5 = randomCustomer();+ const customer6 = randomCustomer();const response = await http.post<TrackLeadResponse>({path: "/track/lead",body: {clickId: trackedClickId,- customerId: customer5.externalId,+ customerId: customer6.externalId,eventName: "Signup",- customerName: customer5.name,- customerEmail: customer5.email,- customerAvatar: customer5.avatar,+ customerName: customer6.name,+ customerEmail: customer6.email,+ customerAvatar: customer6.avatar,},});expectValidLeadResponse({response,- customer: customer5,+ customer: customer6,clickId: trackedClickId,});});diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.tsindex f513d0abdd..84bcd4b226 100644--- a/apps/web/tests/tracks/track-sale.test.ts+++ b/apps/web/tests/tracks/track-sale.test.ts@@ -91,6 +91,7 @@ describe("POST /track/sale", async () => {},});+ // should return the same response since it's idempotentexpectValidSaleResponse(response, sale);});