Skip to content
Merged
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
26 changes: 18 additions & 8 deletions core/tm/controller/consentimiento.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Types } from 'mongoose';
import { Paciente } from '../../../core-v2/mpi/paciente';
import { calcularEdad } from '../../../core-v2/mpi/paciente/paciente.schema';
import { getObraSocial } from '../../../modules/obraSocial/controller/obraSocial';
import { Consentimiento, PadronElectoral } from '../schemas/consentimiento';
import { Consentimiento, ConsentimientoVersion, PadronElectoral } from '../schemas/consentimiento';

async function validarPaciente(pacienteId: any, documento: any, sexo: any) {
async function validarPaciente(pacienteId: any, documento: any, sexo: any, programa: String, version: Number) {
try {
if (!pacienteId && (!documento || !sexo)) {
return { status: 400, message: { mensaje: 'pacienteId o documento y sexo son requeridos' } };
Expand All @@ -22,9 +21,8 @@ async function validarPaciente(pacienteId: any, documento: any, sexo: any) {
if (paciente.fechaFallecimiento && paciente.fechaFallecimiento !== null) {
return { status: 200, message: { validado: false, mensaje: 'Paciente fallecido', Fecha: paciente.fechaFallecimiento } };
}
const edadPaciente = calcularEdad(paciente.fechaNacimiento, paciente.fechaFallecimiento);
if (edadPaciente && edadPaciente >= 65) {
const financiador = tieneFinanciador(paciente.financiador || await getObraSocial(paciente));
if (paciente.edad && paciente.edad >= 65) {
const financiador = await tieneFinanciador(paciente.financiador && paciente.financiador.length ? paciente.financiador : await getObraSocial(paciente), programa, version);
if (!financiador) {
const matriculaNum = Number(paciente.documento);
if (!isNaN(matriculaNum)) {
Expand All @@ -44,8 +42,20 @@ async function validarPaciente(pacienteId: any, documento: any, sexo: any) {
}
}

function tieneFinanciador(financiador: any) {
return financiador && financiador.length > 0 && (financiador.some(f => f.nombre !== 'SUMAR'));
async function tieneFinanciador(financiador: any, programa: String, version: Number) {
const consentimientoVersion: any = await ConsentimientoVersion.findOne({ programa, version });
const incluirFinanciador = consentimientoVersion.condiciones?.incluirFinanciador || ['SUMAR'];
const nombres: string[] = [];
const codigoPuco: number[] = [];
for (const financ of incluirFinanciador) {
if (financ.nombre) {
nombres.push(financ.nombre);
}
if (financ.codigoPuco) {
codigoPuco.push(financ.codigoPuco);
}
}
return financiador && financiador.length > 0 && (financiador.some(f => !nombres.includes(f.nombre) && !codigoPuco.includes(f.codigoPuco)));
}

async function guardarConsentimiento(programa: string, version: number, pacienteId: any, aceptacion: boolean) {
Expand Down
4 changes: 2 additions & 2 deletions core/tm/routes/consentimiento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class PadronElectoralResource extends ResourceBase {
}

router.get('/validarPaciente', Auth.authenticate(), async (req: express.Request, res: express.Response) => {
const { pacienteId, documento, sexo } = req.query;
const resultado = await validarPaciente(pacienteId, documento, sexo);
const { pacienteId, documento, sexo, programa = 'Cuidar65', version = 1 } = req.query;
const resultado = await validarPaciente(pacienteId, documento, sexo, programa, version);
res.status(resultado.status).json(resultado.message);
});

Expand Down
8 changes: 8 additions & 0 deletions core/tm/schemas/consentimiento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ export const consentimientoVersionSchema = new Schema({
texto: { type: String, required: true },
activo: { type: Boolean, required: true },
formato: { type: String, required: true, enum: ['html', 'markdown', 'text'] },
condiciones: {
incluirFinanciador: [
{
codigoPuco: { type: Number, required: false },
nombre: { type: String, required: true },
}
]
},
createdAt: { type: Date, default: Date.now },
createdBy: { type: Schema.Types.Mixed }
});
Expand Down
Loading