diff --git a/apps/hq/scripts/apply-name-fixes.ts b/apps/hq/scripts/apply-name-fixes.ts new file mode 100644 index 0000000..15b6dbf --- /dev/null +++ b/apps/hq/scripts/apply-name-fixes.ts @@ -0,0 +1,37 @@ +// apps/hq/scripts/apply-name-fixes.ts — one-off (D35 data correction): apply the approved +// client-name corrections from name-review.json via the audited updateClient path. +// REVIEW_JSON= DATABASE_URL=postgres://… npx tsx apps/hq/scripts/apply-name-fixes.ts +import { readFileSync } from 'node:fs' +import { openDb } from '../src/db' +import { openPgDb } from '../src/db-pg' +import { updateClient } from '../src/repos-clients' + +interface Row { id: string; current: string; suggested: string; changed: boolean } + +async function main() { + const path = process.env['REVIEW_JSON'] + if (path === undefined) throw new Error('Set REVIEW_JSON to the name-review.json path') + const rows = JSON.parse(readFileSync(path, 'utf8')) as Row[] + const pgUrl = process.env['DATABASE_URL'] ?? '' + const db = pgUrl !== '' ? await openPgDb(pgUrl) : openDb(process.env['HQ_DATA_DIR']) + + let applied = 0, skipped = 0 + for (const r of rows) { + if (!r.changed || r.suggested === r.current) continue + try { + // Guard: only touch a client whose stored name still matches what was reviewed. + const cur = await db.get<{ name: string }>(`SELECT name FROM client WHERE id=?`, r.id) + if (cur === undefined || cur.name !== r.current) { skipped++; continue } + await updateClient(db, 'system', r.id, { name: r.suggested }) + applied++ + } catch (e) { + skipped++ + // eslint-disable-next-line no-console + console.error(` skip ${r.current}: ${e instanceof Error ? e.message : String(e)}`) + } + } + // eslint-disable-next-line no-console + console.log(`Name corrections: ${applied} applied, ${skipped} skipped.`) +} + +main().catch((e) => { console.error(e); process.exit(1) })