Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Enable Banking: don't keep a stale closing booked balance (#5886)

Nikolay Kopylov8140e2c3ffd1
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rb
index f708ee0fff..fc028a8dea 100644
--- a/app/models/enable_banking_item/importer.rb
+++ b/app/models/enable_banking_item/importer.rb
@@ -39,7 +39,9 @@ class EnableBankingItem::Importer
# 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.
+ # newer ITBD/XPCD/CLAV/ITAV, the fresher one wins instead. CLBD gets the same
+ # guard: Commerzbank DE keeps it at the last closed business day while ITAV/XPCD
+ # already include the day's bookings.
BALANCE_TYPE_PRIORITY = %w[
CLBD closingBooked
ITBD interimBooked
@@ -60,7 +62,7 @@ class EnableBankingItem::Importer
ITAV interimAvailable
] + BALANCE_TYPE_PRIORITY).uniq.freeze
- PERIOD_BOUNDARY_TYPES = %w[opbd openingbooked prcd previouslyclosedbooked].freeze
+ PERIOD_BOUNDARY_TYPES = %w[clbd closingbooked 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
@@ -69,7 +71,15 @@ class EnableBankingItem::Importer
# 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
+ FRESHNESS_BALANCE_TYPES = %w[itbd interimbooked xpcd expected clav closingavailable itav interimavailable].freeze
+
+ # A stale CLBD may also be replaced by a newer OPBD/PRCD. Ordered like
+ # BALANCE_TYPE_PRIORITY, which decides between same-day candidates.
+ CLOSING_BOOKED_FRESHNESS_TYPES = (%w[itbd interimbooked opbd openingbooked prcd previouslyclosedbooked] + FRESHNESS_BALANCE_TYPES).uniq.freeze
+
+ # On a credit card read as debt, these report remaining credit, so they can't
+ # replace a booked balance on freshness.
+ AVAILABLE_FRESHNESS_TYPES = %w[clav closingavailable itav interimavailable].freeze
# Balances that already count pending transactions. Sure keeps pending entries
# out of balance history, so the pending amounts are added back before such a
@@ -574,14 +584,15 @@ class EnableBankingItem::Importer
# Checked before the tiebreak, so a tie among OPBD/PRCD entries that a
# fresher balance replaces anyway is neither resolved nor logged.
newest = candidates.max_by { |candidate| parse_reference_date(candidate[:reference_date]) || Date.new(0) }
- fresher = fresher_balance(newest, balances)
+ credit_card_debt = !prefer_available && enable_banking_account.current_account&.accountable_type == "CreditCard"
+ fresher = fresher_balance(newest, balances, credit_card_debt: credit_card_debt)
if fresher
# fresher_balance takes the first entry of the newest date, so
# same-type entries of that date go through the same tiebreak.
# Older entries of that type still make the pick unverified.
same_type = by_type[normalize_balance_type(fresher[:balance_type])]
- fresher_date = parse_reference_date(fresher[:reference_date])
- same_day = same_type.select { |candidate| parse_reference_date(candidate[:reference_date]) == fresher_date }
+ fresher_date = balance_freshness_date(fresher)
+ same_day = same_type.select { |candidate| balance_freshness_date(candidate) == fresher_date }
fresher, fresher_stage = resolve_same_type_balances(same_day, enable_banking_account)
return [ fresher, same_day.size == same_type.size ? fresher_stage : :fresher ]
end
@@ -840,22 +851,34 @@ class EnableBankingItem::Importer
.first&.metadata&.dig("fingerprint")
end
- # Guards the OPBD/PRCD priority (see BALANCE_TYPE_PRIORITY) against picking a
+ # Guards the CLBD/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)
+ def fresher_balance(period_boundary_balance, balances, credit_card_debt: false)
reference_date = parse_reference_date(period_boundary_balance[:reference_date])
return nil unless reference_date
+ types = freshness_types(normalize_balance_type(period_boundary_balance[:balance_type]), credit_card_debt)
+
balances
- .select { |balance| FRESHNESS_BALANCE_TYPES.include?(normalize_balance_type(balance[:balance_type])) }
- .filter_map { |balance| [ balance, parse_reference_date(balance[:reference_date]) ] }
+ .select { |balance| types.include?(normalize_balance_type(balance[:balance_type])) }
+ .filter_map { |balance| [ balance, balance_freshness_date(balance) ] }
.select { |_, date| date && date > reference_date }
- .max_by { |_, date| date }
+ .max_by { |balance, date| [ date, -types.index(normalize_balance_type(balance[:balance_type])) ] }
&.first
end
+ def freshness_types(period_boundary_type, credit_card_debt)
+ types = %w[clbd closingbooked].include?(period_boundary_type) ? CLOSING_BOOKED_FRESHNESS_TYPES : FRESHNESS_BALANCE_TYPES
+ credit_card_debt ? types - AVAILABLE_FRESHNESS_TYPES : types
+ end
+
+ # Some ASPSPs (Commerzbank DE) date interim balances only by last_change_date_time.
+ def balance_freshness_date(balance)
+ parse_reference_date(balance[:reference_date]) || parse_reference_date(balance[:last_change_date_time])
+ end
+
def parse_reference_date(value)
return nil if value.blank?
Date.parse(value.to_s)
diff --git a/test/models/enable_banking_item/importer_balance_test.rb b/test/models/enable_banking_item/importer_balance_test.rb
index cc70197271..7fb54b1592 100644
--- a/test/models/enable_banking_item/importer_balance_test.rb
+++ b/test/models/enable_banking_item/importer_balance_test.rb
@@ -40,9 +40,9 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCase
"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"
+ test "PERIOD_BOUNDARY_TYPES lists both the ISO code and descriptive spelling for CLBD, OPBD and PRCD" do
+ assert_equal %w[clbd closingbooked opbd openingbooked prcd previouslyclosedbooked].sort, EnableBankingItem::Importer::PERIOD_BOUNDARY_TYPES.sort,
+ "PERIOD_BOUNDARY_TYPES must cover both spellings for CLBD/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
@@ -339,6 +339,160 @@ class EnableBankingItem::ImporterBalanceTest < ActiveSupport::TestCase
assert_equal BigDecimal("50.00"), @enable_banking_account.reload.current_balance
end
+ # Commerzbank DE: CLBD stays at the last closed business day, ITAV/XPCD carry
+ # today's bookings and only a last_change_date_time.
+ test "fetch_and_update_balance prefers a newer expected balance over a stale closing booked balance" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("1000.00", reference_date: "2026-11-25"),
+ {
+ balance_type: "ITAV",
+ last_change_date_time: "2026-11-27T18:32:22.389463",
+ balance_amount: { amount: "2500.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "XPCD",
+ last_change_date_time: "2026-11-27T18:32:22.389400",
+ balance_amount: { amount: "2500.01", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("2500.01"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance keeps a closing booked balance from the same day as the interim balances" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("1000.00", reference_date: "2026-11-27"),
+ {
+ balance_type: "XPCD",
+ last_change_date_time: "2026-11-27T18:32:22",
+ balance_amount: { amount: "2500.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("1000.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance prefers a newer interim booked balance over a same-day available one" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("100.00", reference_date: "2026-11-25"),
+ {
+ balance_type: "ITAV",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "900.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "ITBD",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "150.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("150.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance prefers a newer opening booked balance over a same-day available one when replacing a stale closing booked balance" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("1000.00", reference_date: "2026-11-26"),
+ {
+ balance_type: "ITAV",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "6000.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "OPBD",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "1100.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("1100.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance keeps a stale closing booked balance on a credit card over a newer available balance" do
+ link_account(CreditCard.create!)
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("100.00", reference_date: "2026-11-25"),
+ {
+ balance_type: "ITAV",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "900.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("100.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance replaces a stale closing booked balance on a credit card with a newer expected balance" do
+ link_account(CreditCard.create!)
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("100.00", reference_date: "2026-11-25"),
+ {
+ balance_type: "ITAV",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "900.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ },
+ {
+ balance_type: "XPCD",
+ reference_date: "2026-11-26",
+ balance_amount: { amount: "120.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("120.00"), @enable_banking_account.reload.current_balance
+ end
+
+ test "fetch_and_update_balance treats a descriptive-spelling closing booked balance as stale too" do
+ @mock_provider.stubs(:get_account_balances).returns(
+ balances: [
+ clbd("100.00", reference_date: "2026-11-25").merge(balance_type: "closingBooked"),
+ {
+ balance_type: "expected",
+ reference_date: "2026-11-27",
+ balance_amount: { amount: "200.00", currency: "EUR" },
+ credit_debit_indicator: "CRDT"
+ }
+ ]
+ )
+
+ assert @importer.send(:fetch_and_update_balance, @enable_banking_account)
+
+ assert_equal BigDecimal("200.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: [