Implements tasks 1.1-1.4 and 2.1-2.5 of deepen-connected-services:
DB:
- Rename journal.sync_connections -> journal.connected_services.
- Add credential_kind discriminator (oauth | web-login | device) and
credentials JSONB (shape per kind), status column, and a unique index
on (user_id, provider) lifting the previously app-only invariant
into the DB.
- Idempotent backfill in 0002_connected_services.sql moves existing
Wahoo rows' tokens into the JSONB blob with credential_kind='oauth'.
Code:
- New apps/journal/app/lib/connected-services/ module:
- types.ts: ConnectedService, CredentialKind, OAuthCredentials,
NeedsRelinkError, CredentialAdapter, ProviderOAuthConfig, etc.
- credential-adapters/oauth.ts: standard OAuth2 refresh_token flow,
revoke endpoint, 4xx -> NeedsRelinkError / 5xx -> transient.
- manager.ts: ConnectedServiceManager (link, unlink, withFreshCredentials,
markNeedsRelink). Centralizes credential lifecycle in one chokepoint.
- registry.ts: ProviderManifest type + capability seam interfaces
(Importer, RoutePusher, WebhookReceiver). Manifests register
themselves at import time.
Tests:
- manager.test.ts (8 tests): refresh-on-expired, refresh-fail->needs_relink,
ConnectionNotActiveError, link/unlink, revoke is best-effort.
- credential-adapters/oauth.test.ts (10 tests): refresh contract,
refresh_token retention, 4xx vs 5xx behaviour, revoke.
- All 18 new tests pass.
Compatibility:
- apps/journal/app/lib/sync/connections.server.ts is now a thin shim
translating the legacy TokenSet API onto the JSONB-shaped table so
existing callers (routes, pushes.server.ts) keep working until tasks
5.x migrate them to the manager. To be deleted in task 5.6.
Pre-existing journal test failures (12) are unrelated to this change:
they pre-date this PR and stem from a workspace resolution issue with
@trails-cool/fit (verified by running tests against main).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
5.6 KiB
5.6 KiB
1. Database migration
- 1.1 Update Drizzle schema in
packages/db/src/schema/journal.ts: renamesyncConnectionstable toconnectedServices, addcredentialKind(text, NOT NULL) andcredentials(jsonb, NOT NULL) andstatus(text, NOT NULL DEFAULT'active'with CHECK constraint foractive | needs_relink | revoked); dropaccessToken,refreshToken,expiresAt. - 1.2 Generate migration via
pnpm db:generate; hand-edit if needed so backfill runs in the same transaction as the column adds (UPDATE connected_services SET credential_kind = 'oauth', credentials = jsonb_build_object('access_token', access_token, 'refresh_token', refresh_token, 'expires_at', expires_at)), then drop the old columns. - 1.3 Verify the migration on a local database with seeded Wahoo connection rows; verify the inverse rollback restores the columns from JSONB.
- 1.4 Update FK references in
sync_importsandsync_pushesif any reference the table name (likely none — both referenceusers/routes, not connections).
2. ConnectedServiceManager + OAuth CredentialAdapter
- 2.1 Create
apps/journal/app/lib/connected-services/types.tswithConnectedServicerecord type,CredentialKind,Credentials(discriminated union per kind),NeedsRelinkerror class,Statusenum. - 2.2 Create
apps/journal/app/lib/connected-services/credential-adapters/oauth.tsimplementingCredentialAdapterwithrefresh(creds, providerConfig) → Credentials | NeedsRelinkand optionalrevoke. - 2.3 Create
apps/journal/app/lib/connected-services/manager.tsexposinglink / unlink / withFreshCredentials / markNeedsRelink. Persistence via Drizzle on theconnectedServicestable. - 2.4 Write
manager.test.ts: in-memory tests with stubCredentialAdaptercovering refresh-on-expired, refresh-fails-→-needs_relink, link/unlink, markNeedsRelink, status short-circuit. - 2.5 Write
credential-adapters/oauth.test.ts: contract tests against a mocked token endpoint (refresh success, refresh 4xx → NeedsRelink).
3. Provider manifest + registry
- 3.1 Create
apps/journal/app/lib/connected-services/registry.tsandtypes.tscapability interfaces (Importer,RoutePusher,WebhookReceiver,ProviderManifest). - 3.2 Create
apps/journal/app/lib/connected-services/providers/wahoo/manifest.tsdeclaringcredentialKind: 'oauth', OAuth config (auth URL, token URL, scopes), and references to capability adapters. - 3.3 Wire
providers/registry.tsto import the Wahoo manifest. ExposegetManifest(providerId)andgetAllManifests().
4. Wahoo capability adapters
- 4.1 Move and reshape Wahoo importer logic into
providers/wahoo/importer.tsimplementing theImporterseam (listImportable,importOne). Internally callswithFreshCredentialsfor any auth'd Wahoo HTTP. Keep FIT→GPX conversion isolated. - 4.2 Move and reshape Wahoo route push logic into
providers/wahoo/pusher.tsimplementingRoutePusher(pushRoute(service, route) → {remoteId, version}). Keep FIT-Course conversion,external_id = route:<id>, the PUT-vs-POST decision, and the PUT→POST-on-404 fallback inside the adapter. Read/writesync_pushesper the existing idempotency rules. - 4.3 Move and reshape Wahoo webhook handling into
providers/wahoo/webhook.tsimplementingWebhookReceiver(parseWebhook,handle). - 4.4 Reorganize tests: split existing
wahoo.test.ts(285 lines) andpushes.server.test.ts(309 lines) intoproviders/wahoo/{importer.test.ts, pusher.test.ts, webhook.test.ts}against a fakeConnectedServiceManagerthat yields stub credentials. Usegit mvfirst, then split, so blame survives.
5. Caller migration
- 5.1 Update OAuth connect routes (
/api/sync/connect/wahoo, callback) to callConnectedServiceManager.linkwithcredentialKind: 'oauth'and the JSONB credentials shape. Remove direct writes to the old token columns. - 5.2 Update disconnect route (
/api/sync/disconnect/wahoo) to callConnectedServiceManager.unlink. - 5.3 Update webhook route (
/api/sync/webhook/wahoo) to look up the manifest via the registry and dispatch to itsWebhookReceiver. Unknownprovider_user_idstill returns 200. - 5.4 Update the route push action and any background jobs (
apps/journal/app/jobs/) that previously called intoapps/journal/app/lib/sync/. Replace directconnections.server.tsreads withwithFreshCredentials. - 5.5 Update the connections settings page (
/settings/connections) and any UI surfaces readinggranted_scopesto read from theconnected_servicesrow directly. - 5.6 Delete the old
apps/journal/app/lib/sync/directory once all imports are gone. Delete the oldSyncProviderinterface fromtypes.ts.
6. Verification
- 6.1 Run
pnpm typecheck && pnpm lint && pnpm test— all green. - 6.2 Run
pnpm test:e2ewith the Wahoo flow specs — all green. - 6.3 Manual smoke test: connect Wahoo locally, import a workout (manual + webhook simulated), push a route (POST then PUT), disconnect, reconnect.
- 6.4 Check that
connected_servicesmigration applies cleanly to a copy of staging data (or local seed reflecting prod).
7. Documentation + follow-up
- 7.1 Update
CONTEXT.mdif any term was sharpened during implementation. - 7.2 File a follow-up issue/note to amend
openspec/changes/komoot-import/design.mdto useconnected_services+web-logincredential kind instead of the draftedjournal.integrationstable. - 7.3 At archive time, apply the spec deltas in
specs/connected-services/,specs/wahoo-import/,specs/wahoo-route-push/toopenspec/specs/.