Skip to content

Commit

Permalink
fix(wallet): Exclude pay in advance fees from the ongoing balance
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Sep 6, 2024
1 parent 8839d75 commit ce6302d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
24 changes: 16 additions & 8 deletions app/services/wallets/balance/refresh_ongoing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ def initialize(wallet:)
end

def call
total_amount = customer.active_subscriptions.sum do |subscription|
::Invoices::CustomerUsageService.call(
nil, # current_user
customer: customer,
subscription: subscription
).invoice.total_amount
usage_amount_cents = customer.active_subscriptions.map do |subscription|
invoice = ::Invoices::CustomerUsageService.call(nil, customer:, subscription:).invoice

{
total_usage_amount_cents: invoice.total_amount.to_f * wallet.ongoing_balance.currency.subunit_to_unit,
pay_in_advance_usage_amount_cents: pay_in_advance_usage_amount_cents(invoice)
}
end
usage_credits_amount = total_amount.to_f.fdiv(wallet.rate_amount)
Wallets::Balance::UpdateOngoingService.call(wallet:, usage_credits_amount:).raise_if_error!

total_usage_amount_cents = usage_amount_cents.sum { |e| e[:total_usage_amount_cents] }
pay_in_advance_usage_amount_cents = usage_amount_cents.sum { |e| e[:pay_in_advance_usage_amount_cents] }

Wallets::Balance::UpdateOngoingService.call(wallet:, total_usage_amount_cents:, pay_in_advance_usage_amount_cents:).raise_if_error!

result.wallet = wallet
result
Expand All @@ -28,6 +32,10 @@ def call
attr_reader :wallet

delegate :customer, to: :wallet

def pay_in_advance_usage_amount_cents(invoice)
invoice.fees.select { |f| f.charge.pay_in_advance? && f.charge.invoiceable? }.sum(&:amount_cents)
end
end
end
end
19 changes: 10 additions & 9 deletions app/services/wallets/balance/update_ongoing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Wallets
module Balance
class UpdateOngoingService < BaseService
def initialize(wallet:, usage_credits_amount:)
def initialize(wallet:, total_usage_amount_cents:, pay_in_advance_usage_amount_cents:)
super

@wallet = wallet
@usage_credits_amount = usage_credits_amount
@total_usage_amount_cents = total_usage_amount_cents
@pay_in_advance_usage_amount_cents = pay_in_advance_usage_amount_cents
end

def call
Expand All @@ -26,12 +27,12 @@ def call

private

attr_reader :wallet, :usage_credits_amount
attr_reader :wallet, :total_usage_amount_cents, :pay_in_advance_usage_amount_cents

def compute_update_params
params = {
ongoing_usage_balance_cents: usage_amount_cents,
credits_ongoing_usage_balance: usage_credits_amount,
ongoing_usage_balance_cents: total_usage_amount_cents,
credits_ongoing_usage_balance:,
ongoing_balance_cents:,
credits_ongoing_balance:
}
Expand All @@ -49,16 +50,16 @@ def currency
@currency ||= wallet.ongoing_balance.currency
end

def usage_amount_cents
@usage_amount_cents ||= wallet.rate_amount * usage_credits_amount * currency.subunit_to_unit
def credits_ongoing_usage_balance
total_usage_amount_cents.to_f.fdiv(currency.subunit_to_unit).fdiv(wallet.rate_amount)
end

def ongoing_balance_cents
wallet.balance_cents - usage_amount_cents
wallet.balance_cents - total_usage_amount_cents + pay_in_advance_usage_amount_cents
end

def credits_ongoing_balance
wallet.credits_balance - usage_credits_amount
ongoing_balance_cents.to_f.fdiv(currency.subunit_to_unit).fdiv(wallet.rate_amount)
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions spec/services/wallets/balance/update_ongoing_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe Wallets::Balance::UpdateOngoingService, type: :service do
subject(:update_service) { described_class.new(wallet:, usage_credits_amount:) }
subject(:update_service) { described_class.new(wallet:, total_usage_amount_cents:, pay_in_advance_usage_amount_cents:) }

let(:wallet) do
create(
Expand All @@ -16,7 +16,8 @@
credits_ongoing_usage_balance: 2.0
)
end
let(:usage_credits_amount) { BigDecimal('4.5') }
let(:total_usage_amount_cents) { 450 }
let(:pay_in_advance_usage_amount_cents) { 0 }

before { wallet }

Expand All @@ -31,8 +32,8 @@
expect(wallet).not_to be_depleted_ongoing_balance
end

context 'when credits_amount is greater than the balance' do
let(:usage_credits_amount) { BigDecimal('15') }
context 'when usage amount is greater than the balance' do
let(:total_usage_amount_cents) { 1500 }

it 'updates wallet ongoing balance to a negative value' do
expect { update_service.call }
Expand Down

0 comments on commit ce6302d

Please sign in to comment.