Skip to content

Commit

Permalink
feat(wallet): Mark wallets as ready_to_be_refreshed on event creation
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Oct 14, 2024
1 parent 7926d1f commit a65c309
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/services/events/post_process_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def call

expire_cached_charges(subscriptions)
flag_refresh_from_subscription
flag_wallets_for_refresh

handle_pay_in_advance

Expand Down Expand Up @@ -95,6 +96,13 @@ 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.not_ready_to_be_refreshed.update_all(ready_to_be_refreshed: true)
end

def handle_pay_in_advance
return unless billable_metric
return unless charges.any?
Expand Down
6 changes: 6 additions & 0 deletions spec/services/events/post_process_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
expect(subscription.lifetime_usage.recalculate_current_usage).to be(true)
end

it 'flags wallets for refresh' do
wallet = create(:wallet, customer:)

expect { process_service.call }.to change { wallet.reload.ready_to_be_refreshed }.from(false).to(true)
end

context 'when event matches an pay_in_advance charge' do
let(:charge) { create(:standard_charge, :pay_in_advance, plan:, billable_metric:, invoiceable: false) }
let(:billable_metric) { create(:billable_metric, organization:, aggregation_type: 'sum_agg', field_name: 'item_id') }
Expand Down

0 comments on commit a65c309

Please sign in to comment.