Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 bug(ProgressiveBilling) - Set refreshed_at timestamps for LifetimeUsage #2449

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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