diff --git a/modules/huds/hudsAccesos.controller.ts b/modules/huds/hudsAccesos.controller.ts index d579bc82fe..6aaa61d4a3 100644 --- a/modules/huds/hudsAccesos.controller.ts +++ b/modules/huds/hudsAccesos.controller.ts @@ -16,11 +16,13 @@ export async function search(filtros) { if (filtros.fechaDesde) { fechas.push({ 'accesos.fecha': { $gte: new Date(filtros.fechaDesde) } }); filtros['$and'] = fechas; + match['accesos.fecha'] = { $gte: new Date(filtros.fechaDesde) }; } if (filtros.fechaHasta) { fechas.push({ 'accesos.fecha': { $lte: new Date(filtros.fechaHasta) } }); filtros['$and'] = fechas; + match['accesos.fecha'] = { $lte: new Date(filtros.fechaHasta) }; } if (filtros.paciente) { @@ -59,6 +61,15 @@ export async function search(filtros) { { $unwind: '$accesos', }, + { + $lookup: { + from: 'motivosHuds', + localField: 'accesos.motivoAcceso', + foreignField: 'key', + as: 'motivoHudsData' + } + }, + { $sort: { 'accesos.fecha': -1 } }, @@ -76,12 +87,23 @@ export async function search(filtros) { motivoAcceso: '$accesos.motivoAcceso', organizacion: '$accesos.organizacion', detalleMotivo: '$accesos.detalleMotivo', + labelPaciente: { + $ifNull: [ + '$accesos.labelPaciente', + { + $ifNull: [ + { $arrayElemAt: ['$motivoHudsData.labelPaciente', 0] }, + { $arrayElemAt: ['$motivoHudsData.label', 0] } + ] + } + ] + } + } } ]; return await HudsAcceso.aggregate(aggregate); - } export const HudsAccesosCtr = { diff --git a/modules/huds/hudsAccesos.schema.ts b/modules/huds/hudsAccesos.schema.ts index 6c6c243274..7d9682d4a0 100644 --- a/modules/huds/hudsAccesos.schema.ts +++ b/modules/huds/hudsAccesos.schema.ts @@ -22,6 +22,7 @@ export const HudsAccesoSchema = new Schema({ matricula: String, motivoAcceso: String, detalleMotivo: String, + labelPaciente: String, turno: SchemaTypes.ObjectId, prestacion: SchemaTypes.ObjectId, organizacion: SchemaTypes.Mixed, diff --git a/modules/huds/motivosHuds/motivosHuds.routes.ts b/modules/huds/motivosHuds/motivosHuds.routes.ts index f36239b8c6..627c790997 100644 --- a/modules/huds/motivosHuds/motivosHuds.routes.ts +++ b/modules/huds/motivosHuds/motivosHuds.routes.ts @@ -8,8 +8,10 @@ class MotivosHudsResource extends ResourceBase { label: MongoQuery.partialString, key: MongoQuery.partialString, descripcion: MongoQuery.partialString, - moduloDefault: MongoQuery.partialString + moduloDefault: MongoQuery.partialString, + labelPaciente: MongoQuery.partialString }; } + export const MotivosHudsCtr = new MotivosHudsResource({}); export const MotivosHudsRouter = MotivosHudsCtr.makeRoutes(); diff --git a/modules/huds/motivosHuds/motivosHuds.schema.ts b/modules/huds/motivosHuds/motivosHuds.schema.ts index 324d7f10ad..9e953cac88 100644 --- a/modules/huds/motivosHuds/motivosHuds.schema.ts +++ b/modules/huds/motivosHuds/motivosHuds.schema.ts @@ -17,6 +17,10 @@ export const MotivosHudsSchema = new Schema({ descripcion: { type: String, required: false + }, + labelPaciente: { + type: String, + required: false } });