Skip to content

Commit

Permalink
UIQM-735: Return also sub permissions in useUserTenantPermissions hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro-Melnyshyn committed Nov 25, 2024
1 parent a4c1649 commit eaef4bd
Show file tree
Hide file tree
Showing 4 changed files with 51 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 ui-quick-marc

## [8.0.2] (IN PROGRESS)

* [UIQM-735](https://issues.folio.org/browse/UIQM-735) Return also sub permissions in `useUserTenantPermissions` hook.

## [8.0.1] (https://github.com/folio-org/ui-quick-marc/tree/v8.0.1) (2024-04-18)

* [UIQM-641](https://issues.folio.org/browse/UIQM-641) Call `cleanBytesFields` function with correct arguments to fix 008 field.
Expand Down
2 changes: 1 addition & 1 deletion src/MarcRoute/MarcRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const MarcRoute = ({
});

const checkCentralTenantPerm = useCallback((perm) => {
return centralTenantPermissions.some(({ permissionName }) => permissionName === perm);
return centralTenantPermissions.has(perm);
}, [centralTenantPermissions]);

if (isCentralTenantPermissionsLoading) {
Expand Down
19 changes: 18 additions & 1 deletion src/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,26 @@ const useUserTenantPermissions = (
},
);

const centralTenantPermissions = data.permissionNames || INITIAL_DATA;

const flattenCentralTenantPermissions = useMemo(() => {
const permSet = new Set();

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

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

return permSet;
}, [centralTenantPermissions]);

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

const response = {
permissionNames: [],
permissionNames: [
{
permissionName: 'permissionName1',
subPermissions: ['subPermissions1', 'subPermissions2'],
},
{
permissionName: 'permissionName2',
subPermissions: ['subPermissions1', 'subPermissions3'],
},
],
totalRecords: 0,
};

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

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

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

expect([...result.current.userPermissions]).toEqual([
'permissionName1',
'subPermissions1',
'subPermissions2',
'permissionName2',
'subPermissions3',
]);
});
});

0 comments on commit eaef4bd

Please sign in to comment.