Fill a Redbark card's credit limit from its balances

JA
2d ago
+376-5

Fixes jaysbeekay/sure#897
Upstream issue: we-promise/sure#5441
Upstream PR: https://github.com/we-promise/sure/pull/5990 (we-promise/sure#5990, draft, position 5 of the native stack https://github.com/we-promise/sure/stack/5995); same branch, same commits.

Stack: base up/card-limit-chart-line (jaysbeekay/sure#1021) ← jaysbeekay/sure#1020 ← jaysbeekay/sure#1006. Per the owner's decision of 2026-11-21 this slice writes jaysbeekay/sure#900's credit_limit column (added in jaysbeekay/sure#1020) and adds no credit_cards migration.

Summary

Redbark's balances carry an available balance beside the current one, which the importer threw away. It is now stored. For a credit card whose provider behind Redbark is Fiskil, the processor writes credit_limit = owed + available, rounded to the cent, through enrich_attribute(..., source: "redbark"). A limit the user set by hand (and so locked) is kept, and an unlocked one follows every sync. Other providers get no derived limit. available_credit is not touched.

What changed

  • Migration 20261128120100_add_available_balance_to_redbark_accounts: redbark_accounts.available_balance decimal(19,4), nullable.
  • RedbarkItem::Importer#import_balances: stores availableBalance (nil when the reading has none, so an old figure is not kept).
  • RedbarkAccount::Processor#update_credit_limit, CreditCard only:
    • Fiskil only. For a Fiskil card, owed + available matched the card's limit on the owner's live reading. Plaid documents a credit account's available balance as "the limit less the current balance, less any pending outflows plus any pending inflows", so its sum falls short while a charge is pending. A Plaid, blank or unrecognised provider gets no derived limit.
    • A limit Redbark wrote earlier on a card that is not Fiskil's is cleared on the next sync, but only while the card still holds the value Redbark recorded for it. A locked limit, or one another writer replaced, is kept.
    • It reuses the balance update_account_balance already normalised (positive when owing, negative in credit), so normalized_liability_balance is not called a second time. It is skipped when either balance is missing or the sum is not positive.
    • The sum is rounded to two places, so the column and its data_enrichments row hold the same figure and a resync does not save again.
    • It runs after the transaction import and CreditCardDetailsProcessor, so a failed limit write cannot hold up the sync's transactions.
    • Card guard (2026-11-29): it returns on accountable_type before loading the accountable, then returns when the CreditCard row is missing, so such an account syncs its balance without raising NoMethodError. Test "an account whose card record is missing syncs without a limit" (Fiskil and Plaid) failed first with undefined method 'enrich_attribute' for nil.
    • A save the card refuses logs one warning per account per refresh window, through CreditCardDetailsProcessor.warn_once, with the connection, the Redbark account, the limit and the errors. The card in memory is restored afterwards.

What the derivation rests on

The importer calls Redbark v1: Provider::Redbark::BASE_URL = "https://api.redbark.com/v1" (app/models/provider/redbark.rb:9), and get_balances documents its items as { accountId, currentBalance, availableBalance, currency } (:59). There is no public v1 field definition. redbark.com/docs/openapi/v1.json returns 404 and api.redbark.com/v1/openapi.json returns 401. The v2 schema (redbark.com/docs/openapi/v2.json) is not what this code calls.

Behind Redbark, Fiskil serves the Consumer Data Standards. BankingBalance defines currentBalance as "The balance of the account at this time. Should align to the balance available via other channels such as Internet Banking. Assumed to be negative if the customer has money owing", and availableBalance as "Balance representing the amount of funds available for transfer. Assumed to be zero or positive". Neither definition says anything about pending items.

Checked against the owner's Bankwest card through Fiskil, from its stored reading: owed 902.54 + available 5,097.46 = 6,000.00, the card's limit. Unverified: whether the sum still holds while a charge is pending. No live reading has been taken with a pending charge open. The test "a reading with a pending authorisation gives the same limit" pins the arithmetic only, for a reading where a pending item moves both balances by the same amount.

No freshness gate: v1 carries no freshness field (freshness/observed_at are v2 only). The limit comes from one reading, and the account's own balance is written from that same reading with no gate.

Tests (red on #1021: every new test errors with unknown attribute 'available_balance')

test/models/redbark_account/processor_test.rb

  • Fiskil −533.10 / 5,466.90 → 6,000; available_credit 1,234 unchanged
  • pending authorisation reading −633.10 / 5,366.90 → 6,000
  • Fiskil overpaid +100 / 6,100 → 6,000
  • a Plaid card behind Redbark gets no limit (and no enrichment row); a blank or unrecognised provider gets none either
  • two syncs over a stored 5,000: → 6,000 → 7,000
  • locked 5,000 stays 5,000
  • available missing, current missing, or 0 / 0 → limit unchanged (no raise)
  • Loan and Depository with an available balance → accountable unchanged
  • blank provider + available → still exactly one redbark_sync entry from the balance normalising

Added after the Gatekeeper review (each failed first as noted):

  • "a Plaid card behind Redbark gets no limit from its balances": before, Expected [0.6e4, 0] Actual [0.59e4, 1]
  • "a card whose provider is blank or unrecognised gets no limit": before, Expected 0.6e4 to be nil
  • "a limit Redbark wrote on a card that is not Fiskil's is cleared": before, Expected 0.59e4 to be nil; and its guard "a non-Fiskil card keeps a locked limit, or one Redbark did not write"
  • "a sum finer than cents is stored and recorded as the same limit" (a second sync makes no save): before, Expected 0.6e4 Actual 0.60000034e4
  • "a limit Redbark wrote from a sum finer than cents is cleared when the card leaves Fiskil": before, Expected 0.6e4 to be nil
  • "a limit that fails to save is reported once, as a warning": before, no entry
  • "a refused limit leaves the card in memory as it was saved": before, Expected 0.5e4 Actual 0.6e4
  • "nothing is logged when no save of the limit was attempted" (a contract guard: no step on this head leaves errors on the card there)
  • "an error writing the limit leaves the sync's transactions imported", through both process and process_accounts: before, Expected false to be truthy

test/models/redbark_item/importer_test.rb: availableBalance stored beside currentBalance; a reading without it stores nil over an old 99.

Mutations (each alone)

#MutationCaught by
M1no CreditCard guardLoan/Depository test
M2abs(current) + availablePlaid overpaid, Fiskil overpaid
M3always negate currentPlaid test
M4update! instead of enrich_attributelocked limit kept
M5no positive check0/0 test
M6write only when blanktwo-sync refresh
M7importer drops availableBalanceimporter test
M8normalise a second timeexactly one log entry
M9write available_credit instead6 tests
M10Plaid allowed, or every provider treated as FiskilPlaid no-limit, blank/unrecognised, cleared, keeps-locked
M11non-Fiskil clear removedcleared test
M12redbark_wrote_limit? without the value matchkeeps-locked test
M13.round(2) dropped from the sum, or from the comparisonthe two finer-than-cents tests
M14plain capture instead of warn_once, or the connection/limit dropped from itreported-once test
M15restore_attributes or errors.clear droppedrefused-limit test
M16"no save attempted" check droppednothing-logged and user-set-limit tests
M17write moved back before the transactionstransactions-imported test

Verification

  • Full unit suite (beastpc) on 6b7d5425d: 19516 runs, 0 failures, 8 errors, 9 skips (the 8 ReleaseNotesWorkflowTest errors, no node). After merging the updated slices below (3a94f5c87), the targeted card and Redbark files ran 525 runs, 0 failures, 0 errors. With jaysbeekay/sure#1020's new ceiling in place, a sum of 10**8 or more is refused by the card's validation and logged once; nothing reaches the database.
  • bin/rubocop: no offenses. bin/brakeman: 0 warnings.
  • Current head fbd49f5e1 (2026-11-30): it adds a comment-only change to RedbarkAccount::Processor and merges in jaysbeekay/sure#1021's test-helper fix. CI passes on fbd49f5e1 (12 runs). The full-suite figures above are from 6b7d5425d; no full suite has run on fbd49f5e1 itself.

Screenshots

No UI change: the limit this writes shows through #1020's Overview card and #1021's chart line.

Migration notes

One nullable column on redbark_accounts; filled on the next balances sync. No backfill.

Blast radius

  • import_balances runs for every Redbark account type; Depository/Loan/Investment store the new column and nothing reads it.
  • The processor's new step runs after the transaction import and CreditCardDetailsProcessor; it can only write credit_cards.credit_limit and its redbark provenance row (lock-respecting). Its readers are #1020/#1021's (Overview, chart, form, importer/exporter, GetAccounts).
  • Overlap: upstream PRs #3632, #3753, #4873, #5092 touch redbark_account/processor.rb; textual only.