← back to Filemaker Mcp

scripts/probe-32513-invoice.mjs

45 lines

// Probe: find the FM invoice carrying Shopify order #32513's SKU (DWKK-120156)
// and show the line-level lookup fields (Name/Color/Mfr#/VID). Steve's indicator:
// SKU filled but Name+Color+Mfr#+VID all blank => WALLPAPER master missing, create it.
// Read-only. Usage: node scripts/probe-32513-invoice.mjs
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, fieldMetadata } = await import('../src/fm-client.js');

const DB = 'invoice';
const LAYOUT = 'Order Detail';

// what line-level fields exist on this layout (find the lookup columns)
const meta = await fieldMetadata(DB, LAYOUT);
const names = (meta.fieldMetaData || []).map((f) => f.name);
console.log('LINE-ISH FIELDS:', JSON.stringify(names.filter((n) =>
  /name|color|colour|mfr|vid|detail|js|pattern|q1|unit|net|retail/i.test(n))));

// the SKU can be stored with or without the dash — probe both on the JS#/detail column
for (const sku of ['DWKK120156', 'DWKK-120156', 'DWKK-120156-Sample']) {
  for (const field of names.filter((n) => /^(DETAIL 1|JS)/i.test(n) && !/\(/.test(n))) {
    try {
      const { records } = await findRecords(DB, LAYOUT, { [field]: `==${sku}` }, { limit: 3 });
      for (const r of records) {
        console.log(`MATCH via ${field}=${sku} recordId=${r.recordId}`);
        const f = r.fieldData;
        const show = {};
        for (const k of Object.keys(f)) {
          if (/invoice|name|color|colour|mfr|vid|detail|q1|unit|net 1|retail 1|paid|grand/i.test(k)) show[k] = f[k];
        }
        console.log(JSON.stringify(show, null, 1));
      }
    } catch (e) {
      if (e.fmCode !== '401') console.error(`probe ${field}=${sku}: ${e.message}`);
    }
  }
}