diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cd977bc..40eed1cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/MarcRoute/MarcRoute.js b/src/MarcRoute/MarcRoute.js index d6a17316..94c74c3a 100644 --- a/src/MarcRoute/MarcRoute.js +++ b/src/MarcRoute/MarcRoute.js @@ -44,7 +44,7 @@ const MarcRoute = ({ }); const checkCentralTenantPerm = useCallback((perm) => { - return centralTenantPermissions.some(({ permissionName }) => permissionName === perm); + return centralTenantPermissions.has(perm); }, [centralTenantPermissions]); if (isCentralTenantPermissionsLoading) { diff --git a/src/queries/useUserTenantPermissions/useUserTenantPermissions.js b/src/queries/useUserTenantPermissions/useUserTenantPermissions.js index 8bdbf31c..66e322d7 100644 --- a/src/queries/useUserTenantPermissions/useUserTenantPermissions.js +++ b/src/queries/useUserTenantPermissions/useUserTenantPermissions.js @@ -1,3 +1,4 @@ +import { useMemo } from 'react'; import { useQuery } from 'react-query'; import { @@ -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, }); }; diff --git a/src/queries/useUserTenantPermissions/useUserTenantPermissions.test.js b/src/queries/useUserTenantPermissions/useUserTenantPermissions.test.js index 53004a4f..a0427720 100644 --- a/src/queries/useUserTenantPermissions/useUserTenantPermissions.test.js +++ b/src/queries/useUserTenantPermissions/useUserTenantPermissions.test.js @@ -18,7 +18,16 @@ const wrapper = ({ children }) => ( ); const response = { - permissionNames: [], + permissionNames: [ + { + permissionName: 'permissionName1', + subPermissions: ['subPermissions1', 'subPermissions2'], + }, + { + permissionName: 'permissionName2', + subPermissions: ['subPermissions1', 'subPermissions3'], + }, + ], totalRecords: 0, }; @@ -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', + ]); + }); });