Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STCOR-905: based on 'users-keycloak' interface use bl-users or users-keycloak for _self endpoint in useUserTenantPermissions #1556

Merged
merged 12 commits into from
Nov 15, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Conditionally use `/users-keycloak/_self` endpoint when `users-keycloak` interface is present. Refs STCOR-835.
* Wait longer before declaring a rotation request to be stale. Refs STCOR-895.
* Send the stored central tenant name in the header on logout. Refs STCOR-900.
* Add userId parameter to useUserTenantPermissions hook. Refs STCOR-905.

## [10.2.0](https://github.com/folio-org/stripes-core/tree/v10.2.0) (2024-10-11)
[Full Changelog](https://github.com/folio-org/stripes-core/compare/v10.1.1...v10.2.0)
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as useUserTenantPermissions } from './useUserTenantPermissions'; // eslint-disable-line import/prefer-default-export
export { default as useUserTenantPermissions } from './useUserTenantPermissions';
export { default as useUserSelfTenantPermissions } from './useUserSelfTenantPermissions';
6 changes: 2 additions & 4 deletions src/hooks/useUserSelfTenantPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,21 @@ const useUserSelfTenantPermissions = (
});
const [namespace] = useNamespace({ key: 'user-self-permissions' });

const user = stripes.user.user;

