diff --git a/.github/workflows/build-npm-release.yml b/.github/workflows/build-npm-release.yml index 908097a..5669b40 100644 --- a/.github/workflows/build-npm-release.yml +++ b/.github/workflows/build-npm-release.yml @@ -36,7 +36,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: folio-org/checkout@v2 + - uses: folio-org/checkout@v4 with: fetch-depth: 0 @@ -148,7 +148,7 @@ jobs: comment_title: Jest Unit Test Statistics - name: Publish Jest coverage report - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 if: always() with: name: jest-coverage-report @@ -156,7 +156,7 @@ jobs: retention-days: 30 - name: Publish yarn.lock - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 if: failure() with: name: yarn.lock diff --git a/.github/workflows/build-npm.yml b/.github/workflows/build-npm.yml index 1bae936..1204f19 100644 --- a/.github/workflows/build-npm.yml +++ b/.github/workflows/build-npm.yml @@ -32,7 +32,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: folio-org/checkout@v2 + - uses: folio-org/checkout@v4 with: fetch-depth: 0 @@ -102,7 +102,7 @@ jobs: comment_title: Jest Unit Test Statistics - name: Publish Jest coverage report - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 if: always() with: name: jest-coverage-report @@ -110,7 +110,7 @@ jobs: retention-days: 30 - name: Publish yarn.lock - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 if: failure() with: name: yarn.lock diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d98672..091ef78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/queries/useUserTenantPermissions/useUserTenantPermissions.js b/lib/queries/useUserTenantPermissions/useUserTenantPermissions.js index e921906..219e275 100644 --- a/lib/queries/useUserTenantPermissions/useUserTenantPermissions.js +++ b/lib/queries/useUserTenantPermissions/useUserTenantPermissions.js @@ -1,3 +1,4 @@ +import { useMemo } from 'react'; import { useQuery } from 'react-query'; import { @@ -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, }); }; diff --git a/lib/queries/useUserTenantPermissions/useUserTenantPermissions.test.js b/lib/queries/useUserTenantPermissions/useUserTenantPermissions.test.js index ca9d2e0..cf35aa9 100644 --- a/lib/queries/useUserTenantPermissions/useUserTenantPermissions.test.js +++ b/lib/queries/useUserTenantPermissions/useUserTenantPermissions.test.js @@ -21,7 +21,12 @@ const wrapper = ({ children }) => ( ); const response = { - permissionNames: [], + permissionNames: [ + { + permissionName: 'permission-name1', + subPermissions: ['sub-permission1', 'sub-permission2'], + }, + ], totalRecords: 0, }; @@ -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' }, + ]); + }); }); diff --git a/package.json b/package.json index 12ec806..9e7afd6 100644 --- a/package.json +++ b/package.json @@ -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",