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 5d247fb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build-npm-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

runs-on: ubuntu-latest
steps:
- uses: folio-org/checkout@v2
- uses: folio-org/checkout@v4
with:
fetch-depth: 0

Expand Down Expand Up @@ -148,15 +148,15 @@ 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
path: ${{ env.JEST_COVERAGE_REPORT_DIR }}
retention-days: 30

- name: Publish yarn.lock
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: failure()
with:
name: yarn.lock
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:

runs-on: ubuntu-latest
steps:
- uses: folio-org/checkout@v2
- uses: folio-org/checkout@v4
with:
fetch-depth: 0

Expand Down Expand Up @@ -102,15 +102,15 @@ 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
path: ${{ env.JEST_COVERAGE_REPORT_DIR }}
retention-days: 30

- name: Publish yarn.lock
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
if: failure()
with:
name: yarn.lock
Expand Down
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 5d247fb

Please sign in to comment.