← back to Novasuede Min Fix
apply.mjs
27 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';
const changes = JSON.parse(fs.readFileSync('restore-map.json','utf8'));
async function gql(query,variables={}){
const r=await fetch(API,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query,variables})});
return r.json();
}
const M=`mutation($mf:[MetafieldsSetInput!]!){metafieldsSet(metafields:$mf){metafields{id namespace key value ownerType} userErrors{field message}}}`;
let ok=0, errs=[];
for(const c of changes){
const mfs=[];
if(c.prodMin_change) mfs.push({ownerId:c.productId, namespace:'global', key:'v_prods_quantity_order_min', type:'single_line_text_field', value:'5'});
if(c.perYardMin_change) mfs.push({ownerId:c.perYardVariantId, namespace:'custom', key:'v_prod_quantity_order_min', type:'single_line_text_field', value:'5'});
if(!mfs.length) continue;
const res=await gql(M,{mf:mfs});
const ue=res?.data?.metafieldsSet?.userErrors||[];
if(ue.length || res.errors){ errs.push({title:c.title, ue, top:res.errors}); }
else ok++;
if(ok%25===0) console.log(' ...applied',ok);
}
console.log('DONE. products updated:',ok,' errors:',errs.length);
if(errs.length) fs.writeFileSync('apply-errors.json',JSON.stringify(errs,null,2));
console.log(errs.slice(0,3));