← back to Tk10630 Sku Suffix Canary
deep-dive.mjs
64 lines
// Deeper look: can the 2,538 "unrecoverable" be matched to Momentum's live feed?
// Also audit OTHER fields for hidden fabricated codes. READ-ONLY.
import { gql } from './shopify.mjs';
import { FABRICATED } from './lib.mjs';
import { readFileSync, writeFileSync } from 'node:fs';
const feed = JSON.parse(readFileSync('momentum-feed.json', 'utf8'));
const byNumber = new Map(feed.byNumber);
const byPatColor = new Map(feed.byPatColor);
const { skipped } = JSON.parse(readFileSync('plan.json', 'utf8'));
const ids = skipped.map(s => s.id);
const norm = s => (s || '').toLowerCase().replace(/\s+/g, '');
const digits = s => (s || '').replace(/\D/g, '');
const sleep = ms => new Promise(r => setTimeout(r, ms));
const res = { match_mfr_number: 0, match_pat_color: 0, no_match: 0 };
const otherFab = { legacy_main_sku: 0, legacy_sku: 0, product_number: 0 };
const recovered = [], stillOrphan = [];
for (let i = 0; i < ids.length; i += 50) {
const chunk = ids.slice(i, i + 50).map(x => `"${x}"`).join(',');
let data;
for (let a = 0; ; a++) {
try { ({ data } = await gql(`query{ nodes(ids:[${chunk}]){ ... on Product{ id handle status title
mfr:metafield(namespace:"custom",key:"manufacturer_sku"){value}
dmfr:metafield(namespace:"dwc",key:"manufacturer_sku"){value}
lm:metafield(namespace:"custom",key:"legacy_main_sku"){value}
ls:metafield(namespace:"custom",key:"legacy_sku"){value}
pn:metafield(namespace:"global",key:"product_number"){value}
pat:metafield(namespace:"dwc",key:"pattern_name"){value}
col1:metafield(namespace:"dwc",key:"color_name"){value}
col2:metafield(namespace:"custom",key:"color_name"){value} } } }`)); break; }
catch (e) { if (/THROTTLED/i.test(e.message) && a < 8) { await sleep(2000 * (a + 1)); continue; } throw e; }
}
for (const p of data.nodes) {
if (!p) continue;
if (FABRICATED.test(p.lm?.value || '')) otherFab.legacy_main_sku++;
if (FABRICATED.test(p.ls?.value || '')) otherFab.legacy_sku++;
if (FABRICATED.test((p.pn?.value || '').replace(/(\d)/, '-$1'))) otherFab.product_number++;
const mfr = p.mfr?.value || p.dmfr?.value;
// 1) manufacturer_sku digits == a Momentum number
let hit = null, via = null;
if (mfr) { const d = digits(mfr); if (d && byNumber.has(d)) { hit = d; via = 'mfr_number'; }
else if (byNumber.has(String(mfr).trim())) { hit = String(mfr).trim(); via = 'mfr_number'; } }
// 2) pattern+color match
if (!hit) {
const pat = p.pat?.value; const col = p.col1?.value || p.col2?.value;
if (pat && col) { const k = norm(pat) + '|' + norm(col); if (byPatColor.has(k)) { hit = byPatColor.get(k); via = 'pat_color'; } }
}
if (hit) { res[via === 'mfr_number' ? 'match_mfr_number' : 'match_pat_color']++;
recovered.push({ id: p.id, handle: p.handle, status: p.status, momentum_number: hit, via, feed: byNumber.get(hit) }); }
else { res.no_match++; stillOrphan.push({ id: p.id, handle: p.handle, status: p.status, title: p.title, mfr }); }
}
if (i % 500 === 0) process.stderr.write(` ...${i}/${ids.length}\n`);
}
writeFileSync('deep-recovered.json', JSON.stringify(recovered, null, 0));
writeFileSync('deep-orphan.json', JSON.stringify(stillOrphan, null, 0));
console.log(JSON.stringify({ total: ids.length, ...res,
recovered_total: recovered.length, recovered_active: recovered.filter(r => r.status === 'ACTIVE').length,
still_orphan_active: stillOrphan.filter(o => o.status === 'ACTIVE').length,
other_fabricated_fields: otherFab }, null, 2));
console.log('sample recovered:', recovered.slice(0, 6).map(r => `${r.handle}->#${r.momentum_number}(${r.via})`).join(' '));