From 824b3d53b81bb2fb0ee4ed3fb3f5e149f91f3157 Mon Sep 17 00:00:00 2001 From: hbalty Date: Wed, 20 Aug 2025 00:13:55 +0200 Subject: [PATCH 1/2] fix(logs): fixed total count for success and changed events names --- .../_common/model/app-log/LogAction.type.ts | 5 +- .../import/import.controller.spec.ts | 82 +++++++------------ .../controllers/import/import.controller.ts | 18 +++- .../general/components/faq/faq.component.html | 1 - 4 files changed, 46 insertions(+), 60 deletions(-) diff --git a/packages/backend/src/_common/model/app-log/LogAction.type.ts b/packages/backend/src/_common/model/app-log/LogAction.type.ts index 74a2a876ab..c4c727badb 100644 --- a/packages/backend/src/_common/model/app-log/LogAction.type.ts +++ b/packages/backend/src/_common/model/app-log/LogAction.type.ts @@ -26,9 +26,10 @@ export type LogAction = | "GET_STATS_PORTAIL_ADMIN" | "EXPORT_STATS_PORTAIL_ADMIN" | "IMPORT_USAGERS_SUCCESS" // Import réussi + | "IMPORT_USAGERS_PREVIEW" // Import preview réussi | "IMPORT_USAGERS_FAILED" // Import échoué - | "DOWNLOAD_IMPORT_TEMPLATE" // Téléchargement modèle Excel - | "DOWNLOAD_IMPORT_GUIDE" // Téléchargement guide PDF + | "IMPORT_TEMPLATE_DOWNLOAD" // Téléchargement modèle Excel + | "IMPORT_DOWNLOAD_GUIDE" // Téléchargement guide PDF | "USER_ROLE_CHANGE" | "USER_CREATE" | "USER_DELETE" diff --git a/packages/backend/src/usagers/controllers/import/import.controller.spec.ts b/packages/backend/src/usagers/controllers/import/import.controller.spec.ts index fdb77d2e7a..9ce1934d5a 100644 --- a/packages/backend/src/usagers/controllers/import/import.controller.spec.ts +++ b/packages/backend/src/usagers/controllers/import/import.controller.spec.ts @@ -1,4 +1,3 @@ -import { FailedUsagerImportLogContext } from "./../../../modules/app-logs/app-log-context.types"; import { HttpStatus } from "@nestjs/common"; import { pathExists } from "fs-extra"; @@ -18,7 +17,7 @@ import { ImportController } from "./import.controller"; import { UsagerHistoryStateService } from "../../services/usagerHistoryState.service"; import { UsagersModule } from "../../usagers.module"; import { AppLogsService } from "../../../modules/app-logs/app-logs.service"; -import { AppLog } from "../../../_common/model"; +import { appLogsRepository } from "../../../database"; const importFilesDir = resolve( __dirname, @@ -27,26 +26,15 @@ const importFilesDir = resolve( describe("Import Controller", () => { let controller: ImportController; - let appLogService: AppLogsService; let context: AppTestContext; let authInfo: TestUserStructure; beforeAll(async () => { - appLogService = { - create: jest.fn(), - }; context = await AppTestHelper.bootstrapTestApp( { controllers: [ImportController], imports: [UsersModule, StructuresModule, UsagersModule], - providers: [ - UsagersService, - UsagerHistoryStateService, - { - provide: AppLogsService, - useValue: appLogService, - }, - ], + providers: [UsagersService, UsagerHistoryStateService, AppLogsService], }, { initApp: true } ); @@ -58,8 +46,8 @@ describe("Import Controller", () => { controller = context.module.get(ImportController); }); - beforeEach(() => { - jest.clearAllMocks(); + beforeEach(async () => { + await appLogsRepository.clear(); }); afterAll(async () => { @@ -71,20 +59,6 @@ describe("Import Controller", () => { }); it(`❌ Import d'un fichier Incorrect`, async () => { - const expectedLogContextEntree: FailedUsagerImportLogContext = { - nombreActifs: 0, - nombreErreurs: 5, - nombreTotal: 2, - }; - - const expectedLog: AppLog = { - userId: authInfo.id, - structureId: authInfo.structureId, - role: authInfo.role, - context: expectedLogContextEntree, - action: "IMPORT_USAGERS_FAILED", - }; - const importFilePath = resolve(importFilesDir, "import_ko_1.xlsx"); expect(await pathExists(importFilePath)).toBeTruthy(); @@ -98,9 +72,16 @@ describe("Import Controller", () => { context, }); - expect(appLogService.create).toHaveBeenCalledWith(expectedLog); - expect(response.status).toBe(HttpStatus.BAD_REQUEST); + + const logs = await appLogsRepository.find({}); + expect(logs.length).toBe(1); + expect(logs[0].action).toBe("IMPORT_USAGERS_FAILED"); + expect(logs[0].context).toEqual({ + nombreActifs: 0, + nombreErreurs: 5, + nombreTotal: 2, + }); }); it(`✅ Import d'un fichier Valide 1️⃣`, async () => { @@ -116,16 +97,6 @@ describe("Import Controller", () => { attachments: { file: importFilePath }, context, }); - expect(appLogService.create).toHaveBeenCalledWith({ - action: "IMPORT_USAGERS_SUCCESS", - context: { - nombreActifs: 19, - nombreTotal: 0, - }, - userId: authInfo.id, - structureId: authInfo.structureId, - role: authInfo.role, - }); expect(response.status).toBe(HttpStatus.OK); expect(JSON.parse(response.text)).toEqual({ importMode: "confirm", @@ -136,11 +107,18 @@ describe("Import Controller", () => { totalCount: 0, }, }); + + const logs = await appLogsRepository.find({}); + expect(logs.length).toBe(1); + expect(logs[0].action).toBe("IMPORT_USAGERS_SUCCESS"); + expect(logs[0].context).toEqual({ + nombreActifs: 19, + nombreTotal: 19, + }); }); it(`✅ Import d'un fichier Valide 2️⃣`, async () => { const importFilePath = resolve(importFilesDir, "import_ok_2.xlsx"); - expect(await pathExists(importFilePath)).toBeTruthy(); const headers: { [name: string]: string } = {}; @@ -151,16 +129,6 @@ describe("Import Controller", () => { attachments: { file: importFilePath }, context, }); - expect(appLogService.create).toHaveBeenCalledWith({ - action: "IMPORT_USAGERS_SUCCESS", - context: { - nombreActifs: 2, - nombreTotal: 0, - }, - userId: authInfo.id, - structureId: authInfo.structureId, - role: authInfo.role, - }); expect(response.status).toBe(HttpStatus.OK); expect(JSON.parse(response.text)).toEqual({ importMode: "confirm", @@ -171,5 +139,13 @@ describe("Import Controller", () => { totalCount: 0, }, }); + + const logs = await appLogsRepository.find({}); + expect(logs.length).toBe(1); + expect(logs[0].action).toBe("IMPORT_USAGERS_SUCCESS"); + expect(logs[0].context).toEqual({ + nombreActifs: 2, + nombreTotal: 4, + }); }); }); diff --git a/packages/backend/src/usagers/controllers/import/import.controller.ts b/packages/backend/src/usagers/controllers/import/import.controller.ts index f431eb17dd..6ae695dbc1 100644 --- a/packages/backend/src/usagers/controllers/import/import.controller.ts +++ b/packages/backend/src/usagers/controllers/import/import.controller.ts @@ -218,7 +218,9 @@ export class ImportController { totalCount: importPreviewRows.length, errorsCount: importErrors.length, // keep only errors, limit to 50 results - rows: importPreviewRows.filter(({ isValid }) => !isValid).slice(0, 50), + rows: [...importPreviewRows] + .filter(({ isValid }) => !isValid) + .slice(0, 50), }; await this.appLogsService.create({ action: "IMPORT_USAGERS_FAILED", @@ -302,7 +304,7 @@ export class ImportController { role: user.role, context: { nombreActifs: extractUsagersNumber(usagersRows), - nombreTotal: importPreviewRows.length, + nombreTotal: usagersRows.length, }, }); return res.status(HttpStatus.OK).json({ @@ -313,17 +315,25 @@ export class ImportController { @Get("log-document-download/:documentType") public async logDocumentDownload( + @CurrentUser() + user: UserStructureAuthenticated, @Param("documentType", new ParseEnumPipe(ImportDocumentType)) documentType: ImportDocumentType ) { if (documentType === ImportDocumentType.GUIDE) await this.appLogsService.create({ - action: "DOWNLOAD_IMPORT_GUIDE", + role: user.role, + structureId: user.structureId, + userId: user.id, + action: "IMPORT_DOWNLOAD_GUIDE", }); if (documentType === ImportDocumentType.MODELE) await this.appLogsService.create({ - action: "DOWNLOAD_IMPORT_TEMPLATE", + role: user.role, + structureId: user.structureId, + userId: user.id, + action: "IMPORT_TEMPLATE_DOWNLOAD", }); } } diff --git a/packages/frontend/src/app/modules/general/components/faq/faq.component.html b/packages/frontend/src/app/modules/general/components/faq/faq.component.html index 5834d23cfd..066b580d17 100644 --- a/packages/frontend/src/app/modules/general/components/faq/faq.component.html +++ b/packages/frontend/src/app/modules/general/components/faq/faq.component.html @@ -6,7 +6,6 @@

