Mateo Ferreyra commited on
Commit ·
7f24904
1
Parent(s): f485d1b
Alta de cama: cierre en Facturacion (M7) y baja de Monitoreo (M9) antes del cambio de estado
Browse files- procesar_alta_medica ahora llama a facturacion.notify_cierre_cama y monitoreo.notify_baja
ANTES de cambiar el estado de la cama; si cualquiera falla se devuelve 502 y no se da el alta.
- Nueva funcion facturacion.notify_cierre_cama (con print de simulacion en modo mock).
- monitoreo.notify_baja imprime la request simulada en modo mock.
- Test test_alta_medica_ok: el mock de notify_baja ahora devuelve True (nuevo contrato).
- app/integrations/facturacion.py +26 -0
- app/integrations/monitoreo.py +1 -0
- app/services/solicitud_service.py +14 -2
- tests/test_webhooks.py +1 -1
app/integrations/facturacion.py
CHANGED
|
@@ -32,3 +32,29 @@ def notify_cama_lista(cama_id: int, paciente_id: int) -> bool:
|
|
| 32 |
return _post(
|
| 33 |
f"{settings.m7_facturacion_url}/api/v1/webhooks/internacion/cama-lista", payload
|
| 34 |
) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return _post(
|
| 33 |
f"{settings.m7_facturacion_url}/api/v1/webhooks/internacion/cama-lista", payload
|
| 34 |
) is not None
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def notify_cierre_cama(cama_id: int, paciente_id: int) -> bool:
|
| 38 |
+
"""
|
| 39 |
+
Avisa a M7 que debe cerrar la facturación de la cama tras un alta médica.
|
| 40 |
+
|
| 41 |
+
Se invoca ANTES del cambio de estado de la cama: el alta depende de que M7 confirme
|
| 42 |
+
el cierre. `idEventoExterno` es la clave de idempotencia (cama + paciente + fecha).
|
| 43 |
+
"""
|
| 44 |
+
if settings.mock_external_services:
|
| 45 |
+
print(
|
| 46 |
+
f"[SIMULACION] Request a M7 Facturación → cierre de cama "
|
| 47 |
+
f"{cama_id} (paciente {paciente_id})"
|
| 48 |
+
)
|
| 49 |
+
return True
|
| 50 |
+
id_evento_externo = f"M6-CIERRE-CAMA-{cama_id}-{paciente_id}-{date.today().isoformat()}"
|
| 51 |
+
payload = {
|
| 52 |
+
"evento": "CIERRE_CAMA",
|
| 53 |
+
"idEventoExterno": id_evento_externo,
|
| 54 |
+
"cama_id": cama_id,
|
| 55 |
+
"paciente_id": paciente_id,
|
| 56 |
+
"timestamp_evento": datetime.now(timezone.utc).isoformat(),
|
| 57 |
+
}
|
| 58 |
+
return _post(
|
| 59 |
+
f"{settings.m7_facturacion_url}/api/v1/webhooks/internacion/cierre-cama", payload
|
| 60 |
+
) is not None
|
app/integrations/monitoreo.py
CHANGED
|
@@ -21,6 +21,7 @@ def notify_alta(paciente_id: int) -> bool:
|
|
| 21 |
|
| 22 |
def notify_baja(paciente_id: int) -> bool:
|
| 23 |
if settings.mock_external_services:
|
|
|
|
| 24 |
return True
|
| 25 |
payload = {
|
| 26 |
"evento": "BAJA_MONITOREO",
|
|
|
|
| 21 |
|
| 22 |
def notify_baja(paciente_id: int) -> bool:
|
| 23 |
if settings.mock_external_services:
|
| 24 |
+
print(f"[SIMULACION] Request a M9 Monitoreo → baja de paciente {paciente_id}")
|
| 25 |
return True
|
| 26 |
payload = {
|
| 27 |
"evento": "BAJA_MONITOREO",
|
app/services/solicitud_service.py
CHANGED
|
@@ -14,7 +14,7 @@ from app.schemas.enums import EstadoCamaEnum, EstadoReservaEnum
|
|
| 14 |
from app.crud import solicitud as solicitud_crud
|
| 15 |
from app.crud import cama as cama_crud
|
| 16 |
from app.crud import reserva as reserva_crud
|
| 17 |
-
from app.integrations import hce, monitoreo, core
|
| 18 |
|
| 19 |
|
| 20 |
_ERR_NOT_FOUND = {"codigo": "RECURSO_NO_ENCONTRADO", "mensaje": "Solicitud no encontrada"}
|
|
@@ -227,11 +227,23 @@ def procesar_alta_medica(db: Client, paciente_id: int) -> Cama:
|
|
| 227 |
status_code=404,
|
| 228 |
detail={"codigo": "RECURSO_NO_ENCONTRADO", "mensaje": "No se encontró cama asignada al paciente"},
|
| 229 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
cama.estado = EstadoCamaEnum.EN_LIMPIEZA.value
|
| 231 |
cama.ultimo_movimiento_tipo = "ALTA"
|
| 232 |
cama.ultima_actualizacion = datetime.now(timezone.utc)
|
| 233 |
cama_crud.save(db, cama)
|
| 234 |
-
monitoreo.notify_baja(paciente_id)
|
| 235 |
return cama
|
| 236 |
|
| 237 |
|
|
|
|
| 14 |
from app.crud import solicitud as solicitud_crud
|
| 15 |
from app.crud import cama as cama_crud
|
| 16 |
from app.crud import reserva as reserva_crud
|
| 17 |
+
from app.integrations import hce, monitoreo, core, facturacion
|
| 18 |
|
| 19 |
|
| 20 |
_ERR_NOT_FOUND = {"codigo": "RECURSO_NO_ENCONTRADO", "mensaje": "Solicitud no encontrada"}
|
|
|
|
| 227 |
status_code=404,
|
| 228 |
detail={"codigo": "RECURSO_NO_ENCONTRADO", "mensaje": "No se encontró cama asignada al paciente"},
|
| 229 |
)
|
| 230 |
+
# El cierre de facturación y la baja de monitoreo deben completarse ANTES de cambiar
|
| 231 |
+
# el estado de la cama: el alta depende de que ambos terceros confirmen.
|
| 232 |
+
if not facturacion.notify_cierre_cama(cama_id=cama.id, paciente_id=paciente_id):
|
| 233 |
+
raise HTTPException(
|
| 234 |
+
status_code=502,
|
| 235 |
+
detail={"codigo": "SERVICIO_EXTERNO_NO_DISPONIBLE", "mensaje": "No se pudo cerrar la cama en Facturación (M7)"},
|
| 236 |
+
)
|
| 237 |
+
if not monitoreo.notify_baja(paciente_id):
|
| 238 |
+
raise HTTPException(
|
| 239 |
+
status_code=502,
|
| 240 |
+
detail={"codigo": "SERVICIO_EXTERNO_NO_DISPONIBLE", "mensaje": "No se pudo dar de baja el monitoreo (M9)"},
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
cama.estado = EstadoCamaEnum.EN_LIMPIEZA.value
|
| 244 |
cama.ultimo_movimiento_tipo = "ALTA"
|
| 245 |
cama.ultima_actualizacion = datetime.now(timezone.utc)
|
| 246 |
cama_crud.save(db, cama)
|
|
|
|
| 247 |
return cama
|
| 248 |
|
| 249 |
|
tests/test_webhooks.py
CHANGED
|
@@ -21,7 +21,7 @@ def test_alta_medica_ok(client: TestClient, admin_headers, monkeypatch):
|
|
| 21 |
cama = make_cama(estado="OCUPADA", paciente_id=7)
|
| 22 |
monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama)
|
| 23 |
monkeypatch.setattr("app.crud.cama.save", lambda db, c: c)
|
| 24 |
-
monkeypatch.setattr("app.integrations.monitoreo.notify_baja", lambda *a, **k:
|
| 25 |
payload = {
|
| 26 |
"paciente_id": 7,
|
| 27 |
"medico_firmante_id": 10,
|
|
|
|
| 21 |
cama = make_cama(estado="OCUPADA", paciente_id=7)
|
| 22 |
monkeypatch.setattr("app.crud.cama.get_by_paciente", lambda db, pid, estados: cama)
|
| 23 |
monkeypatch.setattr("app.crud.cama.save", lambda db, c: c)
|
| 24 |
+
monkeypatch.setattr("app.integrations.monitoreo.notify_baja", lambda *a, **k: True)
|
| 25 |
payload = {
|
| 26 |
"paciente_id": 7,
|
| 27 |
"medico_firmante_id": 10,
|