From 068739cb20269b4e6d17d33c42fab002eaaa4a02 Mon Sep 17 00:00:00 2001 From: justin Date: Thu, 28 May 2026 23:37:45 -0500 Subject: [PATCH] Add FMCSA API fallback for name search when local DB is empty Tries local fmcsa_carriers table first (ILIKE partial match). If no results, falls back to FMCSA QCMobile API name search. Ensures name search works even before full 2M census is loaded. Co-Authored-By: Claude Opus 4.6 (1M context) --- api/src/routes/dot-lookup.ts | 50 ++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/api/src/routes/dot-lookup.ts b/api/src/routes/dot-lookup.ts index cd5018f..7a0a205 100644 --- a/api/src/routes/dot-lookup.ts +++ b/api/src/routes/dot-lookup.ts @@ -322,6 +322,7 @@ router.get("/api/v1/dot/search", async (req, res) => { } try { + // Try local database first const result = await pool.query( `SELECT dot_number, legal_name, dba_name, phy_city, phy_state, nbr_power_unit, authorized_for_hire, mcs150_parsed @@ -332,9 +333,54 @@ router.get("/api/v1/dot/search", async (req, res) => { [`%${name}%`], ); + if (result.rows.length > 0) { + res.json({ + results: result.rows, + count: result.rows.length, + query: name, + source: "FMCSA Motor Carrier Census", + }); + return; + } + + // Fallback: FMCSA QCMobile API name search + if (FMCSA_API_KEY) { + try { + const apiResp = await fetch( + `${FMCSA_BASE}/name/${encodeURIComponent(name)}?webKey=${FMCSA_API_KEY}`, + { signal: AbortSignal.timeout(10000), headers: { Accept: "application/json" } }, + ); + if (apiResp.ok) { + const apiData = await apiResp.json(); + const carriers = (apiData.content || []).map((item: any) => { + const c = item.carrier || item; + return { + dot_number: String(c.dotNumber || ""), + legal_name: c.legalName || "", + dba_name: c.dbaName || null, + phy_city: c.phyCity || null, + phy_state: c.phyState || null, + nbr_power_unit: c.totalPowerUnits || null, + authorized_for_hire: c.allowedToOperate === "Y", + mcs150_parsed: null, + }; + }); + res.json({ + results: carriers.slice(0, 50), + count: carriers.length, + query: name, + source: "FMCSA API (live)", + }); + return; + } + } catch { + // API fallback failed — return empty + } + } + res.json({ - results: result.rows, - count: result.rows.length, + results: [], + count: 0, query: name, source: "FMCSA Motor Carrier Census", });