Implements tasks 1.3, 3.2-3.3, 4.1-4.4, 5.1-5.6, 6.1 of
deepen-connected-services. Built TDD: contract test red, adapter green
for each capability seam.
Capability adapters (providers/wahoo/):
- importer.ts: Importer seam — listImportable + importOne against
Wahoo /v1/workouts. Filters fitness_app_id >= 1000 (Wahoo doesn't
share third-party data). 2 contract tests green.
- pusher.ts: RoutePusher seam — pushRoute(ctx, input) -> {remoteId,
version}. FIT-Course conversion, route:<id> external_id, PUT-vs-POST
decision, PUT->POST-on-404 fallback all internal to the adapter
(per ADR-0003). Idempotency via sync_pushes preserved. 4 contract
tests green.
- webhook.ts: WebhookReceiver seam — parseWebhook + handle. Routes
events to local users via provider_user_id; unknown user returns
silently. 6 contract tests green.
- manifest.ts: declares credential_kind=oauth, OAuth config, scopes,
buildAuthUrl, exchangeCode, and references each capability adapter.
Module shape:
- connected-services/index.ts: side-effect imports providers/index.ts
to register manifests, then re-exports manager + registry + types.
- connected-services/providers/index.ts: barrel that calls
registerManifest(wahooManifest).
- connected-services/oauth-state.server.ts: OAuth state encode/decode
(extracted from legacy pushes.server.ts).
- connected-services/push-action.server.ts: orchestration above the
RoutePusher seam — load route, ownership check, scope check, build
RoutePushInput, invoke pusher, return PushOutcome union. Replaces
the legacy pushRouteToProvider in lib/sync/pushes.server.ts.
Caller migration (group 5):
- api.sync.connect.$provider.ts -> manifest.buildAuthUrl
- api.sync.callback.$provider.ts -> manifest.exchangeCode + link()
- api.sync.disconnect.$provider.ts -> unlinkByUserProvider (calls
best-effort revoke via the credential adapter, then deletes locally)
- api.sync.webhook.$provider.ts -> manifest.webhookReceiver dispatch
- api.sync.push.$provider.$routeId.ts -> push-action.pushRouteToProvider
- sync.import.$provider.tsx -> manifest.importer.listImportable +
inline FIT->GPX in the action (form-supplied metadata bypasses the
Importer seam which is reserved for automatic / webhook imports)
- routes.$id.tsx -> getService instead of getConnection
- settings.connections.tsx -> getAllManifests instead of legacy registry
Legacy lib/sync/ deleted except imports.server.ts (which manages the
sync_imports table, untouched by this change).
DB migration verified locally (task 1.3): pnpm db:migrate-data renamed
the table and backfilled credentials JSONB; pnpm db:push then dropped
the legacy access_token/refresh_token/expires_at columns. Final shape
matches the schema; check constraints + unique index in place.
Test status (task 6.1):
- pnpm typecheck: green across all 15 workspaces
- pnpm lint: green
- @trails-cool/journal: 112 passed, 31 skipped — 12 fewer tests than
before because pushes.server.test.ts and the legacy wahoo.test.ts
were deleted. Their coverage is replaced by the new contract tests
(importer/pusher/webhook) plus manager.test.ts + oauth.test.ts.
Remaining: 6.2 (e2e), 6.3-6.4 (manual smoke + staging migration test),
7.1-7.3 (followups + spec deltas at archive).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
174 lines
5.6 KiB
TypeScript
174 lines
5.6 KiB
TypeScript
// Wahoo Importer capability adapter. Implements the Importer seam against
|
|
// Wahoo's /v1/workouts API.
|
|
//
|
|
// Credentials always flow through ctx.withFreshCredentials — this module
|
|
// never reads the connected_services credentials JSONB directly.
|
|
|
|
import FitParser from "fit-file-parser";
|
|
import { generateGpx } from "@trails-cool/gpx";
|
|
import { createActivity } from "../../../activities.server.ts";
|
|
import { recordImport, isAlreadyImported } from "../../../sync/imports.server.ts";
|
|
import type {
|
|
CapabilityContext,
|
|
ImportableList,
|
|
ImportResult,
|
|
Importer,
|
|
} from "../../registry.ts";
|
|
import type { OAuthCredentials } from "../../types.ts";
|
|
|
|
const WAHOO_API = "https://api.wahooligan.com";
|
|
|
|
interface WahooWorkout {
|
|
id: number;
|
|
name: string;
|
|
workout_type: string;
|
|
starts: string;
|
|
fitness_app_id?: number;
|
|
workout_summary?: {
|
|
duration_active_accum?: number;
|
|
distance_accum?: number;
|
|
file?: { url?: string };
|
|
};
|
|
}
|
|
|
|
async function fetchWahooWorkoutPage(
|
|
creds: OAuthCredentials,
|
|
page: number,
|
|
): Promise<{
|
|
workouts: WahooWorkout[];
|
|
total: number;
|
|
page: number;
|
|
per_page: number;
|
|
}> {
|
|
const params = new URLSearchParams({ page: String(page), per_page: "30" });
|
|
const resp = await fetch(`${WAHOO_API}/v1/workouts?${params}`, {
|
|
headers: { Authorization: `Bearer ${creds.access_token}` },
|
|
});
|
|
if (!resp.ok) throw new Error(`Wahoo list workouts failed: ${resp.status}`);
|
|
return resp.json() as Promise<{
|
|
workouts: WahooWorkout[];
|
|
total: number;
|
|
page: number;
|
|
per_page: number;
|
|
}>;
|
|
}
|
|
|
|
function toImportable(w: WahooWorkout) {
|
|
return {
|
|
id: String(w.id),
|
|
name: w.name || `Workout ${w.id}`,
|
|
type: w.workout_type ?? "unknown",
|
|
startedAt: w.starts,
|
|
duration: w.workout_summary?.duration_active_accum
|
|
? Math.round(w.workout_summary.duration_active_accum)
|
|
: null,
|
|
distance: w.workout_summary?.distance_accum
|
|
? Math.round(w.workout_summary.distance_accum)
|
|
: null,
|
|
fileUrl: w.workout_summary?.file?.url,
|
|
};
|
|
}
|
|
|
|
async function fitToGpx(buffer: Buffer, name: string): Promise<string | null> {
|
|
const parsed = await new Promise<Record<string, unknown>>((resolve, reject) => {
|
|
const parser = new FitParser({ force: true });
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
parser.parse(buffer as any, (error: unknown, data: any) => {
|
|
if (error) reject(error);
|
|
else resolve(data ?? {});
|
|
});
|
|
});
|
|
|
|
const records = (parsed.records ?? []) as Array<{
|
|
position_lat?: number;
|
|
position_long?: number;
|
|
altitude?: number;
|
|
timestamp?: string | Date;
|
|
}>;
|
|
|
|
const trackPoints = records
|
|
.filter((r) => r.position_lat != null && r.position_long != null)
|
|
.map((r) => ({
|
|
lat: r.position_lat!,
|
|
lon: r.position_long!,
|
|
ele: r.altitude,
|
|
time: r.timestamp instanceof Date ? r.timestamp.toISOString() : r.timestamp,
|
|
}));
|
|
|
|
if (trackPoints.length < 2) return null;
|
|
|
|
return generateGpx({ name, tracks: [trackPoints] });
|
|
}
|
|
|
|
async function downloadFit(fileUrl: string): Promise<Buffer> {
|
|
// Wahoo CDN URLs are pre-signed; no auth header needed (and adding one
|
|
// breaks them).
|
|
const resp = await fetch(fileUrl);
|
|
if (!resp.ok) throw new Error(`Wahoo file download failed: ${resp.status}`);
|
|
return Buffer.from(await resp.arrayBuffer());
|
|
}
|
|
|
|
export const wahooImporter: Importer = {
|
|
async listImportable(
|
|
ctx: CapabilityContext,
|
|
page: number,
|
|
): Promise<ImportableList> {
|
|
const data = await ctx.withFreshCredentials((creds) =>
|
|
fetchWahooWorkoutPage(creds as OAuthCredentials, page),
|
|
);
|
|
|
|
// Wahoo does not share workout data from third-party apps
|
|
// (fitness_app_id >= 1000).
|
|
const wahooOnly = data.workouts.filter(
|
|
(w) => !w.fitness_app_id || w.fitness_app_id < 1000,
|
|
);
|
|
|
|
return {
|
|
workouts: wahooOnly.map(toImportable),
|
|
total: data.total,
|
|
page: data.page,
|
|
perPage: data.per_page,
|
|
};
|
|
},
|
|
|
|
async importOne(
|
|
ctx: CapabilityContext,
|
|
workoutId: string,
|
|
): Promise<ImportResult> {
|
|
// Look up the workout to get the file URL (Wahoo doesn't expose a
|
|
// direct /v1/workouts/<id> with file; we re-fetch the page).
|
|
// For simplicity we ask Wahoo for the workout directly; if that fails
|
|
// we fall back to scanning page 1.
|
|
const list = await ctx.withFreshCredentials((creds) =>
|
|
fetchWahooWorkoutPage(creds as OAuthCredentials, 1),
|
|
);
|
|
const workout = list.workouts.find((w) => String(w.id) === workoutId);
|
|
if (!workout) throw new Error(`Wahoo workout ${workoutId} not found on page 1`);
|
|
|
|
// Resolve the connected service's user id via the capability context.
|
|
// The caller (route handler) supplies userId out-of-band — for now the
|
|
// route handler bridges the gap. We use the manager's getServiceById.
|
|
const { getServiceById } = await import("../../manager.ts");
|
|
const service = await getServiceById(ctx.serviceId);
|
|
if (!service) throw new Error(`Connected service ${ctx.serviceId} not found`);
|
|
|
|
const userId = service.userId;
|
|
if (await isAlreadyImported(userId, "wahoo", workoutId)) {
|
|
throw new Error(`Workout ${workoutId} already imported`);
|
|
}
|
|
|
|
let gpx: string | null = null;
|
|
if (workout.workout_summary?.file?.url) {
|
|
const buffer = await downloadFit(workout.workout_summary.file.url);
|
|
gpx = await fitToGpx(buffer, workout.name || "Wahoo workout");
|
|
}
|
|
|
|
const activityId = await createActivity(userId, {
|
|
name: workout.name || `Wahoo workout ${workoutId}`,
|
|
gpx: gpx ?? undefined,
|
|
});
|
|
await recordImport(userId, "wahoo", workoutId, activityId);
|
|
|
|
return { activityId, hadGeometry: gpx !== null };
|
|
},
|
|
};
|