From 9bd247219ed30fe2056d5729df79088be512541b Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Wed, 22 Jul 2026 22:47:49 -0500 Subject: [PATCH] =?UTF-8?q?fix(automation):=20skip=20RDA/preserv=20si=20el?= =?UTF-8?q?=20tercero=20del=20paciente=20fall=C3=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Acumula COD_PACIENTE en terceros_fallidos cuando paso1 falla. En paso2 y paso3, si el paciente está en esa lista se omite el envío con mensaje claro "Omitido: tercero no creado en TNS". Evita el error confuso "paciente no existe" en TNS. Co-Authored-By: Claude Sonnet 4.6 --- app/routes/automation.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/routes/automation.py b/app/routes/automation.py index a3f478c..cd6beec 100644 --- a/app/routes/automation.py +++ b/app/routes/automation.py @@ -571,6 +571,9 @@ async def run_automation( "paso4_ventas": {"enviados": 0, "errores": 0, "detalle": []}, } + # Pacientes cuyo tercero falló → no enviar RDA para ellos + terceros_fallidos: set[str] = set() + # ── PASO 1: Enviar Terceros (particulares + EPS) ───────────────────────── if "terceros" in pasos_set: async with httpx.AsyncClient(timeout=timeout) as client: @@ -594,6 +597,7 @@ async def run_automation( resultado["paso1_terceros"]["enviados"] += 1 else: resultado["paso1_terceros"]["errores"] += 1 + terceros_fallidos.add(codigo_pac) resultado["paso1_terceros"]["detalle"].append({ "codigo": codigo_pac, "doc": doc, "nombre": nombre, "ok": ok, "msg": msg, @@ -631,6 +635,17 @@ async def run_automation( if "rda" in pasos_set: async with httpx.AsyncClient(timeout=timeout) as client: for id_recepcion, grupo_rows in grupos.items(): + cod_pac = str(grupo_rows[0].get("COD_PACIENTE", "")) + if cod_pac in terceros_fallidos: + resultado["paso2_rda"]["detalle"].append({ + "idrecepcion": id_recepcion, + "factura": str(grupo_rows[0].get("NUM_FACTURA") or id_recepcion), + "paciente": cod_pac, + "examenes": len(grupo_rows), + "ok": False, "msg": "Omitido: tercero no creado en TNS", + }) + resultado["paso2_rda"]["errores"] += 1 + continue num_fac = str(grupo_rows[0].get("NUM_FACTURA") or "").strip() num_override = num_fac rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def, remis_def, prefijo_def, num_override, contrato_map=contrato_map, sin_contrato_set=sin_contrato_set) @@ -670,6 +685,17 @@ async def run_automation( if "preserv" in pasos_set: async with httpx.AsyncClient(timeout=timeout) as client: for id_ps, grupo_rows in grupos_ps.items(): + cod_pac = str(grupo_rows[0].get("COD_PACIENTE", "")) + if cod_pac in terceros_fallidos: + resultado["paso3_preserv"]["detalle"].append({ + "idrecepcion": id_ps, + "factura": str(grupo_rows[0].get("PS_NUMERO") or id_ps), + "paciente": cod_pac, + "examenes": len(grupo_rows), + "ok": False, "msg": "Omitido: tercero no creado en TNS", + }) + resultado["paso3_preserv"]["errores"] += 1 + continue ps_prefijo = str(grupo_rows[0].get("PS_PREFIJO") or "SC").strip() ps_numero = str(grupo_rows[0].get("PS_NUMERO") or "").strip() rda_json = generar_rda_paciente(