const {
isFetching,
isFetched,
isLoading,
data,
} = useQuery(
[namespace, user?.id, tenantId],
[namespace, tenantId],
({ signal }) => {
return api.get(
'users-keycloak/_self',
{ signal },
).json();
},
{
enabled: Boolean(user?.id && tenantId) && stripes.hasInterface('users-keycloak'),
enabled: Boolean(tenantId) && stripes.hasInterface('users-keycloak'),
keepPreviousData: true,
...options,
},
Expand Down
10 changes: 4 additions & 6 deletions src/hooks/useUserTenantPermissionNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import useOkapiKy from '../useOkapiKy';
const INITIAL_DATA = [];

const useUserTenantPermissionNames = (
{ tenantId },
{ userId, tenantId },
options = {},
) => {
const stripes = useStripes();
Expand All @@ -19,8 +19,6 @@ const useUserTenantPermissionNames = (
});
const [namespace] = useNamespace({ key: 'user-affiliation-permissions' });

const user = stripes.user.user;

const searchParams = {
full: 'true',
indexField: 'userId',
Expand All @@ -32,18 +30,18 @@ const useUserTenantPermissionNames = (
isLoading,
data = {},
} = useQuery(
[namespace, user?.id, tenantId],
[namespace, userId, tenantId],
({ signal }) => {
return api.get(
`perms/users/${user.id}/permissions`,
`perms/users/${userId}/permissions`,
{
searchParams,
signal,
},
).json();
},
{
enabled: Boolean(user?.id && tenantId) && !stripes.hasInterface('roles'),
enabled: Boolean(userId && tenantId) && !stripes.hasInterface('roles'),
keepPreviousData: true,
...options,
},
Expand Down
30 changes: 15 additions & 15 deletions src/hooks/useUserTenantPermissions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useStripes } from '../StripesContext';
import useUserSelfTenantPermissions from './useUserSelfTenantPermissions';
import useUserTenantPermissionNames from './useUserTenantPermissionNames';
import useUserTenantRoles from './useUserTenantRoles';

const useUserTenantPermissions = (
{ tenantId },
{ tenantId, userId },
options = {},
) => {
const stripes = useStripes();
Expand All @@ -12,23 +12,23 @@ const useUserTenantPermissions = (
isFetching: isPermissionsFetching,
isFetched: isPermissionsFetched,
isLoading: isPermissionsLoading,
userPermissions: permissionsData = {},
userPermissions: permissionsData,
totalRecords: permissionsTotalRecords
} = useUserTenantPermissionNames({ tenantId }, options);
} = useUserTenantPermissionNames({ tenantId, userId }, options);

const {
isFetching: isSelfPermissionsFetching,
isFetched: isSelfPermissionsFetched,
isLoading: isSelfPermissionsLoading,
userPermissions:selfPermissionsData = {},
totalRecords: selfPermissionsTotalRecords
} = useUserSelfTenantPermissions({ tenantId }, options);
isFetching: isRolesFetching,
isFetched: isRolesFetched,
isLoading: isRolesLoading,
userPermissions:userRolesData,
totalRecords: userRolesTotalRecords
} = useUserTenantRoles({ tenantId, userId }, options);

const isFetching = stripes.hasInterface('roles') ? isSelfPermissionsFetching : isPermissionsFetching;
const isFetched = stripes.hasInterface('roles') ? isSelfPermissionsFetched : isPermissionsFetched;
const isLoading = stripes.hasInterface('roles') ? isSelfPermissionsLoading : isPermissionsLoading;
const userPermissions = stripes.hasInterface('roles') ? selfPermissionsData : permissionsData;
const totalRecords = stripes.hasInterface('roles') ? selfPermissionsTotalRecords : permissionsTotalRecords;
const isFetching = stripes.hasInterface('roles') ? isRolesFetching : isPermissionsFetching;
const isFetched = stripes.hasInterface('roles') ? isRolesFetched : isPermissionsFetched;
const isLoading = stripes.hasInterface('roles') ? isRolesLoading : isPermissionsLoading;
const userPermissions = stripes.hasInterface('roles') ? userRolesData : permissionsData;
const totalRecords = stripes.hasInterface('roles') ? userRolesTotalRecords : permissionsTotalRecords;

return ({
isFetching,
Expand Down
15 changes: 10 additions & 5 deletions src/hooks/useUserTenantPermissions.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { renderHook } from '@folio/jest-config-stripes/testing-library/react';
import { useStripes } from '../StripesContext';
import useUserSelfTenantPermissions from './useUserSelfTenantPermissions';
import useUserTenantPermissionNames from './useUserTenantPermissionNames';
import useUserTenantPermissions from './useUserTenantPermissions';
import useUserTenantRoles from './useUserTenantRoles';

jest.mock('../StripesContext');
jest.mock('./useUserSelfTenantPermissions');
jest.mock('./useUserTenantRoles');
jest.mock('./useUserTenantPermissionNames');

describe('useUserTenantPermissions', () => {
Expand All @@ -14,14 +14,19 @@ describe('useUserTenantPermissions', () => {

beforeEach(() => {
useStripes.mockReturnValue({
hasInterface: jest.fn()
hasInterface: jest.fn(),
user:{
user: {
id: 'userId'
}
}
});
});

it('should return _self permissions data when "roles" interface is present', () => {
useStripes().hasInterface.mockReturnValue(true);

useUserSelfTenantPermissions.mockReturnValue({
useUserTenantRoles.mockReturnValue({
isFetching: true,
isFetched: true,
isLoading: true,
Expand Down Expand Up @@ -51,7 +56,7 @@ describe('useUserTenantPermissions', () => {
it('should return tenant permissions data when "roles" interface is NOT present', () => {
useStripes().hasInterface.mockReturnValue(false);

useUserSelfTenantPermissions.mockReturnValue({
useUserTenantRoles.mockReturnValue({
isFetching: true,
isFetched: true,
isLoading: true,
Expand Down
39 changes: 39 additions & 0 deletions src/hooks/useUserTenantRoles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useQuery } from 'react-query';
import { useStripes } from '../StripesContext';
import useOkapiKy from '../useOkapiKy';
import { useNamespace } from '../components';

const INITIAL_DATA = [];

function useUserTenantRoles({ userId, tenantId }, options) {
const stripes = useStripes();

const searchParams = {
limit: stripes.config.maxUnpagedResourceCount,
query: `userId==${userId}`,
};
const ky = useOkapiKy();
const api = ky.extend({
hooks: {
beforeRequest: [(req) => req.headers.set('X-Okapi-Tenant', tenantId)]
}
});
const [namespace] = useNamespace({ key: 'user-roles' });

const { data, isLoading, isFetched, isFetching } = useQuery([namespace, userId], () => {
return api.get(
'roles/users', { searchParams },
)
.json();
}, { enabled: !!userId && !!tenantId && stripes.hasInterface('roles'), ...options });

return {
isFetching,
isFetched,
isLoading,
userPermissions: data.roles || INITIAL_DATA,
totalRecords: data.totalRecords || 0
};
}

export default useUserTenantRoles;
Loading