Move Brex account linking into the shared account flow

Merged
JA
1d ago
+313-182

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::AccountFlow gains link_new_accounts_result, link_existing_account_result, link_new_accounts!, link_existing_account!, the navigation messages, the link errors (AccountNotFoundError, InvalidAccountNameError, AccountAlreadyLinkedError) and NavigationResult / LinkAccountsResult, moved from BrexItem::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, which BrexAccount already has);
    • a new hook, upsert_provider_account!, creates or refreshes the provider account for a payload.
  • BrexItem::AccountFlow keeps the brex_account_id: keyword on both link methods (thin wrappers over provider_account_id:), implements upsert_provider_account!, and keeps upsert_brex_account! as an alias because Setup still 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): ProviderItemLifecycleTests adds
    • "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, and missing 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 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 B2a): non-test +202 / -181.
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • BrexItems::AccountFlowsController#link_accounts and #link_existing_account call the two _result methods (the latter with brex_account_id:) and read target, flash_type and message; unchanged.
  • Brex's Setup calls upsert_brex_account! (kept as an alias) and raises NoApiTokenError (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_result and link_existing_account_result reject 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.