← back to Tk10895 GroupB
erica/check-207-live.mjs
47 lines
import fs from 'node:fs';
const TOK=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 SHOP='designer-laboratory-sandbox.myshopify.com',API='2024-10';
const flags=JSON.parse(fs.readFileSync('sibling-flags.json','utf8'));
const q=("select mfr_sku||'\t'||replace(shopify_product_id,'gid://shopify/Product/','') from china_seas_catalog where mfr_sku in ("
+flags.map(f=>`'${f.mfr}'`).join(',')+") and shopify_product_id is not null");
const {execSync}=await import('node:child_process');
const pidmap=Object.fromEntries(execSync(`psql -h /tmp -d dw_unified -Atc "${q}"`,{encoding:'utf8'})
.trim().split('\n').filter(Boolean).map(l=>l.split('\t')));
const Q=`query($ids:[ID!]!){ nodes(ids:$ids){ ... on Product { legacyResourceId status
variants(first:25){ edges{ node{ price position title } } } } } }`;
const ids=flags.map(f=>pidmap[f.mfr]).filter(Boolean);
const byPid={};
for(let i=0;i<ids.length;i+=50){
const r=await fetch(`https://${SHOP}/admin/api/${API}/graphql.json`,{method:'POST',
headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},
body:JSON.stringify({query:Q,variables:{ids:ids.slice(i,i+50).map(p=>`gid://shopify/Product/${p}`)}})});
const j=await r.json(); if(j.errors){console.error(JSON.stringify(j.errors));process.exit(1);}
for(const n of j.data.nodes) if(n) byPid[n.legacyResourceId]=n;
await new Promise(s=>setTimeout(s,400));
}
let liveSell=0, sampleOnly=0, notActive=0, exposure=0;
const live=[];
for(const f of flags){
const p=pidmap[f.mfr]; const n=p?byPid[p]:null; if(!n) continue;
const vs=n.variants.edges.map(e=>e.node);
const sell=vs.find(v=>parseFloat(v.price)>10);
if(n.status!=='ACTIVE'){notActive++;continue;}
if(!sell){sampleOnly++;continue;}
liveSell++;
const wrongRetail=+(f.our_cost/0.65/0.85).toFixed(2), rightRetail=+(f.mill_cost/0.65/0.85).toFixed(2);
const matchesOur = Math.abs(parseFloat(sell.price)-wrongRetail)<0.02;
if(matchesOur){ exposure += wrongRetail-rightRetail;
live.push({...f,pid:p,live:sell.price,if_mill_code:rightRetail.toFixed(2),delta:+(wrongRetail-rightRetail).toFixed(2)}); }
}
console.log(`flagged skus: ${flags.length}`);
console.log(` ACTIVE + sellable : ${liveSell}`);
console.log(` ACTIVE but sample-only : ${sampleOnly}`);
console.log(` not ACTIVE : ${notActive}`);
console.log(`\n of the sellable, priced off OUR (higher) row: ${live.length}`);
console.log(` price delta if the mill-code row is the right join: $${exposure.toFixed(2)} across ${live.length} live products`);
for(const r of live.slice(0,12))
console.log(` ${r.mfr} ${r.pattern.slice(0,24).padEnd(25)} live $${r.live} -> $${r.if_mill_code} (-$${r.delta})`);
fs.writeFileSync('sibling-live.json',JSON.stringify(live,null,1));
console.log(`\nwrote sibling-live.json (${live.length})`);