From 1c375fdf016bf9fa60a2825fcfdc90158ad3f4c6 Mon Sep 17 00:00:00 2001 From: Thomas Joise Date: Sun, 19 Jul 2026 21:52:52 +0530 Subject: [PATCH] chore(d35): script to append directory-sourced LTD numbers to client names Appends ' LTD NO. ' where the Kerala co-op directory lookup found a registration number (found=true). Guarded: only found rows, skips names that already contain the number (space/punct-insensitive), appends the number only (no name/casing overwrite), audited via updateClient. Applied on live data after backup: 91 names updated (K-Z half). Co-Authored-By: Claude Fable 5 --- apps/hq/scripts/apply-ltd-numbers.ts | 42 ++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 apps/hq/scripts/apply-ltd-numbers.ts diff --git a/apps/hq/scripts/apply-ltd-numbers.ts b/apps/hq/scripts/apply-ltd-numbers.ts new file mode 100644 index 0000000..513c645 --- /dev/null +++ b/apps/hq/scripts/apply-ltd-numbers.ts @@ -0,0 +1,42 @@ +// apps/hq/scripts/apply-ltd-numbers.ts — one-off (D35): append the directory-sourced LTD +// number to the client name where the co-op directory found one. Keeps the number IN the +// name (owner's choice), in a consistent " LTD NO. " suffix. Idempotent + guarded: +// skips a client whose name already contains that number, and only touches found=true rows. +// Does NOT overwrite the name's spelling/casing — only appends the number. +// LTD_JSON= DATABASE_URL=postgres://… npx tsx apps/hq/scripts/apply-ltd-numbers.ts +import { readFileSync } from 'node:fs' +import { openDb } from '../src/db' +import { openPgDb } from '../src/db-pg' +import { updateClient } from '../src/repos-clients' + +interface Res { clientId: string; ltdNo: string; found: boolean } + +async function main() { + const path = process.env['LTD_JSON'] + if (path === undefined) throw new Error('Set LTD_JSON to the workflow result json path') + const parsed = JSON.parse(readFileSync(path, 'utf8')) as { results?: Res[] } | Res[] + const results = Array.isArray(parsed) ? parsed : (parsed.results ?? []) + const pgUrl = process.env['DATABASE_URL'] ?? '' + const db = pgUrl !== '' ? await openPgDb(pgUrl) : openDb(process.env['HQ_DATA_DIR']) + + let applied = 0, already = 0, skipped = 0 + const seen = new Set() + for (const r of results) { + const ltd = (r.ltdNo ?? '').trim() + if (!r.found || ltd === '' || seen.has(r.clientId)) { skipped++; continue } + seen.add(r.clientId) + const cur = await db.get<{ name: string }>(`SELECT name FROM client WHERE id=?`, r.clientId) + if (cur === undefined) { skipped++; continue } + // Already carries this number (bare, or "LTD. K 1028" with spaces/punct)? Don't duplicate. + // Compare with all non-alphanumerics stripped so "K 1028" matches "K1028". + const squish = (s: string) => s.toUpperCase().replace(/[^A-Z0-9]/g, '') + if (squish(cur.name).includes(squish(ltd))) { already++; continue } + const newName = `${cur.name} LTD NO. ${ltd}` + await updateClient(db, 'system', r.clientId, { name: newName }) + applied++ + } + // eslint-disable-next-line no-console + console.log(`LTD numbers: ${applied} appended, ${already} already present, ${skipped} skipped (not found / no number).`) +} + +main().catch((e) => { console.error(e); process.exit(1) })