@ -5,9 +5,10 @@
// 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 { openDb , sqliteFilePath } from '../src/db'
import { openPgDb , pgTargetDescription , requireDbUrlForEnv } from '../src/db-pg'
import { updateClient } from '../src/repos-clients'
import type { DB } from '../src/db'
interface Res { clientId : string ; ltdNo : string ; found : boolean }
@ -16,9 +17,18 @@ async function main() {
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' ] )
// Pre-ship audit FIX A: the SAME resolver server.ts uses (never re-derived locally) — and,
// unlike the server, this one-off script hard-fails rather than silently falling back to a
// throwaway SQLite file when NODE_ENV=production resolves no Postgres target.
const pgUrl = requireDbUrlForEnv ( )
if ( pgUrl !== '' ) {
console . log ( ` [db] engine: postgres ${ pgTargetDescription ( pgUrl ) } ` )
} else {
console . log ( ` [db] engine: sqlite at ${ sqliteFilePath ( process . env [ 'HQ_DATA_DIR' ] ) } ` )
}
const db : DB = pgUrl !== '' ? await openPgDb ( pgUrl ) : openDb ( process . env [ 'HQ_DATA_DIR' ] )
try {
let applied = 0 , already = 0 , skipped = 0
const seen = new Set < string > ( )
for ( const r of results ) {
@ -37,6 +47,9 @@ async function main() {
}
// eslint-disable-next-line no-console
console . log ( ` LTD numbers: ${ applied } appended, ${ already } already present, ${ skipped } skipped (not found / no number). ` )
} finally {
await db . close ( )
}
}
main ( ) . catch ( ( e ) = > { console . error ( e ) ; process . exit ( 1 ) } )