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.

Improved handling of multiple customers check

Kiran K878127c7a641
diff --git a/apps/web/lib/partnerstack/update-stripe-customers.ts b/apps/web/lib/partnerstack/update-stripe-customers.ts
index d441ae4ea8..01ab288115 100644
--- a/apps/web/lib/partnerstack/update-stripe-customers.ts
+++ b/apps/web/lib/partnerstack/update-stripe-customers.ts
@@ -2,6 +2,7 @@ import { sendEmail } from "@dub/email";
import CampaignImported from "@dub/email/templates/campaign-imported";
import { prisma } from "@dub/prisma";
import { Customer, Project } from "@dub/prisma/client";
+import Stripe from "stripe";
import { stripeAppClient } from "../stripe";
import { MAX_BATCHES, partnerStackImporter } from "./importer";
import { PartnerStackImportPayload } from "./types";
@@ -149,15 +150,26 @@ async function searchStripeAndUpdateCustomer({
return null;
}
+ let stripeCustomer: Stripe.Customer;
+
if (stripeCustomers.data.length > 1) {
- console.error(
- `Stripe search returned multiple customers for ${customer.email}`,
+ // look for the one with metadata.customer_key set
+ const partnerStackStripeCustomer = stripeCustomers.data.find(
+ ({ metadata }) => metadata.customer_key,
);
- return null;
- }
+ if (partnerStackStripeCustomer) {
+ stripeCustomer = partnerStackStripeCustomer;
+ } else {
+ console.error(
+ `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.customer_key set`,
+ );
- const stripeCustomer = stripeCustomers.data[0];
+ return null;
+ }
+ } else {
+ stripeCustomer = stripeCustomers.data[0];
+ }
await prisma.customer.update({
where: {
diff --git a/apps/web/lib/tolt/update-stripe-customers.ts b/apps/web/lib/tolt/update-stripe-customers.ts
index 8228bbc18f..5a41c99c75 100644
--- a/apps/web/lib/tolt/update-stripe-customers.ts
+++ b/apps/web/lib/tolt/update-stripe-customers.ts
@@ -120,7 +120,7 @@ async function searchStripeAndUpdateCustomer({
if (stripeCustomers.data.length > 1) {
// look for the one with metadata.tolt_referral set
const toltReferralStripeCustomer = stripeCustomers.data.find(
- (customer) => customer.metadata.tolt_referral,
+ ({ metadata }) => metadata.tolt_referral,
);
if (toltReferralStripeCustomer) {
@@ -132,10 +132,10 @@ async function searchStripeAndUpdateCustomer({
return null;
}
+ } else {
+ stripeCustomer = stripeCustomers.data[0];
}
- stripeCustomer = stripeCustomers.data[0];
-
await prisma.customer.update({
where: {
id: customer.id,