Skip to content

Commit

Permalink
🐛 bug(ProgressiveBilling) - Set refreshed_at timestamps for LifetimeU…
Browse files Browse the repository at this point in the history
…sage (#2449)

## Description

The timestamp indicating when a calculation has been performed was not
set.
  • Loading branch information
nudded authored Aug 21, 2024
1 parent 7f877cf commit 9171461
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/services/lifetime_usages/calculate_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ def call
if lifetime_usage.recalculate_current_usage
lifetime_usage.current_usage_amount_cents = calculate_current_usage_amount_cents
lifetime_usage.recalculate_current_usage = false
lifetime_usage.current_usage_amount_refreshed_at = Time.current
end
if lifetime_usage.recalculate_invoiced_usage
lifetime_usage.invoiced_usage_amount_cents = calculate_invoiced_usage_amount_cents
lifetime_usage.recalculate_invoiced_usage = false
lifetime_usage.invoiced_usage_amount_refreshed_at = Time.current
end
lifetime_usage.save!

Expand Down
24 changes: 22 additions & 2 deletions spec/services/lifetime_usages/calculate_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
subject(:service) { described_class.new(lifetime_usage: lifetime_usage) }

let(:lifetime_usage) { create(:lifetime_usage, subscription:, recalculate_current_usage:, recalculate_invoiced_usage:) }
let(:recalculate_current_usage) { true }
let(:recalculate_invoiced_usage) { true }
let(:recalculate_current_usage) { false }
let(:recalculate_invoiced_usage) { false }
let(:subscription) { create(:subscription, customer_id: customer.id) }
let(:organization) { subscription.organization }
let(:customer) { create(:customer) }
Expand Down Expand Up @@ -40,11 +40,21 @@
end

describe '#recalculate_invoiced_usage' do
let(:recalculate_invoiced_usage) { true }

context "without previous invoices" do
it "calculates the invoiced_usage as zero" do
result = service.call
expect(result.lifetime_usage.invoiced_usage_amount_cents).to be_zero
end

it "updates the invoiced_usage_amount_refreshed_at" do
expect { service.call }.to change(lifetime_usage, :invoiced_usage_amount_refreshed_at)
end

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

context "with draft invoice" do
Expand Down Expand Up @@ -81,13 +91,23 @@
end

describe '#recalculate_current_usage' do
let(:recalculate_current_usage) { true }

context 'without usage' do
it 'calculates the current_usage as zero' do
result = service.call
expect(result.lifetime_usage.current_usage_amount_cents).to be_zero
end
end

it "updates the current_usage_amount_refreshed_at" do
expect { service.call }.to change(lifetime_usage, :current_usage_amount_refreshed_at)
end

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

context 'with usage' do
before do
events
Expand Down

0 comments on commit 9171461

Please sign in to comment.