From 00fdac296787b893c4968513aae6e57a29a8af61 Mon Sep 17 00:00:00 2001 From: Sergio Nicolas Lavaise Date: Sun, 9 Aug 2026 18:53:06 +0000 Subject: [PATCH] Implement logic to finalize current ATB on modification date and start a new ATB on modification date --- src/sections/HistoriaClinica.tsx | 16 +++++++++++++++- src/sections/SeccionIndicaciones.tsx | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/sections/HistoriaClinica.tsx b/src/sections/HistoriaClinica.tsx index a23e5c1..2d5d1f7 100644 --- a/src/sections/HistoriaClinica.tsx +++ b/src/sections/HistoriaClinica.tsx @@ -2940,8 +2940,22 @@ function SeccionATB({ atb, internacionId, pacienteId, add, update, del, canEdit const handleGuardar = () => { if (!antibiotico.trim() || !fechaInicio) return; + const todayStr = new Date().toISOString().split('T')[0]; if (edit) { - update(edit.id, { antibiotico, fechaInicio, fechaFinalizacion: fechaFinalizacion || undefined }); + // 1. Modificar el antibiótico actual agregando como fecha de finalización la fecha actual en qué se modifica (manteniendo el nombre y fecha de inicio original) + update(edit.id, { + antibiotico: edit.antibiotico, + fechaInicio: edit.fechaInicio, + fechaFinalizacion: todayStr + }); + // 2. Agregar un nuevo esquema antibiótico con el antibiótico nuevo y fecha de inicio de hoy + add({ + pacienteId, + internacionId, + antibiotico, + fechaInicio: todayStr, + fechaFinalizacion: fechaFinalizacion || undefined + }); } else { add({ pacienteId, internacionId, antibiotico, fechaInicio, fechaFinalizacion: fechaFinalizacion || undefined }); } diff --git a/src/sections/SeccionIndicaciones.tsx b/src/sections/SeccionIndicaciones.tsx index d4d6b65..3372067 100644 --- a/src/sections/SeccionIndicaciones.tsx +++ b/src/sections/SeccionIndicaciones.tsx @@ -177,6 +177,32 @@ export function SeccionIndicaciones({ recomendaciones, internacionId, pacienteId const now = new Date(); const fecha = now.toISOString().split('T')[0] + ' ' + now.toTimeString().split(' ')[0].substring(0, 5); if (edit) { + if (edit.tipo === 'Farmacologica Antibiótico' && updateATB && addATB && atbList && pacienteId) { + const oldDrug = edit.droga || ''; + const newDrug = droga || ''; + if (oldDrug.toLowerCase() !== newDrug.toLowerCase()) { + const activeATB = atbList.find(a => + a.internacionId === internacionId && + a.antibiotico.toLowerCase() === oldDrug.toLowerCase() && + !a.fechaFinalizacion + ); + if (activeATB) { + const todayStr = now.toISOString().split('T')[0]; + try { + await updateATB(activeATB.id, { fechaFinalizacion: todayStr }); + await addATB({ + pacienteId, + internacionId, + antibiotico: newDrug, + fechaInicio: todayStr, + }); + } catch (atbErr) { + console.error('Error al actualizar ATB en modificación de indicación:', atbErr); + } + } + } + } + if (onAgregarMovimiento) { await onAgregarMovimiento({ indicacionId: edit.id,