diff --git a/auth/auth.class.ts b/auth/auth.class.ts index 5a7c4ea8ce..e7fc9d64ec 100644 --- a/auth/auth.class.ts +++ b/auth/auth.class.ts @@ -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(); @@ -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(); diff --git a/modules/mobileApp/routes/agendas.ts b/modules/mobileApp/routes/agendas.ts index 26de2de082..4d32a003c7 100644 --- a/modules/mobileApp/routes/agendas.ts +++ b/modules/mobileApp/routes/agendas.ts @@ -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(); @@ -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);