← back to Dw Unbuyable Recovery Pilot

tk11124-dwve-velvet/rollback.mjs

31 lines

#!/usr/bin/env node
/**
 * rollback.mjs — TK-11124 undo. For each row in out/rollback.jsonl:
 *   • productVariantsBulkDelete the added -Yard variant (removes variant + its inventoryItem + levels)
 *   • metafieldsSet global.unit_of_measure back to its recorded old value
 * Reverts each product to sample-only, exactly as before. DRY-RUN default; --apply --i-am-steve to run.
 */
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { gql } from '../../designerwallcoverings/scripts/lib/shopify.mjs';
const __dir = path.dirname(fileURLToPath(import.meta.url));
const APPLY = process.argv.includes('--apply') && process.argv.includes('--i-am-steve');
const map = fs.readFileSync(path.join(__dir, 'out', 'rollback.jsonl'), 'utf8').trim().split('\n').filter(Boolean).map(l => JSON.parse(l));
const DEL = `mutation($pid:ID!,$ids:[ID!]!){ productVariantsBulkDelete(productId:$pid, variantsIds:$ids){ userErrors{ message } } }`;
const MF = `mutation($mf:[MetafieldsSetInput!]!){ metafieldsSet(metafields:$mf){ userErrors{ message } } }`;
console.log(`TK-11124 ROLLBACK — ${APPLY ? 'LIVE' : 'DRY-RUN'} — ${map.length} products`);
for (const r of map) {
  const pid = `gid://shopify/Product/${r.product_id}`;
  console.log(`  ${APPLY?'delete':'would-delete'} ${r.yard_sku} (${r.added_variant_id}) + unit "${r.unit_of_measure_new}"->"${r.unit_of_measure_old}" on ${r.dwsku}`);
  if (!APPLY) continue;
  const d = await gql(DEL, { pid, ids: [r.added_variant_id] });
  const de = d?.productVariantsBulkDelete?.userErrors || []; if (de.length || d?.__err) console.log('    del-err', JSON.stringify(de.length?de:d.__err).slice(0,140));
  if (r.unit_of_measure_old != null) {
    const m = await gql(MF, { mf: [{ ownerId: pid, namespace: 'global', key: 'unit_of_measure', type: 'single_line_text_field', value: r.unit_of_measure_old }] });
    const me = m?.metafieldsSet?.userErrors || []; if (me.length || m?.__err) console.log('    mf-err', JSON.stringify(me.length?me:m.__err).slice(0,140));
  }
  await new Promise(r => setTimeout(r, 120));
}
console.log(APPLY ? 'ROLLBACK DONE' : 'To execute: node rollback.mjs --apply --i-am-steve');