← back to Fentucci Naturals
evidence/TK-11868/width-metafield-distribution-probe.mjs
27 lines
import fs from 'fs'; import os from 'os'; import path from 'path';
const SHOP='designer-laboratory-sandbox.myshopify.com'; const API=`https://${SHOP}/admin/api/2024-10/graphql.json`;
function tok(){const e=fs.readFileSync(path.join(os.homedir(),'Projects/secrets-manager/.env'),'utf8');
const m=e.split('\n').find(l=>/^SHOPIFY_ADMIN_TOKEN=/i.test(l));return m.split('=').slice(1).join('=').trim().replace(/^["']|["']$/g,'');}
const T=tok();
async function gql(q,v){const r=await fetch(API,{method:'POST',headers:{'X-Shopify-Access-Token':T,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});
const j=await r.json(); if(j.errors)throw new Error(JSON.stringify(j.errors)); return j.data;}
const Q=`query($c:String){products(first:50,query:"vendor:'Fentucci' status:active",after:$c){
pageInfo{hasNextPage endCursor}
nodes{id title handle productType options{name values}
cw:metafield(namespace:"custom",key:"width"){value}
sw:metafield(namespace:"specs",key:"width"){value}
variants(first:5){nodes{title price}}}}}`;
let cur=null, all=[];
for(;;){const d=await gql(Q,{c:cur}); all.push(...d.products.nodes);
if(!d.products.pageInfo.hasNextPage)break; cur=d.products.pageInfo.endCursor;}
const py=all.filter(p=>(p.options||[]).some(o=>/per\s*yard/i.test(o.name)||(o.values||[]).some(v=>/per\s*yard/i.test(v)))
|| (p.variants.nodes||[]).some(v=>/per\s*yard/i.test(v.title)));
console.log("Per-Yard total:", py.length);
const dist={};
for(const p of py){const k=`custom.width=${JSON.stringify(p.cw?.value)} | specs.width=${JSON.stringify(p.sw?.value)}`; dist[k]=(dist[k]||0)+1;}
console.log("width-metafield distribution across the 290:");
for(const [k,n] of Object.entries(dist).sort((a,b)=>b[1]-a[1])) console.log(` ${n} ${k}`);
console.log("\nNeither custom.width NOR specs.width:", py.filter(p=>!(p.cw?.value?.trim())&&!(p.sw?.value?.trim())).length);
console.log("\n2 grasscloth samples w/ handle (for live PDP check):");
for(const p of py.filter(p=>/grass/i.test(p.title)).slice(0,2)) console.log(" ", p.handle, "| custom.width:",p.cw?.value, "| specs.width:",p.sw?.value, "| type:",p.productType);