← back to Majilite Onboard

scripts/audit.js

32 lines

const fs=require('fs'),path=require('path');
const STORE='designer-laboratory-sandbox.myshopify.com';
const TOKEN=(fs.readFileSync(path.join(process.env.HOME,'Projects/secrets-manager/.env'),'utf8').match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1].replace(/["']/g,'').trim();
const GQL=`https://${STORE}/admin/api/2024-10/graphql.json`;
async function gql(q,v){const r=await fetch(GQL,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});return r.json();}
(async()=>{
  let all=[],cursor=null;
  do{
    const d=await gql(`query($c:String){ products(first:100, after:$c, query:"vendor:Majilite"){ pageInfo{hasNextPage endCursor} nodes{ id handle status createdAt featuredMedia{id} variants(first:1){nodes{sku price}} } } }`,{c:cursor});
    if(d.errors){console.log('ERR',JSON.stringify(d.errors).slice(0,150));break;}
    const pg=d.data.products; all.push(...pg.nodes); cursor=pg.pageInfo.hasNextPage?pg.pageInfo.endCursor:null;
  }while(cursor);
  console.log('total Majilite products:',all.length);
  // group by dw sku (strip -Sample)
  const bySku={};
  for(const p of all){ const sku=(p.variants.nodes[0]||{}).sku||'(none)'; const base=sku.replace(/-Sample$/,''); (bySku[base]=bySku[base]||[]).push(p); }
  const dupes=Object.entries(bySku).filter(([s,a])=>a.length>1);
  console.log('distinct DW skus:',Object.keys(bySku).length);
  console.log('DUPLICATE skus (same DWMJ on >1 product):',dupes.length);
  for(const [s,a] of dupes) console.log('  DUP',s,'x'+a.length,'→',a.map(p=>p.handle+'['+p.status+(p.featuredMedia?',img':',NOIMG')+']').join(' , '));
  const noimg=all.filter(p=>!p.featuredMedia);
  console.log('products WITHOUT image:',noimg.length, noimg.slice(0,12).map(p=>(p.variants.nodes[0]||{}).sku||p.handle).join(', '));
  const active=all.filter(p=>p.status==='ACTIVE').length, draft=all.filter(p=>p.status==='DRAFT').length;
  console.log('status: active',active,'draft',draft);
  // which of my 160 dw_skus are present?
  const prods=JSON.parse(fs.readFileSync(path.join(__dirname,'..','data/products.json'),'utf8'));
  const present=new Set(Object.keys(bySku));
  const missing=prods.filter(p=>!present.has(p.dw_sku));
  console.log('of my 160 dw_skus, MISSING on store:',missing.length, missing.map(p=>p.dw_sku).join(', '));
  fs.writeFileSync(path.join(__dirname,'..','data/store_audit.json'),JSON.stringify({total:all.length,dupes:dupes.map(([s,a])=>({sku:s,products:a})),noimg,missing:missing.map(p=>p.dw_sku)},null,2));
})();