← back to Shopify Sample Shipping
grandfather-undo.mjs
35 lines
#!/usr/bin/env node
// TK-11333 — UNDO for grandfather-apply.mjs. Removes the `sample-freeship` tag from EXACTLY
// the customers we tagged (verification/grandfather-applied.json), never a broader set.
// Skips entries marked preexisting:true (they had the tag before we ran — leave them).
// DRY-RUN BY DEFAULT; --apply to write.
import { query } from './query.mjs';
import fs from 'node:fs';
const APPLY = process.argv.includes('--apply');
const TAG = 'sample-freeship';
const APPLIED = new URL('./verification/grandfather-applied.json', import.meta.url);
if (!fs.existsSync(APPLIED)) { console.error('no verification/grandfather-applied.json — nothing to undo'); process.exit(1); }
const applied = JSON.parse(fs.readFileSync(APPLIED, 'utf8'));
const toRemove = applied.filter(a => !a.preexisting);
console.log('=== grandfather UNDO (' + (APPLY ? 'APPLY' : 'DRY-RUN') + ') ===');
console.log('would remove', TAG, 'from', toRemove.length, 'customers (', applied.length - toRemove.length, 'preexisting kept )');
if (!APPLY) { console.log('\nDry-run only. Re-run with --apply.'); process.exit(0); }
const sleep = ms => new Promise(r => setTimeout(r, ms));
let removed = 0, i = 0;
const remaining = [];
for (const a of applied) {
i++;
if (a.preexisting) { remaining.push(a); continue; }
try {
const r = (await query(`mutation($id:ID!,$tags:[String!]!){tagsRemove(id:$id,tags:$tags){userErrors{field message}}}`, { id: a.customerId, tags: [TAG] })).tagsRemove;
if (r.userErrors?.length) { console.error(' ERR', a.email, JSON.stringify(r.userErrors)); remaining.push(a); }
else removed++;
} catch (e) { console.error(' ERR', a.email, e.message.slice(0, 80)); remaining.push(a); }
if (i % 50 === 0) { console.log(` ...${i}/${applied.length} removed=${removed}`); fs.writeFileSync(APPLIED, JSON.stringify(remaining.concat(applied.slice(i)), null, 2)); }
await sleep(120);
}
fs.writeFileSync(APPLIED, JSON.stringify(remaining, null, 2));
console.log(`\nDONE: removed ${TAG} from ${removed} customers. Remaining recorded (preexisting/failed): ${remaining.length}`);