[object Object]

← back to Commercialrealestate

CRCP: apply asset_class=commercial filter on the snapshot path too (prod serves /api/brokers/all from snapshot) — TK-10535 desk residential-leak fix

69b35cfd7eb4e789448761d7b1729539e3cb44c7 · 2026-08-14 10:34:29 -0700 · Steve Abrams

Files touched

Diff

commit 69b35cfd7eb4e789448761d7b1729539e3cb44c7
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Fri Aug 14 10:34:29 2026 -0700

    CRCP: apply asset_class=commercial filter on the snapshot path too (prod serves /api/brokers/all from snapshot) — TK-10535 desk residential-leak fix
---
 scripts/serve.js | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/scripts/serve.js b/scripts/serve.js
index bfcb9ef..d964b2f 100644
--- a/scripts/serve.js
+++ b/scripts/serve.js
@@ -340,13 +340,16 @@ function readBrokerSnap() {
     return _brokerSnapCache;
   } catch { return null; }
 }
-function serveBrokerSnapshot(res, scope, reason) {
+function serveBrokerSnapshot(res, scope, reason, ac) {
   try {
     const snap = readBrokerSnap();
     if (!snap) throw new Error(reason || 'snapshot unavailable');
     let brokers = snap.brokers || [];
     if (scope === 'ca') brokers = brokers.filter(b => b.state === 'CA' || b.state == null);
-    return res.json({ brokers, total: brokers.length, scope, source: 'snapshot',
+    // TK-10535: honor ?asset_class=commercial on the snapshot path too (prod serves from snapshot),
+    // so the RENTV CRE desk never surfaces residential brokers.
+    if (ac === 'commercial' || ac === 'residential') brokers = brokers.filter(b => String(b.agent_type || '').toLowerCase() === ac);
+    return res.json({ brokers, total: brokers.length, scope, asset_class: ac || 'all', source: 'snapshot',
       note: scope === 'ca' ? 'California-scoped (snapshot)' : 'all states (snapshot)' });
   } catch (e) {
     return res.json({ brokers: [], total: 0, unavailable: true, reason: reason || String(e.message).split('\n')[0] });
@@ -450,11 +453,12 @@ app.get('/api/brokers/all', async (req, res) => {
   // queryable label (b.state / b.dre_match) rather than a hard filter — pass ?scope=ca to get
   // the California-only cut (state='CA' or still-unknown; hides the 22 confirmed out-of-state).
   const scope = String(req.query.scope || 'all');
-  if (!brokerdb) return serveBrokerSnapshot(res, scope, 'no db module');
   // Optional agent_type filter (TK-10535): ?asset_class=commercial restricts to commercial brokers
   // so the RENTV CRE desk never surfaces residential agents. Whitelisted literals (no SQL injection);
   // default (no param) is unchanged — CRCP's own broker-grid keeps listing all agent_types.
+  // Computed before the snapshot fallback so BOTH the live-DB and snapshot paths honor it.
   const ac = String(req.query.asset_class || req.query.type || '').toLowerCase();
+  if (!brokerdb) return serveBrokerSnapshot(res, scope, 'no db module', ac);
   const acCond = (ac === 'commercial' || ac === 'residential') ? `b.agent_type = '${ac}'` : '';
   const scopeCond = scope === 'ca' ? `(b.state = 'CA' OR b.state IS NULL)` : '';
   const _conds = [scopeCond, acCond].filter(Boolean);
@@ -470,7 +474,7 @@ app.get('/api/brokers/all', async (req, res) => {
          ${caFilter}
          ORDER BY listings DESC NULLS LAST, b.name`)).rows;
     res.json({ brokers: rows, total: rows.length, scope, asset_class: ac || 'all', note: scope === 'all' ? 'all states' : 'California-scoped (CA + unknown; 22 confirmed out-of-state hidden — use ?scope=all to see them)' });
-  } catch (e) { return serveBrokerSnapshot(res, scope, String(e.message).split('\n')[0]); }
+  } catch (e) { return serveBrokerSnapshot(res, scope, String(e.message).split('\n')[0], ac); }
 });
 
 // ── Government-licensed agents (cre.gov_licensed_agent) — multi-metro, loaded from state

← acf2903 CRCP: /api/brokers/all honors ?asset_class=commercial (agent  ·  back to Commercialrealestate  ·  auto-data-snapshot: 2026-08-14T12:24:03 (3 data files) — dat d79654c →