← back to Tk10630 Sku Suffix Canary

verify-mfr.mjs

50 lines

// Verify every ACTIVE Hollywood (commercial) product has a REAL mfr identity:
// a non-fabricated dw_sku/variant code, OR a real manufacturer_sku (code/number,
// not null and not a product-name). Reports products with NEITHER. READ-ONLY.
import { gql } from './shopify.mjs';
import { FABRICATED } from './lib.mjs';
import { writeFileSync } from 'node:fs';

const PAGE = `query($c:String){
  products(first:40, query:"vendor:\\"Hollywood Wallcoverings\\"", after:$c){
    pageInfo{ hasNextPage endCursor }
    nodes{ id handle title status
      g:metafield(namespace:"global",key:"dw_sku"){value}
      d:metafield(namespace:"dwc",key:"dw_sku"){value}
      c:metafield(namespace:"custom",key:"dw_sku"){value}
      mfr:metafield(namespace:"custom",key:"manufacturer_sku"){value}
      dmfr:metafield(namespace:"dwc",key:"manufacturer_sku"){value}
      variants(first:6){ nodes{ sku } }
    }
  }
}`;
const sleep = ms => new Promise(r => setTimeout(r, ms));
// a real code = matches PREFIX-DIGITS and is NOT a fabricated DW code
const realCode = s => !!s && /^[A-Z]{2,6}-?\d/i.test(s) && !FABRICATED.test(s);
// a real mfr number = code-like OR a bare number, not null, not a multi-word name
const realMfr = s => { if (!s) return false; const t = s.trim(); if (t.split(/\s+/).length >= 3) return false; return /\d/.test(t) && !FABRICATED.test(t); };

let scanned = 0, ok = 0, total = 0, cursor = null;
const missing = [];
while (true) {
  let res;
  for (let a = 0; ; a++) { try { res = await gql(PAGE, { cursor }); break; } catch (e) { if (/THROTTLED/i.test(e.message) && a < 8) { await sleep(2000 * (a + 1)); continue; } throw e; } }
  for (const p of res.data.products.nodes) {
    total++;
    if (p.status !== 'ACTIVE') continue; // filter active client-side (server filter breaks cursor)
    scanned++;
    const codes = [p.g?.value, p.d?.value, p.c?.value, ...p.variants.nodes.map(v => (v.sku || '').replace(/-(sample|yard|roll)$/i, ''))];
    const hasCode = codes.some(realCode);
    const hasMfr = realMfr(p.mfr?.value) || realMfr(p.dmfr?.value);
    if (hasCode || hasMfr) ok++;
    else missing.push({ handle: p.handle, title: p.title, dw_sku: p.g?.value, mfr: p.mfr?.value || p.dmfr?.value });
  }
  if (!res.data.products.pageInfo.hasNextPage) break;
  cursor = res.data.products.pageInfo.endCursor;
  if (total > 8000) break; // hard safety (can't exceed product count)
  if (total % 400 === 0) process.stderr.write(`  ...${total} products (${scanned} active), ${missing.length} with NO real mfr\n`);
}
writeFileSync('mfr-missing-active.json', JSON.stringify(missing, null, 2));
console.log(JSON.stringify({ active_commercial_scanned: scanned, have_real_mfr: ok, NO_real_mfr: missing.length }, null, 2));
if (missing.length) console.log('examples:', missing.slice(0, 8).map(m => `${m.handle} [mfr=${m.mfr}]`).join('  '));