Skip to content

Commit

Permalink
GetDiscussionByIdForEstablishment handles user on copy emails
Browse files Browse the repository at this point in the history
  • Loading branch information
enguerranws committed Sep 12, 2024
1 parent 47a6bae commit 1871979
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
copyEmails: ["[email protected]"],
Expand Down
14 changes: 2 additions & 12 deletions back/src/domains/establishment/adapters/PgDiscussionRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
DiscussionDto,
DiscussionId,
DiscussionReadDto,
InclusionConnectDomainJwtPayload,
InclusionConnectedUser,
discussionIdSchema,
errors,
} from "shared";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, {
Expand All @@ -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(),
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 : [email protected], [email protected] (optionnel)",
id: domElementIds.establishment[mode].businessContact.copyEmails,
},
Expand Down

0 comments on commit 1871979

Please sign in to comment.