← back to Filemaker Mcp
scripts/fuzzy-sku-check.mjs
73 lines
// fuzzy-sku-check — reusable existence check for a DW SKU in the WALLPAPER db.
// Strips a trailing -Sample, then queries across the fields a SKU can live in:
// "combo sku" (dashless calc, e.g. DWTT73433), "comboskuwithdash" (DWTT-73433),
// "mfr pattern number", "Mfr Pattern".
// Prints JSON { sku, normalized, exists, matches: [{ field, recordId, comboSku, name }] }.
// Exit 0 = exists, 1 = not found, 2 = usage/error.
// Usage: node scripts/fuzzy-sku-check.mjs DWTT-73433[-Sample]
// Intended consumer: the invoice-import pipeline (wire-up owned by that pipeline, not here).
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
const __dir = dirname(fileURLToPath(import.meta.url));
for (const line of readFileSync(join(__dir, '..', '.env'), 'utf8').split('\n')) {
const m = line.match(/^([A-Z_]+)=(.*)$/);
if (m && !process.env[m[1]]) process.env[m[1]] = m[2];
}
const { findRecords } = await import('../src/fm-client.js');
const DB = 'WALLPAPER';
const LAYOUT = '*List Wallpapers - Shopify Detail Database';
const raw = process.argv[2];
if (!raw) {
console.error('Usage: node scripts/fuzzy-sku-check.mjs <SKU> (e.g. DWTT-73433-Sample)');
process.exit(2);
}
// normalize: trim, strip trailing -Sample (any case), collapse whitespace
const normalized = raw.trim().replace(/[-_ ]?sample$/i, '');
const dashless = normalized.replace(/-/g, '');
// field -> query value. combo sku is stored dashless; comboskuwithdash keeps the dash.
const probes = [
['combo sku', dashless],
['comboskuwithdash', normalized.includes('-') ? normalized : dashless],
['mfr pattern number', normalized],
['Mfr Pattern', normalized],
];
const matches = [];
const seen = new Set();
for (const [field, value] of probes) {
if (!value) continue;
try {
const { records } = await findRecords(DB, LAYOUT, { [field]: `==${value}` }, { limit: 10 });
for (const r of records) {
const key = `${field}:${r.recordId}`;
if (seen.has(key)) continue;
seen.add(key);
matches.push({
field,
value,
recordId: r.recordId,
comboSku: r.fieldData['combo sku'],
comboSkuWithDash: r.fieldData['comboskuwithdash'],
mfrPattern: r.fieldData['Mfr Pattern'],
name: r.fieldData['Name of Pattern'],
});
}
} catch (e) {
if (e.fmCode === '401') continue; // no records match — not an error
console.error(`ERROR probing "${field}": ${e.message}`);
process.exit(2);
}
}
const out = { sku: raw, normalized, exists: matches.length > 0, matches };
console.log(JSON.stringify(out, null, 1));
process.exit(out.exists ? 0 : 1);