← back to Designerwallcoverings
scripts/buyability-audit/policy-mechanism-probe.mjs
28 lines
import fs from 'fs';
const ENV=fs.readFileSync('/Users/stevestudio2/Projects/secrets-manager/.env','utf8');
const TOK=((ENV.match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1]||'').replace(/['"\r]/g,'').trim();
const GQL='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json';
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v){const r=await fetch(GQL,{method:'POST',headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});return r.json();}
const Q=`query($cursor:String){ products(first:50, after:$cursor, query:"status:active"){ pageInfo{hasNextPage endCursor}
edges{node{ variants(first:12){ edges{ node{ availableForSale inventoryQuantity inventoryPolicy } } } } } } }`;
let cursor=null,pages=0,vTot=0; const pol={CONTINUE:0,DENY:0}; let denyPos=0,denyZeroAvail=0,denyZeroUnavail=0,contAvail=0;
const CAP=Number(process.env.CAP||3000); let prodN=0;
while(prodN<CAP){ const j=await gql(Q,{cursor}); if(!j?.data)break; const c=j.data.products;
for(const e of c.edges){ prodN++; for(const ve of (e.node.variants?.edges||[])){ const v=ve.node; vTot++;
pol[v.inventoryPolicy]=(pol[v.inventoryPolicy]||0)+1;
if(v.inventoryPolicy==='CONTINUE'){ if(v.availableForSale) contAvail++; }
else { // DENY
if((v.inventoryQuantity||0)>0) denyPos++;
else if(v.availableForSale) denyZeroAvail++;
else denyZeroUnavail++;
} } }
pages++; if(!c.pageInfo.hasNextPage)break; cursor=c.pageInfo.endCursor; await sleep(250);
}
console.log(`policy probe: ${prodN} products, ${vTot} variants (${pages}p)`);
console.log(` inventoryPolicy: CONTINUE=${pol.CONTINUE} (${(100*pol.CONTINUE/vTot).toFixed(1)}%) DENY=${pol.DENY} (${(100*pol.DENY/vTot).toFixed(1)}%)`);
console.log(` CONTINUE variants available-for-sale (oversell-enabled, always buyable): ${contAvail}`);
console.log(` DENY variants with qty>0 (buyable via real stock): ${denyPos}`);
console.log(` DENY variants qty<=0 but availableForSale=true (oddity): ${denyZeroAvail}`);
console.log(` DENY variants qty<=0 and NOT available (would block sale at variant level): ${denyZeroUnavail}`);