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>
68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { getDb } from "../db.ts";
|
|
import { syncConnections } from "@trails-cool/db/schema/journal";
|
|
import type { TokenSet } from "./types.ts";
|
|
|
|
export async function saveConnection(
|
|
userId: string,
|
|
provider: string,
|
|
tokens: TokenSet,
|
|
) {
|
|
const db = getDb();
|
|
// Upsert: delete existing connection for this user+provider, then insert
|
|
await db
|
|
.delete(syncConnections)
|
|
.where(and(eq(syncConnections.userId, userId), eq(syncConnections.provider, provider)));
|
|
await db.insert(syncConnections).values({
|
|
id: randomUUID(),
|
|
userId,
|
|
provider,
|
|
accessToken: tokens.accessToken,
|
|
refreshToken: tokens.refreshToken,
|
|
expiresAt: tokens.expiresAt,
|
|
providerUserId: tokens.providerUserId ?? null,
|
|
});
|
|
}
|
|
|
|
export async function getConnection(userId: string, provider: string) {
|
|
const db = getDb();
|
|
const [conn] = await db
|
|
.select()
|
|
.from(syncConnections)
|
|
.where(and(eq(syncConnections.userId, userId), eq(syncConnections.provider, provider)));
|
|
return conn ?? null;
|
|
}
|
|
|
|
export async function getConnectionByProviderUser(provider: string, providerUserId: string) {
|
|
const db = getDb();
|
|
const [conn] = await db
|
|
.select()
|
|
.from(syncConnections)
|
|
.where(
|
|
and(eq(syncConnections.provider, provider), eq(syncConnections.providerUserId, providerUserId)),
|
|
);
|
|
return conn ?? null;
|
|
}
|
|
|
|
export async function updateTokens(
|
|
connectionId: string,
|
|
tokens: TokenSet,
|
|
) {
|
|
const db = getDb();
|
|
await db
|
|
.update(syncConnections)
|
|
.set({
|
|
accessToken: tokens.accessToken,
|
|
refreshToken: tokens.refreshToken,
|
|
expiresAt: tokens.expiresAt,
|
|
})
|
|
.where(eq(syncConnections.id, connectionId));
|
|
}
|
|
|
|
export async function deleteConnection(userId: string, provider: string) {
|
|
const db = getDb();
|
|
await db
|
|
.delete(syncConnections)
|
|
.where(and(eq(syncConnections.userId, userId), eq(syncConnections.provider, provider)));
|
|
}
|