The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fix import-customers
Kiran K4df8fd35adfa
diff --git a/apps/web/app/(ee)/api/cron/import/firstpromoter/route.ts b/apps/web/app/(ee)/api/cron/import/firstpromoter/route.tsindex f087945895..2938b367b5 100644--- a/apps/web/app/(ee)/api/cron/import/firstpromoter/route.ts+++ b/apps/web/app/(ee)/api/cron/import/firstpromoter/route.ts@@ -27,12 +27,12 @@ export async function POST(req: Request) {case "import-partners":await importPartners(payload);break;- // case "import-customers":- // await importCustomers(payload);- // break;- // case "import-commissions":- // await importCommissions(payload);- // break;+ case "import-customers":+ await importCustomers(payload);+ break;+ case "import-commissions":+ await importCommissions(payload);+ break;// default:// throw new Error(`Unknown action: ${payload.action}`);}diff --git a/apps/web/lib/firstpromoter/api.ts b/apps/web/lib/firstpromoter/api.tsindex de9a37dbfa..88e7c78a4e 100644--- a/apps/web/lib/firstpromoter/api.ts+++ b/apps/web/lib/firstpromoter/api.ts@@ -1,5 +1,4 @@import { z } from "zod";-import { partnerStackLink } from "../partnerstack/schemas";import { PAGE_LIMIT } from "./importer";import {firstPromoterCampaignSchema,@@ -47,12 +46,10 @@ export class FirstPromoterApi {}async listCampaigns({ page }: { page?: number }) {- const params: Record<string, string> = {+ const searchParams = new URLSearchParams({per_page: PAGE_LIMIT.toString(),...(page ? { page: page.toString() } : {}),- };-- const searchParams = new URLSearchParams(params);+ });const campaigns = await this.fetch(`/promoter_campaigns?${searchParams.toString()}`,@@ -62,21 +59,17 @@ export class FirstPromoterApi {}async listPartners({ page }: { page?: number }) {- const filters = {- archived: false,- state: "accepted",- referrals_count: {- from: 1,- },- };-- const params: Record<string, string> = {- filters: JSON.stringify(filters),+ const searchParams = new URLSearchParams({+ filters: JSON.stringify({+ archived: false,+ state: "accepted",+ referrals_count: {+ from: 1,+ },+ }),per_page: PAGE_LIMIT.toString(),...(page ? { page: page.toString() } : {}),- };-- const searchParams = new URLSearchParams(params);+ });const response = await this.fetch(`/promoters?${searchParams.toString()}`);@@ -89,21 +82,11 @@ export class FirstPromoterApi {return partners;}- // TODO:- // Fix this- async listLinks({ identifier }: { identifier: string }) {- const links = await this.fetch(`/links/partnership/${identifier}`);-- return partnerStackLink.array().parse(links);- }-async listCustomers({ page }: { page?: number }) {- const params: Record<string, string> = {+ const searchParams = new URLSearchParams({per_page: PAGE_LIMIT.toString(),...(page ? { page: page.toString() } : {}),- };-- const searchParams = new URLSearchParams(params);+ });const customers = await this.fetch(`/referrals?${searchParams.toString()}`);@@ -111,12 +94,10 @@ export class FirstPromoterApi {}async listCommissions({ page }: { page?: number }) {- const params: Record<string, string> = {+ const searchParams = new URLSearchParams({per_page: PAGE_LIMIT.toString(),...(page ? { page: page.toString() } : {}),- };-- const searchParams = new URLSearchParams(params);+ });const commissions = await this.fetch(`/commissions?${searchParams.toString()}`,diff --git a/apps/web/lib/firstpromoter/import-customers.ts b/apps/web/lib/firstpromoter/import-customers.tsindex 3aa669e234..1bfde90edb 100644--- a/apps/web/lib/firstpromoter/import-customers.ts+++ b/apps/web/lib/firstpromoter/import-customers.ts@@ -45,6 +45,7 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {break;}+ // Find the partners by their email addressconst promoters = customers.map(({ promoter_campaign }) => promoter_campaign.promoter,);@@ -63,6 +64,7 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {const partnerIds = partners.map(({ id }) => id);if (partnerIds.length > 0) {+ // Find the program enrollments by the partner idsconst programEnrollments = await prisma.programEnrollment.findMany({where: {partnerId: {@@ -120,8 +122,8 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {await firstPromoterImporter.queue({...payload,- page: hasMore ? currentPage : undefined,action: hasMore ? "import-customers" : "import-commissions",+ page: hasMore ? currentPage : undefined,});}@@ -148,7 +150,7 @@ async function createCustomer({await logImportError({...commonImportLogInputs,code: "LINK_NOT_FOUND",- message: `Link not found for customer ${customer.uid}.`,+ message: `Link not found for customer ${customer.id}.`,});return;@@ -158,7 +160,7 @@ async function createCustomer({await logImportError({...commonImportLogInputs,code: "CUSTOMER_EMAIL_NOT_FOUND",- message: `Email not found for customer ${customer.uid}.`,+ message: `Email not found for customer ${customer.id}.`,});return;@@ -208,12 +210,16 @@ async function createCustomer({});const customerId = createId({ prefix: "cus_" });+ const customerName =+ (customer.first_name && customer.last_name+ ? `${customer.first_name} ${customer.last_name}`+ : customer.first_name || customer.last_name) || customer.email;try {await prisma.customer.create({data: {id: customerId,- name: customer.email,+ name: customerName,email: customer.email,projectId: workspace.id,projectConnectId: workspace.stripeConnectId,@@ -226,7 +232,7 @@ async function createCustomer({},});- await Promise.all([+ await Promise.allSettled([recordLeadWithTimestamp({...clickEvent,event_id: nanoid(16),diff --git a/apps/web/lib/firstpromoter/schemas.ts b/apps/web/lib/firstpromoter/schemas.tsindex 636c9d0874..c8cd1fd26c 100644--- a/apps/web/lib/firstpromoter/schemas.ts+++ b/apps/web/lib/firstpromoter/schemas.ts@@ -3,7 +3,6 @@ import { z } from "zod";export const firstPromoterImportSteps = z.enum(["import-campaigns","import-partners",- "import-links","import-customers","import-commissions",]);@@ -90,25 +89,16 @@ export const firstPromoterPartnerSchema = z.object({});export const firstPromoterCustomerSchema = z.object({- id: z.string(),+ id: z.number(),email: z.string(),+ first_name: z.string().nullable(),+ last_name: z.string().nullable(),uid: z.string(),- state: z.enum([- "subscribed",- "signup",- "active",- "cancelled",- "refunded",- "denied",- "pending",- "moved",- ]),- metadata: z.record(z.any()).nullable(),created_at: z.string(),- customer_since: z.string(),+ customer_since: z.string().nullable(),+ metadata: z.record(z.any()).nullable(),promoter_campaign: z.object({promoter: firstPromoterPartnerSchema.pick({- id: true,email: true,}),}),