Questions / Réponses sur DomiFa

Date: Mon, 25 Aug 2025 11:37:47 +0100 Subject: [PATCH 2/2] fix(backend): fix reviews --- .../1756117243336-manual-migration.ts | 27 +++++++++++++++++++ .../import/import.controller.spec.ts | 21 ++++++++++++--- .../controllers/import/import.controller.ts | 24 +++++++---------- 3 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 packages/backend/src/_migrations/1756117243336-manual-migration.ts diff --git a/packages/backend/src/_migrations/1756117243336-manual-migration.ts b/packages/backend/src/_migrations/1756117243336-manual-migration.ts new file mode 100644 index 0000000000..3db8e736ca --- /dev/null +++ b/packages/backend/src/_migrations/1756117243336-manual-migration.ts @@ -0,0 +1,27 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class ManualMigration1756117243336 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + UPDATE "app_log" + SET action = CASE + WHEN action = 'DOWNLOAD_IMPORT_GUIDE' THEN 'IMPORT_DOWNLOAD_GUIDE' + WHEN action = 'DOWNLOAD_IMPORT_TEMPLATE' THEN 'IMPORT_TEMPLATE_DOWNLOAD' + ELSE action + END + WHERE action IN ('DOWNLOAD_IMPORT_GUIDE', 'DOWNLOAD_IMPORT_TEMPLATE'); + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + UPDATE "app_log" + SET action = CASE + WHEN action = 'IMPORT_DOWNLOAD_GUIDE' THEN 'DOWNLOAD_IMPORT_GUIDE' + WHEN action = 'IMPORT_TEMPLATE_DOWNLOAD' THEN 'DOWNLOAD_IMPORT_TEMPLATE' + ELSE action + END + WHERE action IN ('IMPORT_DOWNLOAD_GUIDE', 'IMPORT_TEMPLATE_DOWNLOAD'); + `); + } +} diff --git a/packages/backend/src/usagers/controllers/import/import.controller.spec.ts b/packages/backend/src/usagers/controllers/import/import.controller.spec.ts index 9ce1934d5a..1fd2abe3b1 100644 --- a/packages/backend/src/usagers/controllers/import/import.controller.spec.ts +++ b/packages/backend/src/usagers/controllers/import/import.controller.spec.ts @@ -74,7 +74,12 @@ describe("Import Controller", () => { expect(response.status).toBe(HttpStatus.BAD_REQUEST); - const logs = await appLogsRepository.find({}); + const logs = await appLogsRepository.find({ + where: { + userId: authInfo.id, + structureId: authInfo.structureId, + }, + }); expect(logs.length).toBe(1); expect(logs[0].action).toBe("IMPORT_USAGERS_FAILED"); expect(logs[0].context).toEqual({ @@ -108,7 +113,12 @@ describe("Import Controller", () => { }, }); - const logs = await appLogsRepository.find({}); + const logs = await appLogsRepository.find({ + where: { + userId: authInfo.id, + structureId: authInfo.structureId, + }, + }); expect(logs.length).toBe(1); expect(logs[0].action).toBe("IMPORT_USAGERS_SUCCESS"); expect(logs[0].context).toEqual({ @@ -140,7 +150,12 @@ describe("Import Controller", () => { }, }); - const logs = await appLogsRepository.find({}); + const logs = await appLogsRepository.find({ + where: { + userId: authInfo.id, + structureId: authInfo.structureId, + }, + }); expect(logs.length).toBe(1); expect(logs[0].action).toBe("IMPORT_USAGERS_SUCCESS"); expect(logs[0].context).toEqual({ diff --git a/packages/backend/src/usagers/controllers/import/import.controller.ts b/packages/backend/src/usagers/controllers/import/import.controller.ts index 6ae695dbc1..da5c6b6a0f 100644 --- a/packages/backend/src/usagers/controllers/import/import.controller.ts +++ b/packages/backend/src/usagers/controllers/import/import.controller.ts @@ -320,21 +320,15 @@ export class ImportController { @Param("documentType", new ParseEnumPipe(ImportDocumentType)) documentType: ImportDocumentType ) { - if (documentType === ImportDocumentType.GUIDE) - await this.appLogsService.create({ - role: user.role, - structureId: user.structureId, - userId: user.id, - action: "IMPORT_DOWNLOAD_GUIDE", - }); - - if (documentType === ImportDocumentType.MODELE) - await this.appLogsService.create({ - role: user.role, - structureId: user.structureId, - userId: user.id, - action: "IMPORT_TEMPLATE_DOWNLOAD", - }); + await this.appLogsService.create({ + role: user.role, + structureId: user.structureId, + userId: user.id, + action: + documentType === ImportDocumentType.GUIDE + ? "IMPORT_DOWNLOAD_GUIDE" + : "IMPORT_TEMPLATE_DOWNLOAD", + }); } }