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 log to partnerstack

Kiran Kc6c15ab5c9eb
diff --git a/apps/web/lib/partnerstack/import-commissions.ts b/apps/web/lib/partnerstack/import-commissions.ts
index cac0ba7dd6..8fc1fee523 100644
--- a/apps/web/lib/partnerstack/import-commissions.ts
+++ b/apps/web/lib/partnerstack/import-commissions.ts
@@ -5,6 +5,7 @@ import { convertCurrencyWithFxRates } from "../analytics/convert-currency";
import { createId } from "../api/create-id";
import { syncTotalCommissions } from "../api/partners/sync-total-commissions";
import { getLeadEvent, recordSaleWithTimestamp } from "../tinybird";
+import { logImportError } from "../tinybird/log-import-error";
import { redis } from "../upstash";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { PartnerStackApi } from "./api";
@@ -23,7 +24,7 @@ const toDubStatus: Record<
};
export async function importCommissions(payload: PartnerStackImportPayload) {
- const { programId, startingAfter } = payload;
+ const { importId, programId, startingAfter } = payload;
const { workspaceId } = await prisma.program.findUniqueOrThrow({
where: {
@@ -65,6 +66,7 @@ export async function importCommissions(payload: PartnerStackImportPayload) {
programId,
commission,
fxRates,
+ importId,
}),
),
);
@@ -91,19 +93,39 @@ async function createCommission({
programId,
commission,
fxRates,
+ importId,
}: {
workspaceId: string;
programId: string;
commission: PartnerStackCommission;
fxRates: Record<string, string> | null;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspaceId,
+ import_id: importId,
+ source: "partnerstack",
+ entity: "commission",
+ entity_id: commission.key,
+ } as const;
+
if (!commission.transaction) {
- console.log(`Commission ${commission.key} has no transaction, skipping...`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "TRANSACTION_NOT_FOUND",
+ message: `Commission ${commission.key} has no transaction.`,
+ });
+
return;
}
if (!commission.customer) {
- console.log(`Commission ${commission.key} has no customer, skipping...`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CUSTOMER_NOT_FOUND",
+ message: `Commission ${commission.key} has no customer.`,
+ });
+
return;
}
@@ -135,9 +157,12 @@ async function createCommission({
});
if (!customer) {
- console.log(
- `No customer found for customer email ${commission.customer.email}, skipping...`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CUSTOMER_NOT_FOUND",
+ message: `No customer found for customer email ${commission.customer.email}.`,
+ });
+
return;
}
@@ -195,17 +220,32 @@ async function createCommission({
}
if (!customer.linkId) {
- console.log(`No link found for customer ${customer.id}, skipping...`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `No link found for customer ${customer.id}.`,
+ });
+
return;
}
if (!customer.clickId) {
- console.log(`No click ID found for customer ${customer.id}, skipping...`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CLICK_NOT_FOUND",
+ message: `No click found for customer ${customer.id}.`,
+ });
+
return;
}
if (!customer.link?.partnerId) {
- console.log(`No partner ID found for customer ${customer.id}, skipping...`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "PARTNER_NOT_FOUND",
+ message: `No partner found for customer ${customer.id}.`,
+ });
+
return;
}
@@ -214,7 +254,12 @@ async function createCommission({
});
if (!leadEvent || leadEvent.data.length === 0) {
- console.log(`No lead event found for customer ${customer.id}, skipping...`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LEAD_NOT_FOUND",
+ message: `No lead event found for customer ${customer.id}.`,
+ });
+
return;
}
diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.ts
index abb13b06f0..165c7d8372 100644
--- a/apps/web/lib/partnerstack/import-customers.ts
+++ b/apps/web/lib/partnerstack/import-customers.ts
@@ -3,6 +3,7 @@ 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 { redis } from "../upstash";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { PartnerStackApi } from "./api";
@@ -14,7 +15,7 @@ import {
import { PartnerStackCustomer, PartnerStackImportPayload } from "./types";
export async function importCustomers(payload: PartnerStackImportPayload) {
- const { programId, startingAfter } = payload;
+ const { importId, programId, startingAfter } = payload;
const program = await prisma.program.findUniqueOrThrow({
where: {
@@ -114,6 +115,7 @@ export async function importCustomers(payload: PartnerStackImportPayload) {
workspace: program.workspace,
links,
customer,
+ importId,
});
}),
);
@@ -140,20 +142,38 @@ async function createCustomer({
workspace,
links,
customer,
+ importId,
}: {
workspace: Pick<Project, "id" | "stripeConnectId">;
links: Pick<Link, "id" | "key" | "domain" | "url">[];
customer: PartnerStackCustomer;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "partnerstack",
+ entity: "customer",
+ entity_id: customer.customer_key || customer.email,
+ } as const;
+
if (links.length === 0) {
- console.log(
- `Link not found for customer. See the details at https://app.partnerstack.com/partners/${customer.partnership_key}/details`,
- );
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `Link not found for customer ${customer.customer_key}.`,
+ });
+
return;
}
if (!customer.email) {
- console.log("Customer email not found, skipping...");
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "CUSTOMER_EMAIL_NOT_FOUND",
+ message: `Email not found for customer ${customer.customer_key}.`,
+ });
+
return;
}
diff --git a/apps/web/lib/partnerstack/import-links.ts b/apps/web/lib/partnerstack/import-links.ts
index a9dd7d5e75..4ff85240bd 100644
--- a/apps/web/lib/partnerstack/import-links.ts
+++ b/apps/web/lib/partnerstack/import-links.ts
@@ -1,13 +1,14 @@
import { prisma } from "@dub/prisma";
import { createLink } from "../api/links";
import { generatePartnerLink } from "../api/partners/create-partner-link";
+import { logImportError } from "../tinybird/log-import-error";
import { PartnerProps, ProgramProps, WorkspaceProps } from "../types";
import { PartnerStackApi } from "./api";
import { partnerStackImporter } from "./importer";
import { PartnerStackImportPayload, PartnerStackLink } from "./types";
export async function importLinks(payload: PartnerStackImportPayload) {
- const { programId, userId, startingAfter } = payload;
+ const { importId, programId, userId, startingAfter } = payload;
const program = await prisma.program.findUniqueOrThrow({
where: {
@@ -83,6 +84,7 @@ export async function importLinks(payload: PartnerStackImportPayload) {
partner,
link,
userId,
+ importId,
}),
),
);
@@ -108,17 +110,32 @@ async function createPartnerLink({
partner,
link,
userId,
+ importId,
}: {
workspace: WorkspaceProps;
program: ProgramProps;
partner: Pick<PartnerProps, "id" | "name" | "email">;
link: PartnerStackLink;
userId: string;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: workspace.id,
+ import_id: importId,
+ source: "partnerstack",
+ entity: "link",
+ entity_id: link.key,
+ } as const;
+
const key = link.url.split("/").pop();
if (!key) {
- console.error(`No key found in the link ${link.url}`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "LINK_NOT_FOUND",
+ message: `No key found in the link ${link.url}`,
+ });
+
return null;
}
diff --git a/apps/web/lib/partnerstack/import-partners.ts b/apps/web/lib/partnerstack/import-partners.ts
index de95f98b41..2ee999dbfc 100644
--- a/apps/web/lib/partnerstack/import-partners.ts
+++ b/apps/web/lib/partnerstack/import-partners.ts
@@ -2,6 +2,7 @@ import { prisma } from "@dub/prisma";
import { Program, Reward } from "@dub/prisma/client";
import { COUNTRIES } from "@dub/utils";
import { createId } from "../api/create-id";
+import { logImportError } from "../tinybird/log-import-error";
import { redis } from "../upstash";
import { REWARD_EVENT_COLUMN_MAPPING } from "../zod/schemas/rewards";
import { PartnerStackApi } from "./api";
@@ -13,7 +14,7 @@ import {
import { PartnerStackImportPayload, PartnerStackPartner } from "./types";
export async function importPartners(payload: PartnerStackImportPayload) {
- const { programId, startingAfter } = payload;
+ const { importId, programId, startingAfter } = payload;
const program = await prisma.program.findUniqueOrThrow({
where: {
@@ -62,6 +63,7 @@ export async function importPartners(payload: PartnerStackImportPayload) {
program,
partner,
reward,
+ importId,
}),
),
);
@@ -83,13 +85,28 @@ async function createPartner({
program,
partner,
reward,
+ importId,
}: {
program: Program;
partner: PartnerStackPartner;
reward?: Pick<Reward, "id" | "event">;
+ importId: string;
}) {
+ const commonImportLogInputs = {
+ workspace_id: program.workspaceId,
+ import_id: importId,
+ source: "partnerstack",
+ entity: "partner",
+ entity_id: partner.key,
+ } as const;
+
if (partner.stats.CUSTOMER_COUNT === 0) {
- console.log(`No leads found for partner ${partner.email}`);
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "INACTIVE_PARTNER",
+ message: `No leads found for partner ${partner.email}`,
+ });
+
return;
}
diff --git a/apps/web/lib/partnerstack/update-stripe-customers.ts b/apps/web/lib/partnerstack/update-stripe-customers.ts
index 9fd68557e7..63a90c5d99 100644
--- a/apps/web/lib/partnerstack/update-stripe-customers.ts
+++ b/apps/web/lib/partnerstack/update-stripe-customers.ts
@@ -4,6 +4,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, partnerStackImporter } from "./importer";
import { PartnerStackImportPayload } from "./types";
@@ -18,7 +19,7 @@ const stripe = stripeAppClient({
export async function updateStripeCustomers(
payload: PartnerStackImportPayload,
) {
- const { programId, userId, startingAfter } = payload;
+ const { importId, programId, userId, startingAfter } = payload;
const { workspace, ...program } = await prisma.program.findUniqueOrThrow({
where: {
@@ -80,6 +81,7 @@ export async function updateStripeCustomers(
searchStripeAndUpdateCustomer({
workspace,
customer,
+ importId,
}),
),
);
@@ -132,10 +134,20 @@ export async function updateStripeCustomers(
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: "partnerstack",
+ entity: "customer",
+ entity_id: customer.id,
+ } as const;
+
const stripeCustomers = await stripe.customers.search(
{
query: `email:'${customer.email}'`,
@@ -146,7 +158,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;
}
@@ -161,9 +178,11 @@ async function searchStripeAndUpdateCustomer({
if (partnerStackStripeCustomer) {
stripeCustomer = partnerStackStripeCustomer;
} else {
- console.error(
- `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.customer_key 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/tolt/import-customers.ts b/apps/web/lib/tolt/import-customers.ts
index 608d077124..40cc54387e 100644
--- a/apps/web/lib/tolt/import-customers.ts
+++ b/apps/web/lib/tolt/import-customers.ts
@@ -131,13 +131,13 @@ async function createReferral({
workspace_id: workspace.id,
import_id: importId,
source: "tolt",
+ entity: "customer",
+ entity_id: customer.customer_id,
} as const;
if (links.length === 0) {
await logImportError({
...commonImportLogInputs,
- entity: "customer",
- entity_id: customer.customer_id,
code: "LINK_NOT_FOUND",
message: `Link not found for customer ${customer.customer_id}`,
});
diff --git a/apps/web/lib/zod/schemas/import-error-log.ts b/apps/web/lib/zod/schemas/import-error-log.ts
index 3327354752..e55e4b8ee9 100644
--- a/apps/web/lib/zod/schemas/import-error-log.ts
+++ b/apps/web/lib/zod/schemas/import-error-log.ts
@@ -11,6 +11,7 @@ export const importErrorLogSchema = z.object({
"PARTNER_NOT_FOUND",
"LINK_NOT_FOUND",
"CUSTOMER_NOT_FOUND",
+ "CUSTOMER_EMAIL_NOT_FOUND",
"LEAD_NOT_FOUND",
"CLICK_NOT_FOUND",
"STRIPE_CUSTOMER_NOT_FOUND",