Garmin Connect as the third connected-services provider (spec: garmin-import). The interesting parts: - Push-first ingestion: Garmin has no list endpoint. The webhook normalizes ping (callbackURL) and push (inline) notification batches into events; the slow work (authorized FIT download, FIT→GPX via the shared converter, activity creation) runs in a garmin-import-activity pg-boss job so the webhook answers fast. Callback URLs are validated against Garmin's API host before any fetch (SSRF guard). - History via backfill requests: /sync/import/garmin is a date-range requester with honest async progress (no pick list — the concept doesn't exist in a push model). Ranges chunk to Garmin's 90-day cap; overlaps are free via sync_imports dedupe. Requests persist in import_batches via two new nullable columns (range_start/range_end). - OAuth2 + PKCE on the existing oauth credential kind. Design correction from apply: the verifier rides a short-lived httpOnly cookie scoped to the callback path — the state param is visible in redirect URLs and must never carry it. Manifests opt in via pkce:true. - Deregistration notifications flip the connection to 'revoked' (row kept for audit, imports retained, re-connect prompt shown). - Framework evolutions, all additive: parseWebhook returns WebhookEvent[] (Garmin batches; Wahoo adapted), manifest gains configured()/importUrl/pkce, importActivity accepts summary stats for FIT-less imports, manager gains markRevoked. - Env-gated: no GARMIN_CLIENT_ID → provider hidden on /settings/connections. Privacy manifest entry (DE+EN). i18n en+de. Rollout (§6) stays gated on the Garmin Developer Program application (submitted 2026-06-07). Fixtures are doc-shaped; the staging soak swaps in recorded payloads if shapes differ. Gate: typecheck ✓ lint ✓ unit+integration ✓ e2e 70/72 + both known flakes green isolated ✓ openspec validate ✓ Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
93 lines
2.6 KiB
TypeScript
93 lines
2.6 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";
|
|
import { createActivity } from "../activities.server.ts";
|
|
|
|
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 deleteImportByActivity(activityId: string) {
|
|
const db = getDb();
|
|
await db.delete(syncImports).where(eq(syncImports.activityId, activityId));
|
|
}
|
|
|
|
// Single callsite for "create an activity from an imported workout and record
|
|
// the dedup entry". Providers call this instead of calling createActivity +
|
|
// recordImport separately, so the pair stays atomic from the provider's view.
|
|
export async function importActivity(
|
|
userId: string,
|
|
provider: string,
|
|
externalWorkoutId: string,
|
|
input: {
|
|
name: string;
|
|
gpx?: string;
|
|
// Stats-only imports (no GPS file — e.g. Garmin notifications for
|
|
// FIT-less activities) carry whatever the provider's summary had.
|
|
distance?: number | null;
|
|
duration?: number | null;
|
|
startedAt?: Date | null;
|
|
},
|
|
): Promise<{ activityId: string }> {
|
|
const activityId = await createActivity(userId, {
|
|
name: input.name,
|
|
gpx: input.gpx,
|
|
distance: input.distance,
|
|
duration: input.duration,
|
|
startedAt: input.startedAt,
|
|
});
|
|
await recordImport(userId, provider, externalWorkoutId, activityId);
|
|
return { activityId };
|
|
}
|
|
|
|
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));
|
|
}
|