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.

Refactor import logic and update schemas for affiliates and commissions

Kiran Kbad2606ebe8d
diff --git a/apps/web/lib/tolt/import-affiliates.ts b/apps/web/lib/tolt/import-affiliates.ts
index 81b06d8f01..f9cfd82752 100644
--- a/apps/web/lib/tolt/import-affiliates.ts
+++ b/apps/web/lib/tolt/import-affiliates.ts
@@ -71,7 +71,7 @@ export async function importAffiliates({
await toltImporter.queue({
programId,
- action: hasMore ? "import-affiliates" : "import-referrals",
+ action: hasMore ? "import-affiliates" : "import-links",
...(hasMore && { startingAfter }),
});
}
diff --git a/apps/web/lib/tolt/import-commissions.ts b/apps/web/lib/tolt/import-commissions.ts
index becdc907fb..7f652fad9a 100644
--- a/apps/web/lib/tolt/import-commissions.ts
+++ b/apps/web/lib/tolt/import-commissions.ts
@@ -13,6 +13,14 @@ import { ToltApi } from "./api";
import { toltImporter } from "./importer";
import { ToltCommission } from "./types";
+const toDubStatus: Record<ToltCommission["status"], CommissionStatus> = {
+ pending: "pending",
+ approved: "processed",
+ paid: "paid",
+ rejected: "canceled",
+ refunded: "refunded",
+};
+
export async function importCommissions({
programId,
startingAfter,
@@ -54,8 +62,9 @@ export async function importCommissions({
await Promise.all(
commissions.map((commission) =>
createCommission({
- commission,
+ workspaceId: workspace.id,
programId,
+ commission,
}),
),
);
@@ -115,9 +124,11 @@ export async function importCommissions({
// Backfill historical commissions
async function createCommission({
+ workspaceId,
programId,
commission,
}: {
+ workspaceId: string;
programId: string;
commission: ToltCommission;
}) {
@@ -166,7 +177,8 @@ async function createCommission({
const customerFound = await prisma.customer.findFirst({
where: {
- stripeCustomerId: customer.email,
+ projectId: workspaceId,
+ email: customer.email,
},
include: {
link: true,
@@ -175,7 +187,7 @@ async function createCommission({
if (!customerFound) {
console.log(
- `No customer found for customer ID ${customer.email}, skipping...`,
+ `No customer found for customer email ${customer.email}, skipping...`,
);
return;
}
@@ -208,13 +220,6 @@ async function createCommission({
const eventId = nanoid(16);
- const toDubStatus: Record<ToltCommission["status"], CommissionStatus> = {
- pending: "pending",
- approved: "paid",
- rejected: "canceled",
- refunded: "refunded",
- };
-
await Promise.all([
prisma.commission.create({
data: {
diff --git a/apps/web/lib/tolt/import-referrals.ts b/apps/web/lib/tolt/import-referrals.ts
index db28794424..330884bd5b 100644
--- a/apps/web/lib/tolt/import-referrals.ts
+++ b/apps/web/lib/tolt/import-referrals.ts
@@ -136,17 +136,17 @@ async function createReferral({
return;
}
- const customerFound = await prisma.customer.findFirst({
+ const customerFound = await prisma.customer.findUnique({
where: {
- email: customer.email,
- projectId: workspace.id,
+ projectId_externalId: {
+ projectId: workspace.id,
+ externalId: customer.customer_id,
+ },
},
});
- if (customerFound && customerFound.externalId !== customer.customer_id) {
- console.log(
- `A customer already exists with customer email, ${customer.email}`,
- );
+ if (customerFound) {
+ console.log(`A customer already exists with email ${customer.email}`);
return;
}
diff --git a/apps/web/lib/tolt/schemas.ts b/apps/web/lib/tolt/schemas.ts
index 0128404853..c60ab9f88c 100644
--- a/apps/web/lib/tolt/schemas.ts
+++ b/apps/web/lib/tolt/schemas.ts
@@ -14,7 +14,7 @@ export const ToltAffiliateSchema = z.object({
last_name: z.string(),
company_name: z.string().nullable(),
country_code: z.string().nullable(),
- status: z.enum(["active", "inactive"]).default("active"),
+ status: z.string().optional(),
program: ToltProgramSchema.optional(),
});
@@ -32,7 +32,7 @@ export const ToltCustomerSchema = z.object({
.describe("Internal customer identifier in client's app."),
email: z.string().nullable(),
name: z.string().nullable(),
- status: z.enum(["lead", "trialing", "active", "canceled"]),
+ status: z.string(),
created_at: z.string(),
updated_at: z.string(),
partner: ToltAffiliateSchema,
@@ -42,9 +42,9 @@ export const ToltCommissionSchema = z.object({
id: z.string(),
amount: z.string().describe("Amount of the commission in cents."),
revenue: z.string().describe("Revenue of the commission in cents."),
- transaction_id: z.string(),
+ transaction_id: z.string().nullable(), // this can be null
charge_id: z.string(),
- status: z.enum(["pending", "approved", "rejected", "refunded"]),
+ status: z.string(),
created_at: z.string(),
updated_at: z.string(),
partner: ToltAffiliateSchema.omit({
diff --git a/apps/web/lib/tolt/update-stripe-customers.ts b/apps/web/lib/tolt/update-stripe-customers.ts
index 7700dcd827..354652fcaa 100644
--- a/apps/web/lib/tolt/update-stripe-customers.ts
+++ b/apps/web/lib/tolt/update-stripe-customers.ts
@@ -4,8 +4,7 @@ import { log } from "@dub/utils";
import { stripeAppClient } from "../stripe";
import { MAX_BATCHES, toltImporter } from "./importer";
-// Stripe API have a default limit of 25 requests per second
-const CUSTOMERS_PER_BATCH = 25;
+const CUSTOMERS_PER_BATCH = 20;
const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { livemode: true }),