@@ -65,6 +65,17 @@ async function registrarAppNotificadas(req, recetas, sistema) {
6565    return  recetasUpdated . filter ( r  =>  r  !==  null ) ; 
6666} 
6767
68+ /** 
69+  * Se ontienen las recetas de un paciente segun filtros: 
70+  * @param  pacienteId (optativo con documento y sexo) 
71+  * @param  documento 
72+  * @param  sexo 
73+  * @param  fechaInicio (optativo) si no se envía es hasta 1 año atrás (desde fechaFin u hoy) 
74+  * @param  fechaFin (optativo) si no se envía por defecto es hoy, salvo estado "pendiente" que se limita a próximos 10 días 
75+  * @param  estado   (optativo) 
76+  * @param  estadoDispensa (optativo) por defecto 'sin-dispensa' 
77+  * @returns  
78+  */ 
6879export  async  function  buscarRecetas ( req )  { 
6980    const  options : any  =  { } ; 
7081    const  params  =  req . params . id  ? req . params  : req . query ; 
@@ -80,68 +91,62 @@ export async function buscarRecetas(req) {
8091            id : '_id' , 
8192            pacienteId : 'paciente.id' , 
8293            documento : 'paciente.documento' , 
83-             sexo : 'paciente.sexo' , 
84-             estado : 'estadoActual.tipo' 
94+             sexo : 'paciente.sexo' 
8595        } ; 
8696        Object . keys ( paramMap ) . forEach ( key  =>  { 
8797            if  ( params [ key ] )  { 
8898                options [ paramMap [ key ] ]  =  key  ===  'id'  ? Types . ObjectId ( params [ key ] )  : params [ key ] ; 
8999            } 
90100        } ) ; 
91- 
92-         const  estadoArray  =  params . estado  ? params . estado . split ( ',' )  : [ ] ; 
93- 
94-         if  ( params . estado )  { 
95-             options [ 'estadoActual.tipo' ]  =  {  $in : estadoArray  } ; 
101+         if  ( Object . keys ( options ) . length  ===  0 )  { 
102+             throw  new  ParamsIncorrect ( ) ; 
96103        } 
97104
98105        if  ( params . estadoDispensa )  { 
99-             const  estadoDispensaArray  =  params . estadoDispensa . split ( ',' ) ; 
106+             const  estadoDispensaArray  =  params . estadoDispensa . replace ( /   / g ,   '' ) . split ( ',' ) ; 
100107            options [ 'estadoDispensaActual.tipo' ]  =  {  $in : estadoDispensaArray  } ; 
101108        }  else  { 
102109            options [ 'estadoDispensaActual.tipo' ]  =  'sin-dispensa' ; 
103110        } 
104- 
105-         if  ( params . fechaInicio  ||  params . fechaFin )  { 
106-             const  fechaInicio  =  params . fechaInicio  ? moment ( params . fechaInicio ) . startOf ( 'day' ) . toDate ( )  : moment ( ) . subtract ( 1 ,  'years' ) . startOf ( 'day' ) . toDate ( ) ; 
107-             const  fechaFin  =  params . fechaFin  ? moment ( params . fechaFin ) . endOf ( 'day' ) . toDate ( )  : moment ( ) . endOf ( 'day' ) . toDate ( ) ; 
108-             options [ 'fechaRegistro' ]  =  {  $gte : fechaInicio ,  $lte : fechaFin  } ; 
109-         } 
110- 
111-         // Para recetas pendientes sin filtro de fechas, limitar a próximos 10 días 
112-         const  includePendiente  =  estadoArray . includes ( 'pendiente' ) ; 
113-         if  ( includePendiente  &&  ! params . fechaInicio  &&  ! params . fechaFin )  { 
114-             const  fechaLimite  =  moment ( ) . add ( 10 ,  'days' ) . endOf ( 'day' ) . toDate ( ) ; 
115-             const  fechaActual  =  moment ( ) . startOf ( 'day' ) . toDate ( ) ; 
116- 
117-             if  ( options [ 'fechaRegistro' ] )  { 
118-                 // Si ya existe un filtro de fecha, combinarlo con el límite de 10 días 
119-                 options [ 'fechaRegistro' ]  =  { 
120-                     ...options [ 'fechaRegistro' ] , 
121-                     $lte : fechaLimite 
111+         const  estadoArray  =  params . estado  ? params . estado . replace ( /   / g,  '' ) . split ( ',' )  : [ ] ; 
112+         const  fechaFin  =  params . fechaFin  ? moment ( params . fechaFin ) . endOf ( 'day' ) . toDate ( )  : moment ( ) . endOf ( 'day' ) . toDate ( ) ; 
113+         const  fechaInicio  =  params . fechaInicio  ? moment ( params . fechaInicio ) . startOf ( 'day' ) . toDate ( )  : moment ( fechaFin ) . subtract ( 1 ,  'years' ) . startOf ( 'day' ) . toDate ( ) ; 
114+ 
115+         if  ( estadoArray . length )  { 
116+             const  optPendientes  =  { } ; 
117+             const  optVigentes  =  { } ; 
118+             const  optOtros  =  { } ; 
119+             // Para recetas pendientes sin filtro de fechas, limitar a próximos 10 días 
120+             const  includePendiente  =  estadoArray . includes ( 'pendiente' ) ; 
121+             if  ( includePendiente )  { 
122+                 optPendientes [ 'fechaRegistro' ]  =  { 
123+                     $gte : fechaInicio , 
124+                     $lte : params . fechaFin  ? fechaFin  : moment ( ) . add ( 10 ,  'days' ) . endOf ( 'day' ) . toDate ( ) 
122125                } ; 
123-             }  else  { 
124-                 // Aplicar filtro de próximos 10 días para recetas pendientes 
125-                 options [ 'fechaRegistro' ]  =  {  $gte : fechaActual ,  $lte : fechaLimite  } ; 
126+                 optPendientes [ 'estadoActual.tipo' ]  =  'pendiente' ; 
126127            } 
127-         } 
128- 
129-         if  ( Object . keys ( options ) . length  ===  0 )  { 
130-             throw  new  ParamsIncorrect ( ) ; 
131-         } 
132- 
133-         const  includeVigente  =  estadoArray . includes ( 'vigente' ) ; 
134- 
135-         if  ( includeVigente )  { 
136-             options [ 'fechaRegistro' ]  =  {  $gte : fechaVencimiento  } ; 
128+             // Para recetas vigentes sin filtro de fechas, limitar a 30 días atrás 
129+             const  includeVigente  =  estadoArray . includes ( 'vigente' ) ; 
130+             if  ( includeVigente )  { 
131+                 const  fInicio  =  params . fechaInicio  ? fechaInicio  : fechaVencimiento ; 
132+                 optVigentes [ 'fechaRegistro' ]  =  params . fechaFin  ? {  $gte : fInicio ,  $lte : fechaFin  }  : {  $gte : fInicio  } ; 
133+                 optVigentes [ 'estadoActual.tipo' ]  =  'vigente' ; 
134+             } 
135+             const  includeOtros  =  estadoArray . filter ( e  =>  e  !==  'pendiente'  &&  e  !==  'vigente' ) ; 
136+             if  ( includeOtros . length )  { 
137+                 optOtros [ 'fechaRegistro' ]  =  {  $gte : fechaInicio ,  $lte : fechaFin  } ; 
138+                 optOtros [ 'estadoActual.tipo' ]  =  {  $in : includeOtros  } ; 
139+             } 
140+             options [ '$or' ]  =  [ optPendientes ,  optVigentes ,  optOtros ] . filter ( o  =>  o . hasOwnProperty ( 'estadoActual.tipo' ) ) ; 
141+         }  else  { 
142+             options [ 'fechaRegistro' ]  =  {  $gte : fechaInicio ,  $lte : fechaFin  } ; 
137143        } 
138144
139145        let  recetas : any  =  await  Receta . find ( options ) ; 
140146        if  ( ! recetas . length )  { 
141147            return  [ ] ; 
142148        } 
143149        const  user  =  req . user ; 
144- 
145150        if  ( user . type  ===  'app-token' )  { 
146151            // si es un usuario de app y no tiene nombre de sistema asignado, no se envia recetas 
147152            const  sistema  =  user . app . nombre . toLowerCase ( ) ; 
@@ -584,7 +589,7 @@ export async function crearReceta(dataReceta, req) {
584589    const  cantRecetas  =  ( tratamientoProlongado )  ? parseInt ( medicamento . tiempoTratamiento . id ,  10 )  : 1 ; 
585590    const  recetas  =  [ ] ; 
586591    let  receta ; 
587-     for  ( let  i  =  0 ; i  <  cantRecetas ; i ++ )  { 
592+     for  ( let  i  =  0 ;   i  <  cantRecetas ;   i ++ )  { 
588593        try  { 
589594            receta  =  new  Receta ( ) ; 
590595            receta . idPrestacion  =  dataReceta . idPrestacion ; 
0 commit comments