The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
code improvements
Pedro Ladeirafcf9b794a995
diff --git a/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts b/apps/web/app/(ee)/api/customers/[id]/reattribute/route.tsindex ebd0f20dca..f4aaaa492b 100644--- a/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts+++ b/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts@@ -1,13 +1,10 @@+import { isFirstConversion } from "@/lib/analytics/is-first-conversion";import { createId } from "@/lib/api/create-id";import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";import {CUSTOMER_EVENTS_LIMIT,- computeConversionFlags,- customerEventsExceedLimit,getCustomerReattributeEvents,- isRetiredReattributeStub,recreateCustomerForReattribution,- shouldRecreateCustomer,} from "@/lib/api/customers/reattribute-customer";import { transformCustomer } from "@/lib/api/customers/transform-customer";import { DubApiError } from "@/lib/api/errors";@@ -50,7 +47,12 @@ export const POST = withWorkspace(},);- if (isRetiredReattributeStub(customer)) {+ if (+ customer.externalId?.startsWith("dummy_") &&+ customer.partnerId == null &&+ customer.linkId == null &&+ customer.programId == null+ ) {throw new DubApiError({code: "bad_request",message:@@ -106,7 +108,7 @@ export const POST = withWorkspace(}),]);- if (customerEventsExceedLimit(events.length)) {+ if (events.length >= CUSTOMER_EVENTS_LIMIT) {throw new DubApiError({code: "unprocessable_entity",message: `This customer has too many events to reattribute (limit ${CUSTOMER_EVENTS_LIMIT}).`,@@ -114,12 +116,10 @@ export const POST = withWorkspace(}if (- !shouldRecreateCustomer({- eventCount: events.length,- commissionCount,- clickId: customer.clickId,- sales: customer.sales,- })+ events.length === 0 &&+ commissionCount === 0 &&+ !customer.clickId &&+ customer.sales === 0) {const updatedCustomer = await prisma.customer.update({where: {@@ -149,11 +149,9 @@ export const POST = withWorkspace(const newCustomerId = createId({ prefix: "cus_" });const newClickId = nanoid(16);- const { incrementConversions, decrementConversions } =- computeConversionFlags({- customer,- newLinkId: link.id,- });+ const incrementConversions =+ customer.sales > 0 && isFirstConversion({ customer, linkId: link.id });+ const decrementConversions = customer.sales > 0 && Boolean(customer.linkId);await recreateCustomerForReattribution({customer,diff --git a/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts b/apps/web/app/(ee)/api/workflows/reattribute-customer/route.tsindex 335f736631..9e725351db 100644--- a/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts+++ b/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts@@ -1,7 +1,6 @@import {createClawbackAndReplacementCommissions,decrementOldLinkStats,- deleteOldCustomerTinybirdEvents,incrementNewLinkStats,loadReattributeEventPlan,reingestCustomerEvents,@@ -9,6 +8,7 @@ import {} from "@/lib/api/customers/reattribute-customer";import { logger } from "@/lib/axiom/server";import { prisma } from "@/lib/prisma";+import { deleteTinybirdCustomerEvents } from "@/lib/tinybird/delete-events";import { reattributeCustomerWorkflowSchema } from "@/lib/zod/schemas/customers";import { WorkflowRetryAfterError } from "@upstash/workflow";import { serve } from "@upstash/workflow/nextjs";@@ -126,9 +126,9 @@ export const { POST } = serve<Input>(await context.run("delete-old-events", async () => {try {- await deleteOldCustomerTinybirdEvents({- oldCustomerId: input.oldCustomerId,- oldClickId: input.oldClickId,+ await deleteTinybirdCustomerEvents({+ customerId: input.oldCustomerId,+ clickId: input.oldClickId,});return logAndReturn({ deleted: true });diff --git a/apps/web/lib/api/customers/reattribute-customer-utils.ts b/apps/web/lib/api/customers/reattribute-customer-utils.tsdeleted file mode 100644index 99de4e913f..0000000000--- a/apps/web/lib/api/customers/reattribute-customer-utils.ts+++ /dev/null@@ -1,175 +0,0 @@-export const CUSTOMER_EVENTS_LIMIT = 1000;--export type CustomerTBEvent = {- event: "click" | "lead" | "sale";- timestamp: string;- click_id?: string;- link_id?: string;- event_id?: string;- event_name?: string;- saleAmount?: number;- invoice_id?: string;- payment_processor?: string;- currency?: string;- [key: string]: unknown;-};--export type ReattributeEventPlan = {- hasClick: boolean;- hasLead: boolean;- leadCount: number;- saleCount: number;- saleAmount: number;- leadTimestamp: string | null;- saleTimestamp: string | null;- oldEventIds: string[];- alreadyReingested: boolean;-};--export function isRetiredReattributeStub(customer: {- externalId?: string | null;- partnerId?: string | null;- linkId?: string | null;- programId?: string | null;-}) {- return (- Boolean(customer.externalId?.startsWith("dummy_")) &&- customer.partnerId == null &&- customer.linkId == null &&- customer.programId == null- );-}--export function customerEventsExceedLimit(eventCount: number) {- return eventCount >= CUSTOMER_EVENTS_LIMIT;-}--export function shouldRecreateCustomer({- eventCount,- commissionCount,- clickId,- sales,-}: {- eventCount: number;- commissionCount: number;- clickId?: string | null;- sales?: number;-}) {- return (- eventCount > 0 ||- commissionCount > 0 ||- Boolean(clickId) ||- (sales ?? 0) > 0- );-}--export function selectEventsToReingest({- oldEvents,- newEvents,-}: {- oldEvents: CustomerTBEvent[];- newEvents: CustomerTBEvent[];-}) {- const newHasClick = newEvents.some((event) => event.event === "click");- const newHasLead = newEvents.some((event) => event.event === "lead");- const newSaleInvoiceIds = new Set(- newEvents- .filter((event) => event.event === "sale" && event.invoice_id)- .map((event) => event.invoice_id)- .filter((invoiceId): invoiceId is string => Boolean(invoiceId)),- );- const newInvoicelessSaleTimestamps = new Set(- newEvents- .filter((event) => event.event === "sale" && !event.invoice_id)- .map((event) => event.timestamp),- );-- return {- clickEvent: !newHasClick- ? oldEvents.find((event) => event.event === "click") ?? null- : null,- leadEvent: !newHasLead- ? oldEvents.find((event) => event.event === "lead") ?? null- : null,- saleEvents: oldEvents.filter((event) => {- if (event.event !== "sale") {- return false;- }-- if (event.invoice_id) {- return !newSaleInvoiceIds.has(event.invoice_id);- }-- return !newInvoicelessSaleTimestamps.has(event.timestamp);- }),- };-}--export function selectPaidReplacementEvents({- paidInvoiceIds,- hasPaidLead,- newEvents,- existingNewCommissions,-}: {- paidInvoiceIds: string[];- hasPaidLead: boolean;- newEvents: CustomerTBEvent[];- existingNewCommissions: Array<{- type: string;- invoiceId: string | null;- }>;-}) {- const hasExistingLead = existingNewCommissions.some(- (commission) => commission.type === "lead",- );- const existingInvoiceIds = new Set(- existingNewCommissions- .map((commission) => commission.invoiceId)- .filter((invoiceId): invoiceId is string => Boolean(invoiceId)),- );- const paidInvoiceIdSet = new Set(paidInvoiceIds);-- return {- leadEvent:- hasPaidLead && !hasExistingLead- ? newEvents.find((event) => event.event === "lead") ?? null- : null,- saleEvents: newEvents.filter(- (event) =>- event.event === "sale" &&- Boolean(event.invoice_id) &&- paidInvoiceIdSet.has(event.invoice_id!) &&- !existingInvoiceIds.has(event.invoice_id!),- ),- };-}--export function summarizeCustomerEvents(- events: CustomerTBEvent[],-): Omit<ReattributeEventPlan, "alreadyReingested"> {- const clickEvent = events.find((event) => event.event === "click");- const leadEvent = events.find((event) => event.event === "lead");- const saleEvents = events.filter((event) => event.event === "sale");-- return {- hasClick: Boolean(clickEvent),- hasLead: Boolean(leadEvent),- leadCount: leadEvent ? 1 : 0,- saleCount: saleEvents.length,- saleAmount: saleEvents.reduce(- (sum, event) => sum + (event.saleAmount ?? 0),- 0,- ),- leadTimestamp: leadEvent?.timestamp ?? null,- saleTimestamp:- saleEvents.length > 0- ? saleEvents.reduce((latest, event) =>- event.timestamp > latest.timestamp ? event : latest,- ).timestamp- : null,- oldEventIds: [- leadEvent?.event_id,- ...saleEvents.map((event) => event.event_id),- ].filter((eventId): eventId is string => typeof eventId === "string"),- };-}diff --git a/apps/web/lib/api/customers/reattribute-customer.ts b/apps/web/lib/api/customers/reattribute-customer.tsindex b0bb6850d6..53f71f0662 100644--- a/apps/web/lib/api/customers/reattribute-customer.ts+++ b/apps/web/lib/api/customers/reattribute-customer.ts@@ -1,4 +1,3 @@-import { isFirstConversion } from "@/lib/analytics/is-first-conversion";import { reconcilePayoutAmounts } from "@/lib/api/commissions/reconcile-payout-amounts";import { updateLinkStatsForImporter } from "@/lib/api/links/update-link-stats-for-importer";import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-stats";@@ -7,7 +6,6 @@ import { MUTABLE_PAYOUT_STATUSES } from "@/lib/constants/payouts";import { queuePartnerCommissionCreation } from "@/lib/partners/queue-partner-commission-creation";import { retallyPayoutsAmount } from "@/lib/payouts/retally-payouts-amount";import { prisma } from "@/lib/prisma";-import { deleteTinybirdCustomerEvents } from "@/lib/tinybird/delete-events";import { getCustomerEventsTB } from "@/lib/tinybird/get-customer-events-tb";import {recordClickZod,@@ -21,30 +19,8 @@ import { saleEventSchemaTB } from "@/lib/zod/schemas/sales";import { nanoid } from "@dub/utils";import { Customer, Link } from "@prisma/client";import * as z from "zod/v4";-import {- CUSTOMER_EVENTS_LIMIT,- customerEventsExceedLimit,- selectEventsToReingest,- selectPaidReplacementEvents,- summarizeCustomerEvents,- type CustomerTBEvent,- type ReattributeEventPlan,-} from "./reattribute-customer-utils";--export {- CUSTOMER_EVENTS_LIMIT,- customerEventsExceedLimit,- isRetiredReattributeStub,- selectEventsToReingest,- selectPaidReplacementEvents,- shouldRecreateCustomer,- summarizeCustomerEvents,-} from "./reattribute-customer-utils";-export type {- CustomerTBEvent,- ReattributeEventPlan,-} from "./reattribute-customer-utils";+export const CUSTOMER_EVENTS_LIMIT = 1000;const UNPAID_COMMISSION_STATUSES = ["pending", "hold", "processed"] as const;const STATS_LOCK_TTL_SECONDS = 60 * 60 * 24;@@ -56,6 +32,30 @@ const saleEventSchemaTBWithTimestamp = saleEventSchemaTB.extend({timestamp: z.string(),});+type CustomerTBEvent = {+ event: "click" | "lead" | "sale";+ timestamp: string;+ click_id?: string;+ link_id?: string;+ event_id?: string;+ event_name?: string;+ saleAmount?: number;+ invoice_id?: string;+ payment_processor?: string;+ currency?: string;+ [key: string]: unknown;+};++type ReattributeEventPlan = {+ hasClick: boolean;+ hasLead: boolean;+ leadCount: number;+ saleCount: number;+ saleAmount: number;+ leadTimestamp: string | null;+ saleTimestamp: string | null;+};+async function runOnce({ key, fn }: { key: string; fn: () => Promise<void> }) {const acquired = await redis.set(key, "1", {nx: true,@@ -75,8 +75,13 @@ async function runOnce({ key, fn }: { key: string; fn: () => Promise<void> }) {}}-function parseCustomerEvents(data: unknown[]): CustomerTBEvent[] {- return data.filter(+export async function getCustomerReattributeEvents(customerId: string) {+ const { data } = await getCustomerEventsTB({+ customerId,+ limit: CUSTOMER_EVENTS_LIMIT,+ });++ return (data ?? []).filter((event): event is CustomerTBEvent =>typeof event === "object" &&event !== null &&@@ -87,15 +92,6 @@ function parseCustomerEvents(data: unknown[]): CustomerTBEvent[] {);}-export async function getCustomerReattributeEvents(customerId: string) {- const { data } = await getCustomerEventsTB({- customerId,- limit: CUSTOMER_EVENTS_LIMIT,- });-- return parseCustomerEvents(data ?? []);-}-export async function recreateCustomerForReattribution({customer,link,@@ -161,17 +157,33 @@ export async function loadReattributeEventPlan({getCustomerReattributeEvents(newCustomerId),]);- if (customerEventsExceedLimit(oldEvents.length)) {+ if (oldEvents.length >= CUSTOMER_EVENTS_LIMIT) {throw new Error(`Customer ${oldCustomerId} has too many events to reattribute (limit ${CUSTOMER_EVENTS_LIMIT}).`,);}const sourceEvents = oldEvents.length > 0 ? oldEvents : newEvents;+ const clickEvent = sourceEvents.find((event) => event.event === "click");+ const leadEvent = sourceEvents.find((event) => event.event === "lead");+ const saleEvents = sourceEvents.filter((event) => event.event === "sale");return {- ...summarizeCustomerEvents(sourceEvents),- alreadyReingested: newEvents.length > 0 && oldEvents.length === 0,+ hasClick: Boolean(clickEvent),+ hasLead: Boolean(leadEvent),+ leadCount: leadEvent ? 1 : 0,+ saleCount: saleEvents.length,+ saleAmount: saleEvents.reduce(+ (sum, event) => sum + (event.saleAmount ?? 0),+ 0,+ ),+ leadTimestamp: leadEvent?.timestamp ?? null,+ saleTimestamp:+ saleEvents.length > 0+ ? saleEvents.reduce((latest, event) =>+ event.timestamp > latest.timestamp ? event : latest,+ ).timestamp+ : null,};}@@ -193,15 +205,41 @@ export async function reingestCustomerEvents({getCustomerReattributeEvents(newCustomerId),]);- if (customerEventsExceedLimit(oldEvents.length)) {+ if (oldEvents.length >= CUSTOMER_EVENTS_LIMIT) {throw new Error(`Customer ${oldCustomerId} has too many events to reattribute (limit ${CUSTOMER_EVENTS_LIMIT}).`,);}- const { clickEvent, leadEvent, saleEvents } = selectEventsToReingest({- oldEvents,- newEvents,+ const newHasClick = newEvents.some((event) => event.event === "click");+ const newHasLead = newEvents.some((event) => event.event === "lead");+ const newSaleInvoiceIds = new Set(+ newEvents+ .filter((event) => event.event === "sale" && event.invoice_id)+ .map((event) => event.invoice_id),+ );+ const newInvoicelessSaleTimestamps = new Set(+ newEvents+ .filter((event) => event.event === "sale" && !event.invoice_id)+ .map((event) => event.timestamp),+ );++ const clickEvent = !newHasClick+ ? oldEvents.find((event) => event.event === "click") ?? null+ : null;+ const leadEvent = !newHasLead+ ? oldEvents.find((event) => event.event === "lead") ?? null+ : null;+ const saleEvents = oldEvents.filter((event) => {+ if (event.event !== "sale") {+ return false;+ }++ if (event.invoice_id) {+ return !newSaleInvoiceIds.has(event.invoice_id);+ }++ return !newInvoicelessSaleTimestamps.has(event.timestamp);});if (!clickEvent && !leadEvent && saleEvents.length === 0) {@@ -533,9 +571,11 @@ export async function createClawbackAndReplacementCommissions({return { skipped: true, reason: "no-paid-earnings" as const };}- const paidInvoiceIds = paidCommissions- .map((commission) => commission.invoiceId)- .filter((invoiceId): invoiceId is string => Boolean(invoiceId));+ const paidInvoiceIds = new Set(+ paidCommissions+ .map((commission) => commission.invoiceId)+ .filter((invoiceId): invoiceId is string => Boolean(invoiceId)),+ );const hasPaidLead = paidCommissions.some((commission) => commission.type === "lead",);@@ -603,12 +643,26 @@ export async function createClawbackAndReplacementCommissions({}),]);- const { leadEvent, saleEvents } = selectPaidReplacementEvents({- paidInvoiceIds,- hasPaidLead,- newEvents,- existingNewCommissions,- });+ const hasExistingLead = existingNewCommissions.some(+ (commission) => commission.type === "lead",+ );+ const existingInvoiceIds = new Set(+ existingNewCommissions+ .map((commission) => commission.invoiceId)+ .filter((invoiceId): invoiceId is string => Boolean(invoiceId)),+ );++ const leadEvent =+ hasPaidLead && !hasExistingLead+ ? newEvents.find((event) => event.event === "lead") ?? null+ : null;+ const saleEvents = newEvents.filter(+ (event) =>+ event.event === "sale" &&+ event.invoice_id &&+ paidInvoiceIds.has(event.invoice_id) &&+ !existingInvoiceIds.has(event.invoice_id),+ );if (leadEvent?.event_id) {await queuePartnerCommissionCreation({@@ -668,33 +722,3 @@ export async function createClawbackAndReplacementCommissions({recreated: (leadEvent ? 1 : 0) + saleEvents.length,};}--export async function deleteOldCustomerTinybirdEvents({- oldCustomerId,- oldClickId,-}: {- oldCustomerId: string;- oldClickId: string | null;-}) {- await deleteTinybirdCustomerEvents({- customerId: oldCustomerId,- clickId: oldClickId,- });-}--export function computeConversionFlags({- customer,- newLinkId,-}: {- customer: Pick<Customer, "sales" | "linkId">;- newLinkId: string;-}) {- const incrementConversions =- customer.sales > 0 && isFirstConversion({ customer, linkId: newLinkId });- const decrementConversions = customer.sales > 0 && Boolean(customer.linkId);-- return {- incrementConversions,- decrementConversions,- };-}diff --git a/apps/web/tests/customers/reattribute-customer.test.ts b/apps/web/tests/customers/reattribute-customer.test.tsdeleted file mode 100644index b6824f3839..0000000000--- a/apps/web/tests/customers/reattribute-customer.test.ts+++ /dev/null@@ -1,209 +0,0 @@-import {- CUSTOMER_EVENTS_LIMIT,- customerEventsExceedLimit,- isRetiredReattributeStub,- selectEventsToReingest,- selectPaidReplacementEvents,- shouldRecreateCustomer,- type CustomerTBEvent,-} from "@/lib/api/customers/reattribute-customer-utils";-import { describe, expect, it } from "vitest";--function event(- overrides: Partial<CustomerTBEvent> & Pick<CustomerTBEvent, "event">,-): CustomerTBEvent {- return {- timestamp: "2026-01-01 00:00:00",- ...overrides,- };-}--describe("shouldRecreateCustomer", () => {- it("keeps the fast path for a manual customer", () => {- expect(- shouldRecreateCustomer({- eventCount: 0,- commissionCount: 0,- }),- ).toBe(false);- });-- it("recreates when Tinybird events or commissions exist", () => {- expect(shouldRecreateCustomer({ eventCount: 1, commissionCount: 0 })).toBe(- true,- );- expect(shouldRecreateCustomer({ eventCount: 0, commissionCount: 2 })).toBe(- true,- );- });-- it("recreates when clickId or sales are present even if Tinybird is empty", () => {- expect(- shouldRecreateCustomer({- eventCount: 0,- commissionCount: 0,- clickId: "clk_abc",- }),- ).toBe(true);- expect(- shouldRecreateCustomer({- eventCount: 0,- commissionCount: 0,- sales: 3,- }),- ).toBe(true);- });-});--describe("isRetiredReattributeStub", () => {- it("detects a retired stub", () => {- expect(- isRetiredReattributeStub({- externalId: "dummy_abcdefghijklmnopqrstuvwxyz012345",- partnerId: null,- linkId: null,- programId: null,- }),- ).toBe(true);- });-- it("does not treat a live customer as retired", () => {- expect(- isRetiredReattributeStub({- externalId: "user_123",- partnerId: "pn_1",- linkId: "link_1",- programId: "prog_1",- }),- ).toBe(false);- });-- it("requires dummy_ plus cleared attribution fields", () => {- expect(- isRetiredReattributeStub({- externalId: "dummy_abc",- partnerId: "pn_1",- linkId: null,- programId: null,- }),- ).toBe(false);- });-});--describe("customerEventsExceedLimit", () => {- it("refuses a truncated event page", () => {- expect(customerEventsExceedLimit(CUSTOMER_EVENTS_LIMIT)).toBe(true);- expect(customerEventsExceedLimit(CUSTOMER_EVENTS_LIMIT - 1)).toBe(false);- });-});--describe("selectEventsToReingest", () => {- const oldEvents: CustomerTBEvent[] = [- event({ event: "click", click_id: "old_click" }),- event({ event: "lead", event_id: "lead_1" }),- event({- event: "sale",- event_id: "sale_1",- invoice_id: "inv_1",- saleAmount: 1000,- }),- event({- event: "sale",- event_id: "sale_2",- invoice_id: "inv_2",- saleAmount: 2000,- }),- ];-- it("selects every old event when the new customer is empty", () => {- const selected = selectEventsToReingest({- oldEvents,- newEvents: [],- });-- expect(selected.clickEvent?.click_id).toBe("old_click");- expect(selected.leadEvent?.event_id).toBe("lead_1");- expect(selected.saleEvents.map((sale) => sale.invoice_id)).toEqual([- "inv_1",- "inv_2",- ]);- });-- it("skips types already present and only copies missing sales", () => {- const selected = selectEventsToReingest({- oldEvents,- newEvents: [- event({ event: "click", click_id: "new_click" }),- event({ event: "lead", event_id: "new_lead" }),- event({ event: "sale", invoice_id: "inv_1", saleAmount: 1000 }),- ],- });-- expect(selected.clickEvent).toBeNull();- expect(selected.leadEvent).toBeNull();- expect(selected.saleEvents.map((sale) => sale.invoice_id)).toEqual([- "inv_2",- ]);- });-});--describe("selectPaidReplacementEvents", () => {- const newEvents: CustomerTBEvent[] = [- event({ event: "lead", event_id: "new_lead" }),- event({- event: "sale",- event_id: "new_sale_paid",- invoice_id: "inv_paid",- saleAmount: 5000,- }),- event({- event: "sale",- event_id: "new_sale_unpaid",- invoice_id: "inv_unpaid",- saleAmount: 1500,- }),- ];-- it("recreates paid lead and paid sales even when unpaid rows already moved", () => {- const selected = selectPaidReplacementEvents({- paidInvoiceIds: ["inv_paid"],- hasPaidLead: true,- newEvents,- existingNewCommissions: [{ type: "sale", invoiceId: "inv_unpaid" }],- });-- expect(selected.leadEvent?.event_id).toBe("new_lead");- expect(selected.saleEvents.map((sale) => sale.invoice_id)).toEqual([- "inv_paid",- ]);- });-- it("does not recreate a paid lead that already exists on the new customer", () => {- const selected = selectPaidReplacementEvents({- paidInvoiceIds: ["inv_paid"],- hasPaidLead: true,- newEvents,- existingNewCommissions: [- { type: "lead", invoiceId: null },- { type: "sale", invoiceId: "inv_unpaid" },- ],- });-- expect(selected.leadEvent).toBeNull();- expect(selected.saleEvents.map((sale) => sale.invoice_id)).toEqual([- "inv_paid",- ]);- });-- it("skips paid sales that already have a replacement invoice", () => {- const selected = selectPaidReplacementEvents({- paidInvoiceIds: ["inv_paid"],- hasPaidLead: false,- newEvents,- existingNewCommissions: [{ type: "sale", invoiceId: "inv_paid" }],- });-- expect(selected.leadEvent).toBeNull();- expect(selected.saleEvents).toEqual([]);- });-});