The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Import PartnerStack customers without email when external ID exists (#4332)
Pedro Ladeiraecaab366320e
diff --git a/apps/web/lib/partnerstack/import-customers.ts b/apps/web/lib/partnerstack/import-customers.tsindex 22faee4704..98742b6740 100644--- a/apps/web/lib/partnerstack/import-customers.ts+++ b/apps/web/lib/partnerstack/import-customers.ts@@ -219,25 +219,28 @@ async function createCustomer({await logImportError({...commonImportLogInputs,code: "LINK_NOT_FOUND",- message: `Link not found for customer ${customer.customer_key}.`,+ message: `Link not found for customer ${commonImportLogInputs.entity_id}.`,});return;}- if (!customer.email) {+ const externalId = customer.customer_key || customer.email;++ if (!externalId) {await logImportError({...commonImportLogInputs,code: "CUSTOMER_EMAIL_NOT_FOUND",- message: `Email not found for customer ${customer.customer_key}.`,+ message: `No external ID or email found for customer ${customer.key}.`,});return;}- // Find the customer by email addressconst customerFound = existingCustomers.find(- (c) => c.email === customer.email || c.externalId === customer.customer_key,+ (c) =>+ (customer.email != null && c.email === customer.email) ||+ (customer.customer_key != null && c.externalId === customer.customer_key),);if (customerFound) {@@ -281,9 +284,9 @@ async function createCustomer({data: {id: customerId,name:- // if name is null/undefined or starts with cus_, use email as name+ // if name is null/undefined or starts with cus_, use email or external ID!customer.name || customer.name.startsWith("cus_")- ? customer.email+ ? customer.email || customer.customer_key: customer.name,email: customer.email,projectId: workspace.id,@@ -295,7 +298,10 @@ async function createCustomer({country: clickEvent.country,clickedAt: new Date(customer.created_at),createdAt: new Date(customer.created_at),- externalId: customer.customer_key || customer.email,+ externalId,+ ...(customer.provider_key?.startsWith("cus_") && {+ stripeCustomerId: customer.provider_key,+ }),},});diff --git a/apps/web/lib/partnerstack/update-stripe-customers.ts b/apps/web/lib/partnerstack/update-stripe-customers.tsindex 8cc6218a61..b26887fb34 100644--- a/apps/web/lib/partnerstack/update-stripe-customers.ts+++ b/apps/web/lib/partnerstack/update-stripe-customers.ts@@ -53,6 +53,9 @@ export async function updateStripeCustomers(where: {projectId: workspace.id,stripeCustomerId: null,+ email: {+ not: null,+ },},select: {id: true,@@ -148,6 +151,10 @@ async function searchStripeAndUpdateCustomer({entity_id: customer.id,} as const;+ if (!customer.email) {+ return null;+ }+try {const stripeCustomers = await stripe.customers.search({