From 30d130ca443a4acef1ef1ef70bb7c64861c9c20f Mon Sep 17 00:00:00 2001 From: Anna Velentsevich Date: Tue, 15 Oct 2024 16:54:12 +0200 Subject: [PATCH] add possibility to record credit_note_id when voiding credits --- app/services/wallet_transactions/void_service.rb | 8 +++++--- .../wallet_transactions/void_service_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/services/wallet_transactions/void_service.rb b/app/services/wallet_transactions/void_service.rb index 8490a06e368..7e66193db20 100644 --- a/app/services/wallet_transactions/void_service.rb +++ b/app/services/wallet_transactions/void_service.rb @@ -2,11 +2,12 @@ module WalletTransactions class VoidService < BaseService - def initialize(wallet:, credits:, from_source: :manual, metadata: {}) + def initialize(wallet:, credits:, from_source: :manual, metadata: {}, credit_note_id: nil) @wallet = wallet @credits = credits @from_source = from_source @metadata = metadata + @credit_note_id = credit_note_id super end @@ -23,7 +24,8 @@ def call settled_at: Time.current, source: from_source, transaction_status: :voided, - metadata: + metadata:, + credit_note_id: ) Wallets::Balance::DecreaseService.new(wallet:, credits_amount:).call result.wallet_transaction = wallet_transaction @@ -34,7 +36,7 @@ def call private - attr_reader :wallet, :credits, :from_source, :metadata + attr_reader :wallet, :credits, :from_source, :metadata, :credit_note_id def credits_amount @credits_amount ||= BigDecimal(credits) diff --git a/spec/services/wallet_transactions/void_service_spec.rb b/spec/services/wallet_transactions/void_service_spec.rb index 555c825a23b..6f2a5860753 100644 --- a/spec/services/wallet_transactions/void_service_spec.rb +++ b/spec/services/wallet_transactions/void_service_spec.rb @@ -78,5 +78,17 @@ expect(wallet.balance_cents).to eq(0) expect(wallet.credits_balance).to eq(0.0) end + + context 'when credit_note_id is passed' do + let(:credit_note_id) { create(:credit_note, organization: organization).id } + subject(:void_service) { described_class.call(wallet:, credits:, credit_note_id:) } + + it 'saves credit_note_id in wallet_transaction' do + result = void_service + wallet_transaction = result.wallet_transaction + + expect(wallet_transaction.credit_note_id).to eq(credit_note_id) + end + end end end