test-rda: add preview, ver más pagination, sent tracking, default contrato=011

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-26 19:32:53 -05:00
co-authored by Claude Sonnet 4.6
parent e477aa2bea
commit ef71a1db56
3 changed files with 233 additions and 47 deletions
+49 -4
View File
@@ -70,9 +70,21 @@ async def test_page(request: Request, user: dict = Depends(get_current_user)):
)
@router.get("/enviados")
async def get_enviados(request: Request, user: dict = Depends(get_current_user)):
db = get_connection()
rows = db.execute(
"SELECT idrecepcion, ok FROM rda_test_log ORDER BY created_at DESC"
).fetchall()
db.close()
enviados = {r["idrecepcion"]: bool(r["ok"]) for r in rows}
return JSONResponse({"enviados": enviados})
@router.get("/candidatos")
async def get_candidatos(request: Request, user: dict = Depends(get_current_user),
contrato: str = "", factura: str = "", articulos: str = ""):
contrato: str = "", factura: str = "", articulos: str = "",
offset: int = 0):
db = get_connection()
cfg = {r[0]: r[1] for r in db.execute("SELECT key, value FROM config").fetchall()}
db.close()
@@ -102,8 +114,8 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user
filtros.append("r.NUM_FACTURA = :factura")
params["factura"] = int(factura)
sql_final = """
SELECT FIRST 10
sql_final = f"""
SELECT FIRST 10 SKIP {int(offset)}
r.IDRECEPCION, r.NUM_FACTURA, r.FECHA_RECEPCION, r.COD_PACIENTE, r.NIT_EMPRESA,
e.CODCONTRATO,
LIST(DISTINCT rel.COD_EXAMEN, ', ') AS EXAMENES,
@@ -111,7 +123,7 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user
FROM RECEPCION r
JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION
LEFT JOIN EMPRESA e ON e.NIT = r.NIT_EMPRESA
WHERE """ + " AND ".join(filtros) + """
WHERE {" AND ".join(filtros)}
GROUP BY r.IDRECEPCION, r.NUM_FACTURA, r.FECHA_RECEPCION, r.COD_PACIENTE, r.NIT_EMPRESA, e.CODCONTRATO
ORDER BY r.IDRECEPCION DESC
"""
@@ -138,6 +150,32 @@ async def get_candidatos(request: Request, user: dict = Depends(get_current_user
return JSONResponse({"candidatos": candidatos})
@router.get("/preview")
async def preview_rda(request: Request, user: dict = Depends(get_current_user),
idrecepcion: int = 0):
if not idrecepcion:
return JSONResponse({"error": "Falta idrecepcion"})
db = get_connection()
cfg = {r[0]: r[1] for r in db.execute("SELECT key, value FROM config").fetchall()}
db.close()
prof_def = cfg.get("profesional_default", "XIMENA")
esp_def = cfg.get("especialidad_default", "02")
remis_def = cfg.get("remisionante_default", "00")
prefijo_def = cfg.get("prefijo_tns_default", "00")
fb, fb_ok, fb_msg = get_firebird_from_config(cfg)
if not fb_ok:
return JSONResponse({"error": fb_msg})
ok2, err2, rows_rda = fb.execute_query(
_SQL_RDA_BY_WHERE.format(where="r.IDRECEPCION = :val"), {"val": idrecepcion}
)
fb.disconnect()
if not ok2 or not rows_rda:
return JSONResponse({"error": err2 or "Sin registros"})
grupos = agrupar_por_recepcion(rows_rda)
rda_json = generar_rda_paciente(list(grupos.values())[0], prof_def, esp_def, remis_def, prefijo_def)
return JSONResponse({"json": rda_json})
@router.post("/enviar")
async def test_enviar(request: Request, user: dict = Depends(get_current_user)):
form = await request.form()
@@ -259,6 +297,13 @@ async def test_enviar(request: Request, user: dict = Depends(get_current_user)):
"respuesta_tns": raw_resp,
"json_enviado": rda_json,
})
db_log = get_connection()
db_log.execute(
"INSERT OR REPLACE INTO rda_test_log (idrecepcion, factura, contrato, ok, mensaje) VALUES (?,?,?,?,?)",
(id_rec, rda_json.get("numero", ""), rda_json.get("numeroContrato", ""), 1 if ok_rda else 0, msg_rda)
)
db_log.commit()
db_log.close()
except Exception as ex:
pasos.append({
"tipo": "rda", "idrecepcion": id_rec, "ok": False,