diff --git a/app/routes/debug_fb.py b/app/routes/debug_fb.py index 52be313..f3ba75c 100644 --- a/app/routes/debug_fb.py +++ b/app/routes/debug_fb.py @@ -1,5 +1,5 @@ import traceback -from fastapi import APIRouter, Request, Depends +from fastapi import APIRouter, Request, Depends, Query from fastapi.responses import JSONResponse from app.database import get_connection from app.auth import get_current_user @@ -8,6 +8,65 @@ from app.services.firebird_service import get_firebird_from_config router = APIRouter(prefix="/debug-fb", tags=["debug"]) +@router.get("/diagnosticos") +async def listar_diagnosticos( + q: str = Query(default="", description="Filtro por código o concepto"), + limit: int = Query(default=100, ge=1, le=5000), + user: dict = Depends(get_current_user), +): + """Lista diagnósticos CIE-10 desde Firebird DIAGNOSTICO.""" + try: + conn = get_connection() + cfg = {r["key"]: r["value"] for r in conn.execute("SELECT * FROM config").fetchall()} + conn.close() + + fb, ok, msg = get_firebird_from_config(cfg) + if not ok: + return JSONResponse({"ok": False, "error": msg}) + + # Estructura de la tabla + ok_c, _, cols = fb.execute_query( + "SELECT TRIM(f.RDB$FIELD_NAME) AS col " + "FROM RDB$RELATION_FIELDS f " + "WHERE TRIM(f.RDB$RELATION_NAME) = 'DIAGNOSTICO' " + "ORDER BY f.RDB$FIELD_POSITION", None + ) + + # Conteo total + ok_n, _, cnt = fb.execute_query( + "SELECT COUNT(*) AS TOTAL FROM DIAGNOSTICO", None + ) + + # Registros + if q: + q_up = q.upper() + ok_r, _, rows = fb.execute_query( + f"SELECT FIRST {limit} TRIM(d.COD_DIAG) AS cod, TRIM(d.CONCEPTO) AS concepto " + f"FROM DIAGNOSTICO d " + f"WHERE UPPER(TRIM(d.COD_DIAG)) CONTAINING '{q_up}' " + f" OR UPPER(TRIM(d.CONCEPTO)) CONTAINING '{q_up}' " + f"ORDER BY d.COD_DIAG", None + ) + else: + ok_r, _, rows = fb.execute_query( + f"SELECT FIRST {limit} TRIM(d.COD_DIAG) AS cod, TRIM(d.CONCEPTO) AS concepto " + f"FROM DIAGNOSTICO d ORDER BY d.COD_DIAG", None + ) + + fb.disconnect() + + return JSONResponse({ + "ok": True, + "total": cnt[0]["TOTAL"] if ok_n and cnt else "?", + "columnas": [c["col"] for c in cols] if ok_c and cols else [], + "limite": limit, + "filtro": q or None, + "rows": [{"cod": r["cod"], "concepto": r["concepto"]} for r in (rows or [])], + }) + except Exception: + return JSONResponse({"ok": False, "traceback": traceback.format_exc()}) + + @router.get("/recepcion") async def debug_recepcion(user: dict = Depends(get_current_user)): """Diagnóstico: qué hay en RECEPCION hoy sin filtros restrictivos."""