-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): Extract wallet threshold top up
- Loading branch information
Showing
5 changed files
with
107 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |