← back to Dw Image Shrink

verify-blanked.js

25 lines

// Robust "0 blanked" check: sample N done-products spread across the whole run,
// confirm each still has >=1 live image on Shopify. Read-only.
import fs from 'fs';
import pg from 'pg';
const SHOP='designer-laboratory-sandbox.myshopify.com';
const ATOK=fs.readFileSync('/Users/macstudio3/Projects/secrets-manager/.env','utf8').split('\n').find(l=>l.startsWith('SHOPIFY_ADMIN_TOKEN=')).split('=').slice(1).join('=').replace(/["' ]/g,'');
const N=+(process.argv[2]||100);
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
const db=new pg.Client({host:'/tmp',database:'dw_unified'}); await db.connect();
// spread the sample across the id range (start, middle, end of the run)
const {rows}=await db.query("SELECT product_id FROM img_shrink_ledger WHERE state='done' ORDER BY (id % 97), id LIMIT $1",[N]);
let blanked=0, ok=0, err=0;
for(const r of rows){
  try{
    const res=await fetch(`https://${SHOP}/admin/api/2024-10/products/${r.product_id}.json?fields=id,status,images`,{headers:{'X-Shopify-Access-Token':ATOK}});
    await sleep(120);
    const p=(await res.json()).product;
    const n=(p?.images||[]).length;
    if(n===0){ blanked++; console.log(`  BLANKED product ${r.product_id} (status ${p?.status})`); } else ok++;
  }catch(e){ err++; }
}
console.log(`\nchecked ${rows.length} done-products spread across the run | with-image=${ok} | BLANKED=${blanked} | err=${err}`);
console.log(blanked===0 ? 'VERDICT: 0 blanked across the broad sample — upload-first integrity holds.' : `VERDICT: ${blanked} BLANKED — investigate.`);
await db.end();