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
3 changes: 0 additions & 3 deletions core/tm/routes/profesional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1342,9 +1342,6 @@ router.patch('/profesionales/:id?', Auth.authenticate(), async (req, res, next)
resultado.notas.unshift(nota);
}
break;
case 'updateSancion':
resultado.sansiones.push(req.body.data);
break;
case 'updatePosGrado':
if (!resultado.formacionPosgrado) {
resultado.formacionPosgrado = [];
Expand Down
73 changes: 57 additions & 16 deletions core/tm/schemas/profesional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,52 @@ const matriculacionSchema = new mongoose.Schema({
revalidacionNumero: Number
});

const sancionSchema = new mongoose.Schema({
numero: { type: Number, required: false },
sancion: {
id: Number,
nombre: String,
},
motivo: { type: String, required: false },
normaLegal: { type: String, required: false },
fecha: { type: Date, required: false },
vencimiento: { type: Date, required: false }
}, { _id: false });

function normalizeSanciones(value) {
if (value === undefined || value === null) {
return null;
}
if (!Array.isArray(value)) {
return value;
}
const sanciones = value.filter(sancion => !isEmptySancion(sancion));
return sanciones.length > 0 ? sanciones : null;
}

function isEmptySancion(sancion) {
if (!sancion) {
return true;
}
return isEmptyValue(sancion.numero)
&& isEmptyValue(sancion.motivo)
&& isEmptyValue(sancion.normaLegal)
&& isEmptyValue(sancion.fecha)
&& isEmptyValue(sancion.vencimiento)
&& isEmptyNestedSancion(sancion.sancion);
}

function isEmptyNestedSancion(sancion) {
if (!sancion) {
return true;
}
return isEmptyValue(sancion.id) && isEmptyValue(sancion.nombre);
}

function isEmptyValue(value) {
return value === undefined || value === null || value === '';
}

export const ProfesionalBaseSchema = new mongoose.Schema({
documento: { type: String, required: true },
sexo: { type: String, required: false },
Expand Down Expand Up @@ -110,22 +156,12 @@ ProfesionalSchema.add({
tieneVencimiento: Boolean,
notas: [{ type: String, required: false }]
}],
sanciones: [{
numero: { type: Number, required: false },
sancion: {
id: Number,
nombre: String,
},
motivo: { type: String, required: false },
normaLegal: { type: String, required: false },
fecha: { type: Date, required: false },
vencimiento: { type: Date, required: false }
}],
notas: [{
usuario: { type: String, required: false },
fecha: { type: Date, required: false },
descripcion: { type: String, required: false },
}],
sanciones: {
type: [sancionSchema],
default: undefined,
set: normalizeSanciones
},
notas: [{ type: String, required: false }],
rematriculado: { type: Number, default: false },
agenteMatriculador: { type: String, required: false },
supervisor: {
Expand Down Expand Up @@ -166,6 +202,11 @@ ProfesionalSchema.virtual('fallecido').get(function () {
return this.fechaFallecimiento;
});

ProfesionalSchema.pre('save', function (this: any, next) {
this.sanciones = normalizeSanciones(this.sanciones);
next();
});

ProfesionalSchema.plugin(AuditPlugin);
ProfesionalSchema.plugin(
TokenSearch(['documento', 'nombre', 'apellido'])
Expand Down
67 changes: 56 additions & 11 deletions modules/matriculaciones/schemas/turnoSolicitado.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,52 @@ const matriculacionSchema = new mongoose.Schema({
revalidacionNumero: Number
});

const sancionSchema = new mongoose.Schema({
numero: { type: Number, required: false },
sancion: {
id: Number,
nombre: String,
},
motivo: { type: String, required: false },
normaLegal: { type: String, required: false },
fecha: { type: Date, required: false },
vencimiento: { type: Date, required: false }
}, { _id: false });

function normalizeSanciones(value) {
if (value === undefined || value === null) {
return null;
}
if (!Array.isArray(value)) {
return value;
}
const sanciones = value.filter(sancion => !isEmptySancion(sancion));
return sanciones.length > 0 ? sanciones : null;
}

function isEmptySancion(sancion) {
if (!sancion) {
return true;
}
return isEmptyValue(sancion.numero)
&& isEmptyValue(sancion.motivo)
&& isEmptyValue(sancion.normaLegal)
&& isEmptyValue(sancion.fecha)
&& isEmptyValue(sancion.vencimiento)
&& isEmptyNestedSancion(sancion.sancion);
}

function isEmptyNestedSancion(sancion) {
if (!sancion) {
return true;
}
return isEmptyValue(sancion.id) && isEmptyValue(sancion.nombre);
}

function isEmptyValue(value) {
return value === undefined || value === null || value === '';
}

const nombreSchema = new mongoose.Schema({
nombre: {
type: String,
Expand Down Expand Up @@ -97,17 +143,11 @@ export const turnoSolicitadoSchema = new mongoose.Schema({
},
matriculacion: [matriculacionSchema]
}],
sanciones: [{
numero: { type: Number, required: false },
sancion: {
id: Number,
nombre: String,
},
motivo: { type: String, required: false },
normaLegal: { type: String, required: false },
fecha: { type: Date, required: false },
vencimiento: { type: Date, required: false }
}],
sanciones: {
type: [sancionSchema],
default: undefined,
set: normalizeSanciones
},
notas: [{ type: String, required: false }],
rematriculado: { type: Boolean, default: false },
agenteMatriculador: { type: String, required: false },
Expand All @@ -133,5 +173,10 @@ turnoSolicitadoSchema.virtual('fallecido').get(function () {
return this.fechaFallecimiento;
});

turnoSolicitadoSchema.pre('save', function (this: any, next) {
this.sanciones = normalizeSanciones(this.sanciones);
next();
});

turnoSolicitadoSchema.plugin(AuditPlugin);
export const turnoSolicitado = mongoose.model('turnoSolicitado', turnoSolicitadoSchema, 'turnoSolicitado');
Loading