feat(pacientes): modo insertar en sync-all — solo crea nuevos, omite existentes
sync_todos() ahora acepta modo='insertar'|'upsert'. La ruta /pacientes/sync-all usa insertar: skippea cédulas que ya están en lab_pacientes. Los hooks automáticos (terceros/automation) siguen en upsert. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
e7c6bc794a
commit
1aab14f705
@@ -63,9 +63,16 @@ def mapear_paciente(row: dict) -> dict:
|
||||
}
|
||||
|
||||
|
||||
async def sync_paciente(row: dict, url: str, api_key: str, client: Optional[httpx.AsyncClient] = None) -> dict:
|
||||
async def sync_paciente(
|
||||
row: dict,
|
||||
url: str,
|
||||
api_key: str,
|
||||
client: Optional[httpx.AsyncClient] = None,
|
||||
modo: str = "upsert",
|
||||
) -> dict:
|
||||
"""Envía un paciente al endpoint de WhatsApp. Retorna {ok, action, message}."""
|
||||
payload = mapear_paciente(row)
|
||||
payload["modo"] = modo
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"X-Lab-Key": api_key,
|
||||
@@ -95,17 +102,19 @@ async def sync_paciente(row: dict, url: str, api_key: str, client: Optional[http
|
||||
}
|
||||
|
||||
|
||||
async def sync_todos(rows: list, url: str, api_key: str, timeout: int = 30) -> dict:
|
||||
"""Envía una lista de filas de pacientes. Retorna resumen {total, created, updated, errores}."""
|
||||
resultado = {"total": len(rows), "created": 0, "updated": 0, "errores": 0, "detalle": []}
|
||||
headers = {"Content-Type": "application/json", "X-Lab-Key": api_key}
|
||||
async def sync_todos(rows: list, url: str, api_key: str, timeout: int = 30, modo: str = "upsert") -> dict:
|
||||
"""Envía una lista de filas de pacientes. Retorna resumen {total, created, skipped, updated, errores}."""
|
||||
resultado = {"total": len(rows), "created": 0, "skipped": 0, "updated": 0, "errores": 0, "detalle": []}
|
||||
|
||||
async with httpx.AsyncClient(timeout=timeout) as client:
|
||||
for row in rows:
|
||||
r = await sync_paciente(row, url, api_key, client)
|
||||
r = await sync_paciente(row, url, api_key, client, modo)
|
||||
if r["ok"]:
|
||||
if r["action"] == "created":
|
||||
action = r["action"]
|
||||
if action == "created":
|
||||
resultado["created"] += 1
|
||||
elif action == "skipped":
|
||||
resultado["skipped"] += 1
|
||||
else:
|
||||
resultado["updated"] += 1
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user