Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(enable-banking): add missing booked balance types to priority list (#3739)

GFR73ea65df5141
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rb
index 23a69437bf..a008e44635 100644
--- a/app/models/enable_banking_item/importer.rb
+++ b/app/models/enable_banking_item/importer.rb
@@ -6,14 +6,36 @@ class EnableBankingItem::Importer
# Prefer booked ledger balances for net worth/current_balance. Available balances
# can include an arranged overdraft facility for some ASPSPs (for example CGD PT),
# so they are only a last-resort fallback.
+ #
+ # OPBD/PRCD rank above XPCD despite being period-boundary snapshots (opening of
+ # the current period / closing of the previous one) rather than "instant" values:
+ # this mirrors CLBD outranking ITBD above, a reconciled booked figure is trusted
+ # over one that includes unconfirmed pending activity, even if it's up to a day
+ # older. That trade-off assumes the staleness is bounded to about a day, so
+ # select_current_balance below guards it with the optional `reference_date` field
+ # (see PERIOD_BOUNDARY_TYPES): if the same response also carries a materially
+ # newer XPCD/CLAV/ITAV, the fresher one wins instead.
BALANCE_TYPE_PRIORITY = %w[
CLBD closingBooked
ITBD interimBooked
+ OPBD openingBooked
+ PRCD previouslyClosedBooked
XPCD expected
CLAV closingAvailable
ITAV interimAvailable
].freeze
+ PERIOD_BOUNDARY_TYPES = %w[opbd openingbooked prcd previouslyclosedbooked].freeze
+
+ # Only these types may outrank OPBD/PRCD on freshness (see fresher_balance) —
+ # the same accounting-semantics exclusion as BALANCE_TYPE_PRIORITY itself, so a
+ # forward-looking FWAV or an informational INFO/OTHR can't override a legitimate
+ # booked figure just by carrying a later reference_date. Compared against an
+ # already-normalized balance_type (see normalize_balance_type), so both ISO
+ # codes and Enable Banking's descriptive spellings are listed here pre-normalized,
+ # mirroring BALANCE_TYPE_PRIORITY's own XPCD/CLAV/ITAV entries above.
+ FRESHNESS_BALANCE_TYPES = %w[xpcd expected clav closingavailable itav interimavailable].freeze
+
NETWORK_ERRORS = [
::SocketError,
::Errno::ECONNREFUSED,
@@ -256,13 +278,44 @@ class EnableBankingItem::Importer
by_type = balances.index_by { |balance| normalize_balance_type(balance[:balance_type]) }
BALANCE_TYPE_PRIORITY.each do |type|
- balance = by_type[normalize_balance_type(type)]
- return balance if balance.present?
+ normalized_type = normalize_balance_type(type)
+ balance = by_type[normalized_type]
+ next unless balance.present?
+
+ if PERIOD_BOUNDARY_TYPES.include?(normalized_type)
+ fresher = fresher_balance(balance, balances)
+ return fresher if fresher
+ end
+
+ return balance
end
balances.first
end
+ # Guards the OPBD/PRCD priority (see BALANCE_TYPE_PRIORITY) against picking a
+ # stale period-boundary snapshot when the same response also carries a balance
+ # with a strictly newer `reference_date` — an optional Enable Banking field, so
+ # absence on either side just skips the check rather than treating it as older.
+ def fresher_balance(period_boundary_balance, balances)
+ reference_date = parse_reference_date(period_boundary_balance[:reference_date])
+ return nil unless reference_date
+
+ balances
+ .select { |balance| FRESHNESS_BALANCE_TYPES.include?(normalize_balance_type(balance[:balance_type])) }
+ .filter_map { |balance| [ balance, parse_reference_date(balance[:reference_date]) ] }
+ .select { |_, date| date && date > reference_date }
+ .max_by { |_, date| date }
+ &.first
+ end
+
+ def parse_reference_date(value)
+ return nil if value.blank?
+ Date.parse(value.to_s)
+ rescue ArgumentError, TypeError
+ nil
+ end
+
def normalize_balance_type(type)
type.to_s.delete("_-").downcase
end
diff --git a/test/models/enable_banking_item/importer_balance_test.rb b/test/models/enable_banking_item/importer_balance_test.rb
index d6223e03aa..618f6c1b8c 100644
--- a/test/models/enable_banking_item/importer_balance_test.rb
+++ b/test/models/enable_banking_item/importer_balance_test.rb
@@ -29,6 +29,18 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCase
@importer = EnableBankingItem::Importer.new(@enable_banking_item, enable_banking_provider: @mock_provider)
end
+ test "FRESHNESS_BALANCE_TYPES only lists normalized spellings that BALANCE_TYPE_PRIORITY itself accepts" do
+ normalized_priority_types = EnableBankingItem::Importer::BALANCE_TYPE_PRIORITY.map { |type| type.delete("_-").downcase }
+
+ assert_empty EnableBankingItem::Importer::FRESHNESS_BALANCE_TYPES - normalized_priority_types,
+ "FRESHNESS_BALANCE_TYPES drifted from BALANCE_TYPE_PRIORITY - every accepted spelling here must also be recognized there"
+ end
+
+ test "PERIOD_BOUNDARY_TYPES lists both the ISO code and descriptive spelling for OPBD and PRCD" do
+ assert_equal %w[opbd openingbooked prcd previouslyclosedbooked].sort, EnableBankingItem::Importer::PERIOD_BOUNDARY_TYPES.sort,
+ "PERIOD_BOUNDARY_TYPES must cover both spellings for OPBD/PRCD, mirroring FRESHNESS_BALANCE_TYPES, or the freshness guard silently skips ASPSPs that send the descriptive spelling"
+ end
+
test "fetch_and_update_balance prefers booked balance before available balance" do
@mock_provider.stubs(:get_account_balances).returns(
balances: [
@@ -50,6 +62,211 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCase
assert_equal BigDecimal("-50.00"), @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: [
+ {
+ balance_type: "CLAV",
+ balance_amount: { amount: "2000.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "XPCD",
+ balance_amount: { amount: "1500.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "PRCD",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "DBIT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("-50.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance prefers opening booked balance over previously-closed booked balance" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "PRCD",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "openingBooked",
+ balance_amount: { amount: "75.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("75.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance prefers a materially newer expected balance over a stale opening booked balance" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "OPBD",
+ reference_date: "2026-09-01",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "XPCD",
+ reference_date: "2026-09-25",
+ balance_amount: { amount: "75.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("75.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance keeps the period-boundary balance when reference_date is missing" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "OPBD",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "XPCD",
+ reference_date: "2026-09-25",
+ balance_amount: { amount: "75.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("50.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance prefers a materially newer descriptive-spelling balance over a stale opening booked balance" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "OPBD",
+ reference_date: "2026-09-01",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "closingAvailable",
+ reference_date: "2026-09-25",
+ balance_amount: { amount: "75.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("75.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance prefers a materially newer balance over a stale descriptive-spelling opening booked balance" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "openingBooked",
+ reference_date: "2026-09-01",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "closingAvailable",
+ reference_date: "2026-09-25",
+ balance_amount: { amount: "75.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("75.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance ignores a newer reference_date on a non-accounting balance type" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "OPBD",
+ reference_date: "2026-09-01",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "FWAV",
+ reference_date: "2026-09-25",
+ balance_amount: { amount: "999.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("50.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance keeps the period-boundary balance when it is not older than other balances" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "OPBD",
+ reference_date: "2026-09-25",
+ balance_amount: { amount: "50.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "XPCD",
+ reference_date: "2026-09-01",
+ balance_amount: { amount: "75.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("50.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance falls back to the first balance when only unprioritized types are present" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ {
+ balance_type: "FWAV",
+ balance_amount: { amount: "10.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "OTHR",
+ balance_amount: { amount: "999.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("10.00"), @enable_banking_account.reload.current_balance
+ end
+
test "fetch_and_update_balance handles descriptive booked balance types" do
@mock_provider.stubs(:get_account_balances).returns(
balances: [