Skip to content

Commit

Permalink
fix: Include draft invoices inside wallet's ongoing balance
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 27, 2024
1 parent 9b2b03b commit 5ddb5f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion app/models/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class Wallet < ApplicationRecord
has_many :wallet_transactions
has_many :recurring_transaction_rules

monetize :balance_cents, :ongoing_balance_cents, :ongoing_usage_balance_cents
monetize :balance_cents
monetize :consumed_amount_cents
monetize :ongoing_balance_cents, :ongoing_usage_balance_cents, with_model_currency: :balance_currency

validates :rate_amount, numericality: {greater_than: 0}

Expand Down
6 changes: 5 additions & 1 deletion app/services/wallets/balance/update_ongoing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ def credits_ongoing_usage_balance
end

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

def credits_ongoing_balance
ongoing_balance_cents.to_f.fdiv(currency.subunit_to_unit).fdiv(wallet.rate_amount)
end

def draft_invoices_total_amount_cents
wallet.customer.invoices.draft.sum(:total_amount_cents)
end
end
end
end
25 changes: 15 additions & 10 deletions spec/services/wallets/balance/update_ongoing_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# frozen_string_literal: true

require 'rails_helper'
require "rails_helper"

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

let(:organization) { create(:organization) }
let(:customer) { create(:customer, organization:) }
let(:wallet) do
create(
:wallet,
customer:,
balance_cents: 1000,
ongoing_balance_cents: 800,
ongoing_usage_balance_cents: 200,
Expand All @@ -22,41 +25,43 @@

before { wallet }

describe '#call' do
it 'updates wallet balance' do
describe "#call" do
it "updates wallet balance" do
create(:invoice, :draft, customer:, organization:, total_amount_cents: 150)

expect { update_service.call }
.to change(wallet.reload, :ongoing_usage_balance_cents).from(200).to(450)
.and change(wallet, :credits_ongoing_usage_balance).from(2.0).to(4.5)
.and change(wallet, :ongoing_balance_cents).from(800).to(550)
.and change(wallet, :credits_ongoing_balance).from(8.0).to(5.5)
.and change(wallet, :ongoing_balance_cents).from(800).to(400)
.and change(wallet, :credits_ongoing_balance).from(8.0).to(4.0)
.and change(wallet, :ready_to_be_refreshed).from(true).to(false)

expect(wallet).not_to be_depleted_ongoing_balance
end

context 'when usage amount is greater than the balance' do
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
it "updates wallet ongoing balance to a negative value" do
expect { update_service.call }
.to change(wallet.reload, :ongoing_usage_balance_cents).from(200).to(1500)
.and change(wallet, :credits_ongoing_usage_balance).from(2.0).to(15)
.and change(wallet, :ongoing_balance_cents).from(800).to(-500)
.and change(wallet, :credits_ongoing_balance).from(8.0).to(-5.0)
end

it 'sets depleted_ongoing_balance to true' do
it "sets depleted_ongoing_balance to true" do
expect { update_service.call }
.to change(wallet.reload, :depleted_ongoing_balance).from(false).to(true)

expect { update_service.call }
.not_to change(wallet.reload, :depleted_ongoing_balance).from(true)
end

it 'sends depleted_ongoing_balance webhook' do
it "sends depleted_ongoing_balance webhook" do
expect { update_service.call }
.to have_enqueued_job(SendWebhookJob)
.with('wallet.depleted_ongoing_balance', Wallet)
.with("wallet.depleted_ongoing_balance", Wallet)
end
end
end
Expand Down

0 comments on commit 5ddb5f3

Please sign in to comment.