Skip to content

Commit

Permalink
AgencyDtoForAgencyUsersAndAdmins now have agency admin emails
Browse files Browse the repository at this point in the history
  • Loading branch information
bbohec committed Jan 10, 2025
1 parent 60f6234 commit 5348bb6
Show file tree
Hide file tree
Showing 32 changed files with 167 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AgencyOption,
AgencyPositionFilter,
AgencyStatus,
AgencyWithUsersRights,
DepartmentCode,
GeoPositionDto,
PartialAgencyDto,
Expand All @@ -21,7 +22,6 @@ import { distanceBetweenCoordinatesInMeters } from "../../../utils/distanceBetwe
import {
AgencyRepository,
AgencyRightOfUser,
AgencyWithUsersRights,
GetAgenciesFilters,
} from "../ports/AgencyRepository";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Pool } from "pg";
import {
AgencyDtoBuilder,
AgencyWithUsersRights,
ConflictError,
GeoPositionDto,
InclusionConnectedUserBuilder,
Expand All @@ -14,10 +15,7 @@ import { KyselyDb, makeKyselyDb } from "../../../config/pg/kysely/kyselyUtils";
import { getTestPgPool } from "../../../config/pg/pgUtils";
import { toAgencyWithRights } from "../../../utils/agency";
import { PgUserRepository } from "../../core/authentication/inclusion-connect/adapters/PgUserRepository";
import {
AgencyWithUsersRights,
AgencyWithoutRights,
} from "../ports/AgencyRepository";
import { AgencyWithoutRights } from "../ports/AgencyRepository";
import {
PgAgencyRepository,
safirConflictErrorMessage,
Expand Down
4 changes: 2 additions & 2 deletions back/src/domains/agency/adapters/PgAgencyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
AgencyKind,
AgencyRole,
AgencyStatus,
AgencyUsersRights,
AgencyWithUsersRights,
ConflictError,
DepartmentCode,
OmitFromExistingKeys,
Expand All @@ -28,8 +30,6 @@ import { createLogger } from "../../../utils/logger";
import {
AgencyRepository,
AgencyRightOfUser,
AgencyUsersRights,
AgencyWithUsersRights,
GetAgenciesFilters,
PartialAgencyWithUsersRights,
} from "../ports/AgencyRepository";
Expand Down
3 changes: 1 addition & 2 deletions back/src/domains/agency/entities/Agency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ConflictError } from "shared";
import { AgencyWithUsersRights, ConflictError } from "shared";
import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork";
import { AgencyWithUsersRights } from "../ports/AgencyRepository";

export const throwConflictErrorOnSimilarAgencyFound = async ({
uow,
Expand Down
19 changes: 1 addition & 18 deletions back/src/domains/agency/ports/AgencyRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type {
AgencyKind,
AgencyPositionFilter,
AgencyRight,
AgencyRole,
AgencyStatus,
AgencyWithUsersRights,
DepartmentCode,
OmitFromExistingKeys,
SiretDto,
Expand Down Expand Up @@ -40,23 +40,6 @@ export type AgencyRightOfUser = OmitFromExistingKeys<AgencyRight, "agency"> & {
agencyId: AgencyId;
};

export type AgencyUserRight = {
roles: AgencyRole[];
isNotifiedByEmail: boolean;
};

export type AgencyUsersRights = Partial<Record<UserId, AgencyUserRight>>;

export type WithAgencyUserRights = {
usersRights: AgencyUsersRights;
};

export type AgencyWithUsersRights = OmitFromExistingKeys<
AgencyDto,
"counsellorEmails" | "validatorEmails"
> &
WithAgencyUserRights;

export interface AgencyRepository {
insert(agency: AgencyWithUsersRights): Promise<AgencyId | undefined>;
update(partialAgency: PartialAgencyWithUsersRights): Promise<void>;
Expand Down
6 changes: 2 additions & 4 deletions back/src/domains/agency/use-cases/AddAgency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { toPairs } from "ramda";
import {
AgencyId,
AgencyRole,
AgencyUsersRights,
AgencyWithUsersRights,
CreateAgencyDto,
UserId,
createAgencySchema,
Expand All @@ -16,10 +18,6 @@ import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork";
import { UnitOfWorkPerformer } from "../../core/unit-of-work/ports/UnitOfWorkPerformer";
import { UuidGenerator } from "../../core/uuid-generator/ports/UuidGenerator";
import { throwConflictErrorOnSimilarAgencyFound } from "../entities/Agency";
import {
AgencyUsersRights,
AgencyWithUsersRights,
} from "../ports/AgencyRepository";

type WithUserIdAndIsNotified = {
userId: UserId;
Expand Down
6 changes: 2 additions & 4 deletions back/src/domains/agency/use-cases/ListAgenciesByFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AgencyKind,
AgencyKindFilter,
AgencyOption,
AgencyWithUsersRights,
ListAgencyOptionsRequestDto,
activeAgencyStatuses,
agencyKindList,
Expand All @@ -11,10 +12,7 @@ import {
} from "shared";
import { TransactionalUseCase } from "../../core/UseCase";
import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork";
import {
AgencyWithUsersRights,
GetAgenciesFilters,
} from "../ports/AgencyRepository";
import { GetAgenciesFilters } from "../ports/AgencyRepository";

export class ListAgencyOptionsByFilter extends TransactionalUseCase<
ListAgencyOptionsRequestDto,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { toPairs, uniq } from "ramda";
import {
AgencyUsersRights,
AgencyWithUsersRights,
InclusionConnectedUser,
WithAgencyId,
errors,
Expand All @@ -10,10 +12,6 @@ import { TransactionalUseCase } from "../../core/UseCase";
import { CreateNewEvent } from "../../core/events/ports/EventBus";
import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork";
import { UnitOfWorkPerformer } from "../../core/unit-of-work/ports/UnitOfWorkPerformer";
import {
AgencyUsersRights,
AgencyWithUsersRights,
} from "../ports/AgencyRepository";

export class UpdateAgencyReferringToUpdatedAgency extends TransactionalUseCase<
WithAgencyId,
Expand Down
66 changes: 62 additions & 4 deletions back/src/domains/convention/adapters/pgConventionSql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { sql } from "kysely";
import {
AgencyId,
AgencyKind,
AgencyRole,
AppellationCode,
AppellationLabel,
Beneficiary,
Expand All @@ -15,7 +16,9 @@ import {
RomeLabel,
ScheduleDto,
SiretDto,
UserId,
conventionReadSchema,
pipeWithValue,
} from "shared";
import {
KyselyDb,
Expand All @@ -25,10 +28,6 @@ import {
} from "../../../config/pg/kysely/kyselyUtils";
import { createLogger } from "../../../utils/logger";
import { parseZodSchemaAndLogErrorOnParsingFailure } from "../../../utils/schema.utils";
import {
getEmailsFromUsersWithAgencyRoles,
getUsersWithAgencyRole,
} from "../../core/authentication/inclusion-connect/adapters/agencyUsers.helpers";

export const createConventionQueryBuilder = (transaction: KyselyDb) => {
// biome-ignore format: reads better without formatting
Expand Down Expand Up @@ -347,3 +346,62 @@ export const makeGetLastConventionWithSiretInList =
)
.where("conventions.status", "=", "ACCEPTED_BY_VALIDATOR")
.where("conventions.siret", "in", sirets);

type UserWithAgencyRole = {
userId: UserId;
email: Email;
agencyId: AgencyId;
roles: AgencyRole[];
isNotifiedByEmail: boolean;
};

const getUsersWithAgencyRole = async (
transaction: KyselyDb,
{
agencyIds,
isNotifiedByEmail,
}: {
agencyIds: AgencyId[];
isNotifiedByEmail?: boolean;
},
): Promise<UserWithAgencyRole[]> => {
if (agencyIds.length === 0) return [];

return pipeWithValue(
transaction
.selectFrom("users__agencies")
.innerJoin("users", "users.id", "users__agencies.user_id")
.where("agency_id", "in", agencyIds)
.orderBy("users.email")
.select([
"users.id as userId",
"users.email",
sql<AgencyRole[]>`users__agencies.roles`.as("roles"),
"users__agencies.agency_id as agencyId",
"users__agencies.is_notified_by_email as isNotifiedByEmail",
]),
(builder) => {
if (isNotifiedByEmail !== undefined)
return builder.where("is_notified_by_email", "=", isNotifiedByEmail);
return builder;
},
(builder) => builder.execute(),
);
};

type AgencyMatchingCriteria = {
agencyIdToMatch: AgencyId;
roleToMatch: AgencyRole;
};

const getEmailsFromUsersWithAgencyRoles = (
usersWithAgencyRole: UserWithAgencyRole[],
{ agencyIdToMatch, roleToMatch }: AgencyMatchingCriteria,
) => {
return usersWithAgencyRole
.filter(
(user) =>
user.agencyId === agencyIdToMatch && user.roles.includes(roleToMatch),
)
.map((user) => user.email);
};
3 changes: 2 additions & 1 deletion back/src/domains/convention/entities/Convention.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { uniq } from "ramda";
import {
AgencyDto,
AgencyWithUsersRights,
ApiConsumer,
ConventionDto,
ConventionReadDto,
Expand All @@ -10,7 +11,7 @@ import {
errors,
statusTransitionConfigs,
} from "shared";
import { AgencyWithUsersRights } from "../../agency/ports/AgencyRepository";

import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork";

export const throwIfTransitionNotAllowed = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AgencyWithUsersRights,
ApiConsumer,
ApiConsumerName,
InclusionConnectedUser,
Expand All @@ -7,7 +8,6 @@ import {
userHasEnoughRightsOnConvention,
withConventionIdSchema,
} from "shared";
import { AgencyWithUsersRights } from "../../agency/ports/AgencyRepository";
import { createTransactionalUseCase } from "../../core/UseCase";
import { getIcUserByUserId } from "../../inclusion-connected-users/helpers/inclusionConnectedUser.helper";

Expand Down
2 changes: 1 addition & 1 deletion back/src/domains/convention/use-cases/GetConvention.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { toPairs } from "ramda";
import {
AgencyWithUsersRights,
ConventionDomainPayload,
ConventionId,
ConventionReadDto,
Expand All @@ -14,7 +15,6 @@ import {
} from "shared";
import { agencyWithRightToAgencyDto } from "../../../utils/agency";
import { conventionEmailsByRole } from "../../../utils/convention";
import { AgencyWithUsersRights } from "../../agency/ports/AgencyRepository";
import { TransactionalUseCase } from "../../core/UseCase";
import { makeProvider } from "../../core/authentication/inclusion-connect/port/OAuthGateway";
import { UnitOfWork } from "../../core/unit-of-work/ports/UnitOfWork";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TokenExpiredError, decode } from "jsonwebtoken";
import {
AgencyWithUsersRights,
BadRequestError,
ConventionDto,
ConventionId,
Expand All @@ -19,7 +20,6 @@ import { GenerateConventionMagicLinkUrl } from "../../../config/bootstrap/magicL
import { agencyWithRightToAgencyDto } from "../../../utils/agency";
import { conventionEmailsByRoleForMagicLinkRenewal } from "../../../utils/convention";
import { createLogger } from "../../../utils/logger";
import { AgencyWithUsersRights } from "../../agency/ports/AgencyRepository";
import { TransactionalUseCase } from "../../core/UseCase";
import { CreateNewEvent } from "../../core/events/ports/EventBus";
import { prepareMagicShortLinkMaker } from "../../core/short-link/ShortLink";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { format } from "date-fns";
import {
type AgencyDto,
type AgencyWithUsersRights,
type Beneficiary,
type BeneficiaryCurrentEmployer,
type BeneficiaryRepresentative,
Expand All @@ -23,7 +24,6 @@ import {
import { AppConfig } from "../../../../config/bootstrap/appConfig";
import { GenerateConventionMagicLinkUrl } from "../../../../config/bootstrap/magicLinkUrl";
import { agencyWithRightToAgencyDto } from "../../../../utils/agency";
import { AgencyWithUsersRights } from "../../../agency/ports/AgencyRepository";
import { TransactionalUseCase } from "../../../core/UseCase";
import { ConventionReminderPayload } from "../../../core/events/eventPayload.dto";
import { conventionReminderPayloadSchema } from "../../../core/events/eventPayload.schema";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AbsoluteUrl,
AgencyDtoBuilder,
AgencyWithUsersRights,
ConventionDto,
ConventionDtoBuilder,
EstablishmentRepresentative,
Expand All @@ -24,7 +25,6 @@ import {
ExpectSavedNotificationsBatchAndEvent,
makeExpectSavedNotificationsBatchAndEvent,
} from "../../../../utils/makeExpectSavedNotificationAndEvent.helpers";
import { AgencyWithUsersRights } from "../../../agency/ports/AgencyRepository";
import { makeSaveNotificationsBatchAndRelatedEvent } from "../../../core/notifications/helpers/Notification";
import { makeShortLinkUrl } from "../../../core/short-link/ShortLink";
import { DeterministShortLinkIdGeneratorGateway } from "../../../core/short-link/adapters/short-link-generator-gateway/DeterministShortLinkIdGeneratorGateway";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AgencyWithUsersRights,
ConventionDto,
Signatory,
SignatoryRole,
Expand All @@ -9,7 +10,6 @@ import {
withConventionSchema,
} from "shared";
import { GenerateConventionMagicLinkUrl } from "../../../../config/bootstrap/magicLinkUrl";
import { AgencyWithUsersRights } from "../../../agency/ports/AgencyRepository";
import { TransactionalUseCase } from "../../../core/UseCase";
import { SaveNotificationAndRelatedEvent } from "../../../core/notifications/helpers/Notification";
import { TimeGateway } from "../../../core/time-gateway/ports/TimeGateway";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
AgencyWithUsersRights,
ConventionDto,
ConventionDtoBuilder,
errors,
Expand All @@ -10,7 +11,6 @@ import {
ExpectSavedNotificationsAndEvents,
makeExpectSavedNotificationsAndEvents,
} from "../../../../utils/makeExpectSavedNotificationAndEvent.helpers";
import { AgencyWithUsersRights } from "../../../agency/ports/AgencyRepository";
import { makeSaveNotificationAndRelatedEvent } from "../../../core/notifications/helpers/Notification";
import { CustomTimeGateway } from "../../../core/time-gateway/adapters/CustomTimeGateway";
import { InMemoryUowPerformer } from "../../../core/unit-of-work/adapters/InMemoryUowPerformer";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { values } from "ramda";
import {
AgencyWithUsersRights,
ConventionDto,
ConventionJwtPayload,
Signatory,
Expand All @@ -11,7 +12,6 @@ import {
} from "shared";
import { AppConfig } from "../../../../config/bootstrap/appConfig";
import { GenerateConventionMagicLinkUrl } from "../../../../config/bootstrap/magicLinkUrl";
import { AgencyWithUsersRights } from "../../../agency/ports/AgencyRepository";
import { TransactionalUseCase } from "../../../core/UseCase";
import { SaveNotificationAndRelatedEvent } from "../../../core/notifications/helpers/Notification";
import { prepareMagicShortLinkMaker } from "../../../core/short-link/ShortLink";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AgencyDtoBuilder,
AgencyWithUsersRights,
ConventionDto,
ConventionDtoBuilder,
InclusionConnectedUserBuilder,
Expand All @@ -15,7 +16,6 @@ import {
ExpectSavedNotificationsAndEvents,
makeExpectSavedNotificationsAndEvents,
} from "../../../../utils/makeExpectSavedNotificationAndEvent.helpers";
import { AgencyWithUsersRights } from "../../../agency/ports/AgencyRepository";
import { ConventionPoleEmploiUserAdvisorEntity } from "../../../core/authentication/pe-connect/dto/PeConnect.dto";
import { makeSaveNotificationAndRelatedEvent } from "../../../core/notifications/helpers/Notification";
import { makeShortLinkUrl } from "../../../core/short-link/ShortLink";
Expand Down
Loading

0 comments on commit 5348bb6

Please sign in to comment.