File size: 2,423 Bytes
5f12c93
 
 
 
 
9f05894
5f12c93
 
 
efe5cdf
9f05894
 
efe5cdf
 
 
 
9f05894
5f12c93
 
 
 
 
efe5cdf
5f12c93
 
5ffed1d
 
 
 
 
 
 
5f12c93
 
 
 
efe5cdf
 
 
 
 
 
 
5f12c93
7f24904
5f12c93
 
 
 
efe5cdf
5f12c93
 
5ffed1d
 
 
 
 
 
 
5f12c93
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
M9 Monitoreo — altas y bajas de pacientes en monitoreo.
"""
from datetime import datetime, timezone
from app.config import settings
from app.integrations import core_events
from app.integrations._base import _post


def notify_alta(paciente_id: int, cama_id: int | None = None) -> bool:
    """
    Además del webhook REST directo (sin cambios), publica el mismo evento al bus del
    Core (ALTA_MONITOREO, event_type_id=26) vía `core_events.publish_event`.

    `cama_id`: la cama a la que se asocia el ingreso a monitoreo — la cama destino en un
    ingreso o traslado (ver call sites en `solicitud_service.py`/`cama_service.py`).
    """
    if settings.mock_external_services:
        return True
    payload = {
        "evento": "ALTA_MONITOREO",
        "paciente_id": paciente_id,
        "cama_id": cama_id,
        "timestamp": datetime.now(timezone.utc).isoformat(),
    }
    # Canal primario: evento via M10 Core. Backup HTTP si falla o no está configurado.
    event_type_id = settings.monitoreo_alta_event_type_id
    if event_type_id is not None:
        ok_event = core_events.publish_event(event_type_id, payload)
        if ok_event:
            return True
    return _post(
        f"{settings.m9_monitoreo_url}/api/v1/webhooks/internacion/alta-monitoreo", payload
    ) is not None


def notify_baja(paciente_id: int, cama_id: int | None = None) -> bool:
    """
    Mismo esquema que `notify_alta`, para BAJA_MONITOREO (event_type_id=27).

    `cama_id`: la cama que el paciente deja — la cama origen en un traslado, o la cama que
    se libera en un alta médica (ver call sites en `solicitud_service.py`/`cama_service.py`).
    """
    if settings.mock_external_services:
        print(f"[SIMULACION] Request a M9 Monitoreo → baja de paciente {paciente_id}")
        return True
    payload = {
        "evento": "BAJA_MONITOREO",
        "paciente_id": paciente_id,
        "cama_id": cama_id,
        "timestamp": datetime.now(timezone.utc).isoformat(),
    }
    # Canal primario: evento via M10 Core. Backup HTTP si falla o no está configurado.
    event_type_id = settings.monitoreo_baja_event_type_id
    if event_type_id is not None:
        ok_event = core_events.publish_event(event_type_id, payload)
        if ok_event:
            return True
    return _post(
        f"{settings.m9_monitoreo_url}/api/v1/webhooks/internacion/baja-monitoreo", payload
    ) is not None