Skip to content

Commit

Permalink
do not add agency right to ic user if agency is closed or rejected
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-duport authored and celineung committed Aug 28, 2024
1 parent c26ade7 commit 9734d3f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AgencyGroup,
AgencyRight,
InclusionConnectedUser,
activeAgencyStatuses,
agencyRoleIsNotToReview,
errors,
} from "shared";
Expand Down Expand Up @@ -34,7 +35,7 @@ export class LinkFranceTravailUsersToTheirAgencies extends TransactionalUseCase<

const agency: AgencyDto | undefined =
await uow.agencyRepository.getBySafir(codeSafir);
if (agency) {
if (agency && activeAgencyStatuses.includes(agency.status)) {
await uow.userRepository.updateAgencyRights({
userId,
agencyRights: [
Expand Down Expand Up @@ -64,22 +65,24 @@ export class LinkFranceTravailUsersToTheirAgencies extends TransactionalUseCase<
userId,
agencyRights: [
...agencyRightsWithoutConflicts,
...agencies.map((agency): AgencyRight => {
const existingAgencyRight = agencyRightsWithConflicts.find(
(agencyRight) => agencyRight.agency.id === agency.id,
);
if (
existingAgencyRight &&
agencyRoleIsNotToReview(existingAgencyRight.roles)
)
return existingAgencyRight;
...agencies
.filter((agency) => activeAgencyStatuses.includes(agency.status))
.map((agency): AgencyRight => {
const existingAgencyRight = agencyRightsWithConflicts.find(
(agencyRight) => agencyRight.agency.id === agency.id,
);
if (
existingAgencyRight &&
agencyRoleIsNotToReview(existingAgencyRight.roles)
)
return existingAgencyRight;

return {
agency,
roles: ["agency-viewer"],
isNotifiedByEmail: false,
};
}),
return {
agency,
roles: ["agency-viewer"],
isNotifiedByEmail: false,
};
}),
],
});
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,39 @@ describe("LinkFranceTravailUsersToTheirAgencies", () => {

expectToEqual(uow.userRepository.agencyRightsByUserId, {});
});

it("don't add agency right to IC user if agency is closed or rejected", async () => {
const closedAgency = new AgencyDtoBuilder()
.withId("agency-id-3")
.withCodeSafir("agency-safir-4")
.withStatus("closed")
.build();

const rejectedAgency = new AgencyDtoBuilder()
.withId("agency-id-4")
.withCodeSafir("agency-safir-5")
.withStatus("rejected")
.build();
uow.agencyRepository.setAgencies([
...agenciesInRepo,
closedAgency,
rejectedAgency,
]);

await linkFranceTravailUsersToTheirAgencies.execute({
userId: "my-user-id",
provider: "inclusionConnect",
codeSafir: "agency-safir-4",
});
expectToEqual(uow.userRepository.agencyRightsByUserId, {});

await linkFranceTravailUsersToTheirAgencies.execute({
userId: "my-user-id",
provider: "inclusionConnect",
codeSafir: "agency-safir-5",
});
expectToEqual(uow.userRepository.agencyRightsByUserId, {});
});
});
describe("when safir code matches agency group", () => {
const agencyGroup: AgencyGroup = {
Expand Down Expand Up @@ -248,5 +281,57 @@ describe("LinkFranceTravailUsersToTheirAgencies", () => {
],
});
});

it("don't add agency right to IC user if agency is closed or rejected", async () => {
const closedAgency = new AgencyDtoBuilder()
.withId("agency-id-3")
.withCodeSafir("agency-safir-4")
.withStatus("closed")
.build();

const rejectedAgency = new AgencyDtoBuilder()
.withId("agency-id-4")
.withCodeSafir("agency-safir-5")
.withStatus("rejected")
.build();
uow.agencyRepository.setAgencies([
...agenciesInRepo,
closedAgency,
rejectedAgency,
]);
const agencyGroupWithClosedAndRejectedAgencies: AgencyGroup = {
siret: "12345678902345",
kind: "france-travail",
email: "[email protected]",
codeSafir: agencyGroupCodeSafir,
departments: ["87", "23", "19"],
name: "DR du limousin",
scope: "direction-régionale",
agencyIds: [closedAgency.id, rejectedAgency.id],
ccEmails: ["[email protected]", "[email protected]"],
};
uow.agencyGroupRepository.agencyGroups = [
agencyGroupWithClosedAndRejectedAgencies,
];
const icUser: InclusionConnectedUser = {
...defaultUser,
agencyRights: [],
dashboards: {
agencies: {},
establishments: {},
},
};
uow.userRepository.setInclusionConnectedUsers([icUser]);

await linkFranceTravailUsersToTheirAgencies.execute({
userId: icUser.id,
provider: "inclusionConnect",
codeSafir: agencyGroupCodeSafir,
});

expectToEqual(uow.userRepository.agencyRightsByUserId, {
[icUser.id]: [],
});
});
});
});

0 comments on commit 9734d3f

Please sign in to comment.