← back to Harlequin Pilot Publish

01-locate.mjs

50 lines

// READ-ONLY: locate each of the 23 pilot products live + dump current variants.
import fs from 'node:fs';
import { findProductByVariantSku, getProductVariants } from './lib.mjs';

const pilot = JSON.parse(fs.readFileSync(new URL('./data/pilot-23.json', import.meta.url)));
const out = [];

for (const p of pilot) {
  // Search by the DW SKU exactly, and also try the -Sample variant.
  let hits = await findProductByVariantSku(p.dw_sku);
  let productGid = null, matchedBy = null;

  // exact variant sku match (base)
  let exact = hits.find(h => (h.sku || '').toUpperCase() === p.dw_sku.toUpperCase());
  if (exact) { productGid = exact.product.id; matchedBy = 'base-sku'; }

  if (!productGid) {
    // try sample sku
    const sampleHits = await findProductByVariantSku(p.dw_sku + '-Sample');
    const ex = sampleHits.find(h => (h.sku || '').toUpperCase() === (p.dw_sku + '-SAMPLE').toUpperCase());
    if (ex) { productGid = ex.product.id; matchedBy = 'sample-sku'; }
  }

  if (!productGid) {
    // last resort: prefix match on any returned hit whose sku starts with dw_sku
    const pre = hits.find(h => (h.sku || '').toUpperCase().startsWith(p.dw_sku.toUpperCase()));
    if (pre) { productGid = pre.product.id; matchedBy = 'prefix-sku'; }
  }

  let prod = null;
  if (productGid) prod = await getProductVariants(productGid);

  out.push({
    dw_sku: p.dw_sku, title: p.title, haw: p.haw, retail: p.retail,
    productGid, matchedBy,
    handle: prod?.handle || null,
    liveTitle: prod?.title || null,
    status: prod?.status || null,
    variants: prod ? prod.variants.edges.map(e => ({
      id: e.node.id, title: e.node.title, sku: e.node.sku, price: e.node.price,
      invItemId: e.node.inventoryItem?.id, tracked: e.node.inventoryItem?.tracked,
    })) : null,
  });
  process.stderr.write(`${p.dw_sku}\t${productGid ? matchedBy : 'NOT-FOUND'}\t${prod?.variants.edges.length ?? '-'} variants\t${prod?.status ?? ''}\n`);
}

fs.writeFileSync(new URL('./data/located.json', import.meta.url), JSON.stringify(out, null, 2));
const found = out.filter(o => o.productGid).length;
process.stderr.write(`\nLOCATED ${found}/23\n`);