diff --git a/modules/recetas/recetasController.ts b/modules/recetas/recetasController.ts index 3778b6e4a..e8d8c6709 100644 --- a/modules/recetas/recetasController.ts +++ b/modules/recetas/recetasController.ts @@ -95,6 +95,20 @@ async function registrarAppNotificadas(req, recetas, sistema) { export async function buscarRecetas(req) { const options: any = {}; const params = req.params.id ? req.params : req.query; + let identificadores = []; + if (params.documento) { + const pacienteAndes: any = await Paciente.find({ documento: params.documento }); + identificadores = pacienteAndes[0].identificadores.filter(ident => ident.entidad === 'ANDES'); + identificadores = identificadores.map(item => Types.ObjectId(item.valor)); + identificadores.push(Types.ObjectId(pacienteAndes[0]._id)); + } else { + const pacienteAndes: any = await Paciente.findById(params.pacienteId); + if (pacienteAndes.identificadores) { + identificadores = pacienteAndes.identificadores?.filter(ident => ident.entidad === 'ANDES'); + identificadores = identificadores?.map(item => Types.ObjectId(item.valor)); + identificadores.push(Types.ObjectId(params.pacienteId)); + } + } const fechaVencimiento = moment().subtract(30, 'days').startOf('day').toDate(); const pacienteId = params.pacienteId || null; const documento = params.documento || null; @@ -108,7 +122,8 @@ export async function buscarRecetas(req) { id: '_id', pacienteId: 'paciente.id', documento: 'paciente.documento', - sexo: 'paciente.sexo' + sexo: 'paciente.sexo', + estado: 'estadoActual.tipo' }; Object.keys(paramMap).forEach(key => { if (params[key]) { @@ -119,6 +134,10 @@ export async function buscarRecetas(req) { throw new ParamsIncorrect(); } + if (identificadores.length > 0) { + options['paciente.id'] = { $in: identificadores }; + } + if (params.estadoDispensa) { const estadoDispensaArray = params.estadoDispensa.replace(/ /g, '').split(','); options['estadoDispensaActual.tipo'] = { $in: estadoDispensaArray };