← back to Tk10895 GroupB

erica/check-zigzag.mjs

26 lines

import fs from 'node:fs';
const TOK=fs.readFileSync(process.env.HOME+'/Projects/secrets-manager/.env','utf8')
  .split('\n').find(l=>l.startsWith('SHOPIFY_ADMIN_TOKEN='))?.split('=').slice(1).join('=').trim();
const SHOP='designer-laboratory-sandbox.myshopify.com',API='2024-10';
const ready=JSON.parse(fs.readFileSync('tranche-c-ready.json','utf8'));
const zz=Object.entries(ready).filter(([,v])=>v.pattern.toLowerCase().startsWith('zig zag'));
const Q=`query($ids:[ID!]!){ nodes(ids:$ids){ ... on Product { legacyResourceId title handle status
  variants(first:25){ edges{ node{ id title price position sku inventoryItem{id} } } } } } }`;
const r=await fetch(`https://${SHOP}/admin/api/${API}/graphql.json`,{method:'POST',
  headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},
  body:JSON.stringify({query:Q,variables:{ids:zz.map(([,v])=>`gid://shopify/Product/${v.pid}`)}})});
const j=await r.json(); if(j.errors){console.error(JSON.stringify(j.errors));process.exit(1);}
const byPid=Object.fromEntries(j.data.nodes.filter(Boolean).map(n=>[n.legacyResourceId,n]));
const out=[];
for(const [mfr,v] of zz){
  const n=byPid[v.pid]; if(!n){console.log(`${mfr} NOT FOUND`);continue;}
  const vs=n.variants.edges.map(e=>e.node).sort((a,b)=>a.position-b.position);
  const sell=vs.find(x=>parseFloat(x.price)>10);
  const want=(Math.round((v.cost/0.65/0.85)*100)/100).toFixed(2);
  console.log(`${mfr} ${n.status.padEnd(7)} ${n.handle.padEnd(14)} nvar=${vs.length} sellable=${sell?'$'+sell.price:'NONE'} want=$${want} :: ${vs.map(x=>x.title+' $'+x.price).join(' | ')}`);
  out.push({mfr,pid:v.pid,handle:n.handle,status:n.status,title:n.title,want,width:v.width,cost:v.cost,
            has_sellable:!!sell,variants:vs.map(x=>({id:x.id,title:x.title,price:x.price,pos:x.position,sku:x.sku}))});
}
fs.writeFileSync('zigzag-live.json',JSON.stringify(out,null,1));
console.log(`\n${out.length} rows; sample-only=${out.filter(o=>!o.has_sellable).length} already-sellable=${out.filter(o=>o.has_sellable).length}`);