import { claimRun, getSetting } from '@/lib/db'; import { findAutomation, resolveSteps, resolveAuthSteps } from '@/lib/automations'; import { corsJson, corsPreflight } from '../../cors'; /** The Python runner polls this. Returns the run plus the steps to execute, so * the runner never needs its own copy of the automation definitions. */ export async function POST() { if (getSetting('autobuyer_enabled') !== '1') { return corsJson({ run: null, reason: 'AutoBuyer is switched off' }); } const row = claimRun(); if (!row) return corsJson({ run: null }); const found = findAutomation(row.automation_id); if (!found) { return corsJson({ run: null, reason: `Unknown automation ${row.automation_id}` }); } return corsJson({ run: { id: row.id, automationId: row.automation_id, firm: found.firm.label, label: found.automation.label, steps: resolveSteps(found.firm, found.automation, safeInputs(row.inputs)), authSteps: resolveAuthSteps(found.firm), }, }); } /** Stored inputs are already clamped; this only guards against a malformed row. */ function safeInputs(json: string): Record { try { const parsed = JSON.parse(json); return parsed && typeof parsed === 'object' ? parsed : {}; } catch { return {}; } } export async function OPTIONS() { return corsPreflight(); }