diff --git a/backend/app/api/admin.py b/backend/app/api/admin.py index 9acc227..12c1276 100644 --- a/backend/app/api/admin.py +++ b/backend/app/api/admin.py @@ -473,9 +473,18 @@ def _test_rep_lookup() -> dict: r2.raise_for_status() results = r2.json().get("results", []) for item in results: + if "Congressional" not in (item.get("layerName") or ""): + continue attrs = item.get("attributes", {}) - cd_field = next((k for k in attrs if _re.match(r"CD\d+FP$", k)), None) - if cd_field: + # Primary: GEOID = state FIPS (2) + district (2) + geoid = str(attrs.get("GEOID") or "").strip() + if len(geoid) == 4 and geoid.isdigit(): + district = str(int(geoid[2:])) if geoid[2:].strip("0") else "At-large" + return {"status": "ok", "detail": f"Nominatim + TIGERweb reachable — district {district} found for ZIP 20001"} + # Fallback: STATE + CD{session} (e.g. STATE="06", CD119="30") + state_val = attrs.get("STATE") + cd_field = next((k for k in attrs if _re.match(r"CD\d+$", k)), None) + if state_val and cd_field: district = str(int(str(attrs[cd_field]))) if str(attrs[cd_field]).strip("0") else "At-large" return {"status": "ok", "detail": f"Nominatim + TIGERweb reachable — district {district} found for ZIP 20001"} layers = [r.get("layerName") for r in results]