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
+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"}