You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
2.3 KiB
TypeScript
43 lines
2.3 KiB
TypeScript
// 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) })
|