Skip to content

Commit

Permalink
refactor(wallet): change ApplyPaidCreditsService argument (#2325)
Browse files Browse the repository at this point in the history
## Description

As we're working on some wallet improvements, I was looking at this
service. I'd like to make some modification:

* `*Service.call` methods should return a result as much as possible to
avoid calling `raise_if_error!` on nil
* The Service is under `Wallets` namespace and only need a wallet
transaction so I change the params. The invoice was not needed. I think
this makes the test more straight forward too.
  • Loading branch information
julienbourdeau authored Jul 26, 2024
1 parent b693ba0 commit 1365c27
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
3 changes: 2 additions & 1 deletion app/jobs/invoices/prepaid_credit_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class PrepaidCreditJob < ApplicationJob
unique :until_executed, on_conflict: :log

def perform(invoice)
Wallets::ApplyPaidCreditsService.new.call(invoice)
wallet_transaction = invoice.fees.find_by(fee_type: 'credit')&.invoiceable
Wallets::ApplyPaidCreditsService.call(wallet_transaction:)
end
end
end
18 changes: 14 additions & 4 deletions app/services/wallets/apply_paid_credits_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

module Wallets
class ApplyPaidCreditsService < BaseService
def call(invoice)
wallet_transaction = invoice.fees.find_by(fee_type: 'credit')&.invoiceable
def initialize(wallet_transaction:)
@wallet_transaction = wallet_transaction
super
end

return unless wallet_transaction
return if wallet_transaction.status == 'settled'
def call
return result unless wallet_transaction
return result if wallet_transaction.status == 'settled'

WalletTransactions::SettleService.new(wallet_transaction:).call
Wallets::Balance::IncreaseService
.new(wallet: wallet_transaction.wallet, credits_amount: wallet_transaction.credit_amount).call

result.wallet_transaction = wallet_transaction
result
end

private

attr_reader :wallet_transaction
end
end
29 changes: 5 additions & 24 deletions spec/services/wallets/apply_paid_credits_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,24 @@
require 'rails_helper'

RSpec.describe Wallets::ApplyPaidCreditsService, type: :service do
subject(:service) { described_class.new }
subject(:service) { described_class.new(wallet_transaction:) }

describe '.call' do
let(:invoice) { create(:invoice, customer:, organization: customer.organization) }
let(:customer) { create(:customer) }
let(:subscription) { create(:subscription, customer:) }
let(:wallet) { create(:wallet, customer:, balance_cents: 1000, credits_balance: 10.0) }
let(:wallet) { create(:wallet, balance_cents: 1000, credits_balance: 10.0) }
let(:wallet_transaction) do
create(:wallet_transaction, wallet:, amount: 15.0, credit_amount: 15.0, status: 'pending')
end
let(:fee) do
create(
:fee,
fee_type: 'credit',
invoiceable_type: 'WalletTransaction',
invoiceable_id: wallet_transaction.id,
invoice:
)
end

before do
wallet_transaction
fee
subscription
invoice.update(invoice_type: 'credit')
end

it 'updates wallet balance' do
service.call(invoice)
service.call

expect(wallet.reload.balance_cents).to eq 2500
end

it 'settles the wallet transaction' do
service.call(invoice)
result = service.call

expect(wallet_transaction.reload.status).to eq('settled')
expect(result.wallet_transaction.status).to eq('settled')
end
end
end

0 comments on commit 1365c27

Please sign in to comment.