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.

Improve commissions import efficiency with getLeadEvents

Steven Teybf5f4b464966
diff --git a/apps/web/lib/rewardful/import-commissions.ts b/apps/web/lib/rewardful/import-commissions.ts
index 20ea4af784..8f716200aa 100644
--- a/apps/web/lib/rewardful/import-commissions.ts
+++ b/apps/web/lib/rewardful/import-commissions.ts
@@ -2,13 +2,14 @@ import { sendEmail } from "@dub/email";
import ProgramImported from "@dub/email/templates/program-imported";
import { prisma } from "@dub/prisma";
import { nanoid } from "@dub/utils";
-import { CommissionStatus, Program } from "@prisma/client";
+import { CommissionStatus, Customer, Link, Program } from "@prisma/client";
import { convertCurrencyWithFxRates } from "../analytics/convert-currency";
import { createId } from "../api/create-id";
import { syncTotalCommissions } from "../api/partners/sync-total-commissions";
-import { getLeadEvent } from "../tinybird";
+import { getLeadEvents } from "../tinybird/get-lead-events";
import { logImportError } from "../tinybird/log-import-error";
import { recordSaleWithTimestamp } from "../tinybird/record-sale";
+import { LeadEventTB } from "../types";
import { redis } from "../upstash";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { RewardfulApi } from "./api";
@@ -51,6 +52,23 @@ export async function importCommissions(payload: RewardfulImportPayload) {
break;
}
+ const customersData = await prisma.customer.findMany({
+ where: {
+ stripeCustomerId: {
+ in: commissions
+ .map((commission) => commission.sale.referral.stripe_customer_id)
+ .filter(Boolean),
+ },
+ },
+ include: {
+ link: true,
+ },
+ });
+
+ const customerLeadEvents = await getLeadEvents({
+ customerIds: customersData.map((customer) => customer.id),
+ }).then((res) => res.data);
+
await Promise.all(
commissions.map((commission) =>
createCommission({
@@ -59,6 +77,8 @@ export async function importCommissions(payload: RewardfulImportPayload) {
campaignId,
fxRates,
importId,
+ customersData,
+ customerLeadEvents,
}),
),
);
@@ -115,12 +135,16 @@ async function createCommission({
campaignId,
fxRates,
importId,
+ customersData,
+ customerLeadEvents,
}: {
commission: RewardfulCommission;
program: Program;
campaignId: string;
fxRates: Record<string, string> | null;
importId: string;
+ customersData: (Customer & { link: Link | null })[];
+ customerLeadEvents: LeadEventTB[];
}) {
const commonImportLogInputs = {
workspace_id: program.workspaceId,
@@ -223,14 +247,10 @@ async function createCommission({
return;
}
- const customerFound = await prisma.customer.findUnique({
- where: {
- stripeCustomerId: sale.referral.stripe_customer_id,
- },
- include: {
- link: true,
- },
- });
+ const customerFound = customersData.find(
+ (customer) =>
+ customer.stripeCustomerId === sale.referral.stripe_customer_id,
+ );
if (!customerFound) {
await logImportError({
@@ -280,11 +300,11 @@ async function createCommission({
return;
}
- const leadEvent = await getLeadEvent({
- customerId: customerFound.id,
- });
+ const leadEvent = customerLeadEvents.find(
+ (event) => event.customer_id === customerFound.id,
+ );
- if (!leadEvent || leadEvent.data.length === 0) {
+ if (!leadEvent) {
await logImportError({
...commonImportLogInputs,
entity: "commission",
@@ -298,7 +318,7 @@ async function createCommission({
const clickData = clickEventSchemaTB
.omit({ timestamp: true })
- .parse(leadEvent.data[0]);
+ .parse(leadEvent);
const eventId = nanoid(16);
diff --git a/apps/web/lib/tinybird/get-lead-events.ts b/apps/web/lib/tinybird/get-lead-events.ts
new file mode 100644
index 0000000000..8d081edbc0
--- /dev/null
+++ b/apps/web/lib/tinybird/get-lead-events.ts
@@ -0,0 +1,11 @@
+import z from "../zod";
+import { leadEventSchemaTB } from "../zod/schemas/leads";
+import { tb } from "./client";
+
+export const getLeadEvents = tb.buildPipe({
+ pipe: "get_lead_events",
+ parameters: z.object({
+ customerIds: z.string().array(),
+ }),
+ data: leadEventSchemaTB,
+});
diff --git a/apps/web/scripts/backfill-customers.ts b/apps/web/scripts/backfill-customers.ts
index 260c5b6907..d412e6292c 100644
--- a/apps/web/scripts/backfill-customers.ts
+++ b/apps/web/scripts/backfill-customers.ts
@@ -2,16 +2,9 @@ import { prisma } from "@dub/prisma";
import { Prisma } from "@dub/prisma/client";
import "dotenv-flow/config";
import { tb } from "../lib/tinybird/client";
+import { getLeadEvents } from "../lib/tinybird/get-lead-events";
import z from "../lib/zod";
-export const getLeadEvents = tb.buildPipe({
- pipe: "get_lead_events",
- parameters: z.object({
- customerIds: z.array(z.string()),
- }),
- data: z.any(),
-});
-
export const getClickEvents = tb.buildPipe({
pipe: "get_click_events",
parameters: z.object({