Skip to content

Commit

Permalink
trigger ic user agency right changed when adding user
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-duport committed Sep 4, 2024
1 parent e825420 commit 58a09f9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 39 deletions.
1 change: 1 addition & 0 deletions back/src/config/bootstrap/createUseCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ export const createUseCases = (
uowPerformer,
deps: {
timeGateway: gateways.timeGateway,
createNewEvent,
},
}),
broadcastConventionAgain: makeBroadcastConventionAgain({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
userCreateParamsForAgencySchema,
} from "shared";
import { createTransactionalUseCase } from "../../core/UseCase";
import { DomainEvent } from "../../core/events/events";
import { CreateNewEvent } from "../../core/events/ports/EventBus";
import { TimeGateway } from "../../core/time-gateway/ports/TimeGateway";
import { throwIfNotAdmin } from "../helpers/throwIfIcUserNotBackofficeAdmin";

Expand All @@ -15,50 +17,61 @@ export const makeCreateUserForAgency = createTransactionalUseCase<
UserCreateParamsForAgency,
void,
InclusionConnectedUser,
{ timeGateway: TimeGateway }
{ timeGateway: TimeGateway; createNewEvent: CreateNewEvent }
>(
{
name: "CreateUserForAgency",
inputSchema: userCreateParamsForAgencySchema,
},
async ({
inputParams: { agencyId, email, isNotifiedByEmail, roles, userId },
uow,
currentUser,
deps,
}) => {
async ({ inputParams, uow, currentUser, deps }) => {
throwIfNotAdmin(currentUser);
const agency = await uow.agencyRepository.getById(agencyId);
if (!agency) throw errors.agency.notFound({ agencyId });
const agency = await uow.agencyRepository.getById(inputParams.agencyId);
if (!agency)
throw errors.agency.notFound({ agencyId: inputParams.agencyId });

if (agency.refersToAgencyId && roles.includes("validator"))
if (agency.refersToAgencyId && inputParams.roles.includes("validator"))
throw errors.agency.invalidRoleUpdateForAgencyWithRefersTo({
agencyId: agency.id,
role: "validator",
});

const existingUser = await uow.userRepository.getById(userId);
const existingUser = await uow.userRepository.getById(inputParams.userId);

if (!existingUser) {
await uow.userRepository.save({
createdAt: deps.timeGateway.now().toISOString(),
email,
email: inputParams.email,
externalId: null,
firstName: "",
id: userId,
id: inputParams.userId,
lastName: "",
});
}

const existingUserAgencyRights = existingUser?.agencyRights ?? [];
const agencyRight: AgencyRight = {
roles,
isNotifiedByEmail: isNotifiedByEmail,
roles: inputParams.roles,
isNotifiedByEmail: inputParams.isNotifiedByEmail,
agency,
};
await uow.userRepository.updateAgencyRights({
userId: userId,
agencyRights: [...existingUserAgencyRights, agencyRight],

const event: DomainEvent = deps.createNewEvent({
topic: "IcUserAgencyRightChanged",
payload: {
...inputParams,
triggeredBy: {
kind: "inclusion-connected",
userId: currentUser.id,
},
},
});

await Promise.all([
uow.userRepository.updateAgencyRights({
userId: inputParams.userId,
agencyRights: [...existingUserAgencyRights, agencyRight],
}),
uow.outboxRepository.save(event),
]);
},
);
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {
AgencyDtoBuilder,
InclusionConnectedUserBuilder,
UserCreateParamsForAgency,
errors,
expectPromiseToFailWithError,
expectToEqual,
} from "shared";
import { InMemoryAgencyRepository } from "../../agency/adapters/InMemoryAgencyRepository";
import { InMemoryUserRepository } from "../../core/authentication/inclusion-connect/adapters/InMemoryUserRepository";
import { InMemoryOutboxRepository } from "../../core/events/adapters/InMemoryOutboxRepository";
import {
CreateNewEvent,
makeCreateNewEvent,
} from "../../core/events/ports/EventBus";
import { CustomTimeGateway } from "../../core/time-gateway/adapters/CustomTimeGateway";
import { TimeGateway } from "../../core/time-gateway/ports/TimeGateway";
import { InMemoryUowPerformer } from "../../core/unit-of-work/adapters/InMemoryUowPerformer";
Expand Down Expand Up @@ -39,22 +45,29 @@ describe("CreateUserForAgency", () => {
let agencyRepository: InMemoryAgencyRepository;
let timeGateway: TimeGateway;
let uuidGenerator: UuidGenerator;
let outboxRepository: InMemoryOutboxRepository;
let createNewEvent: CreateNewEvent;

beforeEach(() => {
const uow = createInMemoryUow();

userRepository = uow.userRepository;
agencyRepository = uow.agencyRepository;
outboxRepository = uow.outboxRepository;
uowPerformer = new InMemoryUowPerformer(uow);
userRepository.setInclusionConnectedUsers([
backofficeAdminUser,
notAdminUser,
]);
timeGateway = new CustomTimeGateway();
uuidGenerator = new TestUuidGenerator();
createNewEvent = makeCreateNewEvent({
uuidGenerator,
timeGateway,
});
createUserForAgency = makeCreateUserForAgency({
uowPerformer,
deps: { timeGateway, uuidGenerator },
deps: { timeGateway, createNewEvent },
});
agencyRepository.setAgencies([agency]);
});
Expand Down Expand Up @@ -141,16 +154,15 @@ describe("CreateUserForAgency", () => {
const newUserId = uuidGenerator.new();
userRepository.users = [];

await createUserForAgency.execute(
{
userId: newUserId,
agencyId: agency.id,
roles: ["counsellor"],
isNotifiedByEmail: false,
email: "[email protected]",
},
backofficeAdminUser,
);
const icUserForAgency: UserCreateParamsForAgency = {
userId: newUserId,
agencyId: agency.id,
roles: ["counsellor"],
isNotifiedByEmail: false,
email: "[email protected]",
};

await createUserForAgency.execute(icUserForAgency, backofficeAdminUser);

expect(userRepository.users.length).toBe(1);
expectToEqual(await userRepository.getById(newUserId), {
Expand All @@ -168,6 +180,19 @@ describe("CreateUserForAgency", () => {
establishments: {},
},
});

expectToEqual(outboxRepository.events, [
createNewEvent({
topic: "IcUserAgencyRightChanged",
payload: {
...icUserForAgency,
triggeredBy: {
kind: "inclusion-connected",
userId: backofficeAdminUser.id,
},
},
}),
]);
});

it("add agency rights to an existing user", async () => {
Expand Down Expand Up @@ -195,16 +220,15 @@ describe("CreateUserForAgency", () => {
],
});

await createUserForAgency.execute(
{
userId,
agencyId: anotherAgency.id,
roles: ["counsellor"],
isNotifiedByEmail: false,
email: "[email protected]",
},
backofficeAdminUser,
);
const icUserForAgency: UserCreateParamsForAgency = {
userId,
agencyId: anotherAgency.id,
roles: ["counsellor"],
isNotifiedByEmail: false,
email: "[email protected]",
};

await createUserForAgency.execute(icUserForAgency, backofficeAdminUser);

expectToEqual(await userRepository.getById(userId), {
createdAt: timeGateway.now().toISOString(),
Expand All @@ -230,5 +254,17 @@ describe("CreateUserForAgency", () => {
establishments: {},
},
});
expectToEqual(outboxRepository.events, [
createNewEvent({
topic: "IcUserAgencyRightChanged",
payload: {
...icUserForAgency,
triggeredBy: {
kind: "inclusion-connected",
userId: backofficeAdminUser.id,
},
},
}),
]);
});
});

0 comments on commit 58a09f9

Please sign in to comment.