Select and link Mercury accounts through the provider kit's flow

JA
6d ago
+332-554

Summary

Mercury controller convergence, C2e: selecting and linking. Mercury's preload, select and link actions now run through the provider kit's account flow (MercuryItem::AccountFlow < ProviderItem::AccountFlow) instead of their own code, as Lunch Flow's do since C1e (#734). Mercury already made the user choose among several connections, so that part is unchanged; the rest of the delta is the kit's error handling, type checks and caching.

Part of jaysbeekay/sure#670 (Track B, controller-layer convergence for Mercury, owner 2026-11-16). Stacked on C2d (jaysbeekay/sure#741) in fork stack #687.

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

Behaviour delta (Mercury)

BeforeAfter
Choosing among several connectionsthe user chooses (mercury_item_id)unchanged
Preload with an unknown or stale mercury_item_idno_credentialsselect_connection, "Choose a Mercury connection in Provider Settings."
Preload's "choose a connection" text"Choose a Mercury connection before loading accounts.""Choose a Mercury connection in Provider Settings." (the kit reads select_accounts.select_connection)
Preload's unexpected-error messagethe raw exception textmercury_items.errors.unexpected_error
Cached empty listrefetchedserved from the cache
An account type Mercury doesn't support (crafted URL; Mercury supports Depository only)listed, and linkableselect: "No accounts found…"; link: "Unsupported Mercury account type"; selecting accounts for an existing non-Depository account offers none
An unknown account_id (existing account)404redirect to /accounts, "No account specified"
link_existing_account with no account_id"Missing required parameters""No account specified" (no Mercury account id is still "Missing required parameters")
Existing-account selection when the provider lists nothing"No Mercury accounts found…""All Mercury accounts are already linked" (the kit's message)
Linkinga fresh get_accounts callthe connection's 5-minute cache
The provider lists no accounts (accounts: nil)NoMethodError (500)"Failed to link accounts"
Linking several accountseach saved as it went; Mercury API errors onlyone transaction; any error shows the translated generic message

What changed

  • MercuryItem::AccountFlow: the kit's hooks for Mercury (API error class, connection resolver, fetching with the chosen connection's token, upserting a provider account, cache-sensitive params token / base_url).
  • MercuryItem: with_credentials scope and resolve_for, as Brex's. MercuryAccount: name_for (nickname, name, then business name), currency_for (USD), default_accountable_attributes (none, as before).
  • ProviderItem::BLANK_TOKEN_SENTINELS: moved up from BrexItem (which still resolves it) so Mercury's scope shares it.
  • MercuryItemsController: preload, select, select-existing and both link actions call the flow. The old bodies go, along with the five connection-selection helpers they used.
  • Locales (16): mercury_account_not_found / mercury_account_already_linked → provider_account_*, translations kept. The two orphaned helper blocks (mercury_item_selection_error_payload, render_mercury_item_selection_failure) and select_existing_account.no_accounts_found go. New English keys for the unexpected error, the type check and no_account_specified. Mercury's old texts for these were English literals in the controller, so no locale loses text.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first): new MercuryItemsSelectionTest (12 tests), one per row above, plus a blank-token connection not counting as a second connection. On the old code 10 fail; the "account not found" message and the blank-token case pass before and after as guards.
  • Existing tests unedited: all 17 MercuryItemsControllerTest tests pass, including the connection-choice ones, along with the return-path tests and Brex's.
  • Mutations, each caught: the chosen id ignored (4 existing tests and 1 new); several connections resolved to the first silently (3 existing); blank tokens counted as credentials; the provider account upserted under the family's first connection; fetching with the family's provider instead of the chosen one.
  • bin/rails test (full, at the C2e commit): 16,446 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. Every locale file parses.
  • Size: non-test +160 / -554.
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • Provider::MercuryAdapter.connection_configs is unchanged: it still lists one entry per credentialed connection.
  • BrexItem::BLANK_TOKEN_SENTINELS still resolves (inherited); nothing else names it.
  • Setup, update, create, destroy and sync are unchanged here; they converge in C2f and C2g.

Review notes

A code review of the diff before commit raised ten points. Taken: the orphaned no_accounts_found key is removed. Noted, not changed:

  • Whitespace tokens: the SQL scope excludes only the listed blank sentinels, where the old Ruby filter caught any whitespace. Such a token can't be saved through the app: create validates presence, and update drops a blank token.
  • the type filter on existing-account selection, the empty-list message, the cache read on linking, the stale-id message and the per-parameter messages: all the kit's behaviour, in the table above;
  • update's own cache helpers go in C2g, and setup's own fetch goes in C2f;
  • the touched locale block is sorted, as in Lunch Flow's C1e.