chore(d35): script to append directory-sourced LTD numbers to client names
Appends ' LTD NO. <n>' 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 <noreply@anthropic.com>feat/client-detail-redesign
parent
809976fd8b
commit
1c375fdf01
@ -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. <n>" 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=<results.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<string>()
|
||||
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) })
|
||||
Loading…
Reference in New Issue