From 046803bd15a4277e1d1eb477ca7f6ead4a78f259 Mon Sep 17 00:00:00 2001 From: Lizandro Guarnizo <77708265+lizandrogd@users.noreply.github.com> Date: Thu, 9 Jul 2026 11:05:58 -0500 Subject: [PATCH] =?UTF-8?q?feat(ingest=5Fpaciente):=20modo=20insertar/upse?= =?UTF-8?q?rt=20=E2=80=94=20migraci=C3=B3n=20inicial=20no=20pisa=20existen?= =?UTF-8?q?tes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nuevo campo 'modo' en el body: - "insertar": si la cédula ya existe devuelve skipped sin tocar la BD - "upsert" (default): crea o actualiza como antes Co-Authored-By: Claude Sonnet 4.6 --- api/lab/ingest_paciente.php | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/api/lab/ingest_paciente.php b/api/lab/ingest_paciente.php index 5183594..6ee1e78 100644 --- a/api/lab/ingest_paciente.php +++ b/api/lab/ingest_paciente.php @@ -122,7 +122,13 @@ if (!empty($data['eps'])) { $campos['eps'] = trim($data['eps']); } -// ── UPSERT ─────────────────────────────────────────────────────────────────── +// ── Modo: "insertar" (solo nuevos) | "upsert" (crea o actualiza) ───────────── +$modo = trim($data['modo'] ?? 'upsert'); +if (!in_array($modo, ['insertar', 'upsert'], true)) { + $modo = 'upsert'; +} + +// ── UPSERT / INSERT-ONLY ───────────────────────────────────────────────────── try { $pac = new Paciente(); @@ -134,13 +140,22 @@ try { ob_clean(); if ($existente) { - $pac->actualizar($existente['id'], $campos, null); - echo json_encode([ - 'ok' => true, - 'action' => 'updated', - 'id' => $existente['id'], - 'message' => 'Paciente actualizado', - ]); + if ($modo === 'insertar') { + echo json_encode([ + 'ok' => true, + 'action' => 'skipped', + 'id' => $existente['id'], + 'message' => 'Paciente ya existe', + ]); + } else { + $pac->actualizar($existente['id'], $campos, null); + echo json_encode([ + 'ok' => true, + 'action' => 'updated', + 'id' => $existente['id'], + 'message' => 'Paciente actualizado', + ]); + } } elseif (!empty($campos['nombre_completo'])) { $id = $pac->crear($campos, null); echo json_encode([