From 101a5d60634fd9473dc8a4956ab8fb554c5a77b3 Mon Sep 17 00:00:00 2001 From: Andreas Finke Date: Wed, 13 Apr 2022 22:30:38 +0100 Subject: [PATCH] #228 Fixes bug that eref was not matched against statement.information. Adds test cases. Removes exclude statement (credit_received and debit_received are not the correct values in any case) --- box/business_processes/import_statements.rb | 2 +- .../import_statement_spec.rb | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/box/business_processes/import_statements.rb b/box/business_processes/import_statements.rb index a6c9e175..78d9114f 100644 --- a/box/business_processes/import_statements.rb +++ b/box/business_processes/import_statements.rb @@ -51,7 +51,7 @@ def self.link_statement_to_transaction(account, statement) # find transactions via EREF transaction = account.transactions_dataset.where(eref: statement.eref).first # fallback to finding via statement information - transaction ||= account.transactions_dataset.exclude(currency: 'EUR', status: %w[credit_received debit_received]).where { created_at > 14.days.ago }.detect { |t| statement.information =~ /#{t.eref}/i } + transaction ||= account.transactions_dataset.where { created_at > 14.days.ago }.detect { |t| statement.information =~ /#{t.eref}/i } return unless transaction diff --git a/spec/business_processes/import_statement_spec.rb b/spec/business_processes/import_statement_spec.rb index f38dedea..6fd35208 100644 --- a/spec/business_processes/import_statement_spec.rb +++ b/spec/business_processes/import_statement_spec.rb @@ -205,6 +205,45 @@ def exec_link_action end end + describe '.link_statement_to_transaction fallback' do + let(:statement) { Statement.create(information: 'fallback-eref', account_id: account.id) } + + def exec_link_action + described_class.link_statement_to_transaction(account, statement) + end + + context 'no transaction could be found' do + it 'does not trigger a webhook' do + expect(Event).to_not receive(:statement_created) + exec_link_action + end + end + + context 'transaction exists' do + let!(:transaction) { Transaction.create(account_id: account.id, eref: 'fallback-eref', created_at: Date.today) } + let(:event) { object_double(Event).as_stubbed_const } + + context 'statement is a credit' do + before { statement.update(debit: false) } + + it 'sets correct transaction state' do + expect_any_instance_of(Transaction).to receive(:update_status).with('credit_received') + exec_link_action + end + end + + context 'statement is a debit' do + before { statement.update(debit: true) } + + it 'sets correct transaction state' do + expect_any_instance_of(Transaction).to receive(:update_status).with('debit_received') + exec_link_action + end + end + end + end + + describe 'duplicates' do let(:mt940) { File.read('spec/fixtures/similar_but_not_dup_transactions.mt940') } let(:mt940b) { File.read('spec/fixtures/dup_whitespace_transaction.mt940') }