Enable Banking: don't keep a stale closing booked balance

Merged
NK
9d ago
+191-14

Problem

BALANCE_TYPE_PRIORITY puts CLBD first, and the freshness guard added for period-boundary balances only covers OPBD/PRCD. Commerzbank DE returns a CLBD dated to the last closed business day, next to ITAV and XPCD that already include today's bookings:

CLBD  reference_date=<2 days ago>                         (yesterday's and today's bookings missing)
ITAV  last_change_date_time=<today>T18:32:22              (current)
XPCD  last_change_date_time=<today>T18:32:22              (current)

So the account keeps the old CLBD balance until the bank closes the next day. After a salary or any larger booking, the balance in Sure is off by that amount for one or two days, while the transactions themselves are already imported.

Two things stop the existing guard from catching this:

  1. CLBD is not in PERIOD_BOUNDARY_TYPES.
  2. fresher_balance only reads reference_date, and Commerzbank's interim balances carry only last_change_date_time.

Behavior

  • CLBD guard: CLBD (and closingBooked) is added to PERIOD_BOUNDARY_TYPES. A CLBD is replaced only when the same response carries a balance dated strictly later. A same-day CLBD, or one without reference_date, is kept as before.
  • ITBD as a replacement: ITBD (interimBooked) joins FRESHNESS_BALANCE_TYPES. For OPBD/PRCD nothing changes, because ITBD already outranks them.
  • Date fallback: a candidate without reference_date is dated by last_change_date_time. The stale balance itself still needs a reference_date.
  • Same-day tie: when several fresher balances share the newest date, the type that ranks higher in FRESHNESS_BALANCE_TYPES wins, so ITBD beats XPCD and XPCD beats CLAV/ITAV. Previously the response order decided, and an ITAV may include an arranged overdraft. Same-type ties go through the existing tiebreak unchanged.

Validation

  • Seen on a self-hosted v0.7.6 instance with Commerzbank DE linked: on a payday Sure showed the CLBD from two days before. With this change (applied as a patch on v0.7.6), the account shows the ITAV/XPCD figure, which matches the bank app and the imported transactions.
  • New tests in importer_balance_test.rb:
    • the Commerzbank shape: a stale CLBD plus ITAV/XPCD dated only by last_change_date_time; XPCD wins;
    • a CLBD from the same day as the interim balances is kept;
    • a newer ITBD beats a same-day ITAV;
    • the descriptive closingBooked spelling is guarded too.
  • Without the importer change, these tests and the updated PERIOD_BOUNDARY_TYPES test fail; the same-day test passes either way.
  • bin/rails test over all Enable Banking model, controller and provider tests: 577 runs, 0 failures. bin/rubocop reports no offenses on both files.

šŸ¤– Generated with Claude Code


Summary by cubic

Fixes Enable Banking accounts keeping a stale closing booked balance. Commerzbank DE reports CLBD for the last closed business day while ITAV/XPCD already include today's bookings, so the balance could be off by a salary or other large booking for a day or two.

  • CLBD now gets the same freshness guard as OPBD/PRCD and is replaced only by a balance dated strictly later.
  • OPBD/PRCD can replace a stale CLBD and outrank same-day interim expected and available balances.
  • On a credit card read as debt, available balances no longer replace a stale CLBD since they report remaining credit; ITBD/XPCD still do.
  • Interim balances without a reference_date are dated by last_change_date_time.
  • When several fresher balances share the newest date, the higher-priority balance type wins; ITBD counts as a fresher balance too.

Summary by CodeRabbit

  • Bug Fixes
    • Improved balance selection when comparing reported dates, including when a reference date is unavailable.
    • Updated handling of closing-booked balances to account for fresher interim or expected balances while retaining closing balances when dates tie.
    • Applied consistent selection to ISO balance codes and their descriptive equivalents, including credit-card accounts.