Skip to content

Commit

Permalink
feat(ProgressiveBilling) - rename Fees::ChargeService.create to .call (
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded authored Aug 22, 2024
1 parent f508a44 commit 6341772
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/services/fees/charge_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(invoice:, charge:, subscription:, boundaries:)
super(nil)
end

def create
def call
return result if already_billed?

init_fees
Expand Down
6 changes: 2 additions & 4 deletions app/services/invoices/calculate_fees_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions app/services/invoices/progressive_billing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
60 changes: 30 additions & 30 deletions spec/services/fees/charge_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6341772

Please sign in to comment.