Move Brex account selection into a shared account flow

Merged
JA
2d 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 #5003 (Track B, item 2, first of three slices: B2a selection, B2b linking, B2c setup).

Stacked on #5006. Fork pre-flight: jaysbeekay/sure#716 (same branch, same commit; its CI, Gatekeeper review and screenshot history live there).

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.

Summary by cubic

Moves Brex's account selection logic into a shared ProviderItem::AccountFlow base class so other providers can reuse it. Choosing a connection, preloading accounts, caching them per connection, and offering not-yet-linked accounts now live in the base; Brex keeps only provider-specific hooks and its existing public names.

  • BrexItem::AccountFlow now subclasses the base and keeps brex_item, brex_item_id, result.brex_item, and NoApiTokenError.
  • The base derives cache keys, i18n scope, and supported account types from the provider's declared key, so Brex's keys and log messages stay unchanged.
  • The base rescues provider API errors through a provider_error_class hook; preload_payload and selection results surface them as api_error.
  • Contract tests cover the shared flow, including per-connection cache isolation, provider API errors, and empty selection for unsupported types; Brex's existing tests pass unedited.

Summary by CodeRabbit

  • New Features
    • Provider account selection now offers accounts based on the selected account type and excludes accounts already linked.
    • When multiple provider connections are available, users can select which connection to use; a single eligible connection is selected automatically.
    • Account choices are cached separately for each provider connection, and relevant connection updates refresh the cached choices.
  • Bug Fixes
    • Provider API errors during account selection are now surfaced as clear selection outcomes.