Move Brex account linking into the shared account flow
Summary
Provider kit Track B, B2b: the linking half of Brex's account flow moves into ProviderItem::AccountFlow. Linking selected provider accounts to new accounts, linking one to an existing account, and the messages for each outcome now live in the base.
Part of #5003 (Track B, item 2, second of three slices: B2a selection, B2b linking, B2c setup).
Stacked on #5227. Fork pre-flight: jaysbeekay/sure#717 (same branch, same commit; its CI, Gatekeeper review and screenshot history live there).
What changed
ProviderItem::AccountFlowgainslink_new_accounts_result,link_existing_account_result,link_new_accounts!,link_existing_account!, the navigation messages, the link errors (AccountNotFoundError,InvalidAccountNameError,AccountAlreadyLinkedError) andNavigationResult/LinkAccountsResult, moved fromBrexItem::AccountFlow:- messages come from
<key>_items.*and logs name the provider by its key (for Brex: identical to before); - account names, currencies and accountable attributes come from the provider account class (
name_for,currency_for,default_accountable_attributes, whichBrexAccountalready has); - a new hook,
upsert_provider_account!, creates or refreshes the provider account for a payload.
- messages come from
BrexItem::AccountFlowkeeps thebrex_account_id:keyword on both link methods (thin wrappers overprovider_account_id:), implementsupsert_provider_account!, and keepsupsert_brex_account!as an alias becauseSetupstill calls it; that goes in B2c.
Nothing user-visible changes.
Demo data
No demo data needed: no new records or UI.
Migration notes
No migration.
Verification
- Existing tests unchanged: the
test/diff against B2a touches only the contract module. Brex's flow, controller, item, account, importer and syncer tests pass unedited (205 runs, 0 failures across Brex, provider settings and the provider connections API). - Contract (fail-first):
ProviderItemLifecycleTestsadds- "linking new accounts creates one linked account per selected provider account" (one account and link created, the success message from the provider's scope, one sync; a second link of the same account creates nothing and reports it as already linked),
- "linking skips an account the provider cannot name",
- "linking an existing account attaches it once" (a second account raises
AccountAlreadyLinkedError), - and the old-constants test now covers the link errors and result types.
Without the change they fail:uninitialized constant ProviderItem::AccountFlow::LinkAccountsResult/AccountNotFoundError, andmissing keyword: :brex_account_id.
- Mutations, each caught: relinking an already-linked account; no guard on linking an existing account twice; no sync after linking; accounts created with the wrong type; unnamed accounts not skipped; the success message read from a missing key. (Pointing it at Lunch Flow's scope instead survives, because the English strings are identical.)
bin/rails test(full, at the B2b commit): 16,268 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 B2a): non-test +202 / -181. - Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.
Blast radius
BrexItems::AccountFlowsController#link_accountsand#link_existing_accountcall the two_resultmethods (the latter withbrex_account_id:) and readtarget,flash_typeandmessage; unchanged.- Brex's
Setupcallsupsert_brex_account!(kept as an alias) and raisesNoApiTokenError(the B2a alias). - Siblings: the other providers' controllers still link accounts in their own code; they move in their own slices (B7, B8 and later).
Summary by cubic
Moves the linking half of Brex's account flow into the shared ProviderItem::AccountFlow (part of #5003, Track B, slice B2b). Brex's behavior is unchanged; the other providers keep their own linking code until their slices land. The shared flow now also refuses to offer or link existing accounts of a type a provider doesn't support.
Bug Fixes
select_existing_account_resultandlink_existing_account_resultreject unsupported account types instead of offering or linking them.
Summary by CodeRabbit
- New Features
- Account linking now supports both new and existing provider accounts, with clearer results for invalid, already-linked, or unsupported accounts.
- Successfully linked accounts trigger a sync; unknown account selections are skipped.
- Improvements
- Existing-account linking accepts either supported account identifier, preferring the Brex account identifier when both are provided.