foodsoft/app/lib/bank_account_information_importer.rb
Philipp Rothmann fb2b4d8a8a chore: rubocop
chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
2023-06-09 17:35:05 +02:00

58 lines
2.4 KiB
Ruby

class BankAccountInformationImporter
def initialize(bank_account)
@bank_account = bank_account
end
def import!(content)
return nil if content.empty?
import_data! JSON.parse(content, symbolize_names: true)
end
def import_data!(data)
return 0 if data.empty?
booked = data.fetch(:transactions, {}).fetch(:booked, [])
ret = 0
booked.each do |t|
amount = parse_account_information_amount t[:transactionAmount]
entity_name = amount < 0 ? t[:creditorName] : t[:debtorName]
entity_account = 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({
date: t[:bookingDate],
amount: amount,
iban: entity_account && entity_account[:iban],
reference: reference,
text: entity_name,
receipt: t[:additionalInformation]
})
ret += 1
end
balances = (data[:balances] ? data[:balances].map { |b| [b[:balanceType], b[:balanceAmount]] } : []).to_h
balance = balances.values.first
%w[closingBooked expected authorised openingBooked interimAvailable forwardAvailable nonInvoiced].each do |type|
value = balances[type]
if value
balance = value
break
end
end
@bank_account.balance = parse_account_information_amount(balance) || @bank_account.bank_transactions.sum(:amount)
@bank_account.import_continuation_point = booked.first&.fetch(:entryReference, nil) unless booked.empty?
@bank_account.last_import = Time.now
@bank_account.save!
ret
end
private
def parse_account_information_amount(value)
value && value[:amount].to_f
end
end