Skip to content

Commit

Permalink
[SELC-5637] Feat: Add institution type to roles api
Browse files Browse the repository at this point in the history
  • Loading branch information
OraldoDoci authored Oct 10, 2024
1 parent c1e4c9d commit 60a3afd
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 39 deletions.
14 changes: 11 additions & 3 deletions openApi/dashboard-api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3192,6 +3192,10 @@
"type" : "string"
}
},
"role" : {
"type" : "string",
"description" : "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]"
},
"surname" : {
"type" : "string",
"description" : "User's surname"
Expand Down Expand Up @@ -3728,7 +3732,7 @@
},
"role" : {
"type" : "string",
"description" : "User's role",
"description" : "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"enum" : [ "ADMIN", "ADMIN_EA", "LIMITED" ]
},
"status" : {
Expand Down Expand Up @@ -3986,7 +3990,7 @@
},
"selcRole" : {
"type" : "string",
"description" : "User's role",
"description" : "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"enum" : [ "ADMIN", "ADMIN_EA", "LIMITED" ]
},
"status" : {
Expand Down Expand Up @@ -4070,7 +4074,7 @@
},
"role" : {
"type" : "string",
"description" : "User's role",
"description" : "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"enum" : [ "ADMIN", "ADMIN_EA", "LIMITED" ]
},
"status" : {
Expand Down Expand Up @@ -4495,6 +4499,10 @@
"items" : {
"type" : "string"
}
},
"role" : {
"type" : "string",
"description" : "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]"
}
}
},
Expand Down
14 changes: 11 additions & 3 deletions openApi/generated-dashboard/dashboard-swagger20.json
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,10 @@
"type": "array",
"uniqueItems": true
},
"role": {
"description": "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"type": "string"
},
"surname": {
"description": "User's surname",
"type": "string"
Expand Down Expand Up @@ -3308,7 +3312,7 @@
"type": "array"
},
"role": {
"description": "User's role",
"description": "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"enum": [
"ADMIN",
"ADMIN_EA",
Expand Down Expand Up @@ -3596,7 +3600,7 @@
"type": "string"
},
"selcRole": {
"description": "User's role",
"description": "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"enum": [
"ADMIN",
"ADMIN_EA",
Expand Down Expand Up @@ -3688,7 +3692,7 @@
"description": "Authorized user product"
},
"role": {
"description": "User's role",
"description": "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"enum": [
"ADMIN",
"ADMIN_EA",
Expand Down Expand Up @@ -4150,6 +4154,10 @@
},
"type": "array",
"uniqueItems": true
},
"role": {
"description": "User's role, available value: [MANAGER, DELEGATE, SUBDELEGATE, OPERATOR, ADMIN_EA]",
"type": "string"
}
},
"title": "UserProductRoles",
Expand Down
3 changes: 2 additions & 1 deletion src/api/DashboardApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ export const DashboardApi = {
return extractResponse(result, 200, onRedirectToLogin);
},

