diff --git a/apps/journal/app/routes/sync.import.komoot.tsx b/apps/journal/app/routes/sync.import.komoot.tsx index b8b51f7..2512942 100644 --- a/apps/journal/app/routes/sync.import.komoot.tsx +++ b/apps/journal/app/routes/sync.import.komoot.tsx @@ -50,14 +50,41 @@ export async function action({ request }: Route.ActionArgs) { const user = await getSessionUser(request); if (!user) return redirect("/auth/login"); - const resp = await fetch( - new URL("/api/sync/komoot/import", new URL(request.url).origin), - { method: "POST", headers: { cookie: request.headers.get("cookie") ?? "" } }, - ); - if (!resp.ok) { - const body = (await resp.json()) as { error?: string }; - return data({ error: body.error ?? "failed" }, { status: resp.status }); + const { randomUUID } = await import("node:crypto"); + const { getDb } = await import("~/lib/db"); + const { importBatches } = await import("@trails-cool/db/schema/journal"); + + const service = await getService(user.id, "komoot"); + if (!service) return redirect("/settings/connections/komoot"); + + const db = getDb(); + const batchId = randomUUID(); + await db.insert(importBatches).values({ + id: batchId, + userId: user.id, + connectionId: service.id, + provider: "komoot", + status: "pending", + }); + + // Try to enqueue via pg-boss (available in production / custom server). + // In dev (Vite), boss is never initialized — fall back to running inline + // in a detached promise so the response is still immediate. + try { + const { getBoss } = await import("~/lib/boss.server"); + const boss = getBoss(); + await boss.send("komoot-bulk-import", { + batchId, + userId: user.id, + creds: service.credentials, + }); + } catch { + const { runKomootBulkImport } = await import("~/lib/komoot-bulk-import.server"); + const creds = service.credentials as Parameters[2]; + // Fire-and-forget — don't await so the redirect happens immediately + void runKomootBulkImport(batchId, user.id, creds).catch(() => {}); } + return redirect("/sync/import/komoot"); }