Skip to content

Commit

Permalink
feat(wallet): Flag wallets for refresh when updating a draft invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 27, 2024
1 parent 5ddb5f3 commit 4b4767d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ def overdue_balance_cents
invoices.payment_overdue.where(currency:).sum(:total_amount_cents)
end

def flag_wallets_for_refresh
return unless wallets.active.any?

wallets.active.update_all(ready_to_be_refreshed: true) # rubocop:disable Rails/SkipsModelValidations
end

private

def ensure_slug
Expand Down
9 changes: 1 addition & 8 deletions app/services/events/post_process_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def call

expire_cached_charges(subscriptions)
flag_refresh_from_subscription
flag_wallets_for_refresh
customer&.flag_wallets_for_refresh

handle_pay_in_advance

Expand Down Expand Up @@ -99,13 +99,6 @@ def flag_refresh_from_subscription
subscriptions.select(&:active?).each { |s| LifetimeUsages::FlagRefreshFromSubscriptionService.new(subscription: s).call }
end

def flag_wallets_for_refresh
return unless customer
return unless customer.wallets.active.any?

customer.wallets.active.update_all(ready_to_be_refreshed: true) # rubocop:disable Rails/SkipsModelValidations
end

def handle_pay_in_advance
return unless billable_metric
return unless charges.any?
Expand Down
1 change: 1 addition & 0 deletions app/services/invoices/refresh_draft_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def call

ActiveRecord::Base.transaction do
invoice.update!(ready_to_be_refreshed: false) if invoice.ready_to_be_refreshed?
invoice.customer.flag_wallets_for_refresh

old_fee_values = invoice_credit_note_items.map do |item|
{credit_note_item_id: item.id, fee_amount_cents: item.fee&.amount_cents}
Expand Down
26 changes: 26 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,30 @@
end
end
end

describe "#flag_wallets_for_refresh" do
context "without any wallets" do
it "returns nil" do
expect(customer.flag_wallets_for_refresh).to be_nil
end
end

context "without active wallets" do
it "does not flag wallets for refresh" do
wallet = create(:wallet, :terminated, customer:)

expect { customer.flag_wallets_for_refresh }.not_to change {
wallet.reload.ready_to_be_refreshed
}.from(false)
end
end

it "flags all active wallets for refresh" do
wallet = create(:wallet, customer:)

expect { customer.flag_wallets_for_refresh }.to change {
wallet.reload.ready_to_be_refreshed
}.from(false).to(true)
end
end
end

0 comments on commit 4b4767d

Please sign in to comment.