fix(cobranza): link del panel faltaba SIEMPRE — import faltante en el helper
_link_panel_cobranza referenciaba cobranza_acumulada sin importarlo (solo se importa dentro de otras funciones, no a nivel de módulo) → NameError en cada llamada → el except 'resiliente' lo tragaba → devolvía None → reporte SIN link. Afectaba a AMBOS emisores (automático y on-demand), no solo al del scheduler. No era timing ni cableado: era un import faltante. Fix: importar cobranza_acumulada dentro del helper (mismo patrón que las otras funciones). Además: warning explícito en el scheduler cuando el reporte sale sin link, y el helper loguea con el número. NO toca calculator.py. 105 tests (+2: regresión URL ok y resiliencia None). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
30d203c560
commit
266862f1c2
3 files changed
+32
-1
No files matched your search
+3
-1
@@ -1607,12 +1607,14 @@ def _bloque_resumen(rentas_total: float | None, vr_total: float,
|
||||
async def _link_panel_cobranza(numero: str) -> str | None:
|
||||
"""Link al panel 'Mi Cobranza' (token de sesión única por número). Devuelve
|
||||
None si algo falla — el reporte se envía igual, SIN link, para no romperlo."""
|
||||
from src.services import cobranza_acumulada
|
||||
try:
|
||||
token = await cobranza_acumulada.crear_token_cobranza(_ultimos_10_digitos(numero))
|
||||
base_url = os.getenv("BASE_URL", "http://191.101.233.43:8001").rstrip("/")
|
||||
return f"{base_url}/admin/cobranza/ui?token={token}"
|
||||
except Exception:
|
||||
logger.exception("No se pudo generar el link del panel para el reporte")
|
||||
logger.exception(
|
||||
"No se pudo generar el link del panel para el reporte (%s)", numero)
|
||||
return None
|
||||
|
||||
|
||||
|
||||
@@ -71,6 +71,13 @@ async def ejecutar_cobranza_automatica(numero_wa: str, tipo: str, instance: str)
|
||||
else:
|
||||
# Diario: incluye el link al panel Mi Cobranza (sesión única).
|
||||
link = await _link_panel_cobranza(numero_wa)
|
||||
if not link:
|
||||
# No silencioso: si el reporte sale sin link, queda registrado
|
||||
# (el detalle del fallo ya lo logueó _link_panel_cobranza).
|
||||
logger.warning(
|
||||
"Reporte diario a %s enviado SIN link del panel (token no generado)",
|
||||
numero_wa,
|
||||
)
|
||||
texto = _formatear_reporte(reporte, link=link)
|
||||
await whatsapp.enviar_texto(numero_wa, texto, instance)
|
||||
logger.info(
|
||||
|
||||
@@ -72,3 +72,25 @@ def test_reporte_diario_sin_link_no_rompe():
|
||||
texto = wh._formatear_reporte(_reporte(), link=None)
|
||||
assert "Panel Mi Cobranza" not in texto
|
||||
assert "Rentas del día" in texto # el reporte se arma igual
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_link_panel_resiliente_devuelve_none_si_token_falla(monkeypatch):
|
||||
# Si crear_token_cobranza lanza, el helper NO propaga: devuelve None (y loguea).
|
||||
from src.services import cobranza_acumulada as ca
|
||||
async def _boom(_n):
|
||||
raise RuntimeError("redis caído")
|
||||
monkeypatch.setattr(ca, "crear_token_cobranza", _boom)
|
||||
assert await wh._link_panel_cobranza("5212731562186@s.whatsapp.net") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_link_panel_ok_devuelve_url(monkeypatch):
|
||||
# Regresión del bug: el helper importa cobranza_acumulada y arma la URL bien
|
||||
# (antes fallaba con NameError y el reporte salía siempre sin link).
|
||||
from src.services import cobranza_acumulada as ca
|
||||
async def _tok(_n):
|
||||
return "tok-xyz"
|
||||
monkeypatch.setattr(ca, "crear_token_cobranza", _tok)
|
||||
url = await wh._link_panel_cobranza("5212731562186@s.whatsapp.net")
|
||||
assert url is not None and url.endswith("/admin/cobranza/ui?token=tok-xyz")
|
||||
Reference in new issue
Block a user