From 1871979a2d9b3309e9a00697825f0b7e3edee6a6 Mon Sep 17 00:00:00 2001 From: Enguerran Weiss Date: Thu, 12 Sep 2024 15:11:22 +0200 Subject: [PATCH] GetDiscussionByIdForEstablishment handles user on copy emails --- ...PgDiscussionRepository.integration.test.ts | 2 +- .../adapters/PgDiscussionRepository.ts | 14 +----- .../GetDiscussionByIdForEstablishment.ts | 13 +++++- ...iscussionByIdForEstablishment.unit.test.ts | 43 ++++++++++++++++++- .../forms/establishment/formEstablishment.ts | 5 ++- 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/back/src/domains/establishment/adapters/PgDiscussionRepository.integration.test.ts b/back/src/domains/establishment/adapters/PgDiscussionRepository.integration.test.ts index e6d3713a4a..85332515bb 100644 --- a/back/src/domains/establishment/adapters/PgDiscussionRepository.integration.test.ts +++ b/back/src/domains/establishment/adapters/PgDiscussionRepository.integration.test.ts @@ -620,7 +620,7 @@ describe("PgDiscussionRepository", () => { it("also returns discussion if discussion is searched by contact email and email is in copyEmails", async () => { const discussion = new DiscussionBuilder() - .withId("id-for-discussion-with-found-copy-email") + .withId(uuid()) .withEstablishmentContact({ email: "other@email.com", copyEmails: ["searchedEmail@email.com"], diff --git a/back/src/domains/establishment/adapters/PgDiscussionRepository.ts b/back/src/domains/establishment/adapters/PgDiscussionRepository.ts index 72380e588c..409c7bb174 100644 --- a/back/src/domains/establishment/adapters/PgDiscussionRepository.ts +++ b/back/src/domains/establishment/adapters/PgDiscussionRepository.ts @@ -276,23 +276,13 @@ export class PgDiscussionRepository implements DiscussionRepository { ), eb( "establishment_contact_copy_emails", - "@>", - JSON.stringify([ - params.establishmentRepresentativeEmail, - ]), + "?", + establishmentRepresentativeEmail, ), ]), ) : builder; }, - (builder) => - params.establishmentRepresentativeEmail - ? builder.where( - "establishment_contact_copy_emails", - "@>", - JSON.stringify([params.establishmentRepresentativeEmail]), - ) - : builder, ), ).as("exists"), ]) diff --git a/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.ts b/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.ts index 8916b0a4ae..5b5b1eee4a 100644 --- a/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.ts +++ b/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.ts @@ -1,7 +1,9 @@ import { + DiscussionDto, DiscussionId, DiscussionReadDto, InclusionConnectDomainJwtPayload, + InclusionConnectedUser, discussionIdSchema, errors, } from "shared"; @@ -29,7 +31,7 @@ export class GetDiscussionByIdForEstablishment extends TransactionalUseCase< if (!discussion) throw errors.discussion.notFound({ discussionId }); - if (discussion.establishmentContact.email !== user.email) + if (!this.#hasUserRightToAccessDiscussion(user, discussion)) throw errors.discussion.accessForbidden({ discussionId, userId: jwtPayload.userId, @@ -69,4 +71,13 @@ export class GetDiscussionByIdForEstablishment extends TransactionalUseCase< }, }; } + #hasUserRightToAccessDiscussion( + user: InclusionConnectedUser, + discussion: DiscussionDto, + ) { + return ( + discussion.establishmentContact.email === user.email || + discussion.establishmentContact.copyEmails.includes(user.email) + ); + } } diff --git a/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.unit.test.ts b/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.unit.test.ts index ede6c199c4..b1f1f64579 100644 --- a/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.unit.test.ts +++ b/back/src/domains/establishment/use-cases/discussions/GetDiscussionByIdForEstablishment.unit.test.ts @@ -28,6 +28,15 @@ const userDiscussion = new DiscussionBuilder() .withEstablishmentContact({ email: user.email }) .withId(uuid()) .build(); +const userOnCopyEmailsDiscussion = new DiscussionBuilder() + .withEstablishmentContact({ copyEmails: [user.email] }) + .withId(uuid()) + .build(); + +const userBothOnCopyEmailsAndContactEmailDiscussion = new DiscussionBuilder() + .withEstablishmentContact({ copyEmails: [user.email], email: user.email }) + .withId(uuid()) + .build(); describe("GetDiscussionById use case", () => { let getDiscussionById: GetDiscussionByIdForEstablishment; @@ -83,7 +92,7 @@ describe("GetDiscussionById use case", () => { }); describe("Happy path", () => { - it("Gets the matching discussion", async () => { + it("Gets the matching discussion based on establishment contact email", async () => { uow.userRepository.users = [user]; uow.discussionRepository.discussions = [userDiscussion]; const response = await getDiscussionById.execute(userDiscussion.id, { @@ -94,5 +103,37 @@ describe("GetDiscussionById use case", () => { new DiscussionBuilder(userDiscussion).buildRead(), ); }); + it("Gets the matching discussion based on establishment copy emails", async () => { + uow.userRepository.users = [user]; + uow.discussionRepository.discussions = [userOnCopyEmailsDiscussion]; + const response = await getDiscussionById.execute( + userOnCopyEmailsDiscussion.id, + { + userId: user.id, + }, + ); + expectToEqual( + response, + new DiscussionBuilder(userOnCopyEmailsDiscussion).buildRead(), + ); + }); + it("Gets the matching discussion based on establishment copy emails and contact email", async () => { + uow.userRepository.users = [user]; + uow.discussionRepository.discussions = [ + userBothOnCopyEmailsAndContactEmailDiscussion, + ]; + const response = await getDiscussionById.execute( + userBothOnCopyEmailsAndContactEmailDiscussion.id, + { + userId: user.id, + }, + ); + expectToEqual( + response, + new DiscussionBuilder( + userBothOnCopyEmailsAndContactEmailDiscussion, + ).buildRead(), + ); + }); }); }); diff --git a/front/src/app/contents/forms/establishment/formEstablishment.ts b/front/src/app/contents/forms/establishment/formEstablishment.ts index 672a83232e..09740f656b 100644 --- a/front/src/app/contents/forms/establishment/formEstablishment.ts +++ b/front/src/app/contents/forms/establishment/formEstablishment.ts @@ -73,13 +73,14 @@ export const formEstablishmentFieldsLabels = ( id: domElementIds.establishment[mode].businessContact.phone, }, "businessContact.email": { - label: "E-mail", + label: "E-mail du référent", required: true, id: domElementIds.establishment[mode].businessContact.email, }, "businessContact.copyEmails": { label: "Autres destinataires", - hintText: "Adresses mail à mettre en copie", + hintText: + "Adresses mail à mettre en copie, les utilisateurs liés à ces adresses auront accès aux discussions", placeholder: "Ex : cc1@mail.com, cc2@mail.com (optionnel)", id: domElementIds.establishment[mode].businessContact.copyEmails, },