← back to Sanderson Onboard

scripts/assemble_queue.mjs

26 lines

// assemble_queue.mjs — merge the 4 settlement-clean manifests into a round-robin, most-ready-first
// queue capped at --daily. Skips dw_skus already CREATED (create-audit.jsonl). Writes pilot/queue-today.json.
import fs from 'node:fs';
const DAILY = parseInt((process.argv.find(a => a.startsWith('--daily=')) || '').split('=')[1] || '200', 10);
const CAP = Math.min(DAILY, 500);
const AUDIT = new URL('../pilot/create-audit.jsonl', import.meta.url).pathname;
const done = new Set();
if (fs.existsSync(AUDIT)) for (const l of fs.readFileSync(AUDIT, 'utf8').trim().split('\n').filter(Boolean)) {
  try { const r = JSON.parse(l); if (r.action === 'CREATED') done.add(r.dw); } catch {}
}
const lists = {};
for (const b of ['sanderson', 'harlequin', 'zoffany', 'morris']) {
  const p = new URL(`../pilot/manifest-${b}.json`, import.meta.url).pathname;
  lists[b] = fs.existsSync(p) ? JSON.parse(fs.readFileSync(p, 'utf8')).filter(it => !done.has(it.dw_sku)) : [];
}
const out = [];
const order = ['sanderson', 'harlequin', 'zoffany', 'morris'];
let i = 0;
while (out.length < CAP && order.some(b => lists[b].length)) {
  const b = order[i % order.length]; i++;
  if (lists[b].length) out.push(lists[b].shift());
}
fs.writeFileSync(new URL('../pilot/queue-today.json', import.meta.url).pathname, JSON.stringify(out, null, 2));
const byBrand = out.reduce((a, x) => { a[x.vendor] = (a[x.vendor] || 0) + 1; return a; }, {});
console.log(`queue-today: ${out.length} items ${JSON.stringify(byBrand)}`);