← back to Filemaker Mcp

scripts/test-one.mjs

39 lines

#!/usr/bin/env node
// Read-only single-order test of the client-sync fix (commit:false → no writes).
// Fetches the most recent Shopify order (or #<arg>), shows the mapped Company/Name
// field split, then runs the live FileMaker dedup and prints what it WOULD do.
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import { mapOrder, syncOrder } from '../lib/sync-core.js';

const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
for (const l of readFileSync(join(ROOT, '.env'), 'utf8').split('\n')) {
  const m = l.match(/^([A-Z0-9_]+)=(.*)$/); if (m && !process.env[m[1]]) process.env[m[1]] = m[2];
}
const STORE = process.env.FM_SHOPIFY_STORE || process.env.SHOPIFY_STORE;
const TOKEN = process.env.SHOPIFY_ORDERS_TOKEN;

const arg = process.argv[2];
const url = arg
  ? `https://${STORE}/admin/api/2024-10/orders.json?status=any&name=${encodeURIComponent('#' + arg)}`
  : `https://${STORE}/admin/api/2024-10/orders.json?status=any&limit=1`;
const r = await fetch(url, { headers: { 'X-Shopify-Access-Token': TOKEN } });
if (!r.ok) throw new Error(`Shopify fetch failed: ${r.status}`);
const order = (await r.json()).orders?.[0];
if (!order) { console.log('no order found'); process.exit(0); }

const m = mapOrder(order);
console.log(`\nOrder #${order.order_number}`);
console.log('  shipping.company :', JSON.stringify(order.shipping_address?.company || ''));
console.log('  customer name    :', JSON.stringify(((order.customer?.first_name||'')+' '+(order.customer?.last_name||'')).trim()));
console.log('  → FM Company     :', JSON.stringify(m.fields['Company']), '(should be the BUSINESS, or the person if no business)');
console.log('  → FM Name        :', JSON.stringify(m.fields['Name']), '(should be the CONTACT person)');

const res = await syncOrder(order, { commit: false }); // read-only: dedup runs, no writes
console.log('\nDedup result (read-only):');
console.log('  client action :', res.client);          // would-create | existing | updated
console.log('  matched on    :', res.matchedOn || '(none — no existing client)');
console.log('  account #     :', res.accountNumber || '(n/a)');
console.log('\nNo FileMaker records were created or modified (commit:false).');