FIX: Prevent duplicate stripe subscription list requests (#208)

Users can have multiple customer records in our db, so we need to filter
out non-unique stripe customer ids before fetching subscriptions for each.
This commit is contained in:
Blake Erickson 2024-05-03 12:38:26 -06:00 committed by GitHub
parent 66e8857c20
commit 5eba613f8f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 7 deletions

View File

@ -15,7 +15,7 @@ module DiscourseSubscriptions
begin
customer = Customer.where(user_id: current_user.id)
customer_ids = customer.map { |c| c.id } if customer
stripe_customer_ids = customer.map { |c| c.customer_id } if customer
stripe_customer_ids = customer.map { |c| c.customer_id }.uniq if customer
subscription_ids =
Subscription.where("customer_id in (?)", customer_ids).pluck(
:external_id,
@ -32,6 +32,7 @@ module DiscourseSubscriptions
::Stripe::Subscription.list(customer: stripe_customer_id, status: "all")
all_subscriptions.concat(customer_subscriptions[:data])
end
subscriptions = all_subscriptions.select { |sub| subscription_ids.include?(sub[:id]) }
subscriptions.map! do |subscription|
plan = plans[:data].find { |p| p[:id] == subscription[:items][:data][0][:price][:id] }

View File

@ -7,11 +7,14 @@ describe "Subscription products", type: :system do
fab!(:customer) do
Fabricate(:customer, customer_id: "cus_Q1n", product_id: product.external_id, user_id: user.id)
end
fab!(:customer2) do
Fabricate(:customer, customer_id: "cus_Q1n", product_id: product.external_id, user_id: user.id)
end
fab!(:subscription) do
Fabricate(:subscription, customer_id: customer.id, external_id: "sub_10z", status: "active")
end
fab!(:subscription) do
Fabricate(:subscription, customer_id: customer.id, external_id: "sub_32b", status: "canceled")
fab!(:subscription2) do
Fabricate(:subscription, customer_id: customer2.id, external_id: "sub_32b", status: "canceled")
end
let(:dialog) { PageObjects::Components::Dialog.new }
let(:product_subscriptions_page) { PageObjects::Pages::AdminSubscriptionProduct.new }
@ -82,11 +85,12 @@ describe "Subscription products", type: :system do
it "shows active and canceled subscriptions for users" do
sign_in(user)
active_subscription_row =
user_billing_subscriptions_page.visit_subscriptions.subscription_row("sub_10z")
user_billing_subscriptions_page.visit_subscriptions
user_billing_subscriptions_page.has_number_of_subscriptions?(2)
active_subscription_row = user_billing_subscriptions_page.subscription_row("sub_10z")
expect(active_subscription_row).to have_text("active")
canceled_subscription_row =
user_billing_subscriptions_page.visit_subscriptions.subscription_row("sub_32b")
canceled_subscription_row = user_billing_subscriptions_page.subscription_row("sub_32b")
expect(canceled_subscription_row).to have_text("canceled")
end
end