Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

code improvements

Pedro Ladeirab654589ef2ae
diff --git a/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts b/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts
index 5fa6c4b891..9e364b186e 100644
--- a/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts
+++ b/apps/web/app/(ee)/api/customers/[id]/reattribute/route.ts
@@ -4,7 +4,9 @@ import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
import {
CUSTOMER_EVENTS_LIMIT,
getCustomerReattributeEvents,
+ isReattributedCustomerStub,
recreateCustomerForReattribution,
+ rollbackCustomerRecreation,
} from "@/lib/api/customers/reattribute-customer";
import { transformCustomer } from "@/lib/api/customers/transform-customer";
import { DubApiError } from "@/lib/api/errors";
@@ -32,11 +34,6 @@ export const POST = withWorkspace(
const { partnerId, linkId, createClawback } =
reattributeCustomerBodySchema.parse(await parseRequestBody(req));
- await assertRateLimit({
- policy: RATELIMIT_POLICIES.reattributeCustomer,
- identifier: workspace.id,
- });
-
const customer = await getCustomerOrThrow(
{
id,
@@ -47,12 +44,7 @@ export const POST = withWorkspace(
},
);
- if (
- customer.externalId?.startsWith("dummy_") &&
- customer.partnerId == null &&
- customer.linkId == null &&
- customer.programId == null
- ) {
+ if (isReattributedCustomerStub(customer)) {
throw new DubApiError({
code: "bad_request",
message:
@@ -115,6 +107,11 @@ export const POST = withWorkspace(
});
}
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.reattributeCustomer,
+ identifier: workspace.id,
+ });
+
if (
events.length === 0 &&
commissionCount === 0 &&
@@ -160,7 +157,7 @@ export const POST = withWorkspace(
newClickId,
});
- await dispatchWorkflows({
+ const workflow = await dispatchWorkflows({
name: "reattribute-customer-workflow",
payload: {
workspaceId: workspace.id,
@@ -187,6 +184,19 @@ export const POST = withWorkspace(
},
});
+ if (workflow.failed > 0) {
+ await rollbackCustomerRecreation({
+ customer,
+ newCustomerId,
+ });
+
+ throw new DubApiError({
+ code: "internal_server_error",
+ message:
+ "Customer reattribution failed to start. Please try again in a moment.",
+ });
+ }
+
const newCustomer = await getCustomerOrThrow(
{
id: newCustomerId,
diff --git a/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts b/apps/web/app/(ee)/api/workflows/reattribute-customer/route.ts
index 9e725351db..58bb7abe0c 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,8 @@
import {
- createClawbackAndReplacementCommissions,
+ applyClawbackAndReplacementCommissions,
decrementOldLinkStats,
incrementNewLinkStats,
+ loadClawbackPlan,
loadReattributeEventPlan,
reingestCustomerEvents,
transferUnpaidCommissions,
@@ -26,7 +27,7 @@ type Input = z.infer<typeof reattributeCustomerWorkflowSchema>;
* 2. reingest-events: copy events onto the new customer + link
* 3. increment-new-link-stats
* 4. transfer-unpaid-commissions
- * 5. optional-clawback
+ * 5. load-clawback-plan + optional-clawback
* 6. delete-old-events
* 7. decrement-old-link-stats
*/
@@ -44,17 +45,17 @@ export const { POST } = serve<Input>(
});
await context.run("reingest-events", async () => {
- try {
- const link = await prisma.link.findUniqueOrThrow({
- where: { id: input.newLinkId },
- select: {
- id: true,
- domain: true,
- key: true,
- url: true,
- },
- });
+ const link = await prisma.link.findUniqueOrThrow({
+ where: { id: input.newLinkId },
+ select: {
+ id: true,
+ domain: true,
+ key: true,
+ url: true,
+ },
+ });
+ try {
return logAndReturn(
await reingestCustomerEvents({
oldCustomerId: input.oldCustomerId,
@@ -65,6 +66,14 @@ export const { POST } = serve<Input>(
}),
);
} catch (error) {
+ if (
+ error instanceof z.ZodError ||
+ (error instanceof Error &&
+ error.message.includes("too many events to reattribute"))
+ ) {
+ throw error;
+ }
+
throw new WorkflowRetryAfterError(
error instanceof Error ? error.message : "Failed to re-ingest events",
"5s",
@@ -109,26 +118,38 @@ export const { POST } = serve<Input>(
},
);
- await context.run("optional-clawback", async () => {
- return logAndReturn(
- await createClawbackAndReplacementCommissions({
- oldCustomerId: input.oldCustomerId,
- newCustomerId: input.newCustomerId,
- oldPartnerId: input.oldPartnerId,
- newPartnerId: input.newPartnerId,
- newLinkId: input.newLinkId,
- programId: input.programId,
- customerCountry: newCustomer?.country ?? null,
- }),
- );
+ const clawbackPlan = await context.run("load-clawback-plan", async () => {
+ return await loadClawbackPlan({
+ oldCustomerId: input.oldCustomerId,
+ oldPartnerId: input.oldPartnerId,
+ });
});
+
+ if (!clawbackPlan.skipped && input.oldPartnerId) {
+ const oldPartnerId = input.oldPartnerId;
+
+ await context.run("optional-clawback", async () => {
+ return logAndReturn(
+ await applyClawbackAndReplacementCommissions({
+ oldCustomerId: input.oldCustomerId,
+ newCustomerId: input.newCustomerId,
+ oldPartnerId,
+ newPartnerId: input.newPartnerId,
+ newLinkId: input.newLinkId,
+ programId: input.programId,
+ customerCountry: newCustomer?.country ?? null,
+ plan: clawbackPlan,
+ }),
+ );
+ });
+ }
}
await context.run("delete-old-events", async () => {
try {
await deleteTinybirdCustomerEvents({
customerId: input.oldCustomerId,
- clickId: input.oldClickId,
+ clickId: plan.hasClick ? input.oldClickId : null,
});
return logAndReturn({ deleted: true });
diff --git a/apps/web/lib/api/customers/reattribute-customer.ts b/apps/web/lib/api/customers/reattribute-customer.ts
index 53f71f0662..135322ccc0 100644
--- a/apps/web/lib/api/customers/reattribute-customer.ts
+++ b/apps/web/lib/api/customers/reattribute-customer.ts
@@ -4,7 +4,6 @@ import { syncPartnerLinksStats } from "@/lib/api/partners/sync-partner-links-sta
import { syncTotalCommissions } from "@/lib/api/partners/sync-total-commissions";
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 { getCustomerEventsTB } from "@/lib/tinybird/get-customer-events-tb";
import {
@@ -75,6 +74,27 @@ async function runOnce({ key, fn }: { key: string; fn: () => Promise<void> }) {
}
}
+export function isReattributedCustomerStub({
+ externalId,
+ partnerId,
+ linkId,
+ programId,
+}: {
+ externalId: string | null;
+ partnerId: string | null;
+ linkId: string | null;
+ programId: string | null;
+}) {
+ return (
+ (externalId?.startsWith("reattributed_") === true ||
+ externalId?.startsWith("dummy_") === true ||
+ externalId?.startsWith("retired_") === true) &&
+ partnerId == null &&
+ linkId == null &&
+ programId == null
+ );
+}
+
export async function getCustomerReattributeEvents(customerId: string) {
const { data } = await getCustomerEventsTB({
customerId,
@@ -110,7 +130,7 @@ export async function recreateCustomerForReattribution({
},
data: {
name: customer.name ? `${customer.name} (old)` : undefined,
- externalId: `dummy_${nanoid(32)}`,
+ externalId: `reattributed_${nanoid(32)}`,
stripeCustomerId: null,
linkId: null,
programId: null,
@@ -145,6 +165,37 @@ export async function recreateCustomerForReattribution({
});
}
+export async function rollbackCustomerRecreation({
+ customer,
+ newCustomerId,
+}: {
+ customer: Customer;
+ newCustomerId: string;
+}) {
+ return await prisma.$transaction(async (tx) => {
+ await tx.customer.delete({
+ where: {
+ id: newCustomerId,
+ },
+ });
+
+ return await tx.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ name: customer.name,
+ externalId: customer.externalId,
+ stripeCustomerId: customer.stripeCustomerId,
+ linkId: customer.linkId,
+ programId: customer.programId,
+ partnerId: customer.partnerId,
+ clickId: customer.clickId,
+ },
+ });
+ });
+}
+
export async function loadReattributeEventPlan({
oldCustomerId,
newCustomerId,
@@ -456,95 +507,139 @@ export async function transferUnpaidCommissions({
programId: string;
oldPartnerId: string | null;
}) {
- const unpaidCommissions = await prisma.commission.findMany({
- where: {
- customerId: oldCustomerId,
- status: { in: [...UNPAID_COMMISSION_STATUSES] },
- },
- select: {
- id: true,
- payoutId: true,
- payout: {
+ let transferred = 0;
+
+ const didUpdate = await runOnce({
+ key: `reattribute-customer:${oldCustomerId}:transfer-unpaid`,
+ fn: async () => {
+ const unpaidCommissions = await prisma.commission.findMany({
+ where: {
+ customerId: oldCustomerId,
+ status: { in: [...UNPAID_COMMISSION_STATUSES] },
+ },
select: {
id: true,
status: true,
+ payoutId: true,
+ payout: {
+ select: {
+ id: true,
+ status: true,
+ },
+ },
},
- },
- },
- });
+ });
- if (unpaidCommissions.length === 0) {
- return { transferred: 0 };
- }
+ const transferablePendingOrHoldIds: string[] = [];
+ const transferableProcessedIds: string[] = [];
+ const mutablePayoutIds: string[] = [];
+
+ for (const commission of unpaidCommissions) {
+ const isLocked =
+ Boolean(commission.payoutId) &&
+ commission.payout != null &&
+ !MUTABLE_PAYOUT_STATUSES.includes(commission.payout.status);
+
+ if (isLocked) {
+ continue;
+ }
+
+ if (
+ commission.payoutId &&
+ commission.payout &&
+ MUTABLE_PAYOUT_STATUSES.includes(commission.payout.status)
+ ) {
+ mutablePayoutIds.push(commission.payoutId);
+ }
+
+ if (commission.status === "processed") {
+ transferableProcessedIds.push(commission.id);
+ } else {
+ transferablePendingOrHoldIds.push(commission.id);
+ }
+ }
- const payoutIds = unpaidCommissions
- .map((commission) => commission.payoutId)
- .filter((payoutId): payoutId is string => Boolean(payoutId));
-
- const mutablePayoutIds = unpaidCommissions
- .filter(
- (commission) =>
- commission.payoutId &&
- commission.payout &&
- MUTABLE_PAYOUT_STATUSES.includes(commission.payout.status),
- )
- .map((commission) => commission.payoutId)
- .filter((payoutId): payoutId is string => Boolean(payoutId));
-
- const processedPayoutIds = payoutIds.filter(
- (payoutId) => !mutablePayoutIds.includes(payoutId),
- );
+ const transferableIds = [
+ ...transferablePendingOrHoldIds,
+ ...transferableProcessedIds,
+ ];
- await prisma.commission.updateMany({
- where: {
- id: { in: unpaidCommissions.map((commission) => commission.id) },
- },
- data: {
- customerId: newCustomerId,
- partnerId: newPartnerId,
- linkId: newLinkId,
- payoutId: null,
- },
- });
+ if (transferableIds.length === 0) {
+ return;
+ }
- await reconcilePayoutAmounts(mutablePayoutIds);
- await retallyPayoutsAmount(processedPayoutIds);
+ if (transferablePendingOrHoldIds.length > 0) {
+ await prisma.commission.updateMany({
+ where: {
+ id: { in: transferablePendingOrHoldIds },
+ },
+ data: {
+ customerId: newCustomerId,
+ partnerId: newPartnerId,
+ linkId: newLinkId,
+ payoutId: null,
+ },
+ });
+ }
- await Promise.all([
- syncTotalCommissions({
- partnerId: newPartnerId,
- programId,
- }),
- oldPartnerId
- ? syncTotalCommissions({
- partnerId: oldPartnerId,
+ if (transferableProcessedIds.length > 0) {
+ await prisma.commission.updateMany({
+ where: {
+ id: { in: transferableProcessedIds },
+ },
+ data: {
+ customerId: newCustomerId,
+ partnerId: newPartnerId,
+ linkId: newLinkId,
+ payoutId: null,
+ status: "pending",
+ },
+ });
+ }
+
+ await reconcilePayoutAmounts([...new Set(mutablePayoutIds)]);
+
+ await Promise.all([
+ syncTotalCommissions({
+ partnerId: newPartnerId,
programId,
- })
- : Promise.resolve(),
- ]);
+ }),
+ oldPartnerId
+ ? syncTotalCommissions({
+ partnerId: oldPartnerId,
+ programId,
+ })
+ : Promise.resolve(),
+ ]);
+
+ transferred = transferableIds.length;
+ },
+ });
- return { transferred: unpaidCommissions.length };
+ return { transferred: didUpdate ? transferred : 0 };
}
-export async function createClawbackAndReplacementCommissions({
+export type ClawbackPlan =
+ | { skipped: true; reason: "no-old-partner" | "no-paid-earnings" }
+ | {
+ skipped: false;
+ paidCommissionIds: string[];
+ paidEarnings: number;
+ hasPaidLead: boolean;
+ paidInvoiceIds: string[];
+ paidInvoicelessCount: number;
+ shouldClearIdentifiers: boolean;
+ };
+
+export async function loadClawbackPlan({
oldCustomerId,
- newCustomerId,
oldPartnerId,
- newPartnerId,
- newLinkId,
- programId,
- customerCountry,
}: {
oldCustomerId: string;
- newCustomerId: string;
oldPartnerId: string | null;
- newPartnerId: string;
- newLinkId: string;
- programId: string;
- customerCountry: string | null;
-}) {
+}): Promise<ClawbackPlan> {
if (!oldPartnerId) {
- return { skipped: true, reason: "no-old-partner" as const };
+ return { skipped: true, reason: "no-old-partner" };
}
const paidCommissions = await prisma.commission.findMany({
@@ -568,26 +663,53 @@ export async function createClawbackAndReplacementCommissions({
);
if (paidEarnings <= 0) {
- return { skipped: true, reason: "no-paid-earnings" as const };
+ return { skipped: true, reason: "no-paid-earnings" };
}
- const paidInvoiceIds = new Set(
- paidCommissions
- .map((commission) => commission.invoiceId)
- .filter((invoiceId): invoiceId is string => Boolean(invoiceId)),
- );
- const hasPaidLead = paidCommissions.some(
- (commission) => commission.type === "lead",
- );
-
- if (
- paidCommissions.some(
+ return {
+ skipped: false,
+ paidCommissionIds: paidCommissions.map((commission) => commission.id),
+ paidEarnings,
+ hasPaidLead: paidCommissions.some(
+ (commission) => commission.type === "lead",
+ ),
+ paidInvoiceIds: paidCommissions
+ .filter(
+ (commission) => commission.type === "sale" && commission.invoiceId,
+ )
+ .map((commission) => commission.invoiceId!),
+ paidInvoicelessCount: paidCommissions.filter(
+ (commission) => commission.type === "sale" && !commission.invoiceId,
+ ).length,
+ shouldClearIdentifiers: paidCommissions.some(
(commission) => commission.eventId || commission.invoiceId,
- )
- ) {
+ ),
+ };
+}
+
+export async function applyClawbackAndReplacementCommissions({
+ oldCustomerId,
+ newCustomerId,
+ oldPartnerId,
+ newPartnerId,
+ newLinkId,
+ programId,
+ customerCountry,
+ plan,
+}: {
+ oldCustomerId: string;
+ newCustomerId: string;
+ oldPartnerId: string;
+ newPartnerId: string;
+ newLinkId: string;
+ programId: string;
+ customerCountry: string | null;
+ plan: Extract<ClawbackPlan, { skipped: false }>;
+}) {
+ if (plan.shouldClearIdentifiers && plan.paidCommissionIds.length > 0) {
await prisma.commission.updateMany({
where: {
- id: { in: paidCommissions.map((commission) => commission.id) },
+ id: { in: plan.paidCommissionIds },
},
data: {
eventId: null,
@@ -620,7 +742,7 @@ export async function createClawbackAndReplacementCommissions({
partnerId: oldPartnerId,
programId,
customerId: oldCustomerId,
- amount: -paidEarnings,
+ amount: -plan.paidEarnings,
quantity: 1,
description: "tracking_error",
skipWorkflow: true,
@@ -646,23 +768,37 @@ export async function createClawbackAndReplacementCommissions({
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
+ plan.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),
+
+ const paidInvoiceIds = new Set(plan.paidInvoiceIds);
+ const existingInvoiceIds = new Set(
+ existingNewCommissions
+ .map((commission) => commission.invoiceId)
+ .filter((invoiceId): invoiceId is string => Boolean(invoiceId)),
);
+ const existingInvoicelessCount = existingNewCommissions.filter(
+ (commission) => commission.type === "sale" && !commission.invoiceId,
+ ).length;
+
+ const saleEvents = [
+ ...newEvents.filter(
+ (event) =>
+ event.event === "sale" &&
+ event.invoice_id &&
+ paidInvoiceIds.has(event.invoice_id) &&
+ !existingInvoiceIds.has(event.invoice_id),
+ ),
+ ...newEvents
+ .filter((event) => event.event === "sale" && !event.invoice_id)
+ .slice(
+ 0,
+ Math.max(0, plan.paidInvoicelessCount - existingInvoicelessCount),
+ ),
+ ];
if (leadEvent?.event_id) {
await queuePartnerCommissionCreation({
diff --git a/apps/web/lib/tinybird/delete-events.ts b/apps/web/lib/tinybird/delete-events.ts
index 03781d9648..af71e846ea 100644
--- a/apps/web/lib/tinybird/delete-events.ts
+++ b/apps/web/lib/tinybird/delete-events.ts
@@ -29,6 +29,7 @@ async function deleteDatasourceRows({
"Content-Type": "application/x-www-form-urlencoded",
},
body: `delete_condition=${deleteCondition}`,
+ signal: AbortSignal.timeout(30_000),
},
);
diff --git a/apps/web/playwright/api/customers/customers-reattribute.spec.ts b/apps/web/playwright/api/customers/customers-reattribute.spec.ts
index 1b0a6ee288..bb2c6297e1 100644
--- a/apps/web/playwright/api/customers/customers-reattribute.spec.ts
+++ b/apps/web/playwright/api/customers/customers-reattribute.spec.ts
@@ -99,7 +99,7 @@ test("POST /customers/{id}/reattribute – retired stub", async ({ api }) => {
await prisma.customer.update({
where: { id: created.id },
data: {
- externalId: `dummy_${nanoid(32)}`,
+ externalId: `reattributed_${nanoid(32)}`,
partnerId: null,
linkId: null,
programId: null,
diff --git a/apps/web/ui/modals/reattribute-customer-modal.tsx b/apps/web/ui/modals/reattribute-customer-modal.tsx
index 542655c7e9..dca0236963 100644
--- a/apps/web/ui/modals/reattribute-customer-modal.tsx
+++ b/apps/web/ui/modals/reattribute-customer-modal.tsx
@@ -65,7 +65,12 @@ function ReattributeCustomerModal({
}
}, [showModal, customer, reset]);
- const { data: commissionsCount } = useSWR<CommissionsCount>(
+ const {
+ data: commissionsCount,
+ error: commissionsCountError,
+ mutate: retryCommissionsCount,
+ isValidating: isCommissionsCountValidating,
+ } = useSWR<CommissionsCount>(
workspaceId && customer.id && showModal
? `/api/commissions/count?${new URLSearchParams({
customerId: customer.id,
@@ -77,7 +82,7 @@ function ReattributeCustomerModal({
);
const paidEarnings = commissionsCount?.paid?.earnings ?? 0;
- const showClawback = paidEarnings > 0;
+ const showClawback = !commissionsCountError && paidEarnings > 0;
const isUnchanged =
partnerId === (customer.partner?.id ?? null) &&
linkId === (customer.link?.id ?? null);
@@ -157,6 +162,22 @@ function ReattributeCustomerModal({
/>
</div>
+ {commissionsCountError && (
+ <div className="mt-5 flex items-center justify-between gap-3 rounded-lg border border-neutral-200 bg-neutral-50 p-3">
+ <p className="text-sm text-neutral-700">
+ Failed to load paid commissions. Retry before saving.
+ </p>
+ <Button
+ type="button"
+ variant="secondary"
+ text="Retry"
+ className="h-8 w-fit shrink-0"
+ loading={isCommissionsCountValidating}
+ onClick={() => retryCommissionsCount()}
+ />
+ </div>
+ )}
+
{showClawback && (
<label className="mt-5 flex items-start gap-2.5 rounded-lg border border-neutral-200 bg-neutral-50 p-3">
<Checkbox
@@ -193,7 +214,13 @@ function ReattributeCustomerModal({
text="Save"
className="h-9 w-fit"
loading={isSubmitting}
- disabled={!isDirty || isUnchanged || !partnerId || !linkId}
+ disabled={
+ !isDirty ||
+ isUnchanged ||
+ !partnerId ||
+ !linkId ||
+ Boolean(commissionsCountError)
+ }
/>
</div>
</form>