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
+3 -4
View File
@@ -13,7 +13,7 @@ from app.services.json_generator import (
agrupar_por_recepcion,
)
from app.services.api_client import get_tns_token, TNS_BASE
from app.services.whatsapp_sync import sync_paciente
from app.services.whatsapp_sync import sync_paciente, sync_todos, guardar_sync_log
router = APIRouter(prefix="/automation", tags=["automation"])
@@ -266,9 +266,8 @@ async def run_automation(
if wa_url and wa_key and rows_pac:
ingest_url = f"{wa_url}/api/lab/ingest_paciente.php"
try:
async with httpx.AsyncClient(timeout=timeout) as wa_client:
for row in rows_pac:
await sync_paciente(row, ingest_url, wa_key, wa_client)
wa_resultado = await sync_todos(rows_pac, ingest_url, wa_key, timeout, modo="upsert")
guardar_sync_log(wa_resultado, user["user_id"], origen="automation", modo="upsert")
except Exception:
pass
+18 -1
View File
@@ -4,7 +4,7 @@ from fastapi.responses import JSONResponse
from app.auth import get_current_user
from app.database import get_connection
from app.services.firebird_service import get_firebird_from_config
from app.services.whatsapp_sync import sync_todos
from app.services.whatsapp_sync import sync_todos, guardar_sync_log
router = APIRouter(prefix="/pacientes", tags=["pacientes"])
@@ -118,6 +118,9 @@ async def sync_all(
timeout = int(configs.get("api_timeout", 30))
resultado = await sync_todos(rows, ingest_url, wa_key, timeout, modo="insertar")
guardar_sync_log(resultado, user["user_id"], origen="manual", modo="insertar")
resultado["success"] = True
resultado["message"] = (
f"{resultado['total']} procesados — "
@@ -126,3 +129,17 @@ async def sync_all(
f"{resultado['errores']} errores."
)
return JSONResponse(resultado)
@router.get("/historial")
async def historial(request: Request, user: dict = Depends(get_current_user)):
conn = get_connection()
rows = conn.execute("""
SELECT l.*, u.username
FROM sync_wa_log l
LEFT JOIN users u ON u.id = l.user_id
ORDER BY l.created_at DESC
LIMIT 100
""").fetchall()
conn.close()
return JSONResponse([dict(r) for r in rows])
+15 -2
View File
@@ -8,7 +8,7 @@ from app.auth import get_current_user
from app.services.firebird_service import get_firebird_from_config
from app.services.json_generator import generar_tercero_api
from app.services.api_client import get_tns_token, TNS_BASE
from app.services.whatsapp_sync import sync_paciente
from app.services.whatsapp_sync import sync_paciente, guardar_sync_log
router = APIRouter(prefix="/terceros", tags=["terceros"])
@@ -191,8 +191,21 @@ async def send_terceros(
if wa_url and wa_key and rows:
ingest_url = f"{wa_url}/api/lab/ingest_paciente.php"
try:
wa_result = await sync_paciente(rows[0], ingest_url, wa_key)
wa_result = await sync_paciente(rows[0], ingest_url, wa_key, modo="upsert")
wa_sync = {"ok": wa_result["ok"], "action": wa_result["action"]}
guardar_sync_log(
{
"total": 1,
"created": 1 if wa_result["action"] == "created" else 0,
"skipped": 1 if wa_result["action"] == "skipped" else 0,
"updated": 1 if wa_result["action"] == "updated" else 0,
"errores": 0 if wa_result["ok"] else 1,
"detalle": [] if wa_result["ok"] else [wa_result],
},
user["user_id"],
origen="tercero",
modo="upsert",
)
except Exception:
wa_sync = {"ok": False, "action": "error"}