From 9171461107baea52fea11ad02674a49b655db300 Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Wed, 21 Aug 2024 16:28:54 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20bug(ProgressiveBilling)=20-=20Se?= =?UTF-8?q?t=20refreshed=5Fat=20timestamps=20for=20LifetimeUsage=20(#2449)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Description The timestamp indicating when a calculation has been performed was not set. --- .../lifetime_usages/calculate_service.rb | 2 ++ .../lifetime_usages/calculate_service_spec.rb | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) 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