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.

improve searchStripeAndUpdateCustomer to expand data.subscriptions

Steven Teyecbbea0c51e3
diff --git a/apps/web/lib/firstpromoter/update-stripe-customers.ts b/apps/web/lib/firstpromoter/update-stripe-customers.ts
index 9655a44bcf..479ec31128 100644
--- a/apps/web/lib/firstpromoter/update-stripe-customers.ts
+++ b/apps/web/lib/firstpromoter/update-stripe-customers.ts
@@ -117,6 +117,7 @@ async function searchStripeAndUpdateCustomer({
const stripeCustomers = await stripe.customers.search(
{
query: `email:'${customer.email}'`,
+ expand: ["data.subscriptions"],
},
{
stripeAccount: workspace.stripeConnectId!,
@@ -144,13 +145,24 @@ async function searchStripeAndUpdateCustomer({
if (firstPromoterStripeCustomer) {
stripeCustomer = firstPromoterStripeCustomer;
} else {
- await logImportError({
- ...commonImportLogInputs,
- code: "STRIPE_CUSTOMER_NOT_FOUND",
- message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.fp_uid set`,
- });
-
- return null;
+ // look for the one with subscriptions
+ const customerWithSubcription = stripeCustomers.data.find(
+ ({ subscriptions }) => subscriptions && subscriptions.data.length > 0,
+ );
+
+ if (customerWithSubcription) {
+ console.log(
+ `Found Stripe customer with subscriptions for ${customer.email}: ${customerWithSubcription.id}`,
+ );
+ stripeCustomer = customerWithSubcription;
+ } else {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.fp_uid set`,
+ });
+ return null;
+ }
}
} else {
stripeCustomer = stripeCustomers.data[0];
diff --git a/apps/web/lib/partnerstack/update-stripe-customers.ts b/apps/web/lib/partnerstack/update-stripe-customers.ts
index 09d79e037e..b5579a67f8 100644
--- a/apps/web/lib/partnerstack/update-stripe-customers.ts
+++ b/apps/web/lib/partnerstack/update-stripe-customers.ts
@@ -148,58 +148,75 @@ async function searchStripeAndUpdateCustomer({
entity_id: customer.id,
} as const;
- const stripeCustomers = await stripe.customers.search(
- {
- query: `email:'${customer.email}'`,
- },
- {
- stripeAccount: workspace.stripeConnectId!,
- },
- );
-
- if (stripeCustomers.data.length === 0) {
- await logImportError({
- ...commonImportLogInputs,
- code: "STRIPE_CUSTOMER_NOT_FOUND",
- message: `Stripe search returned no customer for ${customer.email}`,
- });
-
- return null;
- }
-
- let stripeCustomer: Stripe.Customer;
-
- if (stripeCustomers.data.length > 1) {
- // look for the one with metadata.customer_key set
- const partnerStackStripeCustomer = stripeCustomers.data.find(
- ({ metadata }) => metadata.customer_key,
+ try {
+ const stripeCustomers = await stripe.customers.search(
+ {
+ query: `email:'${customer.email}'`,
+ expand: ["data.subscriptions"],
+ },
+ {
+ stripeAccount: workspace.stripeConnectId!,
+ },
);
- if (partnerStackStripeCustomer) {
- stripeCustomer = partnerStackStripeCustomer;
- } else {
+ if (stripeCustomers.data.length === 0) {
await logImportError({
...commonImportLogInputs,
code: "STRIPE_CUSTOMER_NOT_FOUND",
- message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
+ message: `Stripe search returned no customer for ${customer.email}`,
});
return null;
}
- } else {
- stripeCustomer = stripeCustomers.data[0];
- }
- await prisma.customer.update({
- where: {
- id: customer.id,
- },
- data: {
- stripeCustomerId: stripeCustomer.id,
- },
- });
+ let stripeCustomer: Stripe.Customer;
+
+ if (stripeCustomers.data.length > 1) {
+ // look for the one with metadata.customer_key set
+ const partnerStackStripeCustomer = stripeCustomers.data.find(
+ ({ metadata }) => metadata.customer_key,
+ );
+
+ if (partnerStackStripeCustomer) {
+ stripeCustomer = partnerStackStripeCustomer;
+ } else {
+ // look for the one with subscriptions
+ const customerWithSubcription = stripeCustomers.data.find(
+ ({ subscriptions }) =>
+ subscriptions && subscriptions.data.length > 0,
+ );
+
+ if (customerWithSubcription) {
+ console.log(
+ `Found Stripe customer with subscriptions for ${customer.email}: ${customerWithSubcription.id}`,
+ );
+ stripeCustomer = customerWithSubcription;
+ } else {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.customer_key set`,
+ });
+ return null;
+ }
+ }
+ } else {
+ stripeCustomer = stripeCustomers.data[0];
+ }
+
+ await prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ stripeCustomerId: stripeCustomer.id,
+ },
+ });
- console.log(
- `Updated customer ${customer.id} with Stripe customer ID ${stripeCustomer.id}`,
- );
+ console.log(
+ `Updated customer ${customer.id} with Stripe customer ID ${stripeCustomer.id}`,
+ );
+ } catch (error) {
+ console.error(error);
+ }
}
diff --git a/apps/web/lib/tolt/update-stripe-customers.ts b/apps/web/lib/tolt/update-stripe-customers.ts
index b831cffdef..915c5027bf 100644
--- a/apps/web/lib/tolt/update-stripe-customers.ts
+++ b/apps/web/lib/tolt/update-stripe-customers.ts
@@ -110,58 +110,75 @@ async function searchStripeAndUpdateCustomer({
entity_id: customer.id,
} as const;
- const stripeCustomers = await stripe.customers.search(
- {
- query: `email:'${customer.email}'`,
- },
- {
- stripeAccount: workspace.stripeConnectId!,
- },
- );
-
- if (stripeCustomers.data.length === 0) {
- await logImportError({
- ...commonImportLogInputs,
- code: "STRIPE_CUSTOMER_NOT_FOUND",
- message: `Stripe search returned no customer for ${customer.email}`,
- });
-
- return null;
- }
-
- let stripeCustomer: Stripe.Customer;
-
- if (stripeCustomers.data.length > 1) {
- // look for the one with metadata.tolt_referral set
- const toltReferralStripeCustomer = stripeCustomers.data.find(
- ({ metadata }) => metadata.tolt_referral,
+ try {
+ const stripeCustomers = await stripe.customers.search(
+ {
+ query: `email:'${customer.email}'`,
+ expand: ["data.subscriptions"],
+ },
+ {
+ stripeAccount: workspace.stripeConnectId!,
+ },
);
- if (toltReferralStripeCustomer) {
- stripeCustomer = toltReferralStripeCustomer;
- } else {
+ if (stripeCustomers.data.length === 0) {
await logImportError({
...commonImportLogInputs,
code: "STRIPE_CUSTOMER_NOT_FOUND",
- message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
+ message: `Stripe search returned no customer for ${customer.email}`,
});
return null;
}
- } else {
- stripeCustomer = stripeCustomers.data[0];
- }
- await prisma.customer.update({
- where: {
- id: customer.id,
- },
- data: {
- stripeCustomerId: stripeCustomer.id,
- },
- });
+ let stripeCustomer: Stripe.Customer;
+
+ if (stripeCustomers.data.length > 1) {
+ // look for the one with metadata.tolt_referral set
+ const toltReferralStripeCustomer = stripeCustomers.data.find(
+ ({ metadata }) => metadata.tolt_referral,
+ );
+
+ if (toltReferralStripeCustomer) {
+ stripeCustomer = toltReferralStripeCustomer;
+ } else {
+ // look for the one with subscriptions
+ const customerWithSubcription = stripeCustomers.data.find(
+ ({ subscriptions }) =>
+ subscriptions && subscriptions.data.length > 0,
+ );
+
+ if (customerWithSubcription) {
+ console.log(
+ `Found Stripe customer with subscriptions for ${customer.email}: ${customerWithSubcription.id}`,
+ );
+ stripeCustomer = customerWithSubcription;
+ } else {
+ await logImportError({
+ ...commonImportLogInputs,
+ code: "STRIPE_CUSTOMER_NOT_FOUND",
+ message: `Stripe search returned multiple customers for ${customer.email} for workspace ${workspace.slug} and none had metadata.tolt_referral set`,
+ });
+ return null;
+ }
+ }
+ } else {
+ stripeCustomer = stripeCustomers.data[0];
+ }
+
+ await prisma.customer.update({
+ where: {
+ id: customer.id,
+ },
+ data: {
+ stripeCustomerId: stripeCustomer.id,
+ },
+ });
- console.log(
- `Updated customer ${customer.id} with Stripe customer ID ${stripeCustomer.id}`,
- );
+ console.log(
+ `Updated customer ${customer.id} with Stripe customer ID ${stripeCustomer.id}`,
+ );
+ } catch (error) {
+ console.error(error);
+ }
}