Merge pull request #18 from MateFerreyra/jps/healthcheck
Browse filesHealthcheck general repo, eliminacion logica erronea / duplicada
- app/services/_common.py +33 -0
- app/services/cama_service.py +2 -0
- app/services/solicitud_service.py +5 -8
- tests/test_camas.py +14 -0
- tests/test_solicitudes.py +24 -0
app/services/_common.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Helpers de dominio compartidos por los services (`cama_service`, `solicitud_service`).
|
| 3 |
+
|
| 4 |
+
Extraído para no triplicar la validación de paciente contra el Core en cada camino que
|
| 5 |
+
ocupa una cama (internación, traslado, asignación directa) — health check M6 §A2.
|
| 6 |
+
"""
|
| 7 |
+
import logging
|
| 8 |
+
|
| 9 |
+
from fastapi import HTTPException
|
| 10 |
+
|
| 11 |
+
from app.integrations import core
|
| 12 |
+
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def validar_paciente_o_502(paciente_id: int) -> None:
|
| 17 |
+
"""
|
| 18 |
+
Valida que el paciente exista en el Core (M10) antes de asignarle una cama. Se aplica
|
| 19 |
+
en los tres caminos que ocupan una cama (internación, traslado, asignación directa) —
|
| 20 |
+
ningún paciente debe internarse/trasladarse sin existir en el maestro del Core.
|
| 21 |
+
|
| 22 |
+
En `mock_external_services=True`, `core.validate_patient` devuelve un paciente ficticio
|
| 23 |
+
(truthy), así que este chequeo es transparente en dev/tests.
|
| 24 |
+
"""
|
| 25 |
+
if not core.validate_patient(paciente_id):
|
| 26 |
+
logger.warning("Validación de paciente fallida en Core — paciente_id=%s", paciente_id)
|
| 27 |
+
raise HTTPException(
|
| 28 |
+
status_code=502,
|
| 29 |
+
detail={
|
| 30 |
+
"codigo": "SERVICIO_EXTERNO_NO_DISPONIBLE",
|
| 31 |
+
"mensaje": "No se pudo validar el paciente en Core (M10)",
|
| 32 |
+
},
|
| 33 |
+
)
|
app/services/cama_service.py
CHANGED
|
@@ -26,6 +26,7 @@ from app.crud import reserva as reserva_crud
|
|
| 26 |
from app.crud import auditoria as auditoria_crud
|
| 27 |
from app.integrations import hce, monitoreo, turnos
|
| 28 |
from app.schemas.enums import EntidadAuditoriaEnum, TipoEventoAuditoriaEnum, OrigenAuditoriaEnum
|
|
|
|
| 29 |
|
| 30 |
logger = logging.getLogger(__name__)
|
| 31 |
|
|
@@ -246,6 +247,7 @@ def asignar_cama(
|
|
| 246 |
},
|
| 247 |
)
|
| 248 |
|
|
|
|
| 249 |
_verificar_contraindicaciones(paciente_id, cama_destino)
|
| 250 |
|
| 251 |
if tipo == "TRASLADO":
|
|
|
|
| 26 |
from app.crud import auditoria as auditoria_crud
|
| 27 |
from app.integrations import hce, monitoreo, turnos
|
| 28 |
from app.schemas.enums import EntidadAuditoriaEnum, TipoEventoAuditoriaEnum, OrigenAuditoriaEnum
|
| 29 |
+
from app.services._common import validar_paciente_o_502
|
| 30 |
|
| 31 |
logger = logging.getLogger(__name__)
|
| 32 |
|
|
|
|
| 247 |
},
|
| 248 |
)
|
| 249 |
|
| 250 |
+
validar_paciente_o_502(paciente_id)
|
| 251 |
_verificar_contraindicaciones(paciente_id, cama_destino)
|
| 252 |
|
| 253 |
if tipo == "TRASLADO":
|
app/services/solicitud_service.py
CHANGED
|
@@ -16,7 +16,8 @@ from app.crud import solicitud as solicitud_crud
|
|
| 16 |
from app.crud import cama as cama_crud
|
| 17 |
from app.crud import reserva as reserva_crud
|
| 18 |
from app.crud import auditoria as auditoria_crud
|
| 19 |
-
from app.integrations import hce, monitoreo,
|
|
|
|
| 20 |
from app.schemas.enums import (
|
| 21 |
EntidadAuditoriaEnum,
|
| 22 |
TipoEventoAuditoriaEnum,
|
|
@@ -201,13 +202,7 @@ def _aprobar_internacion(
|
|
| 201 |
"Aprobando internación — solicitud_id=%s paciente_id=%s cama_id=%s",
|
| 202 |
sol.solicitud_id, sol.paciente_id, cama_id,
|
| 203 |
)
|
| 204 |
-
|
| 205 |
-
if not paciente:
|
| 206 |
-
logger.warning("Validación de paciente fallida en Core — paciente_id=%s", sol.paciente_id)
|
| 207 |
-
raise HTTPException(
|
| 208 |
-
status_code=502,
|
| 209 |
-
detail={"codigo": "SERVICIO_EXTERNO_NO_DISPONIBLE", "mensaje": "No se pudo validar el paciente en Core (M10)"},
|
| 210 |
-
)
|
| 211 |
|
| 212 |
cama = _get_cama_or_404(db, cama_id)
|
| 213 |
if cama.estado != EstadoCamaEnum.DISPONIBLE.value:
|
|
@@ -282,6 +277,8 @@ def _aprobar_traslado(
|
|
| 282 |
detail={"codigo": "ERROR_VALIDACION", "mensaje": "Se requiere cama destino"},
|
| 283 |
)
|
| 284 |
|
|
|
|
|
|
|
| 285 |
cama_destino = _get_cama_or_404(db, dest_id)
|
| 286 |
if cama_destino.estado != EstadoCamaEnum.DISPONIBLE.value:
|
| 287 |
raise HTTPException(
|
|
|
|
| 16 |
from app.crud import cama as cama_crud
|
| 17 |
from app.crud import reserva as reserva_crud
|
| 18 |
from app.crud import auditoria as auditoria_crud
|
| 19 |
+
from app.integrations import hce, monitoreo, facturacion
|
| 20 |
+
from app.services._common import validar_paciente_o_502
|
| 21 |
from app.schemas.enums import (
|
| 22 |
EntidadAuditoriaEnum,
|
| 23 |
TipoEventoAuditoriaEnum,
|
|
|
|
| 202 |
"Aprobando internación — solicitud_id=%s paciente_id=%s cama_id=%s",
|
| 203 |
sol.solicitud_id, sol.paciente_id, cama_id,
|
| 204 |
)
|
| 205 |
+
validar_paciente_o_502(sol.paciente_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
cama = _get_cama_or_404(db, cama_id)
|
| 208 |
if cama.estado != EstadoCamaEnum.DISPONIBLE.value:
|
|
|
|
| 277 |
detail={"codigo": "ERROR_VALIDACION", "mensaje": "Se requiere cama destino"},
|
| 278 |
)
|
| 279 |
|
| 280 |
+
validar_paciente_o_502(sol.paciente_id)
|
| 281 |
+
|
| 282 |
cama_destino = _get_cama_or_404(db, dest_id)
|
| 283 |
if cama_destino.estado != EstadoCamaEnum.DISPONIBLE.value:
|
| 284 |
raise HTTPException(
|
tests/test_camas.py
CHANGED
|
@@ -179,3 +179,17 @@ def test_asignar_cama_traslado_ok(client: TestClient, admin_headers, monkeypatch
|
|
| 179 |
data = resp.json()
|
| 180 |
assert data["tipo"] == "TRASLADO"
|
| 181 |
assert data["cama_origen_id"] == 101
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
data = resp.json()
|
| 180 |
assert data["tipo"] == "TRASLADO"
|
| 181 |
assert data["cama_origen_id"] == 101
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def test_asignar_cama_paciente_invalido_en_core_502(client: TestClient, admin_headers, monkeypatch):
|
| 185 |
+
"""Health check M6 §A2: asignar_cama valida el paciente contra el Core igual que
|
| 186 |
+
la internación — si el Core no lo reconoce, no se asigna la cama."""
|
| 187 |
+
monkeypatch.setattr("app.crud.cama.get_by_id", lambda db, cid: make_cama(estado="DISPONIBLE"))
|
| 188 |
+
monkeypatch.setattr("app.integrations.core.validate_patient", lambda pid: None)
|
| 189 |
+
resp = client.post(
|
| 190 |
+
f"{PREFIX}/101/asignar",
|
| 191 |
+
json={"tipo": "INGRESO", "paciente_id": 999},
|
| 192 |
+
headers=admin_headers,
|
| 193 |
+
)
|
| 194 |
+
assert resp.status_code == 502
|
| 195 |
+
assert resp.json()["detail"]["codigo"] == "SERVICIO_EXTERNO_NO_DISPONIBLE"
|
tests/test_solicitudes.py
CHANGED
|
@@ -8,6 +8,8 @@ tests dedicados que mockean la capa de integraciones.
|
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
from unittest.mock import MagicMock
|
| 10 |
|
|
|
|
|
|
|
| 11 |
from fastapi.testclient import TestClient
|
| 12 |
|
| 13 |
from app.services import solicitud_service
|
|
@@ -167,6 +169,28 @@ def test_aprobar_traslado_hereda_episodio_de_la_cama_origen(monkeypatch):
|
|
| 167 |
assert cama_destino.fecha_ingreso == fecha_ingreso_original
|
| 168 |
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
def test_resolver_solicitud_rechazar_ok(client: TestClient, admin_headers, monkeypatch):
|
| 171 |
sol = make_solicitud()
|
| 172 |
monkeypatch.setattr("app.crud.solicitud.get_by_id", lambda db, num: sol)
|
|
|
|
| 8 |
from datetime import datetime, timezone
|
| 9 |
from unittest.mock import MagicMock
|
| 10 |
|
| 11 |
+
import pytest
|
| 12 |
+
from fastapi import HTTPException
|
| 13 |
from fastapi.testclient import TestClient
|
| 14 |
|
| 15 |
from app.services import solicitud_service
|
|
|
|
| 169 |
assert cama_destino.fecha_ingreso == fecha_ingreso_original
|
| 170 |
|
| 171 |
|
| 172 |
+
def test_aprobar_traslado_paciente_invalido_en_core_502(monkeypatch):
|
| 173 |
+
"""Health check M6 §A2: el traslado también valida el paciente contra el Core,
|
| 174 |
+
igual que la internación — antes de esta corrección no se validaba."""
|
| 175 |
+
cama_origen = make_cama(id=101, estado="OCUPADA")
|
| 176 |
+
cama_destino = make_cama(id=202, estado="DISPONIBLE")
|
| 177 |
+
sol = make_solicitud(
|
| 178 |
+
tipo="TRASLADO", solicitud_hce_id="HCE-SOL-TRASLADO", cama_origen_id=101, cama_destino_solicitada_id=202,
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
def _get_cama(db, cid):
|
| 182 |
+
return cama_origen if cid == 101 else cama_destino
|
| 183 |
+
|
| 184 |
+
monkeypatch.setattr("app.crud.cama.get_by_id", _get_cama)
|
| 185 |
+
monkeypatch.setattr("app.integrations.core.validate_patient", lambda pid: None)
|
| 186 |
+
|
| 187 |
+
with pytest.raises(HTTPException) as exc_info:
|
| 188 |
+
solicitud_service._aprobar_traslado(MagicMock(), sol, None, None, resuelto_por=1, accion="APROBAR")
|
| 189 |
+
|
| 190 |
+
assert exc_info.value.status_code == 502
|
| 191 |
+
assert exc_info.value.detail["codigo"] == "SERVICIO_EXTERNO_NO_DISPONIBLE"
|
| 192 |
+
|
| 193 |
+
|
| 194 |
def test_resolver_solicitud_rechazar_ok(client: TestClient, admin_headers, monkeypatch):
|
| 195 |
sol = make_solicitud()
|
| 196 |
monkeypatch.setattr("app.crud.solicitud.get_by_id", lambda db, num: sol)
|