Skip to content

Commit

Permalink
#228 Fixes bug that eref was not matched against statement.informatio…
Browse files Browse the repository at this point in the history
…n. Adds test cases. Removes exclude statement (credit_received and debit_received are not the correct values in any case)
  • Loading branch information
Andreas Finke committed Apr 13, 2022
1 parent a563ba0 commit 101a5d6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion box/business_processes/import_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
39 changes: 39 additions & 0 deletions spec/business_processes/import_statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }
Expand Down

0 comments on commit 101a5d6

Please sign in to comment.