From 63417720d0b66d71c5f6fb2d2569ea426fd9ed6b Mon Sep 17 00:00:00 2001 From: Toon Willems Date: Thu, 22 Aug 2024 09:26:01 +0200 Subject: [PATCH] feat(ProgressiveBilling) - rename Fees::ChargeService.create to .call (#2446) --- app/services/fees/charge_service.rb | 2 +- .../invoices/calculate_fees_service.rb | 6 +- .../invoices/progressive_billing_service.rb | 6 +- spec/services/fees/charge_service_spec.rb | 60 +++++++++---------- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/app/services/fees/charge_service.rb b/app/services/fees/charge_service.rb index 179257355e7..785e337308e 100644 --- a/app/services/fees/charge_service.rb +++ b/app/services/fees/charge_service.rb @@ -13,7 +13,7 @@ def initialize(invoice:, charge:, subscription:, boundaries:) super(nil) end - def create + def call return result if already_billed? init_fees diff --git a/app/services/invoices/calculate_fees_service.rb b/app/services/invoices/calculate_fees_service.rb index 9c2450b11f9..bafd9d6f07a 100644 --- a/app/services/invoices/calculate_fees_service.rb +++ b/app/services/invoices/calculate_fees_service.rb @@ -110,8 +110,7 @@ def create_charges_fees(subscription, boundaries) .find_each do |charge| next if should_not_create_charge_fee?(charge, subscription) - fee_result = Fees::ChargeService.new(invoice:, charge:, subscription:, boundaries:).create - fee_result.raise_if_error! + Fees::ChargeService.call(invoice:, charge:, subscription:, boundaries:).raise_if_error! end end @@ -178,8 +177,7 @@ def create_recurring_non_invoiceable_fees(subscription, boundaries) .find_each do |charge| next if should_not_create_charge_fee?(charge, subscription) - fee_result = Fees::ChargeService.new(invoice: nil, charge:, subscription:, boundaries:).create - fee_result.raise_if_error! + fee_result = Fees::ChargeService.call(invoice: nil, charge:, subscription:, boundaries:).raise_if_error! result.non_invoiceable_fees.concat(fee_result.fees) end diff --git a/app/services/invoices/progressive_billing_service.rb b/app/services/invoices/progressive_billing_service.rb index d17e731a9f6..fbf88019d12 100644 --- a/app/services/invoices/progressive_billing_service.rb +++ b/app/services/invoices/progressive_billing_service.rb @@ -117,8 +117,7 @@ def total_lifetime_usage_amount_cents end def create_credit_note_credit - credit_result = Credits::CreditNoteService.new(invoice:).call - credit_result.raise_if_error! + credit_result = Credits::CreditNoteService.call(invoice:).raise_if_error! invoice.total_amount_cents -= credit_result.credits.sum(&:amount_cents) if credit_result.credits end @@ -129,8 +128,7 @@ def create_applied_prepaid_credit return unless invoice.total_amount_cents.positive? return unless wallet.balance.positive? - prepaid_credit_result = Credits::AppliedPrepaidCreditService.call(invoice:, wallet:) - prepaid_credit_result.raise_if_error! + prepaid_credit_result = Credits::AppliedPrepaidCreditService.call(invoice:, wallet:).raise_if_error! invoice.total_amount_cents -= prepaid_credit_result.prepaid_credit_amount_cents end diff --git a/spec/services/fees/charge_service_spec.rb b/spec/services/fees/charge_service_spec.rb index cee8f63d6f1..4f58d921ceb 100644 --- a/spec/services/fees/charge_service_spec.rb +++ b/spec/services/fees/charge_service_spec.rb @@ -48,10 +48,10 @@ ) end - describe '.create' do + describe '.call' do context 'without filters' do it 'creates a fee' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( id: String, @@ -86,7 +86,7 @@ context 'without events' do it 'creates an empty fee' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.count).to eq(1) @@ -139,7 +139,7 @@ end it 'creates a fee for each group' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.count).to eq(2) @@ -199,7 +199,7 @@ end it 'creates a fee for each group' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.count).to eq(2) @@ -235,7 +235,7 @@ let(:billable_metric) { create(:weighted_sum_billable_metric, :recurring, organization:) } it 'creates a fee and a cached aggregation per group' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.count).to eq(2) @@ -248,7 +248,7 @@ create(:custom_aggregation_billable_metric, organization:) it 'creates a fee and a cached aggregation' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.count).to eq(2) @@ -292,7 +292,7 @@ end it 'creates a fee' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( id: String, @@ -314,7 +314,7 @@ end it 'does not create a new fee' do - expect { charge_subscription_service.create }.not_to change(Fee, :count) + expect { charge_subscription_service.call }.not_to change(Fee, :count) end end @@ -352,7 +352,7 @@ end it 'creates a new fee for the complete period' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( id: String, @@ -377,7 +377,7 @@ end it 'creates fees' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( id: String, @@ -421,7 +421,7 @@ context 'with adjusted units' do it 'creates a fee' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( @@ -442,7 +442,7 @@ before { charge.update!(min_amount_cents: 20_000) } it 'creates two fees' do - result = charge_subscription_service.create + result = charge_subscription_service.call aggregate_failures do expect(result).to be_success @@ -557,7 +557,7 @@ it 'creates expected fees for sum_agg aggregation type' do billable_metric.update!(aggregation_type: :sum_agg, field_name: 'foo_bar') - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -619,7 +619,7 @@ end it 'creates a fee' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( @@ -654,7 +654,7 @@ end it 'creates a fee' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( @@ -677,7 +677,7 @@ before { invoice.finalized! } it 'creates a fee without using adjusted fee attributes' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success expect(result.fees.first).to have_attributes( @@ -700,7 +700,7 @@ it 'creates two fees' do travel_to(DateTime.new(2023, 4, 1)) do charge.update!(min_amount_cents: 1000) - result = charge_subscription_service.create + result = charge_subscription_service.call aggregate_failures do expect(result).to be_success @@ -792,7 +792,7 @@ it 'creates expected fees for count_agg aggregation type' do billable_metric.update!(aggregation_type: :count_agg) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -833,7 +833,7 @@ it 'creates expected fees for sum_agg aggregation type' do billable_metric.update!(aggregation_type: :sum_agg, field_name: 'foo_bar') - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -874,7 +874,7 @@ it 'creates expected fees for max_agg aggregation type' do billable_metric.update!(aggregation_type: :max_agg, field_name: 'foo_bar') - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -916,7 +916,7 @@ context 'when unique_count_agg' do it 'creates expected fees for unique_count_agg aggregation type' do billable_metric.update!(aggregation_type: :unique_count_agg, field_name: 'foo_bar') - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -1075,7 +1075,7 @@ it 'creates expected fees for count_agg aggregation type' do billable_metric.update!(aggregation_type: :count_agg) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -1221,7 +1221,7 @@ it 'creates expected fees for count_agg aggregation type' do billable_metric.update!(aggregation_type: :count_agg) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -1364,7 +1364,7 @@ it 'creates expected fees for count_agg aggregation type' do billable_metric.update!(aggregation_type: :count_agg) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -1484,7 +1484,7 @@ it 'creates expected fees for count_agg aggregation type' do billable_metric.update!(aggregation_type: :count_agg) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -1619,7 +1619,7 @@ it 'creates expected fees for count_agg aggregation type' do billable_metric.update!(aggregation_type: :count_agg) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fees = result.fees @@ -1700,7 +1700,7 @@ it 'creates three fees' do travel_to(DateTime.new(2023, 4, 1)) do - result = charge_subscription_service.create + result = charge_subscription_service.call aggregate_failures do expect(result).to be_success @@ -1717,7 +1717,7 @@ let(:billable_metric) { create(:weighted_sum_billable_metric, :recurring, organization:) } it 'creates a fee and a cached aggregation' do - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).to be_success created_fee = result.fees.first cached_aggregation = result.cached_aggregations.first @@ -1763,7 +1763,7 @@ allow(aggregator_service).to receive(:aggregate) .and_return(error_result) - result = charge_subscription_service.create + result = charge_subscription_service.call expect(result).not_to be_success expect(result.error).to be_a(BaseService::ServiceFailure)