Select and link Lunch Flow accounts through the provider kit's flow

Merged
JA
2d ago
+481-534

Summary

Lunch Flow controller convergence, C1e: selecting and linking. Lunch Flow's preload, select and link actions now run through the provider kit's account flow (LunchflowItem::AccountFlow < ProviderItem::AccountFlow) instead of their own 370 lines, so they behave exactly as Brex's do. That fixes three bugs: linking crashed (a 500) when there was no connection, an empty account list crashed linking, and with several connections Lunch Flow silently used whichever one the database returned first. The user now chooses the connection, as decided for the kit.

Part of #5003 (Track B, controller-layer convergence, option (b) and the several-connections decision, decided 2026-11-16). Selection and linking are one slice because the selection forms now post a connection id that only the new link code reads; split, the first slice would leave multi-connection linking broken.

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

Behaviour delta (Lunch Flow)

BeforeAfter
Several connections with API keysthe first by primary key, silently; the account list from one connection's key could be linked under anotherthe user chooses: the new-account page lists "Lunch Flow - " per connection; without a choice, preload returns select_connection and select/link redirect to Provider Settings with "Choose a Lunch Flow connection…"
One connectionthe new-account page shows "Lunch Flow"unchanged (still one "Lunch Flow" entry, no id)
Preloading accounts on the new-account pagealways, for the one entrystill, with one connection; with several there is no preload (the entries are per connection, as Brex's are)
Account listread from the family's first keyread from the chosen connection's key and its 5-minute cache; linking reads the cache too instead of calling the API again
Cached empty listrefetchedserved from the cache (cached: true, has_accounts: false)
link_accounts / link_existing_account with no connectionfirst_or_create! raised RecordInvalid (500)redirect to Provider Settings, nothing created
The provider lists no accounts (accounts: nil)NoMethodError (500)"Failed to link accounts"
An account type Lunch Flow doesn't support (crafted URL or POST)listed and linked anywayselect: "No accounts found…"; link, and selecting or linking an existing account of that type: "Unsupported Lunch Flow account type", nothing linked (the existing-account guard is the kit's, from the flow-linking branch)
An unknown account_id (existing account)404redirect to /accounts, "No account specified"
Existing-account selection when the provider lists nothing"No Lunch Flow accounts found…""All Lunch Flow accounts are already linked" (the kit's message)
Linking several accountseach saved as it wentone transaction (the kit's link_new_accounts!), meant so a failure partway links none. No test here forces a failure partway, so treat that as the kit's design rather than a pinned result. An unexpected error shows the translated generic message
Preload's unexpected-error messagethe raw exception textlunchflow_items.errors.unexpected_error
Currency of a newly linked accountthe provider account's after the snapshot upsert (payload, else stored, else USD)unchanged: the kit now prefers that same value

What changed

  • LunchflowItem::AccountFlow: the kit's hooks for Lunch Flow (API error class, connection resolver, fetching accounts with the chosen connection's key, upserting a provider account, cache-sensitive params api_key / base_url).
  • LunchflowItem: with_credentials scope and resolve_for, as Brex's. LunchflowAccount: name_for, currency_for, default_accountable_attributes (none, as before).
  • LunchflowItemsController: preload, select, select-existing and both link actions call the flow; the old bodies go (-370 lines).
  • ProviderItem::AccountFlow#link_new_accounts!: currency is the provider account's after the upsert, falling back to the payload's. For Brex both come from the same payload (BrexAccount.currency_code_from_money), so Brex is unchanged; Brex's tests pass unedited.
  • Provider::LunchflowAdapter.connection_configs: one entry per credentialed connection when there are several.
  • Views: both selection forms post lunchflow_item_id.
  • Locales (17): no_api_key → no_api_token and lunchflow_account_not_found / lunchflow_account_already_linked → provider_account_*, translations kept; unexpected_error under both selection scopes copies each locale's existing api_error.unexpected_error; the touched blocks are sorted. New English keys for the connection choice, the type check and the per-connection entries; other locales fall back to English for those.
  • Removed, now unused: Family#has_lunchflow_credentials?, Provider::LunchflowAdapter.build_provider, select_existing_account.no_accounts_found.

Screenshots

The new-account method selector for a depository account ("How would you like to add it?"), captured headlessly in Chrome at 900 px from the fixture family: before at the base head (7ec62e78), after at this PR's head (e25c3eb4).

Two Lunch Flow connections ("Everyday banking" and "Joint account"). Before, one "Link with Lunch Flow" entry, which linked from whichever connection the database returned first. After, one entry per connection.

BeforeAfter
Before: one Link with Lunch Flow entry for two connectionsAfter: Link with Lunch Flow - Joint account and Link with Lunch Flow - Everyday banking

One Lunch Flow connection: unchanged, a single "Link with Lunch Flow" entry.

BeforeAfter
Before: one Link with Lunch Flow entryAfter: one Link with Lunch Flow entry, unchanged

The selection dialogs only gain a hidden lunchflow_item_id field, so they look the same.

Demo data

No demo data needed.

Migration notes

No migration.

Verification

  • Tests (fail-first): new LunchflowItemsSelectionTest (20 tests), one per row of the table: choosing among two connections (preload, select, link); the chosen connection's accounts offered and linked under it, with its id in the form; the existing-account form's id; no connection; nil list; unsupported type (select and link); unknown account (select and link); empty list for an existing account; translated preload error; cached empty list; per-connection and single entries on the new-account page; the cache read on linking. On the old code 16 fail. Four pass before and after as guards: the stored-currency case, "account not found", the single entry, and the English no-credentials text. The second connection's id sorts after the fixture's, so code that takes the family's first connection picks the wrong one.
  • Existing tests: five LunchflowItemsControllerTest tests stubbed Provider::LunchflowAdapter.build_provider, which no longer exists; they now stub the connection's lunchflow_provider instead (stub lines only, assertions unedited, including the German error texts). Brex's tests pass unedited.
  • Mutations, each caught: the chosen id ignored; several connections resolved to the first silently; accounts fetched with the family's first key; the provider account upserted under the family's first connection; the kit reverting to the payload's currency; one entry regardless of connections; either form dropping the connection id.
  • bin/rails test (full, at the C1e commit): 16,371 runs, 0 failures; the same 8 ReleaseNotesWorkflowTest errors (no node in the test image).
  • bin/rubocop, erb_lint on the changed views: no offenses. bin/brakeman: no warnings. Every locale file parses.
  • Size: non-test +259 / -509, of which locales +121 / -106 (mostly renames, alphabetical moves).
  • Checked against upstream AGENTS.md (blob 1a5dbebd, 2026-11-17): no deviations.

Blast radius

  • Provider::LunchflowAdapter.connection_configs is read by the new-account method selector (accounts/new), which gates the preload controller on the "lunchflow" key; that is why the single-connection entry keeps that key.
  • ProviderItem::AccountFlow#link_new_accounts! also serves Brex (see above).
  • link_existing_account!, setup, update, create, destroy and sync are unchanged here; setup converges in C1f.
  • Siblings: Mercury converges in its own slices.

Review notes

A code review of the diff before commit raised points across two passes. Taken: linking moved into this slice (the selection forms posted a connection id the old link code ignored); orphans removed (build_provider, has_lunchflow_credentials?, an unused locale key); method spacing and placement; touched locale blocks sorted. Noted, not changed:

  • no preload with several connections, the existing-account empty message, the transaction, and the type check: the kit's behaviour, in the table above;
  • update still expires the cache with its own key: unchanged behaviour, replaced when the controller moves onto the kit;
  • the kit re-queries the credentialed connections a few times per request: the kit's existing behaviour.

Summary by cubic

Lunch Flow's account selection and linking now run through the provider kit's account flow (LunchflowItem::AccountFlow) instead of their own 370 lines, so they match Brex's behavior. This fixes three bugs: linking raised a 500 without a connection, a nil account list crashed linking, and with several connections Lunch Flow silently used the database's first one.

Behavior changes

  • With several credentialed connections the new-account page lists "Lunch Flow - " per connection; preload and selection ask which to use, and both link forms post lunchflow_item_id.
  • Accounts come from the chosen connection's key and its 5-minute cache, and a cached empty list is now served from cache.
  • Unsupported account types are refused; an unknown existing-account id redirects to /accounts, and linking without a connection redirects to Provider Settings.
  • Linking several accounts runs in one transaction, and preload errors show a translated generic message instead of raw exception text.

What changed

  • LunchflowItem::AccountFlow adds the kit hooks (error class, connection resolution, fetch, snapshot upsert); the controller's preload/select/link bodies are deleted.
  • Provider::LunchflowAdapter.connection_configs emits one entry per credentialed connection when several exist.
  • The kit's link_new_accounts! now prefers the snapshot-upsert currency, so a stored currency is kept when the payload omits one (Brex unchanged).
  • Locale keys renamed (no_api_key → no_api_token, lunchflow_account_* → provider_account_*); removed now-unused Family#has_lunchflow_credentials? and Provider::LunchflowAdapter.build_provider.
  • The controller's cache-write guard goes with the code it guarded; the kit's flow guards the cache write the same way, and the helper's cache test is dropped.
  • New LunchflowItemsSelectionTest (20 tests) pins the connection-choice, missing-connection, unsupported-type and cache cases; controller tests now stub lunchflow_provider instead of build_provider.