← back to Vahallan Onboard
gate-check.js
39 lines
// Direct reproduction of the rotation gate on ONE product — runs the REAL
// validateBeforeActivate + a copy of fiveFieldExtra against live Shopify data.
const os=require('os'), path=require('path'), https=require('https');
const { validateBeforeActivate } = require(path.join(os.homedir(),
'Projects/Designer-Wallcoverings/shopify/scripts/lib/validate-before-activate.js'));
const TOKEN=require('fs').readFileSync(path.join(os.homedir(),'Projects/secrets-manager/.env'),'utf8')
.split('\n').find(l=>l.startsWith('SHOPIFY_ADMIN_TOKEN=')).split('=')[1].trim();
const PID=process.argv[2];
function g(p){return new Promise((res,rej)=>{https.get(`https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/${p}`,
{headers:{'X-Shopify-Access-Token':TOKEN}},r=>{let b='';r.on('data',d=>b+=d);r.on('end',()=>res(JSON.parse(b)));}).on('error',rej);});}
// mirror of fiveFieldExtra (rotate-activate.js)
function fiveFieldExtra(n){
const v=n.variants||[]; const hasSample=v.some(x=>/-sample$/i.test(x.sku||''));
const sellable=v.filter(x=>!/-sample$/i.test(x.sku||'')); const hasSellablePriced=sellable.some(x=>parseFloat(x.price)>0);
const isQuoteOnly=(n.tags||[]).includes('quotes'); const tagCount=(n.tags||[]).length; const r=[];
if(!hasSample)r.push('no-sample-variant');
if(!sellable.length&&!isQuoteOnly)r.push('no-sellable-variant');
if(!hasSellablePriced&&!isQuoteOnly)r.push('sellable-price-not-gt-0');
if(tagCount<2)r.push('fewer-than-2-tags'); return {ok:r.length===0,reasons:r};
}
(async()=>{
const p=(await g(`products/${PID}.json`)).product;
const mfs=(await g(`products/${PID}/metafields.json`)).metafields||[];
const mv=(ns,k)=>{const m=mfs.find(x=>x.namespace===ns&&x.key===k);return m&&m.value;};
const tags=(p.tags||'').split(',').map(t=>t.trim()).filter(Boolean);
const specs={width:mv('global','width')||mv('custom','width'),length:mv('global','length'),
repeat:mv('global','repeat'),material:mv('global','material'),unitOfMeasure:mv('global','unit_of_measure')||''};
const canonical=validateBeforeActivate({title:p.title,dwSku:'',vendor:p.vendor,tags,
descriptionHtml:p.body_html||'',specs,vendorSpecs:specs,
images:(p.images||[]).map(i=>i.src),vendorImages:(p.images||[]).map(i=>i.src),
variants:(p.variants||[]).map(v=>({sku:v.sku}))});
const extra=fiveFieldExtra({variants:p.variants,tags});
console.log('title:',p.title);
console.log('quotes tag:',tags.includes('quotes'),'| width:',specs.width,'| status:',p.status);
console.log('canonical gate:',canonical.ok?'PASS':'FAIL',canonical.reasons||'');
console.log('fiveFieldExtra:',extra.ok?'PASS':'FAIL',extra.reasons||'');
console.log('=> WOULD ACTIVATE:',canonical.ok&&extra.ok);
})();