Add provider kit base classes and move Brex onto them

JA
6d ago
+409-163

Summary

Provider kit Track B, B1: abstract base classes for a provider's connection and accounts, a shared syncer and unlinking, with Brex moved onto them.

Part of jaysbeekay/sure#670. Track B now stacks on top of Track A: this PR's base is A9 (up/provider-kit-empty-state, jaysbeekay/sure#694) and it is the next PR in fork stack #687.

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

What changed

  • ProviderItem and ProviderAccount: abstract base classes (self.abstract_class = true) over the existing per-provider tables. No migration, no STI; Sync#syncable_type and AccountProvider#provider_type keep recording BrexItem / BrexAccount.
    • ProviderItem includes Syncable and the shared unlinking, and exposes provider_key and provider_accounts (read from the adapter's declares_connection since 7514143f2).
    • ProviderAccount holds the account_provider / account associations and current_account.
  • ProviderItem::Syncer is Brex's syncer generalised by provider key: i18n scope <key>_items.syncer, import via import_latest_<key>_data, Sentry tag <key>_item_id, stats source <key>. A subclass names its credential errors in #credentials_error?.
  • A failed sync also writes one DebugLogEntry row (2066140ef): category provider_sync_error, the provider key, the family, and <key>_item_id, error class and message in its metadata. This answers the Gatekeeper's debug-log gap on jaysbeekay/sure#726.
  • BrexItem::Syncer stays as a thin subclass, so the constant and BrexItem::Syncer::SafeSyncError still resolve and Brex's existing tests pass unedited.
  • ProviderItem::Unlinking is Brex's unlinking concern, keyed by the provider account's class. brex_item/unlinking.rb is removed.
  • test/support/provider_item_lifecycle_tests.rb: a shared contract (sync, unlink incl. another provider's link surviving, dry-run unlink, destroy) that each provider on the kit includes; Brex is the first.

Nothing user-visible changes for a family. A super admin now sees a failed sync in Settings > Debug (Settings::DebugsController is admin-only). One log line reads "sync failed for BrexItem 1" where it read "sync failed for Brex item 1".

Not changed on purpose: the other 24 <provider>_item/unlinking.rb copies and their syncers move one provider per later PR. The holding update_all in unlinking is redundant with AccountProvider has_many :holdings, dependent: :nullify, but kept so this PR moves code without changing it.

Demo data

No demo data needed: no new records or UI.

Migration notes

No migration.

Verification

  • Existing tests: no file under test/ changed except the three new ones; all Brex model, syncer, account-flow, importer and controller tests pass.
  • Mutations, each caught: unlinking with the wrong provider type; unlinking every link; a dry run that removes; credential errors never matched; the wrong i18n scope; ProviderAccount without dependent: :destroy; the base not abstract. One survives by design: skipping the holding update_all (see above).
  • bin/rails test (full): 15,866 runs, 0 failures; 8 errors in ReleaseNotesWorkflowTest from node missing 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): non-test +92 / -191.

Rebased onto A9 (2026-11-15) and one commit added, 7514143f2: the provider key and accounts association are now read from the adapter's declares_connection (Track A's A1) instead of derived from the model name, so a connection with no declaration raises NotImplementedError rather than guessing. This also answers the light review's "provider_key is name-fragile" nit. Brex's declared brex / :brex_accounts equal the derived values, so nothing changes for Brex.

  • Tests (fail-first): "a provider keys its connection by its adapter's declaration" (a stubbed declaration with another key and association; failed before with "brex"), and "a connection without a declaration has no provider key" (failed before: nothing raised).
  • Mutations, each caught: deriving the key from the model name again; deriving the association from the key again; returning nil instead of raising.
  • Existing tests: test/ diff against A9 is still only the three new files.
  • bin/rails test (full, at 7514143f2): 16,260 runs, 0 failures; the same 8 ReleaseNotesWorkflowTest errors (no node). Rubocop clean.

Full review fixes (2026-11-17), b17701f8f: two tests pin what the syncer passes by provider key, which no test checked before.

  • "counts only the linked account's Brex-sourced transactions": a linked account with one Brex entry and one manual entry; tx_imported and tx_seen are 1. With the source mutated to "BREX": Expected: 1, Actual: 0.
  • "tags the Sentry report with the Brex item id": expects set_tags(brex_item_id:). With the tag key mutated to brex_id: unexpected invocation: #<Mock:scope>.set_tags(brex_id: ...).
  • Existing tests: brex_item/syncer_test.rb gains these two; no existing test changed.
  • Follow-up for the review's residual risk: jaysbeekay/sure#834 (cache the connection_declaration lookup).

Blast radius

  • BrexItem::Syncer is built by BrexItem::Provided#syncer (brex_item/provided.rb:13), which is public and overrides Syncable's private #syncer. Syncable#perform_sync and #perform_post_sync call it from Sync#perform; BrexItemTest "syncer returns BrexItem::Syncer instance" calls it directly. Both paths build the same subclass.
  • BrexItem#unlink_all! has one caller, BrexItemsController#destroy.
  • Family::Syncer.syncable_item_associations checks included_modules.include?(Syncable); an inherited Syncable still counts, and the contract asserts it.
  • Polymorphic types (syncable_type, provider_type) are unchanged because the parents are abstract; the contract asserts the link's type.