feat(sync-log): historial de sincronizaciones WhatsApp en sync_wa_log

- Nueva tabla sync_wa_log (total, created, skipped, updated, errores, origen, modo)
- Log guardado en /pacientes/sync-all, /terceros/send y /automation/run
- Ruta GET /pacientes/historial devuelve últimos 100 registros
- UI: tabla de historial con origen, modo, conteos y detalle de errores

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-09 11:13:28 -05:00
co-authored by Claude Sonnet 4.6
parent 1aab14f705
commit b0175a2ca5
6 changed files with 174 additions and 7 deletions
+37
View File
@@ -4,10 +4,47 @@ Mapea campos Firebird al formato esperado por /api/lab/ingest_paciente.php.
"""
import re
import json
import httpx
from datetime import datetime
from typing import Optional
from app.database import get_connection
def guardar_sync_log(
resultado: dict,
user_id: int,
origen: str = "manual",
modo: str = "insertar",
) -> None:
errores_det = None
errores = [d for d in resultado.get("detalle", []) if not d.get("ok")]
if errores:
errores_det = json.dumps(
[{"doc": e["doc"], "nombre": e["nombre"], "msg": e["message"]} for e in errores],
ensure_ascii=False,
)
conn = get_connection()
conn.execute(
"""INSERT INTO sync_wa_log
(user_id, origen, modo, total, created, skipped, updated, errores, errores_det)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
(
user_id,
origen,
modo,
resultado.get("total", 0),
resultado.get("created", 0),
resultado.get("skipped", 0),
resultado.get("updated", 0),
resultado.get("errores", 0),
errores_det,
),
)
conn.commit()
conn.close()
_TIPO_DOC_MAP = {
"CC": "CC", "TI": "TI", "RC": "RC", "CE": "CE",
"PA": "PA", "NIT": "NIT", "MS": "MS", "AS": "CC",