WFDASH-WEBAPP/scripts/diff-nss-fix.ts

110 lines
3.3 KiB
TypeScript
Raw Permalink Normal View History

import "./_env";
import { getAdminPocketBase } from "../src/lib/server/pocketbase-admin";
import { readFileSync } from "node:fs";
import { join } from "node:path";
const NSS_LOCATION =
"NSS Fasteners price list - hex nut DIN934 (Grade 8 + A2)";
const IMPORT_JSON = join(import.meta.dirname, "nss-fasteners-pb-import.json");
async function main() {
const importDoc = JSON.parse(readFileSync(IMPORT_JSON, "utf8")) as {
records: Array<{ sku: string; name: string; description: string; notes: string }>;
};
const originalBySku = new Map(
importDoc.records.map((r) => [r.sku, r] as const),
);
const pb = await getAdminPocketBase();
const current = await pb.collection("stock_items").getFullList<{
id: string;
sku: string;
name: string;
description: string;
notes: string;
updated: string;
}>({
filter: `location = "${NSS_LOCATION}"`,
fields: "id,sku,name,description,notes,updated",
batch: 200,
sort: "sku",
});
console.log(`Comparing ${current.length} NSS-imported rows: original (from JSON) vs current (in PB)\n`);
let nameChanged = 0;
let descChanged = 0;
let notesChanged = 0;
let unchanged = 0;
const diffs: Array<{
sku: string;
origName: string;
currName: string;
origDesc: string;
currDesc: string;
origNotes: string;
currNotes: string;
updated: string;
}> = [];
for (const r of current) {
const orig = originalBySku.get(r.sku);
if (!orig) continue;
const nameDiff = r.name !== orig.name;
const descDiff = r.description !== orig.description;
const notesDiff = r.notes !== orig.notes;
if (nameDiff) nameChanged++;
if (descDiff) descChanged++;
if (notesDiff) notesChanged++;
if (!nameDiff && !descDiff && !notesDiff) unchanged++;
if (nameDiff || descDiff || notesDiff) {
diffs.push({
sku: r.sku,
origName: orig.name,
currName: r.name,
origDesc: orig.description,
currDesc: r.description,
origNotes: orig.notes,
currNotes: r.notes,
updated: r.updated,
});
}
}
console.log(`Field change tally across ${current.length} rows:`);
console.log(` name changed: ${nameChanged}, unchanged: ${current.length - nameChanged}`);
console.log(` description changed: ${descChanged}, unchanged: ${current.length - descChanged}`);
console.log(` notes changed: ${notesChanged}, unchanged: ${current.length - notesChanged}`);
console.log(` fully unchanged rows: ${unchanged}\n`);
console.log(`Sample of rows with differences (first 5):\n`);
for (const d of diffs.slice(0, 5)) {
console.log(`SKU: ${d.sku} (updated: ${d.updated})`);
console.log(` name:`);
console.log(` before: "${d.origName}"`);
console.log(` after: "${d.currName}"`);
console.log(` description:`);
console.log(` before: "${d.origDesc}"`);
console.log(` after: "${d.currDesc}"`);
const notesChanged = d.origNotes !== d.currNotes;
if (notesChanged) {
console.log(` notes:`);
console.log(` before: "${d.origNotes}"`);
console.log(` after: "${d.currNotes}"`);
}
console.log("");
}
if (diffs.length > 5) {
console.log(`... and ${diffs.length - 5} more rows with differences`);
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});