diff --git a/app/services/lifetime_usages/calculate_service.rb b/app/services/lifetime_usages/calculate_service.rb index a92afcfd43b..7f727cffed5 100644 --- a/app/services/lifetime_usages/calculate_service.rb +++ b/app/services/lifetime_usages/calculate_service.rb @@ -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! diff --git a/spec/services/lifetime_usages/calculate_service_spec.rb b/spec/services/lifetime_usages/calculate_service_spec.rb index bad45174b2a..c802b534ba5 100644 --- a/spec/services/lifetime_usages/calculate_service_spec.rb +++ b/spec/services/lifetime_usages/calculate_service_spec.rb @@ -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) } @@ -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 @@ -81,6 +91,8 @@ 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 @@ -88,6 +100,14 @@ 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