Skip to content

Commit

Permalink
fix(wallet): Save wallet transaction using floor for credits
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Oct 15, 2024
1 parent 5a03fd7 commit 4712e76
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
28 changes: 12 additions & 16 deletions app/services/wallet_transactions/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def call
if params[:paid_credits]
transaction = handle_paid_credits(
wallet: result.current_wallet,
paid_credits: params[:paid_credits],
credits_amount: BigDecimal(params[:paid_credits]).floor(5),
invoice_requires_successful_payment:
)
wallet_transactions << transaction
Expand All @@ -33,7 +33,7 @@ def call
if params[:granted_credits]
transaction = handle_granted_credits(
wallet: result.current_wallet,
granted_credits: params[:granted_credits],
credits_amount: BigDecimal(params[:granted_credits]).floor(5),
reset_consumed_credits: ActiveModel::Type::Boolean.new.cast(params[:reset_consumed_credits]),
invoice_requires_successful_payment:
)
Expand All @@ -43,7 +43,7 @@ def call
if params[:voided_credits]
void_result = WalletTransactions::VoidService.call(
wallet: result.current_wallet,
credits: params[:voided_credits],
credits_amount: BigDecimal(params[:voided_credits]).floor(5),
from_source: source, metadata:
)
wallet_transactions << void_result.wallet_transaction
Expand All @@ -61,16 +61,14 @@ def call

attr_reader :organization, :params, :source, :metadata

def handle_paid_credits(wallet:, paid_credits:, invoice_requires_successful_payment:)
paid_credits_amount = BigDecimal(paid_credits)

return if paid_credits_amount.zero?
def handle_paid_credits(wallet:, credits_amount:, invoice_requires_successful_payment:)
return if credits_amount.zero?

wallet_transaction = WalletTransaction.create!(
wallet:,
transaction_type: :inbound,
amount: wallet.rate_amount * paid_credits_amount,
credit_amount: paid_credits_amount,
amount: wallet.rate_amount * credits_amount,
credit_amount: credits_amount,
status: :pending,
source:,
transaction_status: :purchased,
Expand All @@ -83,17 +81,15 @@ def handle_paid_credits(wallet:, paid_credits:, invoice_requires_successful_paym
wallet_transaction
end

def handle_granted_credits(wallet:, granted_credits:, invoice_requires_successful_payment:, reset_consumed_credits: false)
granted_credits_amount = BigDecimal(granted_credits)

return if granted_credits_amount.zero?
def handle_granted_credits(wallet:, credits_amount:, invoice_requires_successful_payment:, reset_consumed_credits: false)
return if credits_amount.zero?

ActiveRecord::Base.transaction do
wallet_transaction = WalletTransaction.create!(
wallet:,
transaction_type: :inbound,
amount: wallet.rate_amount * granted_credits_amount,
credit_amount: granted_credits_amount,
amount: wallet.rate_amount * credits_amount,
credit_amount: credits_amount,
status: :settled,
settled_at: Time.current,
source:,
Expand All @@ -104,7 +100,7 @@ def handle_granted_credits(wallet:, granted_credits:, invoice_requires_successfu

Wallets::Balance::IncreaseService.new(
wallet:,
credits_amount: granted_credits_amount,
credits_amount:,
reset_consumed_credits:
).call

Expand Down
10 changes: 3 additions & 7 deletions app/services/wallet_transactions/void_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module WalletTransactions
class VoidService < BaseService
def initialize(wallet:, credits:, from_source: :manual, metadata: {})
def initialize(wallet:, credits_amount:, from_source: :manual, metadata: {})
@wallet = wallet
@credits = credits
@credits_amount = credits_amount
@from_source = from_source
@metadata = metadata

Expand Down Expand Up @@ -34,10 +34,6 @@ def call

private

attr_reader :wallet, :credits, :from_source, :metadata

def credits_amount
@credits_amount ||= BigDecimal(credits)
end
attr_reader :wallet, :credits_amount, :from_source, :metadata
end
end
9 changes: 9 additions & 0 deletions spec/services/wallet_transactions/create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,14 @@
expect(result.error.messages[:paid_credits]).to eq(['invalid_paid_credits'])
end
end

context 'with decimal value' do
let(:paid_credits) { '4.399999' }

it 'creates wallet transaction with floored value' do
result = create_service
expect(result.wallet_transactions.first.credit_amount).to eq(4.39999)
end
end
end
end
8 changes: 4 additions & 4 deletions spec/services/wallet_transactions/void_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'rails_helper'

RSpec.describe WalletTransactions::VoidService, type: :service do
subject(:void_service) { described_class.call(wallet:, credits:) }
subject(:void_service) { described_class.call(wallet:, credits_amount:) }

let(:membership) { create(:membership) }
let(:organization) { membership.organization }
Expand All @@ -19,23 +19,23 @@
credits_ongoing_balance: 10.0
)
end
let(:credits) { '10.00' }
let(:credits_amount) { BigDecimal('10.00') }

before do
subscription
end

describe '#call' do
context 'when credits amount is zero' do
let(:credits) { '0.00' }
let(:credits_amount) { BigDecimal('0.00') }

it 'does not create a wallet transaction' do
expect { void_service }.not_to change(WalletTransaction, :count)
end
end

context 'when transaction have metadata' do
subject(:void_service) { described_class.call(wallet:, credits:, metadata:) }
subject(:void_service) { described_class.call(wallet:, credits_amount:, metadata:) }

let(:metadata) { [{'key' => 'valid_value', 'value' => 'also_valid'}] }

Expand Down

0 comments on commit 4712e76

Please sign in to comment.