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.

Update import-customers.ts

Steven Tey99af351f2dc4
diff --git a/apps/web/lib/firstpromoter/import-customers.ts b/apps/web/lib/firstpromoter/import-customers.ts
index 542ac6896f..28314546df 100644
--- a/apps/web/lib/firstpromoter/import-customers.ts
+++ b/apps/web/lib/firstpromoter/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";
@@ -120,6 +120,22 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {
{} as Record<string, Date>,
);
+ const existingCustomers = await prisma.customer.findMany({
+ where: {
+ projectId: workspace.id,
+ OR: [
+ { email: { in: customers.map(({ email }) => email) } },
+ {
+ externalId: {
+ in: customers
+ .map(({ uid }) => uid)
+ .filter((c): c is NonNullable<typeof c> => c !== null),
+ },
+ },
+ ],
+ },
+ });
+
await Promise.allSettled(
customers.map((customer) => {
const links =
@@ -130,6 +146,7 @@ export async function importCustomers(payload: FirstPromoterImportPayload) {
workspace,
links,
customer,
+ existingCustomers,
latestLeadAt:
partnerEmailToLatestLeadAt[
customer.promoter_campaign.promoter.email
@@ -157,6 +174,7 @@ async function createCustomer({
workspace,
links,
customer,
+ existingCustomers,
latestLeadAt,
importId,
}: {
@@ -166,6 +184,7 @@ async function createCustomer({
"id" | "key" | "domain" | "url" | "partnerId" | "programId" | "lastLeadAt"
>[];
customer: FirstPromoterCustomer;
+ existingCustomers: Customer[];
latestLeadAt: Date;
importId: string;
}) {
@@ -198,12 +217,9 @@ async function createCustomer({
}
// Find the customer by email address
- const customerFound = await prisma.customer.findFirst({
- where: {
- projectId: workspace.id,
- OR: [{ externalId: customer.uid }, { email: customer.email }],
- },
- });
+ const customerFound = existingCustomers.find(
+ (c) => c.email === customer.email || c.externalId === customer.uid,
+ );
if (customerFound) {
console.log(`A customer already exists with email ${customer.email}`);