Skip to content

Commit

Permalink
back AgencyDtoWithoutEmails replaced by AgencyDtoForAgencyUsersAndAdmins
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohec committed Jan 10, 2025
1 parent 5348bb6 commit 7c1e0db
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
makeTextImageAndRedirectFeatureFlag,
makeTextWithSeverityFeatureFlag,
technicalRoutes,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { HttpClient } from "shared-routes";
import { ResponsesToHttpResponse } from "shared-routes/src/defineRoutes";
Expand Down Expand Up @@ -545,7 +546,11 @@ describe("Admin router", () => {
firstName: "John",
lastName: "Doe",
agencyRights: [
{ agency, roles: ["to-review"], isNotifiedByEmail: false },
{
agency: toAgencyDtoForAgencyUsersAndAdmins(agency, []),
roles: ["to-review"],
isNotifiedByEmail: false,
},
],
dashboards: { agencies: {}, establishments: {} },
externalId: "john-external-id",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
inclusionConnectTokenExpiredMessage,
inclusionConnectedAllowedRoutes,
queryParamsAsString,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { HttpClient } from "shared-routes";
import { createSupertestSharedClient } from "shared-routes/supertest";
Expand All @@ -29,11 +30,7 @@ import { buildTestApp } from "../../../../utils/buildTestApp";

describe("InclusionConnectedAllowedRoutes", () => {
const agency = new AgencyDtoBuilder().withKind("pole-emploi").build();
const {
counsellorEmails: _,
validatorEmails: __,
...agencyWithoutEmails
} = agency;
const agencyForUsers = toAgencyDtoForAgencyUsersAndAdmins(agency, []);
const agencyUser: User = {
id: "123",
email: "[email protected]",
Expand Down Expand Up @@ -118,7 +115,7 @@ describe("InclusionConnectedAllowedRoutes", () => {
},
agencyRights: [
{
agency: agencyWithoutEmails,
agency: agencyForUsers,
roles: ["validator"],
isNotifiedByEmail: true,
},
Expand Down
3 changes: 2 additions & 1 deletion back/src/domains/agency/use-cases/GetAgencyById.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
errors,
expectPromiseToFailWithError,
expectToEqual,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { toAgencyWithRights } from "../../../utils/agency";
import { InMemoryUowPerformer } from "../../core/unit-of-work/adapters/InMemoryUowPerformer";
Expand Down Expand Up @@ -50,7 +51,7 @@ describe("getAgencyByIdForDashboard", () => {
.withEmail("[email protected]")
.withAgencyRights([
{
agency: agencyWithRefersTo,
agency: toAgencyDtoForAgencyUsersAndAdmins(agencyWithRefersTo, []),
isNotifiedByEmail: true,
roles: ["agency-admin"],
},
Expand Down
3 changes: 2 additions & 1 deletion back/src/domains/agency/use-cases/UpdateAgency.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
expectPromiseToFail,
expectPromiseToFailWithError,
expectToEqual,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { toAgencyWithRights } from "../../../utils/agency";
import { makeCreateNewEvent } from "../../core/events/ports/EventBus";
Expand Down Expand Up @@ -43,7 +44,7 @@ describe("Update agency", () => {
const icAgencyAdmin = agencyAdminBuilder
.withAgencyRights([
{
agency: initialAgencyInRepo,
agency: toAgencyDtoForAgencyUsersAndAdmins(initialAgencyInRepo, []),
roles: ["agency-admin"],
isNotifiedByEmail: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
errors,
expectArraysToMatch,
expectPromiseToFailWithError,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { toAgencyWithRights } from "../../../../utils/agency";
import { makeCreateNewEvent } from "../../../core/events/ports/EventBus";
Expand Down Expand Up @@ -39,7 +40,7 @@ describe("BroadcastConventionAgain", () => {
.withIsAdmin(false)
.withAgencyRights([
{
agency: agency,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency, []),
roles: ["validator", "counsellor"],
isNotifiedByEmail: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { uniq, values } from "ramda";
import { toPairs, uniq, values } from "ramda";
import {
AgencyId,
AgencyRight,
AgencyWithUsersRights,
InclusionConnectedUser,
OAuthGatewayProvider,
UserId,
errors,
} from "shared";
Expand Down Expand Up @@ -32,10 +33,8 @@ export const getIcUsersByUserIds = async (
uow: UnitOfWork,
userIds: UserId[],
): Promise<InclusionConnectedUser[]> => {
const users = await uow.userRepository.getByIds(
userIds,
await makeProvider(uow),
);
const provider = await makeProvider(uow);
const users = await uow.userRepository.getByIds(userIds, provider);

const userRightsByUser = (
await Promise.all(
Expand All @@ -62,18 +61,44 @@ export const getIcUsersByUserIds = async (
{},
);

return users.map<InclusionConnectedUser>((user) => ({
...user,
agencyRights: userRightsByUser[user.id].map<AgencyRight>(
({ agencyId, ...rights }) => {
const { usersRights: _, ...agency } =
agenciesRelatedToUsersByAgencyId[agencyId];
return {
...rights,
agency,
};
},
),
dashboards: { agencies: {}, establishments: {} },
}));
return Promise.all(
users.map<Promise<InclusionConnectedUser>>(async (user) => ({
...user,
agencyRights: await makeAgencyRights(
userRightsByUser[user.id],
agenciesRelatedToUsersByAgencyId,
uow,
provider,
),
dashboards: { agencies: {}, establishments: {} },
})),
);
};

const makeAgencyRights = (
userRights: AgencyRightOfUser[],
agenciesRelatedToUsersByAgencyId: Record<AgencyId, AgencyWithUsersRights>,
uow: UnitOfWork,
provider: OAuthGatewayProvider,
): Promise<AgencyRight[]> =>
Promise.all(
userRights.map<Promise<AgencyRight>>(async ({ agencyId, ...rights }) => {
const { usersRights, ...agency } =
agenciesRelatedToUsersByAgencyId[agencyId];

const adminUsers = await uow.userRepository.getByIds(
toPairs(usersRights)
.filter(([_, userRight]) => userRight?.roles.includes("agency-admin"))
.map(([id]) => id),
provider,
);

return {
...rights,
agency: {
...agency,
admins: adminUsers.map((user) => user.email),
},
};
}),
);
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
errors,
expectPromiseToFailWithError,
expectToEqual,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { toAgencyWithRights } from "../../../utils/agency";
import { emptyName } from "../../core/authentication/inclusion-connect/entities/user.helper";
Expand Down Expand Up @@ -57,7 +58,7 @@ describe("CreateUserForAgency", () => {
.withIsAdmin(false)
.withAgencyRights([
{
agency: agencyWithCounsellor,
agency: toAgencyDtoForAgencyUsersAndAdmins(agencyWithCounsellor, []),
roles: ["agency-admin"],
isNotifiedByEmail: true,
},
Expand Down Expand Up @@ -264,7 +265,7 @@ describe("CreateUserForAgency", () => {
externalId: null,
createdAt: timeGateway.now().toISOString(),
};
uow.userRepository.users = [validator, counsellor];
uow.userRepository.users = [validator, counsellor, icAgencyAdminUser];

const anotherAgency = new AgencyDtoBuilder()
.withId("another-agency-id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
expectPromiseToFailWithError,
expectToEqual,
splitCasesBetweenPassingAndFailing,
toAgencyDtoForAgencyUsersAndAdmins,
} from "shared";
import { toAgencyWithRights } from "../../../utils/agency";
import { StubDashboardGateway } from "../../core/dashboard/adapters/StubDashboardGateway";
Expand Down Expand Up @@ -74,11 +75,6 @@ describe("GetUserAgencyDashboardUrl", () => {
]);

const agency = agencyWithoutCounsellorAndValidatorBuilder.build();
const {
validatorEmails: _,
counsellorEmails: __,
...agencyWithoutEmails
} = agency;

describe("returns the dashboard url", () => {
it.each(agencyRolesAllowedToGetDashboard)(
Expand All @@ -87,11 +83,6 @@ describe("GetUserAgencyDashboardUrl", () => {
const agency = agencyWithoutCounsellorAndValidatorBuilder
.withKind("pole-emploi")
.build();
const {
validatorEmails: _,
counsellorEmails: __,
...agencyWithoutEmails
} = agency;

uow.userRepository.users = [notAdmin];
uow.agencyRepository.agencies = [
Expand All @@ -109,7 +100,10 @@ describe("GetUserAgencyDashboardUrl", () => {
...icNotAdmin,
agencyRights: [
{
agency: agencyWithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(
agency,
agencyUserRole === "agency-admin" ? [icNotAdmin.email] : [],
),
roles: [agencyUserRole],
isNotifiedByEmail: true,
},
Expand Down Expand Up @@ -151,7 +145,7 @@ describe("GetUserAgencyDashboardUrl", () => {
...notAdmin,
agencyRights: [
{
agency: agencyWithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency, []),
roles: [agencyUserRole],
isNotifiedByEmail: true,
},
Expand Down Expand Up @@ -180,7 +174,7 @@ describe("GetUserAgencyDashboardUrl", () => {
...icNotAdmin,
agencyRights: [
{
agency: agencyWithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency, []),
roles: ["validator"],
isNotifiedByEmail: false,
},
Expand Down Expand Up @@ -208,27 +202,6 @@ describe("GetUserAgencyDashboardUrl", () => {
const agency3 = agencyBuilder.withId("3333").build();
const agency4 = agencyBuilder.withId("4444").build();

const {
validatorEmails: _,
counsellorEmails: __,
...agency1WithoutEmails
} = agency1;
const {
validatorEmails: ___,
counsellorEmails: ____,
...agency2WithoutEmails
} = agency2;
const {
validatorEmails: _____,
counsellorEmails: ______,
...agency3WithoutEmails
} = agency3;
const {
validatorEmails: _______,
counsellorEmails: ________,
...agency4WithoutEmails
} = agency4;

uow.userRepository.users = [notAdmin];
uow.agencyRepository.agencies = [
toAgencyWithRights(agency1, {
Expand Down Expand Up @@ -263,22 +236,24 @@ describe("GetUserAgencyDashboardUrl", () => {
...notAdmin,
agencyRights: [
{
agency: agency1WithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency1, []),
isNotifiedByEmail: true,
roles: ["counsellor"],
},
{
agency: agency2WithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency2, []),
isNotifiedByEmail: true,
roles: ["validator"],
},
{
agency: agency3WithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency3, []),
roles: ["to-review"],
isNotifiedByEmail: true,
},
{
agency: agency4WithoutEmails,
agency: toAgencyDtoForAgencyUsersAndAdmins(agency4, [
notAdmin.email,
]),
roles: ["agency-admin"],
isNotifiedByEmail: true,
},
Expand Down
Loading

0 comments on commit 7c1e0db

Please sign in to comment.