← back to Dw Add Sellable Variant Tk10902
tk10875-wolf-gordon/rollback-all.mjs
27 lines
#!/usr/bin/env node
// Reverse the TK-10875 Wolf Gordon variant-restructure: for each acted product, delete the added
// Sample variant and revert the roll variant back to its lone option1='Sample' @ old price state.
// Reads data/prestate.jsonl (written BEFORE each live write).
import { readFileSync } from 'node:fs';
import { execSync } from 'child_process';
const T = execSync(`grep -E '^SHOPIFY_ADMIN_TOKEN=' ${process.env.HOME}/Projects/secrets-manager/.env|cut -d= -f2-`).toString().trim();
const DOMAIN = 'designer-laboratory-sandbox.myshopify.com', API = '2024-10';
const hdr = { 'X-Shopify-Access-Token': T, 'Content-Type': 'application/json' };
const REST = `https://${DOMAIN}/admin/api/${API}`;
const lines = readFileSync(new URL('./data/prestate.jsonl', import.meta.url).pathname, 'utf8').trim().split('\n').map(JSON.parse);
const sleep = ms => new Promise(r => setTimeout(r, ms));
async function api(path, opts) { const r = await fetch(`${REST}${path}`, { headers: hdr, ...opts }); return { status: r.status, json: await r.json().catch(() => ({})) }; }
let n = 0;
for (const l of lines) {
const { json } = await api(`/products/${l.pid}.json`, {});
const vs = json.product?.variants || [];
// delete the added Sample variant (sku={DW}-Sample, price 4.25, different id from the lone/roll variant)
const added = vs.find(v => (v.sku || '').toLowerCase() === l.sampleSku.toLowerCase() && v.id !== l.loneVariantId && parseFloat(v.price) === 4.25);
if (added) { await api(`/products/${l.pid}/variants/${added.id}.json`, { method: 'DELETE' }); }
// revert the lone/roll variant back to option1='Sample' @ old price + old policy
await api(`/variants/${l.loneVariantId}.json`, { method: 'PUT', body: JSON.stringify({ variant: {
id: l.loneVariantId, sku: l.from.sku, option1: l.from.option1 || 'Sample', price: l.from.price, inventory_policy: l.from.inv_pol || 'deny' } }) });
n++; await sleep(400);
}
console.log('reverted', n);