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
15 changes: 14 additions & 1 deletion auth/auth.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class Auth {
* @memberOf Auth
*/
static deniedPatients() {
return (req, res, next) => {
return async (req, res, next) => {

if (req.user.type !== 'paciente-token') {
return next();
Expand All @@ -193,6 +193,19 @@ export class Auth {
if (idPacienteToken) {
return next();
}
// Permitir si el paciente consultado es un familiar vinculado al paciente principal del token
try {
// Se obtiene el modelo en runtime: importar el schema acá lo compilaría antes del plugin global de connections.ts
const principal: any = await mongoose.model('paciente_2').findById(req.user.pacientes[0].id).select('relaciones').exec();
const esFamiliar = (principal?.relaciones as any[])?.find(rel => {
return rel.referencia && String(rel.referencia) === String(idPacienteQuery);
});
if (esFamiliar) {
return next();
}
} catch (err) {
// Si falla la búsqueda del familiar, se deniega por defecto más abajo
}
} else {
if (requestPath.endsWith('.pdf')) {
return next();
Expand Down
26 changes: 25 additions & 1 deletion modules/mobileApp/routes/agendas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { verificarCondicionPaciente } from '../../../modules/turnos/condicionPac
import { CondicionPaciente } from '../../../modules/turnos/condicionPaciente/condicionPaciente.schema';
import { Constantes, Constante } from '../../../modules/constantes/constantes.schema';
import { tipoPrestacion } from '../../../core/tm/schemas/tipoPrestacion';
import { PacienteCtr } from '../../../core-v2/mpi/paciente/paciente.routes';

const router = express.Router();

Expand All @@ -23,7 +24,30 @@ router.get('/agendasDisponibles', async (req: any, res, next) => {

if (req.user.type === 'paciente-token') {
const idPacienteQuery = req.query?.idPaciente;
const idPacienteToken = req.user.pacientes.find(p => String(p.id) === String(idPacienteQuery));

let idPacienteToken = req.user.pacientes?.find(p => String(p.id) === String(idPacienteQuery));

// Si no está en req.user.pacientes, obtener el paciente principal y verificar sus relaciones / vínculos en MPI
if (!idPacienteToken && req.user.pacientes?.length > 0) {
const pacientePrincipalToken = req.user.pacientes.find(p => p.relacion === 'principal') || req.user.pacientes[0];
const pacientePrincipalId = pacientePrincipalToken?.id || pacientePrincipalToken?._id;

if (pacientePrincipalId) {
const pacientePrincipal: any = await PacienteCtr.findById(pacientePrincipalId);

if (pacientePrincipal) {
const esVinculoMPI = pacientePrincipal.vinculos?.some(vinculoId => String(vinculoId) === String(idPacienteQuery));
const esRelacionFamiliar = pacientePrincipal.relaciones?.some((rel: any) => {
const referenciaId = rel.referencia?._id || rel.referencia?.id || rel.referencia;
return referenciaId && String(referenciaId) === String(idPacienteQuery);
});

if (esVinculoMPI || esRelacionFamiliar) {
idPacienteToken = pacientePrincipal;
}
}
}
}

if (!idPacienteToken) {
return next(401);
Expand Down
Loading