← back to Secrets Manager

coordonne-collab-audit/check-membership.js

21 lines

#!/usr/bin/env node
// Verify REAL collection membership by paging products (not the lagging productsCount cache).
const fs=require('fs'),path=require('path');
const ENV=fs.readFileSync(path.join(__dirname,'..','.env'),'utf8');
const TOKEN=(ENV.match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1];
const DOMAIN='designer-laboratory-sandbox.myshopify.com';
const handle=process.argv[2];
(async()=>{
  const cr=await fetch(`https://${DOMAIN}/admin/api/2024-10/graphql.json`,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:`{collections(first:1,query:"handle:${handle}"){nodes{id title productsCount{count}}}}`})});
  const cj=await cr.json(); const c=cj.data.collections.nodes[0];
  if(!c){console.log('NO COLLECTION for handle',handle);return;}
  let after=null,total=0;
  while(true){
    const q=`query($id:ID!,$after:String){ collection(id:$id){ products(first:250, after:$after){ pageInfo{hasNextPage endCursor} nodes{ id } } } }`;
    const r=await fetch(`https://${DOMAIN}/admin/api/2024-10/graphql.json`,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:{id:c.id,after}})});
    const j=await r.json(); const p=j.data.collection.products; total+=p.nodes.length;
    if(!p.pageInfo.hasNextPage) break; after=p.pageInfo.endCursor;
  }
  console.log(`${handle}: REAL membership=${total} | cache productsCount=${c.productsCount.count}`);
})();