← back to Groundworks Activation

scripts/verify.js

35 lines

#!/usr/bin/env node
// Post-publish 5-field audit of every Groundworks product on the LIVE store.
const { Client } = require('pg');
const fs = require('fs');
const DOMAIN='designer-laboratory-sandbox.myshopify.com', API='2024-10';
const TOKEN=(fs.readFileSync(process.env.HOME+'/Projects/secrets-manager/.env','utf8').split('\n').find(l=>l.startsWith('SHOPIFY_ADMIN_TOKEN='))||'').split('=').slice(1).join('=').trim();
const GQL=`https://${DOMAIN}/admin/api/${API}/graphql.json`;
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v){const r=await fetch(GQL,{method:'POST',headers:{'Content-Type':'application/json','X-Shopify-Access-Token':TOKEN},body:JSON.stringify({query:q,variables:v})});return (await r.json()).data;}
(async()=>{
  const pg=new Client({host:'/tmp',database:'dw_unified'});await pg.connect();
  const rows=(await pg.query(`SELECT dw_sku,shopify_product_id FROM groundworks_catalog WHERE shopify_product_id IS NOT NULL ORDER BY dw_sku`)).rows;
  let fails=[];let ok=0;
  for(const r of rows){
    const d=await gql(`query($id:ID!){product(id:$id){status featuredImage{url} tags description variants(first:10){nodes{sku price selectedOptions{value}}}}}`,{id:`gid://shopify/Product/${r.shopify_product_id}`});
    const p=d.product; if(!p){fails.push({dw:r.dw_sku,why:'missing'});continue;}
    const vs=p.variants.nodes;
    const sample=vs.find(v=>v.sku && v.sku.endsWith('-Sample'));
    const sell=vs.find(v=>v.sku && !v.sku.endsWith('-Sample'));
    const issues=[];
    if(!sample||Number(sample.price)!==4.25) issues.push('sample');
    if(!sell||!(Number(sell.price)>0)) issues.push('sellable/price');
    if(!p.featuredImage) issues.push('image');
    if(!p.description||p.description.length<20) issues.push('description');
    if(!p.tags||p.tags.length<2) issues.push('tags<2');
    if(p.status!=='ACTIVE') issues.push('status='+p.status);
    if(issues.length){fails.push({dw:r.dw_sku,pid:r.shopify_product_id,issues});}else ok++;
    await sleep(120);
  }
  await pg.end();
  console.log(`5-FIELD AUDIT: pass=${ok}  fail=${fails.length}  total=${rows.length}`);
  if(fails.length)console.log(JSON.stringify(fails,null,2));
  fs.writeFileSync(__dirname+'/../data/verify-results.json',JSON.stringify({ok,fails},null,2));
})();