Restringir operaciones de grupos al administrador y permitir a usuarios regulares solo modificar el Tipo de camas de su grupo de trabajo

This commit is contained in:
2026-08-11 23:47:45 +00:00
parent e869d6a19e
commit da7a82502f
2 changed files with 66 additions and 30 deletions
+33 -9
View File
@@ -305,10 +305,14 @@ export function useHospitalStore() {
const cama = state.camas.find(c => c.id === id);
if (!cama) return;
const user = state.currentUser;
const grupoId = cama.grupoId || null;
if (user && user.rol !== 'admin' && grupoId && user.grupoId !== grupoId) {
console.warn('No tiene permisos para actualizar cama en esta área');
return;
if (user && user.rol !== 'admin') {
// Must have a working group and the bed must belong to it
if (!user.grupoId || cama.grupoId !== user.grupoId) {
toast.error('No tiene permisos para modificar camas fuera de su grupo de trabajo');
return;
}
// A regular user can ONLY modify the "Tipo" of the bed.
datos = { tipo: datos.tipo };
}
try {
if (datos.numero !== undefined) {
@@ -326,6 +330,10 @@ export function useHospitalStore() {
}, [state, apiCall]);
const agregarCama = useCallback(async (cama: Omit<Cama, 'id'>) => {
if (state.currentUser?.rol !== 'admin') {
toast.error('Solo el usuario administrador puede agregar camas');
return;
}
const nuevaCama: Cama = {
...cama,
id: generateUUID(),
@@ -342,9 +350,13 @@ const agregarCama = useCallback(async (cama: Omit<Cama, 'id'>) => {
console.error('Error al agregar cama:', err);
throw err;
}
}, [apiCall]);
}, [state.currentUser, apiCall]);
const eliminarCama = useCallback(async (id: string) => {
if (state.currentUser?.rol !== 'admin') {
toast.error('Solo el usuario administrador puede eliminar camas');
return;
}
try {
await apiCall('DELETE', `/camas/${id}`);
setState(prev => ({
@@ -356,7 +368,7 @@ const agregarCama = useCallback(async (cama: Omit<Cama, 'id'>) => {
console.error('Error al eliminar cama:', err);
throw err;
}
}, [apiCall]);
}, [state.currentUser, apiCall]);
// Acciones de internaciones
const iniciarInternacion = useCallback(async (internacion: Omit<Internacion, 'id' | 'activa'>) => {
@@ -1211,6 +1223,10 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
// Acciones de grupos
const agregarGrupo = useCallback(async (grupo: Omit<Grupo, 'id'>) => {
if (state.currentUser?.rol !== 'admin') {
toast.error('Solo el usuario administrador puede agregar grupos de trabajo');
return;
}
const nuevaGrupo: Grupo = { ...grupo, id: generateUUID() };
try {
await apiCall('POST', '/grupos', nuevaGrupo);
@@ -1223,9 +1239,13 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
console.error('Error al agregar área:', err);
throw err;
}
}, [apiCall]);
}, [state.currentUser, apiCall]);
const actualizarGrupo = useCallback(async (id: string, datos: Partial<Grupo>) => {
if (state.currentUser?.rol !== 'admin') {
toast.error('Solo el usuario administrador puede modificar grupos de trabajo');
return;
}
try {
await apiCall('PUT', `/grupos/${id}`, datos);
setState(prev => ({
@@ -1236,9 +1256,13 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
console.error('Error al actualizar área:', err);
throw err;
}
}, [apiCall]);
}, [state.currentUser, apiCall]);
const eliminarGrupo = useCallback(async (id: string) => {
if (state.currentUser?.rol !== 'admin') {
toast.error('Solo el usuario administrador puede eliminar grupos de trabajo');
return;
}
try {
await apiCall('DELETE', `/grupos/${id}`);
setState(prev => ({
@@ -1250,7 +1274,7 @@ const actualizarInternacion = useCallback(async (internacionId: string, datos: P
console.error('Error al eliminar área:', err);
throw err;
}
}, [apiCall]);
}, [state.currentUser, apiCall]);
// Funciones de utilidad para consultas
const getPacienteById = useCallback((id: string) => {