Replaces the brittle agent-driven /api/bizportal/search queue with a
deterministic Python FastAPI service that drives BizPortal via a real
headless Chromium. The old flow is untouched.
What this adds:
- scripts/bizportal_service.py — FastAPI + Playwright service with
/search, /reserve, /health endpoints. Persistent login session
via storage_state.json. Locator-based selectors (vs eval JS).
- app/bizportal-2/ — new /bizportal-2 page, synchronous fetch
(no polling), shows round-trip duration, mirrors the existing
bulk-add + link UX.
- app/api/bizportal-2/{search,reserve,health}/route.ts — thin
proxies to the Python sidecar.
- scripts/README.md, .env.example, .gitignore, requirements.txt.
- .gitignore: ignore tsconfig.tsbuildinfo (build artefact).
Wiring:
- components/NavBar.tsx: 'BizPortal 2.0' nav entry with TEST badge.
- app/bizportal/page.tsx: 'Try BizPortal 2.0' action link in
header so the new option is discoverable from the existing page.
Run: cd scripts && source .venv/bin/activate && uvicorn
bizportal_service:app --port 7001
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
// app/api/bizportal-2/reserve/route.ts
|
|
// POST /api/bizportal-2/reserve
|
|
// Reserves a name on BizPortal via the Python sidecar. Replaces
|
|
// the agent-driven /api/register/[id]/apply-bizportal route for
|
|
// test purposes. The old route is untouched.
|
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
export const maxDuration = 180;
|
|
|
|
const SERVICE_URL = process.env.BIZPORTAL_SERVICE_URL ?? "http://127.0.0.1:7001";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const body = await req.json().catch(() => ({}));
|
|
const names: string[] = Array.isArray(body?.names) ? body.names : [];
|
|
const dryRun: boolean = !!body?.dry_run;
|
|
const balanceMin: number | undefined = typeof body?.balance_min === "number" ? body.balance_min : undefined;
|
|
|
|
if (names.length === 0) {
|
|
return NextResponse.json({ ok: false, error: "At least one name is required" }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`${SERVICE_URL}/reserve`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ names, dry_run: dryRun, balance_min: balanceMin }),
|
|
signal: req.signal,
|
|
});
|
|
|
|
const text = await res.text();
|
|
let data: any;
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
return NextResponse.json(
|
|
{ ok: false, error: `Python service returned non-JSON: ${text.slice(0, 200)}` },
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(data, { status: res.ok ? 200 : res.status });
|
|
} catch (err: any) {
|
|
if (err?.name === "AbortError") {
|
|
return NextResponse.json({ ok: false, error: "Reservation cancelled" }, { status: 499 });
|
|
}
|
|
return NextResponse.json(
|
|
{ ok: false, error: `Cannot reach Python service at ${SERVICE_URL}: ${err.message ?? err}` },
|
|
{ status: 503 },
|
|
);
|
|
}
|
|
}
|