getProductRoles: async (productId: string): Promise<Array<ProductRoleMappingsResource>> => {
getProductRoles: async (productId: string, institutionType: string): Promise<Array<ProductRoleMappingsResource>> => {
const result = await apiClient.getProductRolesUsingGET({
productId,
institutionType,
});
return extractResponse(result, 200, onRedirectToLogin);
},
Expand Down
2 changes: 1 addition & 1 deletion src/api/__mocks__/DashboardApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ export const DashboardApi = {
getBillingToken: async (_partyId: string, _environment?: string, _lang?: string): Promise<string> =>
new Promise((resolve) => resolve('DUMMYTOKEN')),

getProductRoles: async (_productId: string): Promise<Array<ProductRoleMappingsResource>> =>
getProductRoles: async (_productId: string, _institutionType: string): Promise<Array<ProductRoleMappingsResource>> =>
new Promise((resolve) => resolve(mockedProductRoles)),

getProductBrokers: async (
Expand Down
14 changes: 9 additions & 5 deletions src/decorators/withSelectedPartyProduct.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { useEffect } from 'react';
import { useHistory, useParams } from 'react-router';
import useErrorDispatcher from '@pagopa/selfcare-common-frontend/lib/hooks/useErrorDispatcher';
import useLoading from '@pagopa/selfcare-common-frontend/lib/hooks/useLoading';
import { resolvePathVariables } from '@pagopa/selfcare-common-frontend/lib/utils/routes-utils';
import { useEffect } from 'react';
import { useHistory, useParams } from 'react-router';
import { useProductRoles } from '../hooks/useProductRoles';
import { useSelectedPartyProduct } from '../hooks/useSelectedPartyProduct';
import { Party } from '../model/Party';
import { Product } from '../model/Product';
import { buildEmptyProductRolesLists, ProductRolesLists } from '../model/ProductRole';
import { useAppSelector } from '../redux/hooks';
import { partiesSelectors } from '../redux/slices/partiesSlice';
import ROUTES from '../routes';
import { useProductRoles } from '../hooks/useProductRoles';
import { LOADING_TASK_FETCH_PRODUCT_ROLES } from '../utils/constants';
import { buildEmptyProductRolesLists, ProductRolesLists } from '../model/ProductRole';

type ProductUrlParams = {
partyId: string;
Expand All @@ -29,6 +32,7 @@ export default function withSelectedPartyProduct<T extends WrappedComponentProps
const { partyId, productId } = useParams<ProductUrlParams>();
const history = useHistory();
const selectedPartyProduct = useSelectedPartyProduct(productId, props.products);
const party = useAppSelector(partiesSelectors.selectPartySelected);
const addError = useErrorDispatcher();

useEffect(() => {
Expand All @@ -54,7 +58,7 @@ export default function withSelectedPartyProduct<T extends WrappedComponentProps

const doFetchProductRoles = (onRetry?: () => void): Promise<ProductRolesLists> => {
setLoading_fetchProductRoles(true);
return fetchSelectedProductRoles(selectedPartyProduct as Product)
return fetchSelectedProductRoles({product:selectedPartyProduct as Product, party: party as Party})
.catch((reason) => {
addError({
id: `FETCH_PRODUCT_ROLES_ERROR_${selectedPartyProduct?.id}`,
Expand Down
10 changes: 5 additions & 5 deletions src/hooks/__tests__/useProductRoles.test.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { appStateActions } from '@pagopa/selfcare-common-frontend/lib/redux/slices/appStateSlice';
import { render, screen, waitFor } from '@testing-library/react';
import { useEffect, useState } from 'react';
import { Provider } from 'react-redux';
import withProductsRolesMap from '../../decorators/withProductsRolesMap';
import { Product } from '../../model/Product';
import { ProductRole } from '../../model/ProductRole';
import { partiesActions } from '../../redux/slices/partiesSlice';
import { createStore } from '../../redux/store';
import { mockedParties } from '../../services/__mocks__/partyService';
import { mockedPartyProducts } from '../../services/__mocks__/productService';
import { useProductRoles } from '../useProductRoles';
import withProductsRolesMap from '../../decorators/withProductsRolesMap';
import { useEffect, useState } from 'react';
import { ProductRole } from '../../model/ProductRole';

jest.mock('../../services/productService');

Expand All @@ -19,7 +19,7 @@ const renderApp = (product: Product, injectedStore?: ReturnType<typeof createSto
const fetchProductRoles = useProductRoles();

useEffect(() => {
fetchProductRoles(product)
fetchProductRoles({ product, party: mockedParties[0] })
.then((r) => setProductRoles(r.list))
.catch((r) => console.error(r));
}, []);
Expand Down
14 changes: 8 additions & 6 deletions src/hooks/useProductRoles.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useReduxCachedValueParametricRetrieverTranscoded } from '@pagopa/selfcare-common-frontend/lib/hooks/useReduxCachedValue';
import { Party } from '../model/Party';
import { Product } from '../model/Product';
import {
productRoles2ProductRolesList,
Expand All @@ -8,14 +9,15 @@ import {
import { partiesActions, partiesSelectors } from '../redux/slices/partiesSlice';
import { fetchProductRoles } from '../services/productService';

export const useProductRoles = (): ((product: Product) => Promise<ProductRolesLists>) =>
export const useProductRoles = (): ((args: { product: Product; party: Party }) => Promise<ProductRolesLists>) =>
useReduxCachedValueParametricRetrieverTranscoded(
'PRODUCT_ROLES',
(product: Product) =>
fetchProductRoles(product).then((roles) => productRoles2ProductRolesList(roles)),
({ product, party }: { product: Product; party: Party }) =>
fetchProductRoles(product, party).then((roles) => productRoles2ProductRolesList(roles)),
partiesSelectors.selectPartySelectedProductsRolesMap,
(roles: ProductRolesLists, product: Product) =>
(roles: ProductRolesLists, { product }: { product: Product }) =>
partiesActions.addPartySelectedProductRoles({ [product.id]: roles }),
(productsRolesMap: ProductsRolesMap, product: Product) => productsRolesMap[product.id],
(productsRolesMap: ProductsRolesMap, product: Product) => !productsRolesMap[product.id]
(productsRolesMap: ProductsRolesMap, { product }: { product: Product }) => productsRolesMap[product.id],
(productsRolesMap: ProductsRolesMap, { product }: { product: Product }) => !productsRolesMap[product.id]
);

5 changes: 3 additions & 2 deletions src/hooks/useProductsRolesMap.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import useReduxCachedValue from '@pagopa/selfcare-common-frontend/lib/hooks/useReduxCachedValue';
import { useMemo } from 'react';
import { Party } from '../model/Party';
import {
ProductRolesLists,
productRoles2ProductRolesList,
ProductRolesLists,
ProductsRolesMap,
} from '../model/ProductRole';
import { useAppSelector } from '../redux/hooks';
Expand Down Expand Up @@ -33,7 +34,7 @@ export const useProductsRolesMap = (): (() => Promise<ProductsRolesMap>) => {
const promises: Array<Promise<[string, ProductRolesLists]>> = activeProducts
.filter((p) => !productsRolesMap[p.id])
.map((p) =>
fetchProductRoles(p).then((roles) => [p.id, productRoles2ProductRolesList(roles)])
fetchProductRoles(p, party as Party).then((roles) => [p.id, productRoles2ProductRolesList(roles)])
);
const fetched: Array<[string, ProductRolesLists]> = await Promise.all(promises);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ test('Test: render with props false', async () => {
test('Test: The techpartner has not been delegated by any body, he will see the menu section and will be able to access it in empty state', async () => {
const institutionPT = mockedParties.find((party) => party.description === 'Maggioli S.p.A.')!;

console.log('institutionPT', institutionPT);
const { history } = renderWithProviders(
<DashboardSideMenu
party={institutionPT}
Expand Down
3 changes: 2 additions & 1 deletion src/services/__mocks__/productService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StatusEnum } from '../../api/generated/b4f-dashboard/SubProductResource';
import { Party } from '../../model/Party';
import { Product } from '../../model/Product';
import { ProductRole } from '../../model/ProductRole';

Expand Down Expand Up @@ -218,7 +219,7 @@ export const verifyFetchPartyProductsMockExecution = (partyProducts: Array<Produ

export const fetchProducts = () => new Promise((resolve) => resolve(mockedPartyProducts));

export const fetchProductRoles = (product: Product): Promise<Array<ProductRole>> => {
export const fetchProductRoles = (product: Product, _party: Party): Promise<Array<ProductRole>> => {
const out = mockedProductRoles.map((r) =>
Object.assign(
{},
Expand Down
15 changes: 8 additions & 7 deletions src/services/__tests__/productService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { DashboardApi } from '../../api/DashboardApiClient';
import { StatusEnum } from '../../api/generated/b4f-dashboard/ProductsResource';
import { Product, productResource2Product } from '../../model/Product';

import { mockedParties } from '../__mocks__/partyService';
import {
fetchProducts,
fetchProductRoles
fetchProductRoles,
fetchProducts
} from '../productService';
import { mockedPartyProducts, fetchProductRoles as fetchProductRolesMocked } from './../__mocks__/productService';
import { fetchProductRoles as fetchProductRolesMocked, mockedPartyProducts } from './../__mocks__/productService';

// Mock the DashboardApi methods
jest.mock('../../api/DashboardApiClient', () => ({
Expand Down Expand Up @@ -108,8 +109,8 @@ describe('productService tests', () => {
];
(fetchProductRolesMocked as jest.Mock).mockResolvedValue(mockRoles);

const result = await fetchProductRoles(mockProduct);
expect(fetchProductRolesMocked).toHaveBeenCalledWith(mockProduct);
const result = await fetchProductRoles(mockProduct, mockedParties[0]);
expect(fetchProductRolesMocked).toHaveBeenCalledWith(mockProduct, mockedParties[0]);
expect(result).toEqual(mockRoles);
});

Expand Down Expand Up @@ -142,8 +143,8 @@ describe('productService tests', () => {
description: 'Admin Role Description'
}
];
const result = await fetchProductRoles(mockProduct);
expect(DashboardApi.getProductRoles).toHaveBeenCalledWith(mockProduct.id);
const result = await fetchProductRoles(mockProduct, mockedParties[0]);
expect(DashboardApi.getProductRoles).toHaveBeenCalledWith(mockProduct.id, mockedParties[0].institutionType);
expect(result).toEqual(expectedRoles);
});
});
Expand Down
9 changes: 5 additions & 4 deletions src/services/productService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { DashboardApi } from '../api/DashboardApiClient';
import { Party } from '../model/Party';
import { Product, productResource2Product } from '../model/Product';
import { ProductRole } from '../model/ProductRole';
import {
mockedPartyProducts,
fetchProductRoles as fetchProductRolesMocked,
mockedPartyProducts,
} from './__mocks__/productService';

export const fetchProducts = (): Promise<Array<Product>> => {
Expand All @@ -17,12 +18,12 @@ export const fetchProducts = (): Promise<Array<Product>> => {
}
};

export const fetchProductRoles = (product: Product): Promise<Array<ProductRole>> => {
export const fetchProductRoles = (product: Product, party: Party): Promise<Array<ProductRole>> => {
/* istanbul ignore if */
if (process.env.REACT_APP_API_MOCK_PRODUCTS === 'true') {
return fetchProductRolesMocked(product);
return fetchProductRolesMocked(product, party);
} else {
return DashboardApi.getProductRoles(product.id)
return DashboardApi.getProductRoles(product.id ,party.institutionType ?? '')
.then((roles) => roles
?.map((pr) =>
pr?.productRoles?.map((r) => ({
Expand Down

0 comments on commit 60a3afd

Please sign in to comment.