Skip to content

Commit 19ce95a

Browse files
committed
feat(RUP):"Arreglos en la funcionalidad"
1 parent 3f0145c commit 19ce95a

2 files changed

Lines changed: 52 additions & 51 deletions

File tree

modules/rup/routes/prestacion.ts

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -590,62 +590,63 @@ router.post('/prestaciones', async (req, res, next) => {
590590
* @param registrosNuevos - Registros que vienen en el request
591591
* @returns Registros mergeados con auditoría preservada
592592
*/
593-
function mergeRegistrosPreservandoAuditoria(registrosExistentes: any[], registrosNuevos: any[]): any[] {
594-
if (!registrosExistentes || registrosExistentes.length === 0) {
593+
594+
function mergeRegistrosPreservandoAuditoria(
595+
registrosExistentes: any[],
596+
registrosNuevos: any[]
597+
): any[] {
598+
if (!registrosExistentes?.length) {
595599
return registrosNuevos;
596600
}
597-
598-
if (!registrosNuevos || registrosNuevos.length === 0) {
601+
if (!registrosNuevos?.length) {
599602
return registrosExistentes;
600603
}
601604

602-
const registrosExistentesMap = new Map();
603-
registrosExistentes.forEach(reg => {
604-
if (reg._id) {
605-
registrosExistentesMap.set(reg._id.toString(), reg);
606-
}
607-
});
605+
const existentesMap = new Map<string, any>(
606+
registrosExistentes
607+
.filter(r => r._id)
608+
.map(r => [r._id.toString(), r])
609+
);
608610

609-
const registrosMergeados = registrosNuevos.map(regNuevo => {
610-
const idNuevo = regNuevo._id ? regNuevo._id.toString() : null;
611+
return registrosNuevos.map(regNuevo => {
612+
const idNuevo = regNuevo._id?.toString() || regNuevo.id?.toString();
613+
const regExistente = idNuevo ? existentesMap.get(idNuevo) : null;
611614

612-
if (idNuevo && registrosExistentesMap.has(idNuevo)) {
613-
const regExistente = registrosExistentesMap.get(idNuevo);
615+
// 🔹 Registro completamente nuevo
616+
if (!regExistente) {
617+
return regNuevo;
618+
}
614619

615-
const registroMergeado = {
616-
...regNuevo,
617-
createdAt: regExistente.createdAt,
618-
createdBy: regExistente.createdBy,
619-
};
620+
// 🔹 Comparación profunda para detectar si hubo cambios reales
621+
const valorIgual =
622+
JSON.stringify(regExistente.valor) === JSON.stringify(regNuevo.valor);
620623

621-
if (regNuevo.registros && regNuevo.registros.length > 0) {
622-
registroMergeado.registros = mergeRegistrosPreservandoAuditoria(
623-
regExistente.registros || [],
624-
regNuevo.registros
625-
);
626-
}
624+
const subRegistrosIguales =
625+
JSON.stringify(regExistente.registros || []) ===
626+
JSON.stringify(regNuevo.registros || []);
627627

628-
return registroMergeado;
629-
} else {
630-
if (regNuevo.registros && regNuevo.registros.length > 0) {
631-
const registrosAnidadosExistentes = [];
632-
registrosExistentes.forEach(regEx => {
633-
if (regEx.registros && regEx.registros.length > 0) {
634-
registrosAnidadosExistentes.push(...regEx.registros);
635-
}
636-
});
628+
// 🔹 No hubo cambios → devolver registro original (preserva auditoría)
629+
if (valorIgual && subRegistrosIguales) {
630+
return regExistente;
631+
}
637632

638-
regNuevo.registros = mergeRegistrosPreservandoAuditoria(
639-
registrosAnidadosExistentes,
640-
regNuevo.registros
641-
);
642-
}
633+
// 🔹 Hubo cambios → merge preservando auditoría original
634+
const registroMergeado: any = {
635+
...regNuevo,
636+
createdAt: regExistente.createdAt,
637+
createdBy: regExistente.createdBy
638+
};
643639

644-
return regNuevo;
640+
// 🔹 Merge recursivo de subregistros
641+
if (regNuevo.registros?.length && regExistente.registros?.length) {
642+
registroMergeado.registros = mergeRegistrosPreservandoAuditoria(
643+
regExistente.registros,
644+
regNuevo.registros
645+
);
645646
}
646-
});
647647

648-
return registrosMergeados;
648+
return registroMergeado;
649+
});
649650
}
650651

651652
/**
@@ -654,17 +655,17 @@ function mergeRegistrosPreservandoAuditoria(registrosExistentes: any[], registro
654655
* @param prestacion - Prestación a actualizar
655656
* @param profesional - Profesional actual que está registrando
656657
*/
657-
function actualizarProfesionalesQueRegistran(prestacion: any, profesional: any) {
658-
if (!prestacion.profesionalesQueRegistran) {
659-
prestacion.profesionalesQueRegistran = [];
658+
function actualizarProfesionalesRegistrantes(prestacion: any, profesional: any) {
659+
if (!prestacion.profesionalesRegistrantes) {
660+
prestacion.profesionalesRegistrantes = [];
660661
}
661662

662-
const yaExiste = prestacion.profesionalesQueRegistran.some(
663+
const yaExiste = prestacion.profesionalesRegistrantes.some(
663664
(prof: any) => prof.id && prof.id.toString() === profesional.id.toString()
664665
);
665666

666667
if (!yaExiste) {
667-
prestacion.profesionalesQueRegistran.push({
668+
prestacion.profesionalesRegistrantes.push({
668669
id: profesional.id,
669670
nombreCompleto: profesional.nombreCompleto,
670671
nombre: profesional.nombre,
@@ -738,7 +739,7 @@ router.patch('/prestaciones/:id', (req: Request, res, next) => {
738739
// Actualizar lista de profesionales que registran
739740
const profesionalQueRegistra = Auth.getProfesional(req);
740741
if (profesionalQueRegistra) {
741-
actualizarProfesionalesQueRegistran(data, profesionalQueRegistra);
742+
actualizarProfesionalesRegistrantes(data, profesionalQueRegistra);
742743
}
743744
}
744745
if (req.body.ejecucion?.fecha) {
@@ -787,7 +788,7 @@ router.patch('/prestaciones/:id', (req: Request, res, next) => {
787788
// Actualizar lista de profesionales que registran
788789
const profesionalQueRegistra = Auth.getProfesional(req);
789790
if (profesionalQueRegistra) {
790-
actualizarProfesionalesQueRegistran(data, profesionalQueRegistra);
791+
actualizarProfesionalesRegistrantes(data, profesionalQueRegistra);
791792
}
792793

793794
if (req.body.solicitud) {
@@ -874,7 +875,7 @@ router.patch('/prestaciones/:id', (req: Request, res, next) => {
874875
// Actualizar lista de profesionales que registran
875876
const profesionalActual = Auth.getProfesional(req);
876877
if (profesionalActual) {
877-
actualizarProfesionalesQueRegistran(data, profesionalActual);
878+
actualizarProfesionalesRegistrantes(data, profesionalActual);
878879
}
879880
break;
880881
default:

modules/rup/schemas/prestacion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export const PrestacionSchema = new Schema({
168168

169169
},
170170

171-
profesionalesQueRegistran: [
171+
profesionalesRegistrantes: [
172172
{
173173
_id: false,
174174
id: Schema.Types.ObjectId,

0 commit comments

Comments
 (0)