Skip to content

Commit

Permalink
merge update and create user for agency params types
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-duport committed Sep 4, 2024
1 parent 58a09f9 commit dcc506b
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import {
UserUpdateParamsForAgency,
errors,
userUpdateParamsForAgencySchema,
} from "shared";
import { UserParamsForAgency, errors, userParamsForAgencySchema } from "shared";
import { TransactionalUseCase } from "../../../core/UseCase";
import { SaveNotificationAndRelatedEvent } from "../../../core/notifications/helpers/Notification";
import { UnitOfWork } from "../../../core/unit-of-work/ports/UnitOfWork";
import { UnitOfWorkPerformer } from "../../../core/unit-of-work/ports/UnitOfWorkPerformer";

export class NotifyIcUserAgencyRightChanged extends TransactionalUseCase<
UserUpdateParamsForAgency,
UserParamsForAgency,
void
> {
protected inputSchema = userUpdateParamsForAgencySchema;
protected inputSchema = userParamsForAgencySchema;

readonly #saveNotificationAndRelatedEvent: SaveNotificationAndRelatedEvent;

Expand All @@ -25,7 +21,7 @@ export class NotifyIcUserAgencyRightChanged extends TransactionalUseCase<
}

protected async _execute(
params: UserUpdateParamsForAgency,
params: UserParamsForAgency,
uow: UnitOfWork,
): Promise<void> {
const agency = await uow.agencyRepository.getById(params.agencyId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AgencyDtoBuilder,
InclusionConnectedUser,
UserUpdateParamsForAgency,
UserParamsForAgency,
errors,
expectPromiseToFailWithError,
} from "shared";
Expand All @@ -19,7 +19,7 @@ import {
import { UuidV4Generator } from "../../../core/uuid-generator/adapters/UuidGeneratorImplementations";
import { NotifyIcUserAgencyRightChanged } from "./NotifyIcUserAgencyRightChanged";

const icUserRoleParams: UserUpdateParamsForAgency = {
const icUserRoleParams: UserParamsForAgency = {
roles: ["counsellor"],
agencyId: "agency-1",
userId: "jbab-123",
Expand Down
4 changes: 2 additions & 2 deletions back/src/domains/core/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Role,
SiretDto,
UserId,
UserUpdateParamsForAgency,
UserParamsForAgency,
WithAgencyDto,
WithAssessmentDto,
WithConventionDto,
Expand Down Expand Up @@ -155,7 +155,7 @@ export type DomainEvent =
// We don't put full OAuth in payload to avoid private data in logs etc...
| GenericEvent<"UserAuthenticatedSuccessfully", UserAuthenticatedPayload & WithTriggeredBy>
| GenericEvent<"AgencyRegisteredToInclusionConnectedUser", { userId: UserId; agencyIds: AgencyId[] } & WithTriggeredBy>
| GenericEvent<"IcUserAgencyRightChanged", UserUpdateParamsForAgency & WithTriggeredBy>
| GenericEvent<"IcUserAgencyRightChanged", UserParamsForAgency & WithTriggeredBy>
| GenericEvent<"IcUserAgencyRightRejected", RejectIcUserRoleForAgencyParams & WithTriggeredBy>
// API CONSUMER related
| GenericEvent<"ApiConsumerSaved", { consumerId: string } & WithTriggeredBy>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
AgencyRight,
InclusionConnectedUser,
UserCreateParamsForAgency,
UserParamsForAgency,
errors,
userCreateParamsForAgencySchema,
userParamsForAgencySchema,
} from "shared";
import { createTransactionalUseCase } from "../../core/UseCase";
import { DomainEvent } from "../../core/events/events";
Expand All @@ -14,14 +14,14 @@ import { throwIfNotAdmin } from "../helpers/throwIfIcUserNotBackofficeAdmin";
export type CreateUserForAgency = ReturnType<typeof makeCreateUserForAgency>;

export const makeCreateUserForAgency = createTransactionalUseCase<
UserCreateParamsForAgency,
UserParamsForAgency,
void,
InclusionConnectedUser,
{ timeGateway: TimeGateway; createNewEvent: CreateNewEvent }
>(
{
name: "CreateUserForAgency",
inputSchema: userCreateParamsForAgencySchema,
inputSchema: userParamsForAgencySchema,
},
async ({ inputParams, uow, currentUser, deps }) => {
throwIfNotAdmin(currentUser);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
AgencyDtoBuilder,
InclusionConnectedUserBuilder,
UserCreateParamsForAgency,
UserParamsForAgency,
errors,
expectPromiseToFailWithError,
expectToEqual,
Expand Down Expand Up @@ -154,7 +154,7 @@ describe("CreateUserForAgency", () => {
const newUserId = uuidGenerator.new();
userRepository.users = [];

const icUserForAgency: UserCreateParamsForAgency = {
const icUserForAgency: UserParamsForAgency = {
userId: newUserId,
agencyId: agency.id,
roles: ["counsellor"],
Expand Down Expand Up @@ -220,7 +220,7 @@ describe("CreateUserForAgency", () => {
],
});

const icUserForAgency: UserCreateParamsForAgency = {
const icUserForAgency: UserParamsForAgency = {
userId,
agencyId: anotherAgency.id,
roles: ["counsellor"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
AgencyDto,
AgencyRight,
Email,
InclusionConnectedUser,
UserUpdateParamsForAgency,
UserParamsForAgency,
errors,
replaceElementWhere,
userUpdateParamsForAgencySchema,
userParamsForAgencySchema,
} from "shared";
import { TransactionalUseCase } from "../../core/UseCase";
import { UserRepository } from "../../core/authentication/inclusion-connect/port/UserRepository";
Expand All @@ -17,7 +18,7 @@ import { throwIfNotAdmin } from "../helpers/throwIfIcUserNotBackofficeAdmin";

const rejectIfAgencyWontHaveValidators = async (
uow: UnitOfWork,
params: UserUpdateParamsForAgency,
params: UserParamsForAgency,
agency: AgencyDto,
) => {
if (
Expand All @@ -43,7 +44,7 @@ const rejectIfAgencyWontHaveValidators = async (
};

const rejectIfOneStepValidationAgencyWontHaveValidatorsReceivingEmails = async (
params: UserUpdateParamsForAgency,
params: UserParamsForAgency,
agency: AgencyDto,
) => {
if (
Expand All @@ -59,7 +60,7 @@ const rejectIfOneStepValidationAgencyWontHaveValidatorsReceivingEmails = async (

const rejectIfAgencyWithRefersToWontHaveCounsellors = async (
uow: UnitOfWork,
params: UserUpdateParamsForAgency,
params: UserParamsForAgency,
agency: AgencyDto,
) => {
if (
Expand All @@ -86,7 +87,7 @@ const rejectIfAgencyWithRefersToWontHaveCounsellors = async (

const makeAgencyRights = async (
uow: UnitOfWork,
params: UserUpdateParamsForAgency,
params: UserParamsForAgency,
userToUpdate: InclusionConnectedUser,
) => {
const agency = await uow.agencyRepository.getById(params.agencyId);
Expand Down Expand Up @@ -127,7 +128,7 @@ const makeAgencyRights = async (

const rejectEmailModificationIfInclusionConnectedUser = (
user: InclusionConnectedUser,
newEmail: string | null,
newEmail: Email,
) => {
if (!newEmail || !user.externalId) return;
if (user.email !== newEmail) {
Expand All @@ -137,19 +138,19 @@ const rejectEmailModificationIfInclusionConnectedUser = (

const updateIfUserEmailChanged = async (
user: InclusionConnectedUser,
newEmail: string | null,
newEmail: Email,
userRepository: UserRepository,
) => {
if (!newEmail || user.email === newEmail) return;
if (user.email === newEmail || user.externalId) return;
await userRepository.updateEmail(user.id, newEmail);
};

export class UpdateUserForAgency extends TransactionalUseCase<
UserUpdateParamsForAgency,
UserParamsForAgency,
void,
InclusionConnectedUser
> {
protected inputSchema = userUpdateParamsForAgencySchema;
protected inputSchema = userParamsForAgencySchema;

readonly #createNewEvent: CreateNewEvent;

Expand All @@ -163,7 +164,7 @@ export class UpdateUserForAgency extends TransactionalUseCase<
}

protected async _execute(
params: UserUpdateParamsForAgency,
params: UserParamsForAgency,
uow: UnitOfWork,
currentUser: InclusionConnectedUser,
): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
InclusionConnectedUser,
InclusionConnectedUserBuilder,
User,
UserUpdateParamsForAgency,
UserParamsForAgency,
errors,
expectPromiseToFailWithError,
expectToEqual,
Expand Down Expand Up @@ -204,33 +204,6 @@ describe("UpdateUserForAgency", () => {
);
});

it("does not modify email if not requested", async () => {
const icUser: InclusionConnectedUser = {
...notAdminUser,
externalId: null,
agencyRights: [agencyRight],
dashboards: {
agencies: {},
establishments: {},
},
};
userRepository.setInclusionConnectedUsers([icUser]);

await updateIcUserRoleForAgency.execute(
{
agencyId: agency.id,
roles: ["validator"],
userId: icUser.id,
isNotifiedByEmail: true,
email: null,
},
backofficeAdminUser,
);

const updatedUser = await userRepository.getById(icUser.id);
expectToEqual(updatedUser?.email, icUser.email);
});

it("does not modify email if it hasn't changed", async () => {
const icUser: InclusionConnectedUser = {
...notAdminUser,
Expand Down Expand Up @@ -322,7 +295,7 @@ describe("UpdateUserForAgency", () => {
validator,
]);

const icUserRoleForAgency: UserUpdateParamsForAgency = {
const icUserRoleForAgency: UserParamsForAgency = {
roles: ["counsellor", "validator", "agencyOwner"],
agencyId: agency.id,
userId: notAdminUser.id,
Expand Down Expand Up @@ -383,7 +356,7 @@ describe("UpdateUserForAgency", () => {

userRepository.setInclusionConnectedUsers([backofficeAdminUser, icUser]);
const newRole: AgencyRole = "validator";
const icUserRoleForAgency: UserUpdateParamsForAgency = {
const icUserRoleForAgency: UserParamsForAgency = {
userId: notAdminUser.id,
agencyId: agency.id,
roles: [newRole],
Expand Down
12 changes: 6 additions & 6 deletions front/src/app/components/agency/AgencyUserModificationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { keys } from "react-design-system";
import { FormProvider, useForm } from "react-hook-form";
import { useDispatch } from "react-redux";
import {
UserUpdateParamsForAgency,
UserParamsForAgency,
domElementIds,
userUpdateParamsForAgencySchema,
userParamsForAgencySchema,
} from "shared";
import { agencyRoleToDisplay } from "src/app/components/agency/AgencyUsers";
import { EmailValidationInput } from "src/app/components/forms/commons/EmailValidationInput";
Expand All @@ -23,14 +23,14 @@ export const AgencyUserModificationForm = ({
agencyUser,
closeModal,
}: {
agencyUser: UserUpdateParamsForAgency & { isIcUser: boolean };
agencyUser: UserParamsForAgency & { isIcUser: boolean };
closeModal: () => void;
}) => {
const agency = useAppSelector(agencyAdminSelectors.agency);
const dispatch = useDispatch();

const methods = useForm<UserUpdateParamsForAgency>({
resolver: zodResolver(userUpdateParamsForAgencySchema),
const methods = useForm<UserParamsForAgency>({
resolver: zodResolver(userParamsForAgencySchema),
mode: "onTouched",
defaultValues: agencyUser,
});
Expand All @@ -41,7 +41,7 @@ export const AgencyUserModificationForm = ({

const getFieldError = makeFieldError(formState);

const onValidSubmit = (values: UserUpdateParamsForAgency) => {
const onValidSubmit = (values: UserParamsForAgency) => {
dispatch(
icUsersAdminSlice.actions.updateUserOnAgencyRequested({
...values,
Expand Down
4 changes: 2 additions & 2 deletions front/src/app/components/agency/AgencyUsers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useDispatch } from "react-redux";
import {
AgencyId,
AgencyRole,
UserUpdateParamsForAgency,
UserParamsForAgency,
domElementIds,
} from "shared";
import { AgencyUserModificationForm } from "src/app/components/agency/AgencyUserModificationForm";
Expand Down Expand Up @@ -65,7 +65,7 @@ export const AgencyUsers = ({ agencyId }: AgencyUsersProperties) => {
const dispatch = useDispatch();

const [selectedUserData, setSelectedUserData] = useState<
(UserUpdateParamsForAgency & { isIcUser: boolean }) | null
(UserParamsForAgency & { isIcUser: boolean }) | null
>(null);

return (
Expand Down
Loading

0 comments on commit dcc506b

Please sign in to comment.