The personal finance app for everyone (by everyone)
Ruby
Prefer the available balance for Enable Banking cards treated as available credit (#4277)
alexcastgonz264af3c456d9
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rbindex d4927e9313..f8afd55220 100644--- a/app/models/enable_banking_item/importer.rb+++ b/app/models/enable_banking_item/importer.rb@@ -27,6 +27,16 @@ class EnableBankingItem::ImporterITAV interimAvailable].freeze+ # With "Balance is available credit" on for a credit card, the bank's figure+ # is the available credit, so available types come first. Some cards report+ # the credit limit as the booked balance (mBank PL: ITBD = limit, ITAV =+ # available credit). XPCD (expected) keeps its place after the booked types:+ # it is a projected booked balance, not an available-credit figure.+ AVAILABLE_CREDIT_BALANCE_TYPE_PRIORITY = (%w[+ CLAV closingAvailable+ ITAV interimAvailable+ ] + BALANCE_TYPE_PRIORITY).uniq.freeze+PERIOD_BOUNDARY_TYPES = %w[opbd openingbooked prcd previouslyclosedbooked].freeze# Only these types may outrank OPBD/PRCD on freshness (see fresher_balance) —@@ -235,7 +245,9 @@ class EnableBankingItem::Importerendcurrency, kept, skipped = split_balances_by_currency(balances, enable_banking_account)- balance = select_current_balance(kept)+ balance = select_current_balance(+ kept, prefer_available: prefer_available_balance?(enable_banking_account)+ )unless balance.present?mark_balance_unavailable(enable_banking_account)@@ -337,10 +349,19 @@ class EnableBankingItem::Importer)end- def select_current_balance(balances)+ # The flag only changes how a credit card's balance is read (see+ # EnableBankingAccount::Processor#process_account!), so any other linked+ # account stays booked-first even if the flag is still set from an earlier link.+ def prefer_available_balance?(enable_banking_account)+ enable_banking_account.treat_balance_as_available_credit? &&+ enable_banking_account.current_account&.accountable_type == "CreditCard"+ end++ def select_current_balance(balances, prefer_available: false)by_type = balances.index_by { |balance| normalize_balance_type(balance[:balance_type]) }+ priority = prefer_available ? AVAILABLE_CREDIT_BALANCE_TYPE_PRIORITY : BALANCE_TYPE_PRIORITY- BALANCE_TYPE_PRIORITY.each do |type|+ priority.each do |type|normalized_type = normalize_balance_type(type)balance = by_type[normalized_type]next unless balance.present?diff --git a/test/models/enable_banking_item/importer_balance_test.rb b/test/models/enable_banking_item/importer_balance_test.rbindex ec9f6a53db..89b51da631 100644--- a/test/models/enable_banking_item/importer_balance_test.rb+++ b/test/models/enable_banking_item/importer_balance_test.rb@@ -62,6 +62,95 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCaseassert_equal BigDecimal("-50.00"), @enable_banking_account.reload.current_balanceend+ # Some cards report the credit limit as the booked balance and the available+ # credit as the available balance (mBank PL: ITBD = limit, ITAV = available).+ test "fetch_and_update_balance prefers available balance when the balance is treated as available credit" do+ link_account(CreditCard.create!)+ @enable_banking_account.update!(treat_balance_as_available_credit: true)+ @mock_provider.stubs(:get_account_balances).returns(+ balances: [+ {+ balance_type: "ITBD",+ balance_amount: { amount: "11500.00", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ },+ {+ balance_type: "ITAV",+ balance_amount: { amount: "4531.68", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ }+ ]+ )++ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)++ assert_equal BigDecimal("4531.68"), @enable_banking_account.reload.current_balance+ end++ test "fetch_and_update_balance falls back to booked balance when treated as available credit and no available balance is reported" do+ link_account(CreditCard.create!)+ @enable_banking_account.update!(treat_balance_as_available_credit: true)+ @mock_provider.stubs(:get_account_balances).returns(+ balances: [+ {+ balance_type: "ITBD",+ balance_amount: { amount: "4531.68", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ }+ ]+ )++ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)++ assert_equal BigDecimal("4531.68"), @enable_banking_account.reload.current_balance+ end++ test "fetch_and_update_balance keeps booked before expected when treated as available credit" do+ link_account(CreditCard.create!)+ @enable_banking_account.update!(treat_balance_as_available_credit: true)+ @mock_provider.stubs(:get_account_balances).returns(+ balances: [+ {+ balance_type: "XPCD",+ balance_amount: { amount: "11500.00", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ },+ {+ balance_type: "ITBD",+ balance_amount: { amount: "4531.68", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ }+ ]+ )++ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)++ assert_equal BigDecimal("4531.68"), @enable_banking_account.reload.current_balance+ end++ test "fetch_and_update_balance stays booked-first for a non-card account with the available credit flag set" do+ link_account(Depository.create!)+ @enable_banking_account.update!(treat_balance_as_available_credit: true)+ @mock_provider.stubs(:get_account_balances).returns(+ balances: [+ {+ balance_type: "ITBD",+ balance_amount: { amount: "4531.68", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ },+ {+ balance_type: "ITAV",+ balance_amount: { amount: "11500.00", currency: "EUR" },+ credit_debit_indicator: "CRDT"+ }+ ]+ )++ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)++ assert_equal BigDecimal("4531.68"), @enable_banking_account.reload.current_balance+ end+test "fetch_and_update_balance prefers opening/previously-closed booked balance over expected and available" do@mock_provider.stubs(:get_account_balances).returns(balances: [@@ -507,6 +596,18 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCaseprivate+ def link_account(accountable)+ account = Account.create!(+ family: @family,+ name: "CGD linked",+ balance: 123.45,+ cash_balance: 123.45,+ currency: "EUR",+ accountable: accountable+ )+ AccountProvider.create!(account: account, provider: @enable_banking_account)+ end+# Shaped like PayPal's response: the primary EUR balance first, then empty# CAD and USD balances under the same type.def paypal_balances