Skip to content

Commit 4cfef76

Browse files
committed
feat(domifa): log des action utilisateur structure
1 parent 84adc28 commit 4cfef76

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

packages/backend/src/_common/model/app-log/LogAction.type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export type LogAction =
2929
| "IMPORT_USAGERS_FAILED" // Import échoué
3030
| "DOWNLOAD_IMPORT_TEMPLATE" // Téléchargement modèle Excel
3131
| "DOWNLOAD_IMPORT_GUIDE" // Téléchargement guide PDF
32+
| "USER_ROLE_CHANGE"
33+
| "USER_CREATE"
34+
| "USER_DELETE"
3235
// ADMIN
3336
| "ADMIN_STRUCTURE_VALIDATE"
3437
| "ADMIN_STRUCTURE_DELETE"

packages/backend/src/modules/app-logs/app-log-context.types.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UserSupervisorRole } from "@domifa/common";
1+
import { UserStructureRole, UserSupervisorRole } from "@domifa/common";
22

33
export type FailedUsagerImportLogContext = {
44
// IMPORT_USAGERS_FAILED
@@ -25,3 +25,18 @@ export type AdminUserRoleChangeLogContext = {
2525
oldRole: UserSupervisorRole;
2626
newRole: UserSupervisorRole;
2727
};
28+
29+
export type UserStructureRoleChangeLogContext = {
30+
// USER_ROLE_CHANGE
31+
userId: number;
32+
structureId: number;
33+
oldRole: UserStructureRole;
34+
newRole: UserStructureRole;
35+
};
36+
37+
export type UserStructureCreateLogContext = {
38+
// USER_CREATE
39+
userId: number;
40+
structureId: number;
41+
role: UserStructureRole;
42+
};

packages/backend/src/modules/portail-admin/controllers/admin-users/admin-users.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class AdminUsersController {
6565
@Res() res: ExpressResponse,
6666
@Body() registerUserDto: RegisterUserStructureAdminDto
6767
): Promise<ExpressResponse> {
68-
const userController = new UsersController();
68+
const userController = new UsersController(this.appLogsService); // D'ou c'est un mauvaise idée de instancier directement le controleur ici
6969
await this.appLogsService.create({
7070
userId: user.id,
7171
action: "ADMIN_CREATE_USER_STRUCTURE",

packages/backend/src/modules/users/controllers/users.controller.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ import { POST_USER_STRUCTURE_BODY } from "../../../_common/mocks";
1616
import { TESTS_USERS_STRUCTURE } from "../../../_tests";
1717
import { usersDeletor } from "../services/users-deletor.service";
1818
import { MailsModule } from "../../mails/mails.module";
19+
import { AppLogsService } from "../../app-logs/app-logs.service";
1920

2021
describe("Users Controller", () => {
2122
let controller: UsersController;
2223
let context: AppTestContext;
23-
24+
let appLogService: AppLogsService;
2425
beforeAll(async () => {
26+
appLogService = {
27+
create: jest.fn(),
28+
};
2529
context = await AppTestHelper.bootstrapTestApp({
2630
controllers: [UsersController],
2731
imports: [MailsModule, StructuresModule, UsagersModule, HttpModule],
32+
providers: [
33+
{
34+
provide: AppLogsService,
35+
useValue: appLogService,
36+
},
37+
],
2838
});
2939

3040
const authInfo =
@@ -54,7 +64,14 @@ describe("Users Controller", () => {
5464
structure: { ...POST_USER_STRUCTURE_BODY.structure, id: 1 },
5565
},
5666
});
57-
67+
expect(appLogService.create).toHaveBeenCalledWith({
68+
action: "USER_CREATE",
69+
context: {
70+
role: POST_USER_STRUCTURE_BODY.role,
71+
structureId: 1,
72+
userId: POST_USER_STRUCTURE_BODY.id,
73+
},
74+
});
5875
expect(response.status).toBe(200);
5976
expect(response.text).toBe('{"message":"OK"}');
6077
});

packages/backend/src/modules/users/controllers/users.controller.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ import {
4646
} from "../services";
4747
import { userAccountCreatedByAdminEmailSender } from "../../mails/services/templates-renderers";
4848
import { RegisterUserStructureAdminDto } from "../../portail-admin";
49+
import { AppLogsService } from "../../app-logs/app-logs.service";
50+
import {
51+
UserStructureCreateLogContext,
52+
UserStructureRoleChangeLogContext,
53+
} from "../../app-logs/app-log-context.types";
4954

5055
const userProfile: UserProfile = "structure";
5156

@@ -55,6 +60,7 @@ const userProfile: UserProfile = "structure";
5560
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL)
5661
@UseGuards(AuthGuard("jwt"), AppUserGuard)
5762
export class UsersController {
63+
constructor(private readonly appLogService: AppLogsService) {}
5864
@ApiBearerAuth()
5965
@ApiOperation({ summary: "Liste des utilisateurs" })
6066
@Get("")
@@ -118,13 +124,25 @@ export class UsersController {
118124
@Param("userUuid", new ParseUUIDPipe()) _userUuid: string,
119125
@CurrentChosenUserStructure() chosenUserStructure: UserStructure
120126
): Promise<UserStructureProfile> {
127+
const userToUpdate = await userStructureRepository.findOneBy({
128+
uuid: chosenUserStructure.uuid,
129+
});
121130
await userStructureRepository.update(
122131
{
123132
uuid: chosenUserStructure.uuid,
124133
structureId: userStructureAuth.structureId,
125134
},
126135
{ role: updateRoleDto.role }
127136
);
137+
await this.appLogService.create<UserStructureRoleChangeLogContext>({
138+
action: "USER_ROLE_CHANGE",
139+
context: {
140+
newRole: updateRoleDto.role,
141+
oldRole: userToUpdate.role,
142+
userId: userToUpdate.id,
143+
structureId: userToUpdate.structureId,
144+
},
145+
});
128146
return userStructureRepository.findOneBy({
129147
uuid: chosenUserStructure.uuid,
130148
});
@@ -200,6 +218,14 @@ export class UsersController {
200218
userId: chosenUserStructure.id,
201219
structureId: userStructureAuth.structureId,
202220
});
221+
await this.appLogService.create<UserStructureCreateLogContext>({
222+
action: "USER_DELETE",
223+
context: {
224+
role: chosenUserStructure.role,
225+
userId: chosenUserStructure.id,
226+
structureId: userStructureAuth.structureId,
227+
},
228+
});
203229

204230
return res.status(HttpStatus.OK).json({ message: "OK" });
205231
}
@@ -266,7 +292,15 @@ export class UsersController {
266292
userProfile,
267293
})
268294
.then(
269-
() => {
295+
async () => {
296+
await this.appLogService.create<UserStructureCreateLogContext>({
297+
action: "USER_CREATE",
298+
context: {
299+
role: user.role,
300+
userId: user.id,
301+
structureId: user.structureId,
302+
},
303+
});
270304
return res.status(HttpStatus.OK).json({ message: "OK" });
271305
},
272306
() => {

0 commit comments

Comments
 (0)