Skip to content
// Put the drafts in the review queue (nothing is published until someone approves). Dry runs save nothing.
const db = $getWorkflowStaticData('global');
db.drafts ??= {};
const items = $input.all().map((i) => i.json);
const run = $('Pick Products').first().json._run;
const now = new Date().toISOString();
const drafts = items.filter((d) => d.product_id);
if (!run.dry_run) for (const d of drafts) {
  const prev = db.drafts[d.product_id];
  db.drafts[d.product_id] = { ...d, _run: undefined, state: 'pending', created_at: now, version: (prev?.version ?? 0) + 1 };
}
return [{
  json: {
    ...run,
    drafts: drafts.map(({ _run, ...d }) => d),
    ready: drafts.filter((d) => d.status === 'ready').length,
    needs_review: drafts.filter((d) => d.status === 'needs_review').length,
    review_url: '/webhook/pd/review',
  },
}];