FIX: Exception with campaign refresh data job (#222)

Depending on the invoices in stripe there could be some nil values for
things so we need to account for those in this job or an exception will
be thrown causing the job to fail.
This commit is contained in:
Blake Erickson 2024-07-16 11:54:56 -06:00 committed by GitHub
parent c02193943b
commit 572aa0f4a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 17 deletions

View File

@ -154,7 +154,8 @@ module DiscourseSubscriptions
all_invoices = all_invoices =
::Stripe::Invoice.list(limit: 100, starting_after: current_set[:last_record]) ::Stripe::Invoice.list(limit: 100, starting_after: current_set[:last_record])
current_set[:last_record] = all_invoices[:data].last[:id] if all_invoices[:data].present? if all_invoices[:data].present?
current_set[:last_record] = all_invoices[:data].last[:id]
current_set[:has_more] = all_invoices[:has_more] current_set[:has_more] = all_invoices[:has_more]
all_invoices[:data].each do |invoice| all_invoices[:data].each do |invoice|
@ -162,7 +163,7 @@ module DiscourseSubscriptions
next if invoice[:paid] != true next if invoice[:paid] != true
line_item = invoice[:lines][:data][0] if invoice[:lines] && invoice[:lines][:data] # Discourse only makes single-line item charges line_item = invoice[:lines][:data][0] if invoice[:lines] && invoice[:lines][:data] # Discourse only makes single-line item charges
# check if non-subscription and that the plan is active # check if non-subscription and that the plan is active
if line_item[:plan] == nil && line_item[:price] && if line_item && line_item[:plan] == nil && line_item[:price] &&
line_item[:price][:recurring] == nil && line_item[:price][:active] == true line_item[:price][:recurring] == nil && line_item[:price][:active] == true
product_id = line_item[:price][:product] product_id = line_item[:price][:product]
if product_ids.include? product_id if product_ids.include? product_id
@ -175,6 +176,9 @@ module DiscourseSubscriptions
end end
end end
end end
else
current_set[:has_more] = false
end
end end
end end

View File

@ -62,6 +62,10 @@ describe DiscourseSubscriptions::Campaign do
}, },
} }
end end
let(:invoice_with_nil_lines) { { id: "in_1236", paid: true, lines: nil } }
let(:invoice_with_nil_price) do
{ id: "in_1237", paid: true, lines: { data: [{ plan: nil, price: nil }] } }
end
before do before do
Fabricate(:product, external_id: "prodct_23456") Fabricate(:product, external_id: "prodct_23456")
@ -104,6 +108,20 @@ describe DiscourseSubscriptions::Campaign do
DiscourseSubscriptions::Campaign.new.refresh_data DiscourseSubscriptions::Campaign.new.refresh_data
expect(Discourse.redis.get("subscriptions_goal_met_date")).to be_blank expect(Discourse.redis.get("subscriptions_goal_met_date")).to be_blank
end end
it "handles invoices with nil lines gracefully" do
::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
::Stripe::Invoice.expects(:list).returns(data: [invoice_with_nil_lines], has_more: false)
expect { DiscourseSubscriptions::Campaign.new.refresh_data }.not_to raise_error
end
it "handles invoices with nil price gracefully" do
::Stripe::Subscription.expects(:list).returns(data: [subscription], has_more: false)
::Stripe::Invoice.expects(:list).returns(data: [invoice_with_nil_price], has_more: false)
expect { DiscourseSubscriptions::Campaign.new.refresh_data }.not_to raise_error
end
end end
context "with a campaign product set" do context "with a campaign product set" do