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.

add import error logs to tolt

Kiran Ke1b6d8104ee1
diff --git a/apps/web/lib/rewardful/import-commissions.ts b/apps/web/lib/rewardful/import-commissions.ts
index 8f716200aa..c16e3ba52b 100644
--- a/apps/web/lib/rewardful/import-commissions.ts
+++ b/apps/web/lib/rewardful/import-commissions.ts
@@ -150,6 +150,8 @@ async function createCommission({
workspace_id: program.workspaceId,
import_id: importId,
source: "rewardful",
+ entity: "commission",
+ entity_id: commission.id,
} as const;
if (commission.campaign.id !== campaignId) {
@@ -168,9 +170,7 @@ async function createCommission({
) {
await logImportError({
...commonImportLogInputs,
- entity: "commission",
- entity_id: commission.id,
- code: "STRIPE_CUSTOMER_ID_NOT_FOUND",
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
message: `No Stripe customer ID provided for referral ${sale.referral.id}`,
});
@@ -255,8 +255,6 @@ async function createCommission({
if (!customerFound) {
await logImportError({
...commonImportLogInputs,
- entity: "commission",
- entity_id: commission.id,
code: "CUSTOMER_NOT_FOUND",
message: `No customer found for Stripe customer ID ${sale.referral.stripe_customer_id}.`,
});
@@ -267,8 +265,6 @@ async function createCommission({
if (!customerFound.linkId) {
await logImportError({
...commonImportLogInputs,
- entity: "commission",
- entity_id: commission.id,
code: "LINK_NOT_FOUND",
message: `No link found for customer ${customerFound.id}.`,
});
@@ -291,8 +287,6 @@ async function createCommission({
if (!customerFound.link?.partnerId) {
await logImportError({
...commonImportLogInputs,
- entity: "commission",
- entity_id: commission.id,
code: "PARTNER_NOT_FOUND",
message: `No partner ID found for customer ${customerFound.id}.`,
});
@@ -307,8 +301,6 @@ async function createCommission({
if (!leadEvent) {
await logImportError({
...commonImportLogInputs,
- entity: "commission",
- entity_id: commission.id,
code: "LEAD_NOT_FOUND",
message: `No lead event found for customer ${customerFound.id}.`,
});
diff --git a/apps/web/lib/rewardful/import-customers.ts b/apps/web/lib/rewardful/import-customers.ts
index 80de88cb3d..02e2bc7bdc 100644
--- a/apps/web/lib/rewardful/import-customers.ts
+++ b/apps/web/lib/rewardful/import-customers.ts
@@ -77,12 +77,6 @@ async function createCustomer({
campaignId: string;
importId: string;
}) {
- const commonImportLogInputs = {
- workspace_id: workspace.id,
- import_id: importId,
- source: "rewardful",
- } as const;
-
const referralId = referral.customer ? referral.customer.email : referral.id;
if (
referral.affiliate?.campaign?.id &&
@@ -95,6 +89,14 @@ async function createCustomer({
return;
}
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "rewardful",
+ entity: "customer",
+ entity_id: referralId,
+ } as const;
+
const link = await prisma.link.findUnique({
where: {
domain_key: {
@@ -107,8 +109,6 @@ async function createCustomer({
if (!link) {
await logImportError({
...commonImportLogInputs,
- entity: "customer",
- entity_id: referralId,
code: "LINK_NOT_FOUND",
message: `Link not found for referral ${referralId} (token: ${referral.link.token}).`,
});
@@ -122,9 +122,7 @@ async function createCustomer({
) {
await logImportError({
...commonImportLogInputs,
- entity: "customer",
- entity_id: referralId,
- code: "STRIPE_CUSTOMER_ID_NOT_FOUND",
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
message: `No Stripe customer ID provided for referral ${referralId}.`,
});
diff --git a/apps/web/lib/rewardful/import-partners.ts b/apps/web/lib/rewardful/import-partners.ts
index 331f562842..89deb22f81 100644
--- a/apps/web/lib/rewardful/import-partners.ts
+++ b/apps/web/lib/rewardful/import-partners.ts
@@ -43,6 +43,13 @@ export async function importPartners(payload: RewardfulImportPayload) {
},
});
+ const commonImportLogInputs = {
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "rewardful",
+ entity: "partner",
+ } as const;
+
while (hasMore && processedBatches < MAX_BATCHES) {
const affiliates = await rewardfulApi.listPartners({
campaignId,
@@ -78,17 +85,10 @@ export async function importPartners(payload: RewardfulImportPayload) {
);
}
- const commonImportLogInputs = {
- workspace_id: program.workspaceId,
- import_id: importId,
- source: "rewardful",
- } as const;
-
if (notImportedAffiliates.length > 0) {
await logImportError(
notImportedAffiliates.map((affiliate) => ({
...commonImportLogInputs,
- entity: "partner",
entity_id: affiliate.id,
code: "INACTIVE_PARTNER",
message: `Partner ${affiliate.email} not imported because it is not active or has no leads.`,
diff --git a/apps/web/lib/tolt/import-commissions.ts b/apps/web/lib/tolt/import-commissions.ts
index aab07eb4b8..c59845edbb 100644
--- a/apps/web/lib/tolt/import-commissions.ts
+++ b/apps/web/lib/tolt/import-commissions.ts
@@ -7,6 +7,7 @@ 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 { logImportError } from "../tinybird/log-import-error";
import { recordSaleWithTimestamp } from "../tinybird/record-sale";
import { redis } from "../upstash";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
@@ -23,7 +24,7 @@ const toDubStatus: Record<ToltCommission["status"], CommissionStatus> = {
};
export async function importCommissions(payload: ToltImportPayload) {
- let { programId, toltProgramId, userId, startingAfter } = payload;
+ let { importId, programId, toltProgramId, userId, startingAfter } = payload;
const program = await prisma.program.findUniqueOrThrow({
where: {
@@ -67,6 +68,7 @@ export async function importCommissions(payload: ToltImportPayload) {
commission,
fxRates,
programCurrency: toltProgram.currency_code.toLowerCase(),
+ importId,
}),
),
);
@@ -114,6 +116,7 @@ export async function importCommissions(payload: ToltImportPayload) {
workspace,
program,
provider: "Tolt",
+ importId,
}),
});
}
@@ -132,19 +135,32 @@ async function createCommission({
commission,
fxRates,
programCurrency,
+ importId,
}: {
workspaceId: string;
programId: string;
commission: ToltCommission;
fxRates: Record<string, string> | null;
programCurrency: string;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspaceId,
+ import_id: importId,
+ source: "tolt",
+ entity: "commission",
+ entity_id: commission.id,
+ } as const;
+
const { customer, partner, ...sale } = commission;
if (!sale.transaction_id) {
- console.log(
- `Commission ${commission.id} has no transaction ID, skipping...`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "TRANSACTION_NOT_FOUND",
+ message: `No transaction ID provided for commission ${commission.id}`,
+ });
+
return;
}
@@ -173,9 +189,12 @@ async function createCommission({
});
if (!customerFound) {
- console.log(
- `No customer found for customer email ${customer.email}, skipping...`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CUSTOMER_NOT_FOUND",
+ message: `No customer ${customer.email} found for commission ${commission.id}.`,
+ });
+
return;
}
@@ -230,14 +249,33 @@ async function createCommission({
return;
}
- if (
- !customerFound.linkId ||
- !customerFound.clickId ||
- !customerFound.link?.partnerId
- ) {
- console.log(
- `No link or click ID or partner ID found for customer ${customerFound.id}, skipping...`,
- );
+ if (!customerFound.linkId) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `No link found for customer ${customerFound.id}.`,
+ });
+
+ return;
+ }
+
+ if (!customerFound.clickId) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CLICK_NOT_FOUND",
+ message: `No click found for customer ${customerFound.id}.`,
+ });
+
+ return;
+ }
+
+ if (!customerFound.link?.partnerId) {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "PARTNER_NOT_FOUND",
+ message: `No partner found for customer ${customerFound.id}.`,
+ });
+
return;
}
@@ -246,9 +284,12 @@ async function createCommission({
});
if (!leadEvent || leadEvent.data.length === 0) {
- console.log(
- `No lead event found for customer ${customerFound.id}, skipping...`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LEAD_NOT_FOUND",
+ message: `No lead event found for customer ${customerFound.id}.`,
+ });
+
return;
}
diff --git a/apps/web/lib/tolt/import-customers.ts b/apps/web/lib/tolt/import-customers.ts
index 7c48b4b2d7..608d077124 100644
--- a/apps/web/lib/tolt/import-customers.ts
+++ b/apps/web/lib/tolt/import-customers.ts
@@ -3,13 +3,14 @@ import { nanoid } from "@dub/utils";
import { Link, Project } from "@prisma/client";
import { createId } from "../api/create-id";
import { recordClick, recordLeadWithTimestamp } from "../tinybird";
+import { logImportError } from "../tinybird/log-import-error";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { ToltApi } from "./api";
import { MAX_BATCHES, toltImporter } from "./importer";
import { ToltAffiliate, ToltCustomer, ToltImportPayload } from "./types";
export async function importCustomers(payload: ToltImportPayload) {
- let { programId, toltProgramId, startingAfter } = payload;
+ let { importId, programId, toltProgramId, startingAfter } = payload;
const { workspace } = await prisma.program.findUniqueOrThrow({
where: {
@@ -94,6 +95,7 @@ export async function importCustomers(payload: ToltImportPayload) {
customer,
partner,
links: partnerEmailToLinks.get(partner.email) ?? [],
+ importId,
}),
),
);
@@ -117,17 +119,29 @@ async function createReferral({
workspace,
links,
partner,
+ importId,
}: {
customer: Omit<ToltCustomer, "partner">;
partner: ToltAffiliate;
workspace: Pick<Project, "id" | "stripeConnectId">;
links: Pick<Link, "id" | "key" | "domain" | "url">[];
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "tolt",
+ } as const;
+
if (links.length === 0) {
- console.log("Link not found for referral, skipping...", {
- customerEmail: customer.email,
- partnerEmail: partner.email,
+ await logImportError({
+ ...commonImportLogInputs,
+ entity: "customer",
+ entity_id: customer.customer_id,
+ code: "LINK_NOT_FOUND",
+ message: `Link not found for customer ${customer.customer_id}`,
});
+
return;
}
diff --git a/apps/web/lib/tolt/import-partners.ts b/apps/web/lib/tolt/import-partners.ts
index db430812a6..4c0ced154f 100644
--- a/apps/web/lib/tolt/import-partners.ts
+++ b/apps/web/lib/tolt/import-partners.ts
@@ -1,13 +1,14 @@
import { prisma } from "@dub/prisma";
import { Partner, Program, Reward } from "@dub/prisma/client";
import { createId } from "../api/create-id";
+import { logImportError } from "../tinybird/log-import-error";
import { REWARD_EVENT_COLUMN_MAPPING } from "../zod/schemas/rewards";
import { ToltApi } from "./api";
import { MAX_BATCHES, toltImporter } from "./importer";
import { ToltAffiliate, ToltImportPayload } from "./types";
export async function importPartners(payload: ToltImportPayload) {
- let { programId, toltProgramId, startingAfter } = payload;
+ let { importId, programId, toltProgramId, startingAfter } = payload;
const program = await prisma.program.findUniqueOrThrow({
where: {
@@ -34,6 +35,12 @@ export async function importPartners(payload: ToltImportPayload) {
const clickReward = program.rewards.find((r) => r.event === "click");
const defaultReward = saleReward || leadReward || clickReward;
+ const commonImportLogInputs = {
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "tolt",
+ } as const;
+
while (hasMore && processedBatches < MAX_BATCHES) {
const affiliates = await toltApi.listPartners({
programId: toltProgramId,
@@ -45,9 +52,16 @@ export async function importPartners(payload: ToltImportPayload) {
break;
}
- const activeAffiliates = affiliates.filter(
- ({ status }) => status === "active",
- );
+ const activeAffiliates: typeof affiliates = [];
+ const notImportedAffiliates: typeof affiliates = [];
+
+ for (const affiliate of affiliates) {
+ if (affiliate.status === "active") {
+ activeAffiliates.push(affiliate);
+ } else {
+ notImportedAffiliates.push(affiliate);
+ }
+ }
if (activeAffiliates.length > 0) {
const partnersPromise = await Promise.allSettled(
@@ -74,6 +88,18 @@ export async function importPartners(payload: ToltImportPayload) {
}
}
+ if (notImportedAffiliates.length > 0) {
+ await logImportError(
+ notImportedAffiliates.map((affiliate) => ({
+ ...commonImportLogInputs,
+ entity: "partner",
+ entity_id: affiliate.id,
+ code: "INACTIVE_PARTNER",
+ message: `Partner ${affiliate.email} not imported because it is not active.`,
+ })),
+ );
+ }
+
await new Promise((resolve) => setTimeout(resolve, 2000));
processedBatches++;
diff --git a/apps/web/lib/tolt/update-stripe-customers.ts b/apps/web/lib/tolt/update-stripe-customers.ts
index cd5258a16e..826e91a9cd 100644
--- a/apps/web/lib/tolt/update-stripe-customers.ts
+++ b/apps/web/lib/tolt/update-stripe-customers.ts
@@ -2,6 +2,7 @@ import { prisma } from "@dub/prisma";
import { Customer, Project } from "@dub/prisma/client";
import Stripe from "stripe";
import { stripeAppClient } from "../stripe";
+import { logImportError } from "../tinybird/log-import-error";
import { MAX_BATCHES, toltImporter } from "./importer";
import { ToltImportPayload } from "./types";
@@ -14,7 +15,7 @@ const stripe = stripeAppClient({
// Tolt API doesn't return the Stripe customer ID,
// so we'll search for Stripe customers by email and update the customer record with the Stripe customer ID, if found.
export async function updateStripeCustomers(payload: ToltImportPayload) {
- let { programId, startingAfter } = payload;
+ let { importId, programId, startingAfter } = payload;
const { workspace } = await prisma.program.findUniqueOrThrow({
where: {
@@ -74,6 +75,7 @@ export async function updateStripeCustomers(payload: ToltImportPayload) {
searchStripeAndUpdateCustomer({
workspace,
customer,
+ importId,
}),
),
);
@@ -94,10 +96,20 @@ export async function updateStripeCustomers(payload: ToltImportPayload) {
async function searchStripeAndUpdateCustomer({
workspace,
customer,
+ importId,
}: {
workspace: Pick<Project, "id" | "slug" | "stripeConnectId">;
customer: Pick<Customer, "id" | "email">;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "tolt",
+ entity: "customer",
+ entity_id: customer.id,
+ } as const;
+
const stripeCustomers = await stripe.customers.search(
{
query: `email:'${customer.email}'`,
@@ -108,7 +120,12 @@ async function searchStripeAndUpdateCustomer({
);
if (stripeCustomers.data.length === 0) {
- console.error(`Stripe search returned no customer for ${customer.email}`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned no customer for ${customer.email}`,
+ });
+
return null;
}
@@ -123,9 +140,11 @@ async function searchStripeAndUpdateCustomer({
if (toltReferralStripeCustomer) {
stripeCustomer = toltReferralStripeCustomer;
} else {
- console.error(
- `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
+ });
return null;
}
diff --git a/apps/web/lib/zod/schemas/import-error-log.ts b/apps/web/lib/zod/schemas/import-error-log.ts
index e22ddfa7fb..3327354752 100644
--- a/apps/web/lib/zod/schemas/import-error-log.ts
+++ b/apps/web/lib/zod/schemas/import-error-log.ts
@@ -13,7 +13,8 @@ export const importErrorLogSchema = z.object({
"CUSTOMER_NOT_FOUND",
"LEAD_NOT_FOUND",
"CLICK_NOT_FOUND",
- "STRIPE_CUSTOMER_ID_NOT_FOUND",
+ "STRIPE_CUSTOMER_NOT_FOUND",
+ "TRANSACTION_NOT_FOUND"
]),
message: z.string(),
});