Skip to content

Commit

Permalink
🐛 bug(ProgressiveBilling) - Only perform calculations for active subs…
Browse files Browse the repository at this point in the history
…criptions (#2454)

## Description

If the subscription is no active, we don't have to recalculate anything.
This prevents us from calculating current usage for past subscriptions
  • Loading branch information
nudded authored Aug 22, 2024
1 parent 9171461 commit f508a44
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion app/services/lifetime_usages/calculate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ def initialize(lifetime_usage:)
end

def call
result.lifetime_usage = lifetime_usage

# clear boolean flags without recalculating if the subscription is not active.
if !lifetime_usage.subscription.active?
lifetime_usage.update!(recalculate_current_usage: false, recalculate_invoiced_usage: false)
return result
end

if lifetime_usage.recalculate_current_usage
lifetime_usage.current_usage_amount_cents = calculate_current_usage_amount_cents
lifetime_usage.recalculate_current_usage = false
Expand All @@ -20,7 +28,6 @@ def call
end
lifetime_usage.save!

result.lifetime_usage = lifetime_usage
result
end

Expand Down
15 changes: 15 additions & 0 deletions spec/services/lifetime_usages/calculate_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@
expect { service.call }.not_to change(lifetime_usage, :invoiced_usage_amount_refreshed_at)
end

context "with terminated subscription" do
before do
lifetime_usage.subscription.mark_as_terminated!(20.seconds.ago)
end

it "clears the recalculate_current_usage flag" do
result = service.call
expect(result.lifetime_usage.recalculate_current_usage).to eq(false)
end

it "does not update the current_usage_amount_refreshed_at" do
expect { service.call }.not_to change(lifetime_usage, :current_usage_amount_refreshed_at)
end
end

context 'with usage' do
before do
events
Expand Down

0 comments on commit f508a44

Please sign in to comment.