WIP jordan
This commit is contained in:
61
scripts/import-forms.mjs
Normal file
61
scripts/import-forms.mjs
Normal file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Optional: convert a CSV export into app/data/forms-catalog.json
|
||||
* Usage: node scripts/import-forms.mjs path/to/forms.csv
|
||||
*
|
||||
* Expected CSV headers (example):
|
||||
* id,description,insurerSlugs,subRamoKey,subRamoLabel,personKinds,productLine,fileUrl,fileLabel,badge,kind,fieldGroupIds
|
||||
* insurerSlugs: semicolon-separated e.g. optima;mapfre
|
||||
* fieldGroupIds: semicolon-separated optional
|
||||
*/
|
||||
import { readFileSync, writeFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { dirname, join } from 'node:path'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const root = join(__dirname, '..')
|
||||
const csvPath = process.argv[2]
|
||||
|
||||
if (!csvPath) {
|
||||
console.error('Usage: node scripts/import-forms.mjs <file.csv>')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const raw = readFileSync(csvPath, 'utf8')
|
||||
const lines = raw.trim().split(/\r?\n/)
|
||||
const header = lines[0].split(',').map((h) => h.trim())
|
||||
const rows = []
|
||||
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
const cols = lines[i].split(',')
|
||||
if (cols.length < header.length) continue
|
||||
const row = {}
|
||||
header.forEach((h, idx) => {
|
||||
row[h] = (cols[idx] ?? '').trim()
|
||||
})
|
||||
rows.push({
|
||||
id: Number(row.id),
|
||||
description: row.description,
|
||||
insurerSlugs: row.insurerSlugs.split(';').map((s) => s.trim()).filter(Boolean),
|
||||
subRamoKey: row.subRamoKey,
|
||||
subRamoLabel: row.subRamoLabel,
|
||||
personKinds: row.personKinds,
|
||||
productLine: row.productLine === '' ? null : row.productLine,
|
||||
fileUrl: row.fileUrl,
|
||||
fileLabel: row.fileLabel,
|
||||
badge: row.badge ? Number(row.badge) : undefined,
|
||||
kind: row.kind || 'carrier_pdf',
|
||||
fieldGroupIds: row.fieldGroupIds
|
||||
? row.fieldGroupIds.split(';').map((s) => s.trim()).filter(Boolean)
|
||||
: undefined
|
||||
})
|
||||
}
|
||||
|
||||
const out = {
|
||||
version: 1,
|
||||
rows
|
||||
}
|
||||
|
||||
const outPath = join(root, 'app/data/forms-catalog.json')
|
||||
writeFileSync(outPath, `${JSON.stringify(out, null, 2)}\n`, 'utf8')
|
||||
console.log(`Wrote ${rows.length} rows to ${outPath}`)
|
||||
Reference in New Issue
Block a user