Move Brex account selection into a shared account flow
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 fromBrexItem::AccountFlowwith the provider named by its declared key:- i18n scope
<key>_items.*, cache key<key>_accounts_<family>_<item>(for Brex: identical to before), supported types fromProvider::<Key>Adapter; - the connection's item class is the flow's parent (
BrexItemforBrexItem::AccountFlow), its association and key come from the adapter'sdeclares_connection; - hooks a provider implements:
fetch_accounts,resolve_item,provider_error_class,.cache_sensitive_params, and optionallyaccount_offered_as?to narrow accounts per type.
- i18n scope
BrexItem::AccountFlow < ProviderItem::AccountFlowkeeps its public names:brex_item,brex_item_id, thebrex_item:/brex_item_id:keywords,result.brex_item, andBrexItem::AccountFlow::NoApiTokenError(the same class as the base's). Linking andSetupstay in Brex's file until B2b and B2c.- One alias,
BrexItem::AccountFlow::NoApiTokenError = ProviderItem::AccountFlow::NoApiTokenError:Setupis a nested module whose constant lookup does not see inherited constants. It goes whenSetupmoves 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):
ProviderItemLifecycleTestsadds- "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::AccountFlowandunknown 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 8ReleaseNotesWorkflowTesterrors (nonodein the test image).bin/rubocopon changed files: no offenses.bin/brakeman: no warnings. No ERB or JS changed.- Diff with copy detection (
git diff -Cagainst B1): non-test +296 / -203. - Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.
Blast radius
BrexItems::AccountFlowsControllercallspreload_payload,select_accounts_resultandselect_existing_account_resultand readsresult.brex_item;BrexItems::AccountSetupsControllerbuilds the flow withbrex_item:;BrexItemsController#updatecallsupdate_item_with_cache_expiration. All keep their names.- Brex's linking methods and
Setupcallbrex_item,accounts,indexed_accountsandsupported_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::AccountFlownow subclasses the base and keepsbrex_item,brex_item_id,result.brex_item, andNoApiTokenError.- 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_classhook;preload_payloadand selection results surface them asapi_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.