import { NextRequest } from 'next/server'; import { FIRMS, automationKey, describeStep } from '@/lib/automations'; import { queueRun, QueueRunError } from '@/lib/queue-run'; import { corsJson, corsPreflight } from '../cors'; /** The dashboard renders a tab per firm, and a button per automation inside it. */ export async function GET() { return corsJson({ firms: FIRMS.map((firm) => ({ id: firm.id, label: firm.label, urlPattern: firm.urlPattern, automations: firm.automations.map((a) => ({ key: automationKey(firm.id, a.id), id: a.id, label: a.label, description: a.description, confirm: a.confirm ?? null, inputs: a.inputs ?? [], steps: a.steps.map(describeStep), })), })), }); } /** Queue a run. The Python runner picks it up. */ export async function POST(req: NextRequest) { try { const body = await req.json() as { automationId?: unknown; inputs?: unknown }; if (typeof body.automationId !== 'string') { return corsJson({ error: '`automationId` is required' }, { status: 400 }); } return corsJson(queueRun(body.automationId, (body.inputs ?? {}) as Record)); } catch (err: any) { if (err instanceof QueueRunError) { return corsJson({ error: err.message }, { status: err.status }); } return corsJson({ error: err?.message ?? 'Failed to queue run' }, { status: 500 }); } } export async function OPTIONS() { return corsPreflight(); }