Fix import action crashing in dev when pg-boss is not initialized
The action was doing an internal self-fetch to /api/sync/komoot/import, which returned plain text on error (boss not initialized) — the JSON.parse then threw "Unexpected token U". Fix: inline the action logic directly in the route and fall back to a fire-and-forget runKomootBulkImport call when pg-boss is unavailable (i.e. dev mode with Vite, not the custom server). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
600ecc6685
commit
6a441675b9
1 changed files with 34 additions and 7 deletions
|
|
@ -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<typeof runKomootBulkImport>[2];
|
||||
// Fire-and-forget — don't await so the redirect happens immediately
|
||||
void runKomootBulkImport(batchId, user.id, creds).catch(() => {});
|
||||
}
|
||||
|
||||
return redirect("/sync/import/komoot");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue