Move Brex account selection into a shared account flow

JA
6d ago
+411-205

Summary

Provider kit Track B, B2a: the selection half of Brex's account flow moves into a shared ProviderItem::AccountFlow. Choosing a connection, preloading and caching its accounts, and offering the ones not linked yet now live in the base; Brex keeps thin hooks.

Part of jaysbeekay/sure#670 (Track B, item 2, first of three slices: B2a selection, B2b linking, B2c setup). Stacked on B1 (jaysbeekay/sure#685) in fork stack #687.

Upstream draft PR: https://github.com/we-promise/sure/pull/5227 (same branch, same commit), in native stack we-promise/sure#5268 on refactor/provider-kit.

What changed

  • ProviderItem::AccountFlow (new): selected?, selection_required?, preload_payload, select_accounts_result, select_existing_account_result, the accounts cache and the unlinked-accounts filter, moved from BrexItem::AccountFlow with the provider named by its declared key:
    • i18n scope <key>_items.*, cache key <key>_accounts_<family>_<item> (for Brex: identical to before), supported types from Provider::<Key>Adapter;
    • the connection's item class is the flow's parent (BrexItem for BrexItem::AccountFlow), its association and key come from the adapter's declares_connection;
    • hooks a provider implements: fetch_accounts, resolve_item, provider_error_class, .cache_sensitive_params, and optionally account_offered_as? to narrow accounts per type.
  • BrexItem::AccountFlow < ProviderItem::AccountFlow keeps its public names: brex_item, brex_item_id, the brex_item: / brex_item_id: keywords, result.brex_item, and BrexItem::AccountFlow::NoApiTokenError (the same class as the base's). Linking and Setup stay in Brex's file until B2b and B2c.
  • One alias, BrexItem::AccountFlow::NoApiTokenError = ProviderItem::AccountFlow::NoApiTokenError: Setup is a nested module whose constant lookup does not see inherited constants. It goes when Setup moves in B2c.

Nothing user-visible changes. Log lines read the same ("Brex preload error", "Brex API error in select_accounts").

Demo data

No demo data needed: no new records or UI.

Migration notes

No migration.

Verification

  • Existing tests unchanged: the test/ diff against B1 touches only the contract module and Brex's lifecycle test (both new in B1). Brex's flow, controller, item, account, importer and syncer tests pass unedited (201 runs, 0 failures across Brex, provider settings and the provider connections API).
  • Contract (fail-first): ProviderItemLifecycleTests adds
    • "the account flow is the shared flow and keeps its old constants",
    • "selection offers the connection's accounts that are not linked yet" (before and after linking one),
    • "selection offers nothing for a type the provider does not support",
    • "each connection caches its own accounts under the provider's key" (a second connection does not read the first one's cache).
      Without the change, all fail: uninitialized constant ProviderItem::AccountFlow and unknown keyword: :item.
  • Brex test: "Brex offers cash accounts as depository and cards as credit cards" (its account_offered_as? hook).
  • Mutations, each caught: cache key without the item id; linked accounts not excluded; Brex offering every account for every type; the result dropping the item; a cache key not prefixed by the provider key; the cache never expiring on a credentials update; an unsupported type offered accounts.
  • bin/rails test (full, at the B2a commit): 16,265 runs, 0 failures; the same 8 ReleaseNotesWorkflowTest errors (no node in the test image).
  • bin/rubocop on changed files: no offenses. bin/brakeman: no warnings. No ERB or JS changed.
  • Diff with copy detection (git diff -C against B1): non-test +296 / -203.
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • BrexItems::AccountFlowsController calls preload_payload, select_accounts_result and select_existing_account_result and reads result.brex_item; BrexItems::AccountSetupsController builds the flow with brex_item:; BrexItemsController#update calls update_item_with_cache_expiration. All keep their names.
  • Brex's linking methods and Setup call brex_item, accounts, indexed_accounts and supported_account_type?, which now come from the base.
  • Siblings: Lunch Flow, Mercury and Sophtron build their own <key>_accounts_… cache keys in their controllers and models; they move in their own slices (B7, B8 and later), so they are untouched here.