← back to York Reprice 2026 08
backfill5.mjs
17 lines
import fs from 'node:fs';
const SHOP='designer-laboratory-sandbox.myshopify.com',VER='2024-10';
const TOKEN=(fs.readFileSync(process.env.HOME+'/Projects/secrets-manager/.env','utf8').match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1]?.trim();
const URL=`https://${SHOP}/admin/api/${VER}/graphql.json`;
const gql=async(q,v)=>{const r=await fetch(URL,{method:'POST',headers:{'X-Shopify-Access-Token':TOKEN,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});return r.json();};
const pay=JSON.parse(fs.readFileSync('data/york-new-phase1-payloads-enriched.json','utf8')).slice(0,5);
const H2G=new Map();let cur=null;for(let pg=0;pg<40;pg++){const d=(await gql(`query($c:String){collections(first:250,after:$c){pageInfo{hasNextPage endCursor} nodes{id handle ruleSet{appliedDisjunctively}}}}`,{c:cur})).data.collections;for(const c of d.nodes)H2G.set(c.handle,{id:c.id,smart:!!c.ruleSet});if(!d.pageInfo.hasNextPage)break;cur=d.pageInfo.endCursor;}
const Madd=`mutation($id:ID!,$pids:[ID!]!){collectionAddProducts(id:$id,productIds:$pids){userErrors{message}}}`;
let added=0,smart=0,err=0;
for(const p of pay){
const pv=await gql(`query{productVariants(first:1,query:${JSON.stringify(`sku:${p.variants[1].sku}`)}){nodes{product{id}}}}`);
const pid=pv.data.productVariants.nodes[0].product.id;
for(const h of (p.collections||[])){const c=H2G.get(h);if(!c)continue;if(c.smart){smart++;continue;} // smart auto-populates
const d=await gql(Madd,{id:c.id,pids:[pid]});const ue=(d.data?.collectionAddProducts?.userErrors||[]).filter(e=>!/smart/i.test(e.message));if(ue.length){err++;}else added++;}
}
console.log(`backfill 5: custom-added=${added}, smart-skipped(auto)=${smart}, realErr=${err}`);