← back to Contractwallpaper

tmp-kravet-cat360.mjs

25 lines

#!/usr/bin/env node
// 360 authoritative category fix — product_type per Kravet feed 'Use'. LIVE Shopify + PG mirror.
import fs from 'node:fs';
import { execFileSync } from 'node:child_process';
const env = Object.fromEntries(fs.readFileSync('/Users/macstudio3/Projects/secrets-manager/.env','utf8')
  .split('\n').filter(l=>l.includes('=')).map(l=>{const i=l.indexOf('='); return [l.slice(0,i).trim(), l.slice(i+1).trim()];}));
const DOMAIN=(env.SHOPIFY_STORE_DOMAIN||env.SHOPIFY_STORE).replace(/^https?:\/\//,'').replace(/\/$/,'');
const API=`https://${DOMAIN}/admin/api/2024-10`;
const H={'X-Shopify-Access-Token':env.SHOPIFY_ADMIN_TOKEN,'Content-Type':'application/json'};
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
const targets=fs.readFileSync('/tmp/kravet-cat-targets.txt','utf8').trim().split('\n')
  .map(l=>{const [sku,id,type]=l.split('|'); return {sku,id,type};});
let ok=0,fail=0; const byType={};
for(const {sku,id,type} of targets){
  try{
    const r=await fetch(`${API}/products/${id}.json`,{method:'PUT',headers:H,body:JSON.stringify({product:{id:Number(id),product_type:type}})});
    if(!r.ok) throw new Error(`${r.status} ${(await r.text()).slice(0,80)}`);
    execFileSync('psql',['-h','/tmp','-d','dw_unified','-c',`UPDATE shopify_products SET product_type='${type}' WHERE sku='${sku.replace(/'/g,"''")}';`]);
    ok++; byType[type]=(byType[type]||0)+1;
    if(ok%40===0) console.log(`  ...${ok}/${targets.length}`);
  }catch(e){ fail++; console.error(`  FAIL ${sku} [${id}]->${type}: ${e.message}`); }
  await sleep(500);
}
console.log(`DONE. category fixed: ${ok}/${targets.length} | ${JSON.stringify(byType)} | failures: ${fail}`);