← back to Designer Wallcoverings
audits/designtex-uom-fix/flip-3-pebble-metafield.mjs
30 lines
import fs from 'fs'; import os from 'os'; import path from 'path';
const TOK = fs.readFileSync(path.join(os.homedir(),'Projects/secrets-manager/.env'),'utf8').match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)[1].trim();
const DOMAIN='designer-laboratory-sandbox.myshopify.com', VER='2024-10';
const gql=(q,v)=>fetch(`https://${DOMAIN}/admin/api/${VER}/graphql.json`,{method:'POST',headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})}).then(r=>r.json());
const ids=[
'gid://shopify/Product/7866902937651', // Pebble Hematite
'gid://shopify/Product/7866902970419', // Pebble Alabaster
'gid://shopify/Product/7866903003187', // Pebble Starfish
];
const mfMut=`mutation($mf:[MetafieldsSetInput!]!){ metafieldsSet(metafields:$mf){ userErrors{ field message } } }`;
const tagRm=`mutation($id:ID!,$tags:[String!]!){ tagsRemove(id:$id,tags:$tags){ userErrors{ message } } }`;
const tagAdd=`mutation($id:ID!,$tags:[String!]!){ tagsAdd(id:$id,tags:$tags){ userErrors{ message } } }`;
const varQ=`query($id:ID!){ product(id:$id){ variants(first:5){ edges{ node{ id title } } } } }`;
const varMut=`mutation($pid:ID!,$variants:[ProductVariantsBulkInput!]!){ productVariantsBulkUpdate(productId:$pid,variants:$variants){ userErrors{ message } } }`;
const EXECUTE=process.argv.includes('--execute');
console.log('Mode:',EXECUTE?'EXECUTE':'DRY',' targets:',ids.length,'(relabel-only, price never sent)');
if(!EXECUTE) process.exit(0);
for(const id of ids){
const errs=[];
const m=await gql(mfMut,{mf:[{ownerId:id,namespace:'global',key:'unit_of_measure',type:'single_line_text_field',value:'YARD'}]});
(m.data?.metafieldsSet?.userErrors||[]).forEach(e=>errs.push('mf:'+e.message));
const tr=await gql(tagRm,{id,tags:['Priced Per Single Roll']}); (tr.data?.tagsRemove?.userErrors||[]).forEach(e=>errs.push('tagRm:'+e.message));
const ta=await gql(tagAdd,{id,tags:['Priced Per Yard']}); (ta.data?.tagsAdd?.userErrors||[]).forEach(e=>errs.push('tagAdd:'+e.message));
const vq=await gql(varQ,{id});
const vs=(vq.data?.product?.variants?.edges||[]).map(e=>e.node).filter(v=>!/Sample/i.test(v.title)&&/Roll/i.test(v.title));
if(vs.length){ const vm=await gql(varMut,{pid:id,variants:vs.map(v=>({id:v.id,optionValues:[{optionName:'Title',name:'Yard'}]}))}); (vm.data?.productVariantsBulkUpdate?.userErrors||[]).forEach(e=>errs.push('var:'+e.message)); }
console.log(id, errs.length?('ERR '+errs.join('; ')):'ok');
await new Promise(x=>setTimeout(x,400));
}