Small improvements and style fixes in BankAccountInformationImporter

This commit is contained in:
Patrick Gansterer 2022-05-27 19:52:56 +02:00
parent bc5bc2d5ac
commit 3a25a50d8d
2 changed files with 13 additions and 10 deletions

View file

@ -6,8 +6,10 @@ class BankAccountInformationImporter
def import!(content) def import!(content)
return nil if content.empty? return nil if content.empty?
data = JSON.parse content, symbolize_names: true import_data! JSON.parse(content, symbolize_names: true)
end
def import_data!(data)
return 0 if data.empty? return 0 if data.empty?
booked = data.fetch(:transactions, {}).fetch(:booked, []) booked = data.fetch(:transactions, {}).fetch(:booked, [])
@ -17,30 +19,31 @@ class BankAccountInformationImporter
amount = parse_account_information_amount t[:transactionAmount] amount = parse_account_information_amount t[:transactionAmount]
entityName = amount < 0 ? t[:creditorName] : t[:debtorName] entityName = amount < 0 ? t[:creditorName] : t[:debtorName]
entityAccount = amount < 0 ? t[:creditorAccount] : t[:debtorAccount] entityAccount = amount < 0 ? t[:creditorAccount] : t[:debtorAccount]
reference = [t[:endToEndId], t[:remittanceInformationUnstructured]].join("\n").strip
@bank_account.bank_transactions.where(external_id: t[:transactionId]).first_or_create.update({ @bank_account.bank_transactions.where(external_id: t[:transactionId]).first_or_create.update({
date: t[:bookingDate], date: t[:bookingDate],
amount: amount, amount: amount,
iban: entityAccount && entityAccount[:iban], iban: entityAccount && entityAccount[:iban],
reference: t[:remittanceInformationUnstructured], reference: reference,
text: entityName, text: entityName,
receipt: t[:additionalInformation], receipt: t[:additionalInformation]
}) })
ret += 1 ret += 1
end end
balances = Hash[data[:balances] ? data[:balances].map { |b| [b[:balanceType], b[:balanceAmount]] } : []] balances = (data[:balances] ? data[:balances].map { |b| [b[:balanceType], b[:balanceAmount]] } : []).to_h
balance = balances.values.first balance = balances.values.first
%w(closingBooked expected authorised openingBooked interimAvailable forwardAvailable nonInvoiced).each do |type| %w(closingBooked expected authorised openingBooked interimAvailable forwardAvailable nonInvoiced).each do |type|
value = balances[type] value = balances[type]
if value then if value
balance = value balance = value
break break
end end
end end
@bank_account.balance = parse_account_information_amount(balance) || @bank_account.bank_transactions.sum(:amount) @bank_account.balance = parse_account_information_amount(balance) || @bank_account.bank_transactions.sum(:amount)
@bank_account.import_continuation_point = booked.first&.fetch(:entryReference, nil) @bank_account.import_continuation_point = booked.first&.fetch(:entryReference, nil) unless booked.empty?
@bank_account.last_import = Time.now @bank_account.last_import = Time.now
@bank_account.save! @bank_account.save!

View file

@ -242,7 +242,7 @@ describe BankTransaction do
expect(bt.date).to eq('2019-02-13'.to_date) expect(bt.date).to eq('2019-02-13'.to_date)
expect(bt.text).to eq('Hammersmith Inc.') expect(bt.text).to eq('Hammersmith Inc.')
expect(bt.iban).to be(nil) expect(bt.iban).to be(nil)
expect(bt.reference).to eq('Martin Schöneicher, Inv# 123453423, Thx') expect(bt.reference).to eq("Corvette Ersatzteile\nMartin Schöneicher, Inv# 123453423, Thx")
expect(bt.receipt).to eq('Auslands-Überweisung') expect(bt.receipt).to eq('Auslands-Überweisung')
end end
@ -318,7 +318,7 @@ describe BankTransaction do
expect(bt.date).to eq('2019-02-14'.to_date) expect(bt.date).to eq('2019-02-14'.to_date)
expect(bt.text).to eq('Maria Reithuber') expect(bt.text).to eq('Maria Reithuber')
expect(bt.iban).to eq('AT251657674147449499') expect(bt.iban).to eq('AT251657674147449499')
expect(bt.reference).to eq("Danke für's Auslegen") expect(bt.reference).to eq("Auslage von Martin S.\nDanke für's Auslegen")
expect(bt.receipt).to eq('Gutschrift') expect(bt.receipt).to eq('Gutschrift')
end end
@ -390,7 +390,7 @@ describe BankTransaction do
expect(bt1.date).to eq('2020-01-01'.to_date) expect(bt1.date).to eq('2020-01-01'.to_date)
expect(bt1.text).to eq('DN1') expect(bt1.text).to eq('DN1')
expect(bt1.iban).to eq('DE72957284895783674747') expect(bt1.iban).to eq('DE72957284895783674747')
expect(bt1.reference).to be(nil) expect(bt1.reference).to eq('')
expect(bt1.receipt).to eq('AI1') expect(bt1.receipt).to eq('AI1')
bt2 = bank_account.bank_transactions.find_by_external_id("T2") bt2 = bank_account.bank_transactions.find_by_external_id("T2")
@ -406,7 +406,7 @@ describe BankTransaction do
expect(bt3.date).to eq('2000-03-01'.to_date) expect(bt3.date).to eq('2000-03-01'.to_date)
expect(bt3.text).to eq('DN3') expect(bt3.text).to eq('DN3')
expect(bt3.iban).to be(nil) expect(bt3.iban).to be(nil)
expect(bt3.reference).to be(nil) expect(bt3.reference).to eq('')
expect(bt3.receipt).to be(nil) expect(bt3.receipt).to be(nil)
end end
end end