import { NextRequest } from 'next/server'; import { getSetting, setSetting, countPendingLocate } from '@/lib/db'; import { corsJson, corsPreflight } from '../cors'; /** Polled by the Chromium extension every few seconds, and by the AutoBuyer page. * `pendingLocate` rides along so the extension learns about queued clicks without * a second request on every tick. */ export async function GET() { return corsJson({ enabled: getSetting('autobuyer_enabled') === '1', pendingLocate: countPendingLocate(), }); } /** Flipped by the ON/OFF button on the AutoBuyer page. */ export async function PATCH(req: NextRequest) { try { const body = await req.json() as { enabled?: unknown }; if (typeof body.enabled !== 'boolean') { return corsJson({ error: '`enabled` must be a boolean' }, { status: 400 }); } setSetting('autobuyer_enabled', body.enabled ? '1' : '0'); return corsJson({ enabled: body.enabled }); } catch (err: any) { return corsJson({ error: err?.message ?? 'Failed to set status' }, { status: 500 }); } } export async function OPTIONS() { return corsPreflight(); }