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.

address coderabbit feedback

Steven Teyf93e714153fb
diff --git a/apps/web/lib/actions/partners/create-manual-commission.ts b/apps/web/lib/actions/partners/create-manual-commission.ts
index c2e554016c..5aa8b90b94 100644
--- a/apps/web/lib/actions/partners/create-manual-commission.ts
+++ b/apps/web/lib/actions/partners/create-manual-commission.ts
@@ -134,9 +134,13 @@ export const createManualCommissionAction = authActionClient
}
const tbEventsToRecord: Promise<any>[] = []; // a list of promises of events to record in Tinybird
- const commissionsToTransferEventIds: string[] = []; // eventIds for the commissions to transfer to the new customer-partner pair – we need to nullify them later
+ const commissionsToTransferEventIds: string[] = []; // eventIds for the commissions to transfer to the new customer-partner pair – we need to nullify them later
const commissionsToCreate: CreatePartnerCommissionProps[] = [];
+ // Track event timestamps for updating link stats
+ let leadEventTimestamp: Date | null = null;
+ let saleEventTimestamp: Date | null = null;
+
// If we're using existing events, we need to duplicate them under the new customer.id
if (useExistingEvents) {
if (!customer.linkId) {
@@ -223,6 +227,8 @@ export const createManualCommissionAction = authActionClient
customer: { country: customer.country },
},
});
+ // Track the lead event timestamp for link stats update
+ leadEventTimestamp = new Date(leadEventData.timestamp + "Z");
}
}
@@ -279,6 +285,13 @@ export const createManualCommissionAction = authActionClient
},
})),
);
+ // Track the latest sale event timestamp for link stats update
+ const latestSaleTimestamp = Math.max(
+ ...saleEventsData.map((data) =>
+ new Date(data.timestamp + "Z").getTime(),
+ ),
+ );
+ saleEventTimestamp = new Date(latestSaleTimestamp);
}
totalSales = existingSaleEvents.length;
totalSaleAmount = existingSaleEvents.reduce(
@@ -373,6 +386,8 @@ export const createManualCommissionAction = authActionClient
customer: { country: customer.country },
},
});
+ // Track the lead event timestamp for link stats update
+ leadEventTimestamp = new Date(leadEventData.timestamp);
}
// Prepare sale event if requested
@@ -413,6 +428,8 @@ export const createManualCommissionAction = authActionClient
sale: { productId },
},
});
+ // Track the sale event timestamp for link stats update
+ saleEventTimestamp = new Date(saleEventData.timestamp);
}
}
}
@@ -450,14 +467,14 @@ export const createManualCommissionAction = authActionClient
},
lastLeadAt: updateLinkStatsForImporter({
currentTimestamp: link.lastLeadAt,
- newTimestamp: new Date(),
+ newTimestamp: leadEventTimestamp || new Date(),
}),
conversions: {
increment: 1,
},
lastConversionAt: updateLinkStatsForImporter({
currentTimestamp: link.lastConversionAt,
- newTimestamp: new Date(),
+ newTimestamp: saleEventTimestamp || new Date(),
}),
}),
sales: {
diff --git a/apps/web/lib/firstpromoter/import-customers.ts b/apps/web/lib/firstpromoter/import-customers.ts
index ba1625d9e7..8e2f1d5d76 100644
--- a/apps/web/lib/firstpromoter/import-customers.ts
+++ b/apps/web/lib/firstpromoter/import-customers.ts
@@ -100,6 +100,23 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {
{} as Record<string, (typeof programEnrollments)[number]["links"]>,
);
+ const partnerEmailToLatestLeadAt = customers.reduce(
+ (acc, customer) => {
+ if (!customer.promoter_campaign.promoter.email) {
+ return acc;
+ }
+ const existing =
+ acc[customer.promoter_campaign.promoter.email] ?? new Date(0);
+ if (new Date(customer.created_at) > existing) {
+ acc[customer.promoter_campaign.promoter.email] = new Date(
+ customer.created_at,
+ );
+ }
+ return acc;
+ },
+ {} as Record<string, Date>,
+ );
+
await Promise.allSettled(
customers.map((customer) => {
const links =
@@ -110,6 +127,10 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {
workspace,
links,
customer,
+ latestLeadAt:
+ partnerEmailToLatestLeadAt[
+ customer.promoter_campaign.promoter.email
+ ],
importId,
});
}),
@@ -133,11 +154,13 @@ async function createCustomer({
workspace,
links,
customer,
+ latestLeadAt,
importId,
}: {
workspace: Pick<Project, "id" | "stripeConnectId">;
links: Pick<Link, "id" | "key" | "domain" | "url" | "lastLeadAt">[];
customer: FirstPromoterCustomer;
+ latestLeadAt: Date;
importId: string;
}) {
const commonImportLogInputs = {
@@ -254,7 +277,7 @@ async function createCustomer({
},
lastLeadAt: updateLinkStatsForImporter({
currentTimestamp: link.lastLeadAt,
- newTimestamp: new Date(customer.created_at),
+ newTimestamp: latestLeadAt,
}),
},
}),
diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.ts
index 5ef57441fa..b87bdcef56 100644
--- a/apps/web/lib/partnerstack/import-customers.ts
+++ b/apps/web/lib/partnerstack/import-customers.ts
@@ -108,6 +108,20 @@ export async function importCustomers(payload: PartnerStackImportPayload) {
partnerIdToLinks.set(partnerId, [...existing, ...links]);
}
+ const partnerKeysToLatestLeadAt = customers.reduce(
+ (acc, customer) => {
+ if (!customer.partnership_key) {
+ return acc;
+ }
+ const existing = acc[customer.partnership_key] ?? new Date(0);
+ if (new Date(customer.created_at) > existing) {
+ acc[customer.partnership_key] = new Date(customer.created_at);
+ }
+ return acc;
+ },
+ {} as Record<string, Date>,
+ );
+
await Promise.allSettled(
customers.map((customer) => {
const partnerId = partnerKeysToId[customer.partnership_key];
@@ -117,6 +131,7 @@ export async function importCustomers(payload: PartnerStackImportPayload) {
workspace: program.workspace,
links,
customer,
+ latestLeadAt: partnerKeysToLatestLeadAt[customer.partnership_key],
importId,
});
}),
@@ -144,11 +159,13 @@ async function createCustomer({
workspace,
links,
customer,
+ latestLeadAt,
importId,
}: {
workspace: Pick<Project, "id" | "stripeConnectId">;
links: Pick<Link, "id" | "key" | "domain" | "url" | "lastLeadAt">[];
customer: PartnerStackCustomer;
+ latestLeadAt: Date;
importId: string;
}) {
const commonImportLogInputs = {
@@ -264,7 +281,7 @@ async function createCustomer({
},
lastLeadAt: updateLinkStatsForImporter({
currentTimestamp: link.lastLeadAt,
- newTimestamp: new Date(customer.created_at),
+ newTimestamp: latestLeadAt,
}),
},
}),
diff --git a/apps/web/lib/tolt/import-customers.ts b/apps/web/lib/tolt/import-customers.ts
index 9f4a2f7410..f322383a96 100644
--- a/apps/web/lib/tolt/import-customers.ts
+++ b/apps/web/lib/tolt/import-customers.ts
@@ -90,12 +90,28 @@ export async function importCustomers(payload: ToltImportPayload) {
partnerEmailToLinks.set(partner.email, links);
}
+ // get the latest lead at for each partner link
+ const partnerEmailToLatestLeadAt = customers.reduce(
+ (acc, customer) => {
+ if (!customer.partner.email) {
+ return acc;
+ }
+ const existing = acc[customer.partner.email] ?? new Date(0);
+ if (new Date(customer.created_at) > existing) {
+ acc[customer.partner.email] = new Date(customer.created_at);
+ }
+ return acc;
+ },
+ {} as Record<string, Date>,
+ );
+
await Promise.allSettled(
customers.map(({ partner, ...customer }) =>
createReferral({
workspace,
customer,
links: partnerEmailToLinks.get(partner.email) ?? [],
+ latestLeadAt: partnerEmailToLatestLeadAt[partner.email],
importId,
}),
),
@@ -119,11 +135,13 @@ async function createReferral({
customer,
workspace,
links,
+ latestLeadAt,
importId,
}: {
customer: Omit<ToltCustomer, "partner">;
workspace: Pick<Project, "id" | "stripeConnectId">;
links: Pick<Link, "id" | "key" | "domain" | "url" | "lastLeadAt">[];
+ latestLeadAt: Date;
importId: string;
}) {
const commonImportLogInputs = {
@@ -236,7 +254,7 @@ async function createReferral({
},
lastLeadAt: updateLinkStatsForImporter({
currentTimestamp: link.lastLeadAt,
- newTimestamp: new Date(customer.created_at),
+ newTimestamp: latestLeadAt,
}),
},
}),