trails/apps/journal/app/lib/sync/imports.server.ts
Ullrich Schäfer b6711d23d6
Add Wahoo activity sync with provider-agnostic framework
Provider-agnostic sync framework + Wahoo as first implementation:

Framework (apps/journal/app/lib/sync/):
- SyncProvider interface for OAuth2, webhooks, import, conversion
- Provider registry for settings UI iteration
- Generic sync_connections + sync_imports tables
- Token storage with auto-refresh

Wahoo provider:
- OAuth2 with workouts_read, user_read, offline_data scopes
- Webhook-based auto-import (workout_summary events)
- Manual import page with pagination
- FIT→GPX conversion via fit-file-parser
- Webhook token verification

Routes:
- /api/sync/connect/:provider — OAuth redirect
- /api/sync/callback/:provider — OAuth callback
- /api/sync/disconnect/:provider — remove connection
- /api/sync/webhook/:provider — webhook receiver
- /sync/import/:provider — manual import page

Settings: Connected Services section with per-provider connect/disconnect

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 11:22:02 +01:00

59 lines
1.5 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { eq, and, inArray } from "drizzle-orm";
import { getDb } from "../db.ts";
import { syncImports } from "@trails-cool/db/schema/journal";
export async function recordImport(
userId: string,
provider: string,
externalWorkoutId: string,
activityId: string,
) {
const db = getDb();
await db.insert(syncImports).values({
id: randomUUID(),
userId,
provider,
externalWorkoutId,
activityId,
});
}
export async function isAlreadyImported(
userId: string,
provider: string,
externalWorkoutId: string,
): Promise<boolean> {
const db = getDb();
const [row] = await db
.select({ id: syncImports.id })
.from(syncImports)
.where(
and(
eq(syncImports.userId, userId),
eq(syncImports.provider, provider),
eq(syncImports.externalWorkoutId, externalWorkoutId),
),
);
return !!row;
}
export async function getImportedIds(
userId: string,
provider: string,
externalWorkoutIds: string[],
): Promise<Set<string>> {
if (externalWorkoutIds.length === 0) return new Set();
const db = getDb();
const rows = await db
.select({ externalWorkoutId: syncImports.externalWorkoutId })
.from(syncImports)
.where(
and(
eq(syncImports.userId, userId),
eq(syncImports.provider, provider),
inArray(syncImports.externalWorkoutId, externalWorkoutIds),
),
);
return new Set(rows.map((r) => r.externalWorkoutId));
}