diff --git a/app/routes/automation.py b/app/routes/automation.py index 81194a2..5c83152 100644 --- a/app/routes/automation.py +++ b/app/routes/automation.py @@ -59,7 +59,7 @@ SELECT rel.PRECIO, rel.FECHA_REPORTADO, m.COD_ESPECIALIDAD, - COALESCE(NULLIF(TRIM(r.USUARIO), ''), TRIM(m.CODIGO), '') AS profesional + COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO @@ -229,10 +229,12 @@ async def run_automation( endpoint = f"{TNS_BASE}/v2/rda/RdaPaciente/Insertar" if api_sucursal: endpoint += f"?codigosucursal={api_sucursal}" + prof_def = configs.get("profesional_default", "") + esp_def = configs.get("especialidad_default", "") async with httpx.AsyncClient(timeout=timeout) as client: for id_recepcion, grupo_rows in grupos.items(): - rda_json = generar_rda_paciente(grupo_rows) + rda_json = generar_rda_paciente(grupo_rows, prof_def, esp_def) factura = str(grupo_rows[0].get("NUM_FACTURA", id_recepcion)) try: resp = await client.post(endpoint, json=rda_json, headers=headers) diff --git a/app/routes/config.py b/app/routes/config.py index 5bd8677..16616bd 100644 --- a/app/routes/config.py +++ b/app/routes/config.py @@ -23,6 +23,8 @@ DEFAULT_KEYS = [ ("tns_password", "Nicolas2796*+"), ("num_documento_obligado", ""), ("cod_prestador", ""), + ("profesional_default", ""), + ("especialidad_default", ""), ] diff --git a/app/routes/queries.py b/app/routes/queries.py index 6c9d675..a6e9ee2 100644 --- a/app/routes/queries.py +++ b/app/routes/queries.py @@ -51,7 +51,7 @@ WHERE p.DOCIDENT = :doc_num""", rel.PRECIO, rel.FECHA_REPORTADO, m.COD_ESPECIALIDAD, - COALESCE(NULLIF(TRIM(r.USUARIO), ''), TRIM(m.CODIGO), '') AS profesional + COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO @@ -79,7 +79,7 @@ WHERE r.NUM_FACTURA = :num_factura""", rel.PRECIO, rel.FECHA_REPORTADO, m.COD_ESPECIALIDAD, - COALESCE(NULLIF(TRIM(r.USUARIO), ''), TRIM(m.CODIGO), '') AS profesional + COALESCE(NULLIF(TRIM(m.CODIGO), ''), NULLIF(TRIM(r.USUARIO), ''), '') AS profesional FROM RECEPCION r JOIN RELACION rel ON rel.IDRECEPCION = r.IDRECEPCION LEFT JOIN MEDICO m ON m.CODIGO = r.COD_MEDICO @@ -101,6 +101,11 @@ def ensure_defaults(): "INSERT INTO queries (name, query_type, query_text, description) VALUES (?, ?, ?, ?)", (q["name"], q["query_type"], q["query_text"], q["description"]), ) + else: + conn.execute( + "UPDATE queries SET query_text = ?, description = ? WHERE name = ?", + (q["query_text"], q["description"], q["name"]), + ) conn.commit() conn.close() diff --git a/app/routes/transaccion.py b/app/routes/transaccion.py index 6841766..0d756bd 100644 --- a/app/routes/transaccion.py +++ b/app/routes/transaccion.py @@ -76,7 +76,9 @@ async def preview_transaccion( return JSONResponse({"success": False, "message": error}) grupos = agrupar_por_recepcion(rows) - json_result = [generar_rda_paciente(grupo) for grupo in grupos.values()] + prof_def = configs.get("profesional_default", "") + esp_def = configs.get("especialidad_default", "") + json_result = [generar_rda_paciente(grupo, prof_def, esp_def) for grupo in grupos.values()] return JSONResponse({ "success": True, @@ -153,10 +155,12 @@ async def send_transaccion( total_enviados = 0 total_errores = 0 resultados = [] + prof_def = configs.get("profesional_default", "") + esp_def = configs.get("especialidad_default", "") async with httpx.AsyncClient(timeout=int(configs.get("api_timeout", 30))) as client: for id_recepcion, grupo_rows in grupos.items(): - trans_json = generar_rda_paciente(grupo_rows) + trans_json = generar_rda_paciente(grupo_rows, prof_def, esp_def) status_ok = False response_text = "" diff --git a/app/services/json_generator.py b/app/services/json_generator.py index 6975a23..ac966ca 100644 --- a/app/services/json_generator.py +++ b/app/services/json_generator.py @@ -96,7 +96,7 @@ def agrupar_por_recepcion(rows: list) -> dict: return grupos -def generar_rda_paciente(rows: list) -> dict: +def generar_rda_paciente(rows: list, default_profesional: str = "", default_especialidad: str = "") -> dict: if not rows: return {} h = rows[0] @@ -126,8 +126,8 @@ def generar_rda_paciente(rows: list) -> dict: "porcentajeIva": 0, "impConsumo": 0, "observacion": "", - "profesional": str(h.get("profesional") or "").strip(), - "especialidad": (lambda e: e if e not in (".", "-", "0", "00") else "")(str(row.get("COD_ESPECIALIDAD") or "").strip()), + "profesional": str(row.get("profesional") or "").strip() or default_profesional, + "especialidad": (lambda e: e if e not in (".", "-", "0", "00") else default_especialidad)(str(row.get("COD_ESPECIALIDAD") or "").strip()), "diagnosticoprincipal": str(h.get("DIAG_PPAL") or "").strip(), "fechaHoraRealizacion": fecha_real or egreso, })