diff --git a/core-v2/mpi/paciente/paciente.controller.ts b/core-v2/mpi/paciente/paciente.controller.ts index 6a75a82e3..5e089ac79 100644 --- a/core-v2/mpi/paciente/paciente.controller.ts +++ b/core-v2/mpi/paciente/paciente.controller.ts @@ -41,6 +41,15 @@ export function set(paciente: IPacienteDoc, body: any) { delete body['documento']; delete body['estado']; } + + if (body.relaciones?.length) { + body.relaciones.forEach(rel => { + if (rel.referencia && typeof rel.referencia === 'object') { + rel.referencia = rel.referencia.id || rel.referencia._id; + } + }); + } + paciente.set(body); if (paciente.foto && !paciente.fotoId) { (paciente as any).fotoId = new Types.ObjectId(); @@ -112,6 +121,10 @@ export async function findById(id: string | String | Types.ObjectId, options = n if (fields) { queryFind.select(fields); } + queryFind.populate({ + path: 'relaciones.referencia', + select: 'nombre apellido documento numeroIdentificacion fechaNacimiento fechaFallecimiento fotoId sexo genero activo' + }); const paciente = await queryFind; EventCore.emitAsync('mpi:pacientes:findById', paciente); return paciente; @@ -218,7 +231,13 @@ export async function multimatch(searchText: string, filter: any, options?: any) }; const skip = parseInt(options.skip || 0, 10); const limit = parseInt(options.limit || 30, 10); - const pacientes = await Paciente.find(query).skip(skip).limit(limit); + const pacientes = await Paciente.find(query) + .skip(skip) + .limit(limit) + .populate({ + path: 'relaciones.referencia', + select: 'nombre apellido documento numeroIdentificacion fechaNacimiento fechaFallecimiento fotoId sexo genero activo' + }); return pacientes; } @@ -413,7 +432,8 @@ export async function agregarHijo(progenitor, hijo, req) { // Familiar ya existe? progenitor.relaciones = progenitor.relaciones || []; const poseeFamiliar = progenitor.relaciones.find(rel => { - return rel.referencia.toString() === hijo._id.toString(); + const refId = rel.referencia?.toString() || rel.referencia?.id?.toString() || rel.referencia?._id?.toString(); + return refId === hijo._id.toString(); }); if (!poseeFamiliar) { @@ -422,12 +442,7 @@ export async function agregarHijo(progenitor, hijo, req) { const progenitorRelacion = { relacion: parentescoProgenitor, referencia: progenitor._id, - nombre: progenitor.nombre, - apellido: progenitor.apellido, - documento: progenitor.documento ? progenitor.documento : null, - numeroIdentificacion: progenitor.numeroIdentificacion ? progenitor.numeroIdentificacion : null, - fotoId: progenitor.fotoId ? progenitor.fotoId : null, - fechaFallecimiento: progenitor.fechaFallecimiento ? progenitor.fechaFallecimiento : null + activo: true }; hijo.relaciones = hijo.relaciones || []; @@ -439,12 +454,7 @@ export async function agregarHijo(progenitor, hijo, req) { const familiarRelacion = { relacion: parentescoHijo, referencia: hijo._id, - nombre: hijo.nombre, - apellido: hijo.apellido, - documento: hijo.documento, - numeroIdentificacion: hijo.numeroIdentificacion ? hijo.numeroIdentificacion : null, - fotoId: hijo.fotoId ? hijo.fotoId : null, - fechaFallecimiento: hijo.fechaFallecimiento ? hijo.fechaFallecimiento : null + activo: true }; progenitor.relaciones.push(familiarRelacion); diff --git a/core-v2/mpi/paciente/paciente.schema.ts b/core-v2/mpi/paciente/paciente.schema.ts index e783b66e1..bc896a048 100644 --- a/core-v2/mpi/paciente/paciente.schema.ts +++ b/core-v2/mpi/paciente/paciente.schema.ts @@ -71,7 +71,8 @@ export const PacienteSchema: mongoose.Schema = new mongoose.Schema({ id: mongoose.Schema.Types.ObjectId, nombre: String, apellido: String, - documento: Number }, + documento: Number + }, registradoEn: Date }, estadoCivil: ESTADOCIVIL, @@ -91,19 +92,14 @@ export const PacienteSchema: mongoose.Schema = new mongoose.Schema({ required: false }, // -------------------- + relaciones: [{ relacion: ParentescoSchema, referencia: { type: mongoose.Schema.Types.ObjectId, - ref: 'paciente' + ref: 'paciente_2', + required: true }, - nombre: String, - apellido: String, - documento: String, - fechaNacimiento: Date, - fechaFallecimiento: Date, - numeroIdentificacion: String, - fotoId: mongoose.Schema.Types.ObjectId, activo: Boolean }], financiador: [FinanciadorSchema],