Skip to content
// Approve (optionally with edits) or reject one draft. Approved drafts go on to Shopify.
const b = $('Decide API').first().json.body ?? {};
const db = $getWorkflowStaticData('global');
const d = (db.drafts ?? {})[String(b.product_id ?? '')];
if (!d) return [{ json: { found: false, product_id: b.product_id ?? null, publish: false } }];
if (!['approve', 'reject'].includes(b.decision)) return [{ json: { found: true, error: 'decision must be approve or reject', publish: false } }];
if (d.state === 'published' && b.decision === 'approve') return [{ json: { found: true, product_id: d.product_id, state: 'published', note: 'already published', publish: false } }];

if (b.decision === 'reject') {
  d.state = 'rejected'; d.note = String(b.note ?? ''); d.decided_at = new Date().toISOString();
  return [{ json: { found: true, product_id: d.product_id, state: 'rejected', note: d.note, publish: false } }];
}
const esc = (t) => String(t ?? '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
const e = b.edits ?? {};
const description = e.description ?? d.copy.description;
const bullets = e.bullets ?? d.copy.bullets;
const body_html = `<p>${esc(description)}</p>` + (bullets.length ? `\n<ul>\n${bullets.map((x) => `  <li>${esc(x)}</li>`).join('\n')}\n</ul>` : '');
const final = { ...d.copy, description, bullets, body_html, tags: e.tags ?? d.copy.tags };
d.approved_copy = final; d.edited = Object.keys(e).length > 0;
return [{ json: { found: true, product_id: d.product_id, publish: true, body_html, tags: final.tags.join(', '), edited: d.edited, dry_run: b.dry_run === true } }];