Mate11 commited on
Commit
5ffed1d
·
1 Parent(s): 05ab07e

feat: M7 y M9 usan evento M10 como canal primario, HTTP como backup

Browse files

Las llamadas a M7 (Facturación) y M9 (Monitoreo) ya no ejecutan ambos
canales en simultáneo. Ahora intentan primero el evento vía M10 Core;
solo si falla o el event_type_id no está configurado cae al HTTP directo.

app/integrations/facturacion.py CHANGED
@@ -61,9 +61,6 @@ def notify_cama_lista(cama: Cama, db: Client) -> bool:
61
  "idPacienteExterno": str(paciente_id),
62
  "timestampEvento": _iso_z(datetime.now(timezone.utc)),
63
  }
64
- ok_rest = _post(
65
- f"{settings.m7_facturacion_url}/api/v1/webhooks/internacion/cama-lista", payload_rest
66
- ) is not None
67
 
68
  # Payload del evento del Core: exactamente el contrato que confirmó M7 por escrito
69
  # (`M7_integration.md`) y que documenta `M6_DISEÑO.md` §7.2 — sin campos extra
@@ -79,7 +76,16 @@ def notify_cama_lista(cama: Cama, db: Client) -> bool:
79
  "tipoAlta": cama.alta_tipo,
80
  "timestampEvento": _iso_z(datetime.now(timezone.utc)),
81
  }
82
- core_events.publish_event(
83
- settings.facturacion_cama_lista_event_type_id, payload_evento, publisher_module="modulo_6"
84
- )
85
- return ok_rest
 
 
 
 
 
 
 
 
 
 
61
  "idPacienteExterno": str(paciente_id),
62
  "timestampEvento": _iso_z(datetime.now(timezone.utc)),
63
  }
 
 
 
64
 
65
  # Payload del evento del Core: exactamente el contrato que confirmó M7 por escrito
66
  # (`M7_integration.md`) y que documenta `M6_DISEÑO.md` §7.2 — sin campos extra
 
76
  "tipoAlta": cama.alta_tipo,
77
  "timestampEvento": _iso_z(datetime.now(timezone.utc)),
78
  }
79
+
80
+ # Canal primario: evento via M10 Core (si el event_type_id está configurado).
81
+ # Solo se cae al backup HTTP si el evento falla o no está configurado.
82
+ event_type_id = settings.facturacion_cama_lista_event_type_id
83
+ if event_type_id is not None:
84
+ ok_event = core_events.publish_event(event_type_id, payload_evento, publisher_module="modulo_6")
85
+ if ok_event:
86
+ return True
87
+
88
+ # Backup: REST directo a M7 (fallback/testing manual según M7_integration.md)
89
+ return _post(
90
+ f"{settings.m7_facturacion_url}/api/v1/webhooks/internacion/cama-lista", payload_rest
91
+ ) is not None
app/integrations/monitoreo.py CHANGED
@@ -21,11 +21,15 @@ def notify_alta(paciente_id: int) -> bool:
21
  "paciente_id": paciente_id,
22
  "timestamp": datetime.now(timezone.utc).isoformat(),
23
  }
24
- ok_rest = _post(
 
 
 
 
 
 
25
  f"{settings.m9_monitoreo_url}/api/v1/webhooks/internacion/alta-monitoreo", payload
26
  ) is not None
27
- core_events.publish_event(settings.monitoreo_alta_event_type_id, payload)
28
- return ok_rest
29
 
30
 
31
  def notify_baja(paciente_id: int) -> bool:
@@ -38,8 +42,12 @@ def notify_baja(paciente_id: int) -> bool:
38
  "paciente_id": paciente_id,
39
  "timestamp": datetime.now(timezone.utc).isoformat(),
40
  }
41
- ok_rest = _post(
 
 
 
 
 
 
42
  f"{settings.m9_monitoreo_url}/api/v1/webhooks/internacion/baja-monitoreo", payload
43
  ) is not None
44
- core_events.publish_event(settings.monitoreo_baja_event_type_id, payload)
45
- return ok_rest
 
21
  "paciente_id": paciente_id,
22
  "timestamp": datetime.now(timezone.utc).isoformat(),
23
  }
