@@ -590,81 +590,51 @@ 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 ) {
595- return registrosNuevos ;
596- }
597-
598- if ( ! registrosNuevos || registrosNuevos . length === 0 ) {
599- return registrosExistentes ;
600- }
601-
602- const registrosExistentesMap = new Map ( ) ;
603- registrosExistentes . forEach ( reg => {
604- if ( reg . _id ) {
605- registrosExistentesMap . set ( reg . _id . toString ( ) , reg ) ;
606- }
607- } ) ;
608-
609- const registrosMergeados = registrosNuevos . map ( regNuevo => {
610- const idNuevo = regNuevo . _id ? regNuevo . _id . toString ( ) : null ;
611-
612- if ( idNuevo && registrosExistentesMap . has ( idNuevo ) ) {
613- const regExistente = registrosExistentesMap . get ( idNuevo ) ;
614-
593+ function mergeRegistrosPreservandoAuditoria ( registrosExistentes , registrosNuevos ) {
594+ if ( ! registrosExistentes || registrosExistentes . length === 0 ) { return registrosNuevos ; }
595+ if ( ! registrosNuevos || registrosNuevos . length === 0 ) { return registrosExistentes ; }
596+ const existentesMap = new Map ( registrosExistentes . filter ( r => r . _id ) . map ( r => [ r . _id . toString ( ) , r ] ) ) ;
597+ return registrosNuevos . map ( regNuevo => {
598+ const idNuevo = regNuevo . _id ?. toString ( ) || regNuevo . id ?. toString ( ) ;
599+ const regExistente : any = existentesMap . get ( idNuevo ) ;
600+ if ( regExistente ) {
615601 const registroMergeado = {
616602 ...regNuevo ,
617603 createdAt : regExistente . createdAt ,
618604 createdBy : regExistente . createdBy ,
605+ updatedAt : regExistente . updatedAt || regExistente . createdAt ,
606+ updatedBy : regExistente . updatedBy || regExistente . createdBy
619607 } ;
620-
621- if ( regNuevo . registros && regNuevo . registros . length > 0 ) {
608+ if ( regNuevo . registros ?. length && regExistente . registros ?. length ) {
622609 registroMergeado . registros = mergeRegistrosPreservandoAuditoria (
623- regExistente . registros || [ ] ,
610+ regExistente . registros ,
624611 regNuevo . registros
625612 ) ;
626613 }
627-
628614 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- } ) ;
637-
638- regNuevo . registros = mergeRegistrosPreservandoAuditoria (
639- registrosAnidadosExistentes ,
640- regNuevo . registros
641- ) ;
642- }
643-
644- return regNuevo ;
645615 }
616+ return regNuevo ;
646617 } ) ;
647-
648- return registrosMergeados ;
649618}
650619
620+
651621/**
652622 * Actualiza la lista de profesionales que han registrado en esta prestación
653623 *
654624 * @param prestacion - Prestación a actualizar
655625 * @param profesional - Profesional actual que está registrando
656626 */
657- function actualizarProfesionalesQueRegistran ( prestacion : any , profesional : any ) {
658- if ( ! prestacion . profesionalesQueRegistran ) {
659- prestacion . profesionalesQueRegistran = [ ] ;
627+ function actualizarProfesionalesRegistrantes ( prestacion : any , profesional : any ) {
628+ if ( ! prestacion . profesionalesRegistrantes ) {
629+ prestacion . profesionalesRegistrantes = [ ] ;
660630 }
661631
662- const yaExiste = prestacion . profesionalesQueRegistran . some (
632+ const yaExiste = prestacion . profesionalesRegistrantes . some (
663633 ( prof : any ) => prof . id && prof . id . toString ( ) === profesional . id . toString ( )
664634 ) ;
665635
666636 if ( ! yaExiste ) {
667- prestacion . profesionalesQueRegistran . push ( {
637+ prestacion . profesionalesRegistrantes . push ( {
668638 id : profesional . id ,
669639 nombreCompleto : profesional . nombreCompleto ,
670640 nombre : profesional . nombre ,
@@ -738,7 +708,7 @@ router.patch('/prestaciones/:id', (req: Request, res, next) => {
738708 // Actualizar lista de profesionales que registran
739709 const profesionalQueRegistra = Auth . getProfesional ( req ) ;
740710 if ( profesionalQueRegistra ) {
741- actualizarProfesionalesQueRegistran ( data , profesionalQueRegistra ) ;
711+ actualizarProfesionalesRegistrantes ( data , profesionalQueRegistra ) ;
742712 }
743713 }
744714 if ( req . body . ejecucion ?. fecha ) {
@@ -787,7 +757,7 @@ router.patch('/prestaciones/:id', (req: Request, res, next) => {
787757 // Actualizar lista de profesionales que registran
788758 const profesionalQueRegistra = Auth . getProfesional ( req ) ;
789759 if ( profesionalQueRegistra ) {
790- actualizarProfesionalesQueRegistran ( data , profesionalQueRegistra ) ;
760+ actualizarProfesionalesRegistrantes ( data , profesionalQueRegistra ) ;
791761 }
792762
793763 if ( req . body . solicitud ) {
@@ -874,7 +844,7 @@ router.patch('/prestaciones/:id', (req: Request, res, next) => {
874844 // Actualizar lista de profesionales que registran
875845 const profesionalActual = Auth . getProfesional ( req ) ;
876846 if ( profesionalActual ) {
877- actualizarProfesionalesQueRegistran ( data , profesionalActual ) ;
847+ actualizarProfesionalesRegistrantes ( data , profesionalActual ) ;
878848 }
879849 break ;
880850 default :
0 commit comments