The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
customer.findMany instead of customer.findFirst for other importers
Steven Teyf17adde84e6f
diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.tsindex 9a9c706d50..70d9ef6833 100644--- a/apps/web/lib/partnerstack/import-customers.ts+++ b/apps/web/lib/partnerstack/import-customers.ts@@ -1,5 +1,5 @@import { prisma } from "@dub/prisma";-import { Link, Project } from "@dub/prisma/client";+import { Customer, Link, Project } from "@dub/prisma/client";import { nanoid } from "@dub/utils";import { createId } from "../api/create-id";import { updateLinkStatsForImporter } from "../api/links/update-link-stats-for-importer";@@ -125,6 +125,28 @@ export async function importCustomers(payload: PartnerStackImportPayload) {{} as Record<string, Date>,);+ const existingCustomers = await prisma.customer.findMany({+ where: {+ projectId: program.workspace.id,+ OR: [+ {+ email: {+ in: customers+ .map(({ email }) => email)+ .filter((e): e is string => e != null),+ },+ },+ {+ externalId: {+ in: customers+ .map(({ customer_key }) => customer_key)+ .filter((c): c is string => c != null),+ },+ },+ ],+ },+ });+await Promise.allSettled(customers.map((customer) => {const partnerId = partnerKeysToId[customer.partnership_key];@@ -134,6 +156,7 @@ export async function importCustomers(payload: PartnerStackImportPayload) {workspace: program.workspace,links,customer,+ existingCustomers,latestLeadAt: partnerKeysToLatestLeadAt[customer.partnership_key],importId,});@@ -162,6 +185,7 @@ async function createCustomer({workspace,links,customer,+ existingCustomers,latestLeadAt,importId,}: {@@ -171,6 +195,7 @@ async function createCustomer({"id" | "key" | "domain" | "url" | "partnerId" | "programId" | "lastLeadAt">[];customer: PartnerStackCustomer;+ existingCustomers: Customer[];latestLeadAt: Date;importId: string;}) {@@ -203,12 +228,10 @@ async function createCustomer({}// Find the customer by email address- const customerFound = await prisma.customer.findFirst({- where: {- projectId: workspace.id,- OR: [{ email: customer.email }, { externalId: customer.customer_key }],- },- });+ const customerFound = existingCustomers.find(+ (c) =>+ c.email === customer.email || c.externalId === customer.customer_key,+ );if (customerFound) {console.log(`A customer already exists with email ${customer.email}`);diff --git a/apps/web/lib/rewardful/import-customers.ts b/apps/web/lib/rewardful/import-customers.tsindex bb9860e5a5..a1bd3a455a 100644--- a/apps/web/lib/rewardful/import-customers.ts+++ b/apps/web/lib/rewardful/import-customers.ts@@ -1,5 +1,5 @@import { prisma } from "@dub/prisma";-import { Program, Project } from "@dub/prisma/client";+import { Customer, Program, Project } from "@dub/prisma/client";import { nanoid } from "@dub/utils";import { createId } from "../api/create-id";import { updateLinkStatsForImporter } from "../api/links/update-link-stats-for-importer";@@ -42,6 +42,26 @@ export async function importCustomers(payload: RewardfulImportPayload) {break;}+ const stripeCustomerIds = referrals+ .filter(+ (r) =>+ r.stripe_customer_id && r.stripe_customer_id.startsWith("cus_"),+ )+ .map((r) => r.stripe_customer_id!);+ const externalIds = referrals.map((r) => r.customer.id);++ const existingCustomers = await prisma.customer.findMany({+ where: {+ OR: [+ { stripeCustomerId: { in: stripeCustomerIds } },+ {+ projectId: workspace.id,+ externalId: { in: externalIds },+ },+ ],+ },+ });+await Promise.allSettled(referrals.map((referral) =>createCustomer({@@ -50,6 +70,7 @@ export async function importCustomers(payload: RewardfulImportPayload) {program,campaignIds,importId,+ existingCustomers,}),),);@@ -72,12 +93,14 @@ async function createCustomer({program,campaignIds,importId,+ existingCustomers,}: {referral: RewardfulReferral;workspace: Project;program: Program;campaignIds: string[];importId: string;+ existingCustomers: Customer[];}) {const referralId = referral.customer ? referral.customer.email : referral.id;if (@@ -148,11 +171,9 @@ async function createCustomer({return;}- const customerFoundStripeId = await prisma.customer.findUnique({- where: {- stripeCustomerId: referral.stripe_customer_id,- },- });+ const customerFoundStripeId = existingCustomers.find(+ (c) => c.stripeCustomerId === referral.stripe_customer_id,+ );if (customerFoundStripeId) {console.log(@@ -161,14 +182,9 @@ async function createCustomer({return;}- const customerFoundExternalId = await prisma.customer.findUnique({- where: {- projectId_externalId: {- projectId: workspace.id,- externalId: referral.customer.id,- },- },- });+ const customerFoundExternalId = existingCustomers.find(+ (c) => c.externalId === referral.customer.id,+ );if (customerFoundExternalId) {console.log(diff --git a/apps/web/lib/tolt/import-customers.ts b/apps/web/lib/tolt/import-customers.tsindex f728f2371e..4bca9cf88f 100644--- a/apps/web/lib/tolt/import-customers.ts+++ b/apps/web/lib/tolt/import-customers.ts@@ -1,5 +1,5 @@import { prisma } from "@dub/prisma";-import { Link, Project } from "@dub/prisma/client";+import { Customer, Link, Project } from "@dub/prisma/client";import { nanoid } from "@dub/utils";import { createId } from "../api/create-id";import { updateLinkStatsForImporter } from "../api/links/update-link-stats-for-importer";@@ -108,12 +108,26 @@ export async function importCustomers(payload: ToltImportPayload) {{} as Record<string, Date>,);+ const externalIds = customers.map(+ (c) => c.customer_id || c.email || c.id,+ );++ const existingCustomers = await prisma.customer.findMany({+ where: {+ projectId: workspace.id,+ externalId: {+ in: externalIds,+ },+ },+ });+await Promise.allSettled(customers.map(({ partner, ...customer }) =>createReferral({workspace,customer,links: partnerEmailToLinks.get(partner.email) ?? [],+ existingCustomers,latestLeadAt: partnerEmailToLatestLeadAt[partner.email],importId,}),@@ -138,6 +152,7 @@ async function createReferral({customer,workspace,links,+ existingCustomers,latestLeadAt,importId,}: {@@ -147,6 +162,7 @@ async function createReferral({Link,"id" | "key" | "domain" | "url" | "partnerId" | "programId" | "lastLeadAt">[];+ existingCustomers: Customer[];latestLeadAt: Date;importId: string;}) {@@ -172,14 +188,9 @@ async function createReferral({const customerExternalId =customer.customer_id || customer.email || customer.id;- const customerFound = await prisma.customer.findUnique({- where: {- projectId_externalId: {- projectId: workspace.id,- externalId: customerExternalId,- },- },- });+ const customerFound = existingCustomers.find(+ (c) => c.externalId === customerExternalId,+ );if (customerFound) {console.log(