From a64fe83407e694dbcdb1bbe3c6daa6f8e8a18cc6 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Mon, 26 Aug 2024 09:08:22 +0100 Subject: [PATCH] feat: migrate document visualization menu --- src/packages/auth/components/auth.ts | 4 +- src/packages/model/Sims.ts | 4 + src/packages/model/operations/document.ts | 6 ++ .../modules-operations/document/menu.spec.tsx | 8 +- .../document/visualization/Menu.spec.tsx | 102 ++++++++++++++++++ .../document/visualization/Menu.tsx | 61 +++++++++++ .../document/visualization/index.js | 52 +-------- 7 files changed, 182 insertions(+), 55 deletions(-) create mode 100644 src/packages/model/operations/document.ts create mode 100644 src/packages/modules-operations/document/visualization/Menu.spec.tsx create mode 100644 src/packages/modules-operations/document/visualization/Menu.tsx diff --git a/src/packages/auth/components/auth.ts b/src/packages/auth/components/auth.ts index 50facc154..7f7a835a7 100644 --- a/src/packages/auth/components/auth.ts +++ b/src/packages/auth/components/auth.ts @@ -19,7 +19,7 @@ const mapDispatchToProps = { type AuthDumbTypes = { userRoles?: string[]; userStamp?: string; - roles: string[]; + roles: Array boolean]>; fallback?: any; complementaryCheck?: boolean; loadUserStamp?: any; @@ -49,7 +49,7 @@ export function AuthDumb({ const isAuthorized = !!roles.find((role) => { if (Array.isArray(role)) { const [r, check] = role; - return userRoles?.includes(r) && check(userStamp); + return userRoles?.includes(r) && check(userStamp!); } return userRoles?.includes(role); }); diff --git a/src/packages/model/Sims.ts b/src/packages/model/Sims.ts index a609e3c30..d141ff43d 100644 --- a/src/packages/model/Sims.ts +++ b/src/packages/model/Sims.ts @@ -1,3 +1,7 @@ export type Rubric = { idAttribute: string; }; + +export type Sims = { + creators: string[]; +}; diff --git a/src/packages/model/operations/document.ts b/src/packages/model/operations/document.ts new file mode 100644 index 000000000..d5a367b0c --- /dev/null +++ b/src/packages/model/operations/document.ts @@ -0,0 +1,6 @@ +import { Sims } from '../Sims'; + +export type Document = { + id: string; + sims: Sims[]; +}; diff --git a/src/packages/modules-operations/document/menu.spec.tsx b/src/packages/modules-operations/document/menu.spec.tsx index af4a4a23d..bf76f25e5 100644 --- a/src/packages/modules-operations/document/menu.spec.tsx +++ b/src/packages/modules-operations/document/menu.spec.tsx @@ -7,7 +7,7 @@ import { SERIES_CONTRIBUTOR, } from '../../auth/roles'; -describe('Structures Home Page Menu', () => { +describe('Document Home Page Menu', () => { it('an admin can create a new structure if he does not have the Gestionnaire_structures_RMESGNCS role', () => { render( @@ -19,7 +19,7 @@ describe('Structures Home Page Menu', () => { screen.getByText('New Document'); }); - it('a user with INDICATOR_CONTRIBUTOR role can create a structure', () => { + it('a user with INDICATOR_CONTRIBUTOR role can create a document', () => { render( @@ -30,7 +30,7 @@ describe('Structures Home Page Menu', () => { screen.getByText('New Document'); }); - it('a user with SERIES_CONTRIBUTOR role can create a structure', () => { + it('a user with SERIES_CONTRIBUTOR role can create a document', () => { render( @@ -41,7 +41,7 @@ describe('Structures Home Page Menu', () => { screen.getByText('New Document'); }); - it('a user without Admin or INDICATOR_CONTRIBUTOR or SERIES_CONTRIBUTOR role cannot create a structure', () => { + it('a user without Admin or INDICATOR_CONTRIBUTOR or SERIES_CONTRIBUTOR role cannot create a document', () => { render( diff --git a/src/packages/modules-operations/document/visualization/Menu.spec.tsx b/src/packages/modules-operations/document/visualization/Menu.spec.tsx new file mode 100644 index 000000000..295ef4b76 --- /dev/null +++ b/src/packages/modules-operations/document/visualization/Menu.spec.tsx @@ -0,0 +1,102 @@ +import { render, screen } from '@testing-library/react'; +import { Menu } from './Menu'; +import { RBACMock } from '../../../tests-utils/rbac'; +import { + ADMIN, + INDICATOR_CONTRIBUTOR, + SERIES_CONTRIBUTOR, +} from '../../../auth/roles'; +import { Document } from '../../../model/operations/document'; +describe('Document Visualization Page Menu', () => { + it('an admin can create a new structure if he does not have the Gestionnaire_structures_RMESGNCS role', () => { + render( + + + + ); + + screen.getByText('Update'); + }); + + it('a user with INDICATOR_CONTRIBUTOR role can update a document if the stamp match', () => { + render( + + + + ); + + screen.getByText('Update'); + }); + + it('a user with INDICATOR_CONTRIBUTOR role cannot update a document if the stamp does not match', () => { + render( + + + + ); + + expect(screen.queryByText('Update')).toBeNull(); + }); + + it('a user with SERIES_CONTRIBUTOR role can update a document if the stamp match', () => { + render( + + + + ); + + screen.getByText('Update'); + }); + + it('a user with SERIES_CONTRIBUTOR role cannot update a document if the stamp does not match', () => { + render( + + + + ); + + expect(screen.queryByText('Update')).toBeNull(); + }); + + it('a user without Admin or INDICATOR_CONTRIBUTOR or SERIES_CONTRIBUTOR role cannot create a document', () => { + render( + + + + ); + + expect(screen.queryByText('Update')).toBeNull(); + }); +}); diff --git a/src/packages/modules-operations/document/visualization/Menu.tsx b/src/packages/modules-operations/document/visualization/Menu.tsx new file mode 100644 index 000000000..844cd5aac --- /dev/null +++ b/src/packages/modules-operations/document/visualization/Menu.tsx @@ -0,0 +1,61 @@ +import { ActionToolbar, Button, ReturnButton } from '@inseefr/wilco'; +import Auth from '../../../auth/components/auth'; +import { + ADMIN, + INDICATOR_CONTRIBUTOR, + SERIES_CONTRIBUTOR, +} from '../../../auth/roles'; +import D from '../../../deprecated-locales/build-dictionary'; +import { useGoBack } from '../../../utils/hooks/useGoBack'; +import { Document } from '../../../model/operations/document'; + +type MenuTypes = { + document: Document; + type: string; +}; + +const checkContributorRight = (document: Document) => { + return (stamp: string) => { + const sims = document.sims; + if (sims?.length === 0) { + return true; + } + const stamps = sims.map(({ creators }) => creators); + for (let i = 1; i < stamps.length; i++) { + // we first check if all stamps array have the same size. + if (stamps[i - 1].length !== stamps[i].length) { + return false; + } + if ( + stamps[i - 1].length > 0 && + stamps[i - 1].filter((s) => stamps[i].includes(s)).length === 0 + ) { + return false; + } + } + return stamps[0].includes(stamp); + }; +}; + +export const Menu = ({ document, type }: Readonly) => { + const goBack = useGoBack(); + + return ( + + goBack('/operations/documents')} /> + + +