← back to Dw Staged Active Viewer
scripts/build-snapshot.js
67 lines
#!/usr/bin/env node
/**
* build-snapshot.js (Mac2-only, READ-ONLY)
* -----------------------------------------
* Dumps the FULL staged-for-active payload from the MAC2-LOCAL dw_unified mirror
* (the canonical source for dedup_skip + cadence gate) into data/staged-snapshot.json.
*
* The snapshot is what the Kamatera viewer serves (SNAPSHOT_ONLY=1), so the live
* site never queries Kamatera-canonical dw_unified (which LACKS the dedup_skip flags
* → would show ~1,500 phantom Tres Tintas). We dump:
* - summary → /api/summary payload
* - calendar → /api/calendar payload (projection computed at build time)
* - rows → ALL staged SKU rows (every vendor, full backlog; client pages over them)
*
* HARD read-only: only the shared server.js SELECT helpers run here. No writes anywhere.
*/
const fs = require('fs');
const path = require('path');
const lib = require('../server.js'); // exports the read-only builders when required as a module
const OUT_DIR = path.join(__dirname, '..', 'data');
const OUT = path.join(OUT_DIR, 'staged-snapshot.json');
function main() {
const t0 = Date.now();
fs.mkdirSync(OUT_DIR, { recursive: true });
console.error('[snapshot] building summary…');
const summary = lib.buildSummary();
console.error('[snapshot] building calendar projection…');
const calendar = lib.buildCalendarPayload();
console.error('[snapshot] dumping ALL staged SKU rows per vendor…');
const ready = lib.readyVendors();
const rows = [];
for (const { vendor, cfg } of ready) {
let offset = 0;
const CHUNK = 2000;
for (;;) {
const batch = lib.vendorRows(vendor, cfg, CHUNK, offset);
rows.push(...batch);
if (batch.length < CHUNK) break;
offset += batch.length;
}
console.error(`[snapshot] ${vendor}: ${rows.filter(r => r.vendor === vendor).length} rows`);
}
// Global newest-first order so the blended ("All vendors") client paging is stable.
rows.sort((a, b) => String(b.staged_at || '').localeCompare(String(a.staged_at || '')));
const snapshot = {
schema: 1,
generatedAt: new Date().toISOString(),
source: 'mac2-local dw_unified (canonical dedup_skip)',
summary,
calendar,
rowCount: rows.length,
rows,
};
fs.writeFileSync(OUT, JSON.stringify(snapshot));
const kb = Math.round(fs.statSync(OUT).size / 1024);
console.error(`[snapshot] wrote ${OUT} (${rows.length} rows, ${kb} KB) in ${((Date.now() - t0) / 1000).toFixed(1)}s`);
}
main();