The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix duplicate customer errors on /track/sale (#4033)
Pedro Ladeirabe4867cb33ca
diff --git a/apps/web/lib/api/conversions/track-sale.ts b/apps/web/lib/api/conversions/track-sale.tsindex 7fa895f3b6..fc74f3470f 100644--- a/apps/web/lib/api/conversions/track-sale.ts+++ b/apps/web/lib/api/conversions/track-sale.ts@@ -31,7 +31,7 @@ import {trackSaleResponseSchema,} from "@/lib/zod/schemas/sales";import { nanoid, R2_URL } from "@dub/utils";-import { Customer } from "@prisma/client";+import { Customer, Prisma } from "@prisma/client";import { waitUntil } from "@vercel/functions";import * as z from "zod/v4";import { createId } from "../create-id";@@ -62,6 +62,7 @@ export const trackSale = async ({let existingCustomer: Customer | null = null;let newCustomer: Customer | null = null;let leadEventData: LeadEventTB | null = null;+ let shouldTrackDirectSaleLead = false;// Return idempotent response if invoiceId is already processedif (invoiceId) {@@ -139,9 +140,9 @@ export const trackSale = async ({}}- // If no existing customer is found and a click event is found, create a new customer (for direct sale tracking)+ // Direct sale tracking: create the customer from the click event.+ // On concurrent requests, fall back to fetching the existing row (P2002) instead of failing.if (!existingCustomer && clickData) {- // Create a new customerconst link = await prisma.link.findUnique({where: {id: clickData.link_id,@@ -174,32 +175,55 @@ export const trackSale = async ({});}- const finalCustomerId = createId({ prefix: "cus_" });+ const newCustomerId = createId({ prefix: "cus_" });const finalCustomerName =customerName || customerEmail || generateRandomName();const finalCustomerAvatar =customerAvatar && !isStored(customerAvatar)- ? `${R2_URL}/customers/${finalCustomerId}/avatar_${nanoid(7)}`+ ? `${R2_URL}/customers/${newCustomerId}/avatar_${nanoid(7)}`: customerAvatar;- newCustomer = await prisma.customer.create({- data: {- id: finalCustomerId,- name: finalCustomerName,- email: customerEmail,- avatar: finalCustomerAvatar,- externalId: customerExternalId,- linkId: clickData.link_id,- clickId: clickData.click_id,- country: clickData.country,- projectId: workspace.id,- projectConnectId: workspace.stripeConnectId,- clickedAt: new Date(clickData.timestamp + "Z"),- },- });+ try {+ newCustomer = await prisma.customer.create({+ data: {+ id: newCustomerId,+ name: finalCustomerName,+ email: customerEmail,+ avatar: finalCustomerAvatar,+ externalId: customerExternalId,+ linkId: clickData.link_id,+ clickId: clickData.click_id,+ country: clickData.country,+ projectId: workspace.id,+ projectConnectId: workspace.stripeConnectId,+ clickedAt: new Date(clickData.timestamp + "Z"),+ },+ });+ } catch (error) {+ if (+ error instanceof Prisma.PrismaClientKnownRequestError &&+ error.code === "P2002"+ ) {+ existingCustomer = await prisma.customer.findUniqueOrThrow({+ where: {+ projectId_externalId: {+ projectId: workspace.id,+ externalId: customerExternalId,+ },+ },+ });+ } else {+ throw error;+ }+ }- if (customerAvatar && !isStored(customerAvatar) && finalCustomerAvatar) {- // persist customer avatar to R2 if it's not already stored+ // Persist customer avatar to R2 if it's not already stored+ if (+ customerAvatar &&+ !isStored(customerAvatar) &&+ finalCustomerAvatar &&+ newCustomer+ ) {waitUntil(storage.upload({@@ -212,26 +236,71 @@ export const trackSale = async ({}).catch(async (error) => {console.error("Error persisting customer avatar to R2", error);+// if the avatar fails to upload to R2, set the avatar to null in the databaseif (newCustomer) {await prisma.customer.update({- where: { id: newCustomer.id },- data: { avatar: null },+ where: {+ id: newCustomer.id,+ },+ data: {+ avatar: null,+ },});}}),);}- leadEventData = {- ...clickData,- event_id: nanoid(16),- // if leadEventName is provided, use it- // otherwise use "Direct sale tracking lead event" (since it's for direct sale tracking)- event_name: leadEventName ?? "Direct sale tracking lead event",- customer_id: newCustomer.id,- metadata: metadata ? JSON.stringify(metadata) : "",- };+ // if leadEventName is provided, use it+ // otherwise use "Direct sale tracking lead event" (since it's for direct sale tracking)+ const finalLeadEventName =+ leadEventName ?? "Direct sale tracking lead event";++ if (newCustomer) {+ leadEventData = {+ ...clickData,+ event_id: nanoid(16),+ event_name: finalLeadEventName,+ customer_id: newCustomer.id,+ metadata: metadata ? JSON.stringify(metadata) : "",+ };+ } else if (existingCustomer) {+ const leadEvent = await getLeadEvent({+ customerId: existingCustomer.id,+ eventName: leadEventName,+ });++ leadEventData = leadEvent+ ? {+ ...leadEvent,+ ...clickData,+ }+ : {+ ...clickData,+ event_id: nanoid(16),+ event_name: finalLeadEventName,+ customer_id: existingCustomer.id,+ metadata: metadata ? JSON.stringify(metadata) : "",+ };+ }++ // Deduplicate lead events across concurrent direct sale requests+ if (leadEventData) {+ const cacheKey = `directSaleTrackLead:${workspace.id}:${customerExternalId}:${finalLeadEventName.toLowerCase().replaceAll(" ", "-")}`;+ const cachedLeadEvent = await redis.set(+ cacheKey,+ {+ timestamp: Date.now(),+ },+ {+ ex: 30, // 30 seconds+ nx: true,+ },+ );++ shouldTrackDirectSaleLead = cachedLeadEvent !== null;+ }}const customer = existingCustomer ?? newCustomer;@@ -246,11 +315,11 @@ export const trackSale = async ({}const [_, trackedSale] = await Promise.all([- newCustomer &&+ shouldTrackDirectSaleLead &&_trackLead({workspace,leadEventData,- customer: newCustomer,+ customer,}),_trackSale({