Skip to content

Commit

Permalink
feat(wallet): Extract wallet threshold top up
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Jun 4, 2024
1 parent 857a754 commit 82fd183
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 78 deletions.
24 changes: 2 additions & 22 deletions app/services/wallets/balance/update_ongoing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ def call
ongoing_usage_balance_cents = wallet.ongoing_usage_balance_cents
update_params = compute_update_params
wallet.update!(update_params)
wallet.reload

if update_params[:depleted_ongoing_balance] == true
SendWebhookJob.perform_later('wallet.depleted_ongoing_balance', wallet)
end

handle_threshold_top_up(ongoing_usage_balance_cents)
Wallets::TopUpService.call(wallet:)

result.wallet = wallet
result
Expand All @@ -46,23 +47,6 @@ def compute_update_params
params
end

def handle_threshold_top_up(ongoing_usage_balance_cents)
threshold_rule = wallet.recurring_transaction_rules.where(trigger: :threshold).first

return if threshold_rule.nil? || wallet.credits_ongoing_balance > threshold_rule.threshold_credits
return if (pending_transactions_amount + credits_ongoing_balance) > threshold_rule.threshold_credits

WalletTransactions::CreateJob.set(wait: 2.seconds).perform_later(
organization_id: wallet.organization.id,
params: {
wallet_id: wallet.id,
paid_credits: threshold_rule.paid_credits.to_s,
granted_credits: threshold_rule.granted_credits.to_s,
source: :threshold
}
)
end

def currency
@currency ||= wallet.ongoing_balance.currency
end
Expand All @@ -78,10 +62,6 @@ def ongoing_balance_cents
def credits_ongoing_balance
wallet.credits_balance - usage_credits_amount
end

def pending_transactions_amount
wallet.wallet_transactions.pending.sum(:amount)
end
end
end
end
8 changes: 0 additions & 8 deletions app/services/wallets/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ def initialize(params:)
end

def call
<<<<<<< HEAD
return result unless valid?
=======
return result unless valid?(**params)
>>>>>>> 62c2da89 (feat(wallet): Add granted and paid credits on recurring transaction rule)

wallet = Wallet.new(
customer_id: result.current_customer.id,
Expand Down Expand Up @@ -58,11 +54,7 @@ def call

attr_reader :params

<<<<<<< HEAD
def valid?
=======
def valid?(**params)
>>>>>>> 62c2da89 (feat(wallet): Add granted and paid credits on recurring transaction rule)
Wallets::ValidateService.new(result, **params).valid?
end
end
Expand Down
38 changes: 38 additions & 0 deletions app/services/wallets/threshold_top_up_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Wallets
class ThresholdTopUpService < BaseService
def initialize(wallet:)
@wallet = wallet
super
end

def call
return if threshold_rule.nil?
return if wallet.credits_ongoing_balance > threshold_rule.threshold_credits
return if (pending_transactions_amount + wallet.credits_ongoing_balance) > threshold_rule.threshold_credits

WalletTransactions::CreateJob.set(wait: 2.seconds).perform_later(
organization_id: wallet.organization.id,
params: {
wallet_id: wallet.id,
paid_credits: threshold_rule.paid_credits.to_s,
granted_credits: threshold_rule.granted_credits.to_s,
source: :threshold
}
)
end

private

attr_reader :wallet

def threshold_rule
@threshold_rule ||= wallet.recurring_transaction_rules.where(trigger: :threshold).first
end

def pending_transactions_amount
@pending_transactions_amount ||= wallet.wallet_transactions.pending.sum(:amount)
end
end
end
48 changes: 0 additions & 48 deletions spec/services/wallets/balance/update_ongoing_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,53 +56,5 @@
.with('wallet.depleted_ongoing_balance', Wallet)
end
end

context 'with recurring transaction threshold rule' do
let(:recurring_transaction_rule) do
create(:recurring_transaction_rule, wallet:, trigger: 'threshold', threshold_credits: '6.0')
end

before { recurring_transaction_rule }

it 'calls wallet transaction create job when threshold border has been crossed' do
expect { update_service.call }.to have_enqueued_job(WalletTransactions::CreateJob)
end

context 'when border has NOT been crossed' do
let(:recurring_transaction_rule) do
create(:recurring_transaction_rule, wallet:, trigger: 'threshold', threshold_credits: '2.0')
end

it 'does not call wallet transaction create job' do
expect { update_service.call }.not_to have_enqueued_job(WalletTransactions::CreateJob)
end
end

context 'with pending transactions' do
it 'does not call wallet transaction create job' do
create(:wallet_transaction, wallet:, amount: 1.0, credit_amount: 1.0, status: 'pending')
expect { update_service.call }.not_to have_enqueued_job(WalletTransactions::CreateJob)
end
end

context 'without any usage' do
let(:wallet) do
create(
:wallet,
balance_cents: 200,
ongoing_balance_cents: 200,
ongoing_usage_balance_cents: 0,
credits_balance: 2.0,
credits_ongoing_balance: 2.0,
credits_ongoing_usage_balance: 0.0
)
end
let(:credits_amount) { BigDecimal('0.0') }

it 'calls wallet transaction create job' do
expect { update_service.call }.to have_enqueued_job(WalletTransactions::CreateJob)
end
end
end
end
end
67 changes: 67 additions & 0 deletions spec/services/wallets/threshold_top_up_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Wallets::ThresholdTopUpService, type: :service do
subject(:top_up_service) { described_class.new(wallet:) }

let(:wallet) do
create(
:wallet,
balance_cents: 1000,
ongoing_balance_cents: 550,
ongoing_usage_balance_cents: 450,
credits_balance: 10.0,
credits_ongoing_balance: 5.5,
credits_ongoing_usage_balance: 4.0,
)
end

describe '#call' do
let(:recurring_transaction_rule) do
create(:recurring_transaction_rule, wallet:, trigger: 'threshold', threshold_credits: '6.0')
end

before { recurring_transaction_rule }

it 'calls wallet transaction create job when threshold border has been crossed' do
expect { top_up_service.call }.to have_enqueued_job(WalletTransactions::CreateJob)
end

context 'when border has NOT been crossed' do
let(:recurring_transaction_rule) do
create(:recurring_transaction_rule, wallet:, trigger: 'threshold', threshold_credits: '2.0')
end

it 'does not call wallet transaction create job' do
expect { top_up_service.call }.not_to have_enqueued_job(WalletTransactions::CreateJob)
end
end

context 'with pending transactions' do
it 'does not call wallet transaction create job' do
create(:wallet_transaction, wallet:, amount: 1.0, credit_amount: 1.0, status: 'pending')
expect { top_up_service.call }.not_to have_enqueued_job(WalletTransactions::CreateJob)
end
end

context 'without any usage' do
let(:wallet) do
create(
:wallet,
balance_cents: 200,
ongoing_balance_cents: 200,
ongoing_usage_balance_cents: 0,
credits_balance: 2.0,
credits_ongoing_balance: 2.0,
credits_ongoing_usage_balance: 0.0,
)
end
let(:credits_amount) { BigDecimal('0.0') }

it 'calls wallet transaction create job' do
expect { top_up_service.call }.to have_enqueued_job(WalletTransactions::CreateJob)
end
end
end
end

0 comments on commit 82fd183

Please sign in to comment.