import { getSharedStore } from "./shared-store.js"; export interface ExportProgressSnapshot { processedRows: number; totalEstimate: number | null; updatedAt: string | null; } /** * Reads the latest export progress published by the worker job into the * shared store. The worker writes `export:progress:` every N rows * (default 5_000) with a 1h TTL; the API surfaces that snapshot to the * front-end so users can poll for a row-count counter without going to * Postgres. * * Returns `null` when no progress entry exists (job not started, expired, * or never wrote any rows). Returns malformed payloads as `null` to keep * polling resilient against future schema changes. */ export async function readExportProgressFromSharedStore(args: { exportJobId: string; tenantId: string }): Promise { void args.tenantId; // tenant scoping is enforced by the caller; key is global per jobId. const raw = await getSharedStore().get(`export:progress:${args.exportJobId}`); if (!raw) return null; try { const parsed = JSON.parse(raw) as Partial; if (typeof parsed.processedRows !== "number") return null; return { processedRows: parsed.processedRows, totalEstimate: typeof parsed.totalEstimate === "number" ? parsed.totalEstimate : null, updatedAt: typeof parsed.updatedAt === "string" ? parsed.updatedAt : null, }; } catch { return null; } }