← back to Commercialrealestate
scripts/export-brokers-snapshot.js
36 lines
#!/usr/bin/env node
/*
* export-brokers-snapshot.js — dump the broker grid's data to data/brokers-snapshot.json so
* PROD (crcp.agentabrams.com, which has NO Postgres) can serve /api/brokers/all from the
* snapshot, exactly like /api/residential does. Same columns the live route selects.
* Run locally (where the `cre` DB lives), then deploy the snapshot + serve.js.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const { pool, graph, topBrokers, enrichStats } = require('./db/brokers-db');
async function main() {
const brokers = (await pool.query(
`SELECT b.id, b.name, f.name firm, b.agent_type, b.phone, b.email, b.website, b.linkedin,
b.license, b.title, b.total_assets, b.specialties, b.office_addr, b.crexi_id, b.source, b.created_at,
b.state, b.dre_match, b.dre_license,
(SELECT count(*) FROM broker_listing bl WHERE bl.broker_id=b.id)
+ (SELECT count(*) FROM broker_condo bc WHERE bc.broker_id=b.id) AS listings
FROM broker b LEFT JOIN firm f ON f.id=b.firm_id
ORDER BY listings DESC NULLS LAST, b.name`)).rows;
// Also snapshot the mind-map page's endpoints (brokers.html): /api/graph, /api/brokers/top,
// /api/brokers/enrich-stats. Reuse the live functions so the shapes are byte-identical to the
// DB path — prod (no Postgres) serves these from the snapshot exactly like /api/brokers/all.
const graphData = await graph(400);
const top = await topBrokers(50);
const enrich = await enrichStats();
const out = { brokers, total: brokers.length, top, graph: graphData, enrichStats: enrich,
source: 'snapshot', exported_at: new Date().toISOString() };
const dest = path.join(__dirname, '..', 'data', 'brokers-snapshot.json');
fs.writeFileSync(dest, JSON.stringify(out));
console.log(`✔ wrote ${dest} — ${brokers.length} brokers (${(fs.statSync(dest).size/1e6).toFixed(1)}MB)`);
await pool.end();
}
if (require.main === module) main().catch(e => { console.error(e); process.exit(1); });