Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
62 lines
2.3 KiB
Python
62 lines
2.3 KiB
Python
import traceback
|
|
from fastapi import APIRouter, Request, Depends
|
|
from fastapi.responses import JSONResponse
|
|
from app.database import get_connection
|
|
from app.auth import get_current_user
|
|
from app.services.firebird_service import get_firebird_from_config
|
|
|
|
router = APIRouter(prefix="/debug-fb", tags=["debug"])
|
|
|
|
|
|
@router.get("/cups")
|
|
async def find_cups(request: Request, user: dict = Depends(get_current_user)):
|
|
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({"error": msg})
|
|
|
|
results = {}
|
|
|
|
BUSCAR = "903967"
|
|
|
|
# Tablas y sus campos de texto para buscar
|
|
tablas_candidatas = [
|
|
("PROTOCOLO", ["CODIGO", "NOMBRE"]),
|
|
("ITEM", ["COD_PROTOCOLO", "CUPS_DETALLE", "CODITEMINTERFACE"]),
|
|
("RELACION", ["COD_EXAMEN"]),
|
|
("RELACION_HIMS", ["CUPS_REF"]),
|
|
("ARTICULO", ["CODIGO", "CUPS"]),
|
|
("SERVICIO", ["CODIGO", "CUPS", "COD_CUPS"]),
|
|
("PROCEDIMIENTO", ["CODIGO", "CUPS"]),
|
|
]
|
|
|
|
encontrado = {}
|
|
for tabla, campos in tablas_candidatas:
|
|
for campo in campos:
|
|
ok_t, _, rows_t = fb.execute_query(
|
|
f"SELECT FIRST 3 t.* FROM {tabla} t "
|
|
f"WHERE TRIM(t.{campo}) = '{BUSCAR}'"
|
|
)
|
|
if ok_t and rows_t:
|
|
encontrado[f"{tabla}.{campo}"] = list(rows_t)
|
|
|
|
results["valor_903967_encontrado_en"] = encontrado if encontrado else "no encontrado en tablas comunes"
|
|
|
|
# Buscar en campos de tipo char/varchar de PROTOCOLO e ITEM que no conocemos
|
|
for tabla in ["PROTOCOLO", "ITEM"]:
|
|
ok_c, _, rows_c = fb.execute_query(
|
|
f"SELECT TRIM(f.RDB$FIELD_NAME) AS c FROM RDB$RELATION_FIELDS f "
|
|
f"WHERE TRIM(f.RDB$RELATION_NAME)='{tabla}' ORDER BY f.RDB$FIELD_POSITION"
|
|
)
|
|
if ok_c:
|
|
results[f"columnas_{tabla.lower()}"] = [r["c"] for r in rows_c]
|
|
|
|
fb.disconnect()
|
|
return JSONResponse(results)
|
|
except Exception:
|
|
return JSONResponse({"traceback": traceback.format_exc()})
|