ENCOA-316 ENCOA-317:

Refactor components to remove Layout wrapper and pass it in the App component , implemented a skeleton feedback while loading page and improved API calls related to Dashboard/User Profile
This commit is contained in:
José Marques Lima
2025-01-25 19:38:29 +00:00
parent 4d788e13b4
commit 37216e2a5a
56 changed files with 4440 additions and 2979 deletions

View File

@@ -36,6 +36,43 @@ export function checkAccess(user: User, types: Type[], permissions?: PermissionT
return true;
}
export function groupAllowedEntitiesByPermissions(
user: User,
entities: EntityWithRoles[],
permissions: RolePermission[]
): { [key: string]: EntityWithRoles[] } {
if (["admin", "developer"].includes(user?.type)) {
return permissions.reduce((acc, permission) => {
acc[permission] = entities;
return acc;
}, {} as { [key: string]: EntityWithRoles[] });
}
const userEntityMap = new Map(user.entities.map(e => [e.id, e]));
const roleCache = new Map<string, Role | null>();
return entities.reduce((acc, entity) => {
const userEntity = userEntityMap.get(entity.id);
const role = userEntity
? roleCache.get(userEntity.role) ??
(() => {
const foundRole = entity.roles.find(r => r.id === userEntity.role) || null;
roleCache.set(userEntity.role, foundRole);
return foundRole;
})()
: null;
permissions.forEach(permission => {
if (!acc[permission]) acc[permission] = [];
if (role && role.permissions.includes(permission)) {
acc[permission].push(entity);
}
});
return acc;
}, {} as { [key: string]: EntityWithRoles[] });
}
export function findAllowedEntities(user: User, entities: EntityWithRoles[], permission: RolePermission) {
if (["admin", "developer"].includes(user?.type)) return entities
@@ -52,7 +89,6 @@ export function findAllowedEntitiesSomePermissions(user: User, entities: EntityW
export function doesEntityAllow(user: User, entity: EntityWithRoles, permission: RolePermission) {
if (isAdmin(user)) return true
const userEntity = findBy(user.entities, 'id', entity?.id)
if (!userEntity) return false