Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions core-v2/mpi/paciente/paciente.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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 || [];
Expand All @@ -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);
Expand Down
14 changes: 5 additions & 9 deletions core-v2/mpi/paciente/paciente.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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],
Expand Down