24
+ # Canal primario: evento via M10 Core. Backup HTTP si falla o no está configurado.
25
+ event_type_id = settings.monitoreo_alta_event_type_id
26
+ if event_type_id is not None:
27
+ ok_event = core_events.publish_event(event_type_id, payload)
28
+ if ok_event:
29
+ return True
30
+ return _post(
31
  f"{settings.m9_monitoreo_url}/api/v1/webhooks/internacion/alta-monitoreo", payload
32
  ) is not None
 
 
33
 
34
 
35
  def notify_baja(paciente_id: int) -> bool:
 
42
  "paciente_id": paciente_id,
43
  "timestamp": datetime.now(timezone.utc).isoformat(),
44
  }
45
+ # Canal primario: evento via M10 Core. Backup HTTP si falla o no está configurado.
46
+ event_type_id = settings.monitoreo_baja_event_type_id
47
+ if event_type_id is not None:
48
+ ok_event = core_events.publish_event(event_type_id, payload)
49
+ if ok_event:
50
+ return True
51
+ return _post(
52
  f"{settings.m9_monitoreo_url}/api/v1/webhooks/internacion/baja-monitoreo", payload
53
  ) is not None
 
 
tests/test_core_events.py CHANGED
@@ -81,7 +81,8 @@ def test_notify_resultado_solicitud_sigue_llamando_rest_igual_que_antes(monkeypa
81
  assert event_payload["solicitud_hce_id"] == "HCE-SOL-1"
82
 
83
 
84
- def test_notify_alta_sigue_llamando_rest_igual_que_antes(monkeypatch):
 
85
  monkeypatch.setattr(settings, "mock_external_services", False)
86
  monkeypatch.setattr(settings, "monitoreo_alta_event_type_id", None)
87
  rest_calls = []
@@ -99,11 +100,11 @@ def test_notify_alta_sigue_llamando_rest_igual_que_antes(monkeypatch):
99
 
100
  assert ok is True
101
  assert len(rest_calls) == 1
102
- assert len(publish_calls) == 1
103
- assert publish_calls[0][0] is None
104
 
105
 
106
- def test_notify_baja_sigue_llamando_rest_igual_que_antes(monkeypatch):
 
107
  monkeypatch.setattr(settings, "mock_external_services", False)
108
  monkeypatch.setattr(settings, "monitoreo_baja_event_type_id", None)
109
  rest_calls = []
@@ -121,8 +122,7 @@ def test_notify_baja_sigue_llamando_rest_igual_que_antes(monkeypatch):
121
 
122
  assert ok is True
123
  assert len(rest_calls) == 1
124
- assert len(publish_calls) == 1
125
- assert publish_calls[0][0] is None
126
 
127
 
128
  def test_notify_cama_lista_sigue_llamando_rest_con_payload_minimo(monkeypatch):
 
81
  assert event_payload["solicitud_hce_id"] == "HCE-SOL-1"
82
 
83
 
84
+ def test_notify_alta_sin_event_type_id_usa_rest_como_backup(monkeypatch):
85
+ """Cuando monitoreo_alta_event_type_id es None, va directo al HTTP sin llamar publish_event."""
86
  monkeypatch.setattr(settings, "mock_external_services", False)
87
  monkeypatch.setattr(settings, "monitoreo_alta_event_type_id", None)
88
  rest_calls = []
 
100
 
101
  assert ok is True
102
  assert len(rest_calls) == 1
103
+ assert len(publish_calls) == 0 # no se llama publish_event cuando event_type_id es None
 
104
 
105
 
106
+ def test_notify_baja_sin_event_type_id_usa_rest_como_backup(monkeypatch):
107
+ """Cuando monitoreo_baja_event_type_id es None, va directo al HTTP sin llamar publish_event."""
108
  monkeypatch.setattr(settings, "mock_external_services", False)
109
  monkeypatch.setattr(settings, "monitoreo_baja_event_type_id", None)
110
  rest_calls = []
 
122
 
123
  assert ok is True
124
  assert len(rest_calls) == 1
125
+ assert len(publish_calls) == 0 # no se llama publish_event cuando event_type_id es None
 
126
 
127
 
128
  def test_notify_cama_lista_sigue_llamando_rest_con_payload_minimo(monkeypatch):