← back to Novasuede Min Fix

revert-reorder.mjs

18 lines

import fs from 'fs';
const env=fs.readFileSync(process.env.HOME+'/Projects/secrets-manager/.env','utf8');
const get=k=>(env.match(new RegExp('^'+k+'=(.*)$','m'))||[])[1];
const TOKEN=get('SHOPIFY_ADMIN_TOKEN');
const API='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json';
async function gql(q,v={}){const r=await fetch(API,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});return r.json();}
const REORDER=`mutation($id:ID!,$moves:[ProductVariantPositionInput!]!){ productVariantsBulkReorder(productId:$id, positions:$moves){ userErrors{ field message } } }`;
const changes=JSON.parse(fs.readFileSync(new URL('./restore-map-reorder.json',import.meta.url)));
let ok=0,err=0;
for(const c of changes){
  const moves=c.oldOrder.map(v=>({id:v.id, position:v.position})).sort((a,b)=>a.position-b.position);
  const r=await gql(REORDER,{id:c.productId, moves});
  const ue=r?.data?.productVariantsBulkReorder?.userErrors||[];
  if(ue.length||r.errors){ err++; console.log('ERR',c.title,JSON.stringify(ue),JSON.stringify(r.errors)); }
  else { ok++; console.log('reverted',c.title,'->',moves.map(m=>m.position).join(',')); }
}
console.log('REVERT-REORDER done. ok:',ok,'err:',err);