← back to Shopify Sample Shipping

create-freeship-codes-undo.mjs

23 lines

#!/usr/bin/env node
// TK-11333 — UNDO for create-freeship-codes.mjs. Deletes EXACTLY the codes we created
// (verification/freeship-codes-created.json), skipping any marked preexisting:true.
// DRY-RUN BY DEFAULT; --apply to write.
import { query } from './query.mjs';
import fs from 'node:fs';

const APPLY = process.argv.includes('--apply');
const REC = new URL('./verification/freeship-codes-created.json', import.meta.url);
if (!fs.existsSync(REC)) { console.error('no verification/freeship-codes-created.json — nothing to undo'); process.exit(1); }
const rec = JSON.parse(fs.readFileSync(REC, 'utf8'));
const toDelete = (rec.created || []).filter(c => !c.preexisting && c.id);
console.log('=== create-freeship-codes UNDO (' + (APPLY ? 'APPLY' : 'DRY-RUN') + ') ===');
for (const c of toDelete) console.log('  would delete', c.code, c.id);
if (!toDelete.length) console.log('  (nothing we created to delete)');
if (!APPLY) { console.log('\nDry-run only. Re-run with --apply.'); process.exit(0); }

for (const c of toDelete) {
  const r = (await query(`mutation($id:ID!){discountCodeDelete(id:$id){deletedCodeDiscountId userErrors{field message}}}`, { id: c.id })).discountCodeDelete;
  console.log('  deleted', c.code, '->', r.deletedCodeDiscountId || JSON.stringify(r.userErrors));
}
console.log('undo complete.');