Skip to content

Commit

Permalink
UISAUTCOMP-137: Return also sub permissions in useUserTenantPermissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro-Melnyshyn committed Dec 24, 2024
1 parent 5f84839 commit a2746f7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change history for stripes-authoriy-components

## [4.0.2] (IN PROGRESS)

- [UISAUTCOMP-137](https://issues.folio.org/browse/UISAUTCOMP-137) Return also sub permissions in `useUserTenantPermissions`.

## [4.0.1] (https://github.com/folio-org/stripes-authority-components/tree/v4.0.1) (2024-04-02)

- [UISAUTCOMP-111](https://issues.folio.org/browse/UISAUTCOMP-111) `useUsers` hook - add user ids to cache keys to fetch users when user ids change.
Expand Down
24 changes: 23 additions & 1 deletion lib/queries/useUserTenantPermissions/useUserTenantPermissions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMemo } from 'react';
import { useQuery } from 'react-query';

import {
Expand Down Expand Up @@ -48,10 +49,31 @@ const useUserTenantPermissions = (
},
);

const tenantPermissions = data.permissionNames || INITIAL_DATA;

const flattenTenantPermissions = useMemo(() => {
const permSet = new Set();
const allPermissions = [];

tenantPermissions.forEach(perm => {
permSet.add(perm.permissionName);

perm.subPermissions?.forEach(subPermission => {
permSet.add(subPermission);
});
});

permSet.forEach(perm => {
allPermissions.push({ permissionName: perm });
});

return allPermissions;
}, [tenantPermissions]);

return ({
isFetching,
isLoading,
userPermissions: data.permissionNames || INITIAL_DATA,
userPermissions: flattenTenantPermissions,
totalRecords: data.totalRecords,
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ const wrapper = ({ children }) => (
);

const response = {
permissionNames: [],
permissionNames: [
{
permissionName: 'permission-name1',
subPermissions: ['sub-permission1', 'sub-permission2'],
},
],
totalRecords: 0,
};

Expand Down Expand Up @@ -57,4 +62,20 @@ describe('useUserTenantPermissions', () => {
expect(setHeaderMock).toHaveBeenCalledWith('X-Okapi-Tenant', options.tenantId);
expect(getMock).toHaveBeenCalledWith(`perms/users/${options.userId}/permissions`, expect.objectContaining({}));
});

it('should also return sub permissions', async () => {
const options = {
userId: 'userId',
tenantId: 'tenantId',
};
const { result } = renderHook(() => useUserTenantPermissions(options), { wrapper });

await act(async () => !result.current.isLoading);

expect(result.current.userPermissions).toEqual([
{ permissionName: 'permission-name1' },
{ permissionName: 'sub-permission1' },
{ permissionName: 'sub-permission2' },
]);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@folio/stripes-authority-components",
"version": "4.0.1",
"version": "4.0.2",
"description": "Component library for Stripes Authority modules",
"repository": "https://github.com/folio-org/stripes-authority-components",
"main": "index.js",
Expand Down

0 comments on commit a2746f7

Please sign in to comment.