import { db } from "@/lib/db"; export type SyncTrigger = "manual" | "webhook" | "scheduled" | "source-created"; /** Queue one pull per source at a time. Returns true only when a new job was created. */ export function queuePull(sourceId: number, trigger: SyncTrigger, payload: unknown = {}) { const result = db.prepare(` INSERT INTO sync_jobs(source_id,kind,payload_json,trigger) SELECT ?, 'pull', ?, ? WHERE NOT EXISTS ( SELECT 1 FROM sync_jobs WHERE source_id=? AND kind='pull' AND status IN ('queued','running') ) `).run(sourceId, JSON.stringify(payload), trigger, sourceId); return result.changes === 1; }