← back to Fentucci Naturals

evidence/TK-11868/remeasure-probe.mjs

36 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
    cw:metafield(namespace:"custom",key:"width"){value}
    variants(first:5){nodes{title sku}}}}}`;
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.variants.nodes||[]).some(v=>/per\s*yard/i.test(v.title)));
function cat(p){const v=(p.cw?.value||'').trim();
  if(!v)return 'MISSING';
  if(/ft/i.test(v))return 'WRONGVAL';                 // "3in x 1.5ft" sample-memo dim
  const m=v.match(/(\d+(?:\.\d+)?)\s*("|in|inch)/i);
  if(m && parseFloat(m[1])<18)return 'WRONGVAL';
  return 'OK';}
const g={MISSING:[],WRONGVAL:[],OK:[]};
for(const p of py)g[cat(p)].push(p);
console.log(`per_yard=${py.length}  OK=${g.OK.length}  WRONGVAL=${g.WRONGVAL.length}  MISSING=${g.MISSING.length}`);
console.log('\nWRONGVAL sample values:', [...new Set(g.WRONGVAL.map(p=>p.cw.value))]);
console.log('WRONGVAL ids:', g.WRONGVAL.map(p=>p.id.split('/').pop()).join(','));
fs.writeFileSync('fentucci-width-scope.json',JSON.stringify({
  measured_at:new Date().toISOString(), source:'Shopify Admin API live, vendor=Fentucci status=active',
  per_yard_total:py.length, ok:g.OK.length, wrongval:g.WRONGVAL.length, missing:g.MISSING.length,
  wrongval:g.WRONGVAL.map(p=>({gid:p.id,pid:p.id.split('/').pop(),title:p.title,cw:p.cw.value,
     per_yard_sku:(p.variants.nodes.find(v=>/per\s*yard/i.test(v.title))||{}).sku})),
  missing:g.MISSING.map(p=>({gid:p.id,pid:p.id.split('/').pop(),title:p.title,handle:p.handle,
     per_yard_sku:(p.variants.nodes.find(v=>/per\s*yard/i.test(v.title))||{}).sku}))},null,2));
console.log('\nwrote fentucci-width-scope.json');