Skip to content

Commit d49cb49

Browse files
authored
fix(logs): fixed total count for success and changed events names (#3865)
* fix(logs): fixed total count for success and changed events names * fix(backend): fix reviews
1 parent e73f6e3 commit d49cb49

File tree

5 files changed

+89
-67
lines changed

5 files changed

+89
-67
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ export type LogAction =
2727
| "GET_STATS_PORTAIL_ADMIN"
2828
| "EXPORT_STATS_PORTAIL_ADMIN"
2929
| "IMPORT_USAGERS_SUCCESS" // Import réussi
30+
| "IMPORT_USAGERS_PREVIEW" // Import preview réussi
3031
| "IMPORT_USAGERS_FAILED" // Import échoué
31-
| "DOWNLOAD_IMPORT_TEMPLATE" // Téléchargement modèle Excel
32-
| "DOWNLOAD_IMPORT_GUIDE" // Téléchargement guide PDF
32+
| "IMPORT_TEMPLATE_DOWNLOAD" // Téléchargement modèle Excel
33+
| "IMPORT_DOWNLOAD_GUIDE" // Téléchargement guide PDF
3334
| "USER_ROLE_CHANGE"
3435
| "USER_CREATE"
3536
| "USER_DELETE"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class ManualMigration1756117243336 implements MigrationInterface {
4+
public async up(queryRunner: QueryRunner): Promise<void> {
5+
await queryRunner.query(`
6+
UPDATE "app_log"
7+
SET action = CASE
8+
WHEN action = 'DOWNLOAD_IMPORT_GUIDE' THEN 'IMPORT_DOWNLOAD_GUIDE'
9+
WHEN action = 'DOWNLOAD_IMPORT_TEMPLATE' THEN 'IMPORT_TEMPLATE_DOWNLOAD'
10+
ELSE action
11+
END
12+
WHERE action IN ('DOWNLOAD_IMPORT_GUIDE', 'DOWNLOAD_IMPORT_TEMPLATE');
13+
`);
14+
}
15+
16+
public async down(queryRunner: QueryRunner): Promise<void> {
17+
await queryRunner.query(`
18+
UPDATE "app_log"
19+
SET action = CASE
20+
WHEN action = 'IMPORT_DOWNLOAD_GUIDE' THEN 'DOWNLOAD_IMPORT_GUIDE'
21+
WHEN action = 'IMPORT_TEMPLATE_DOWNLOAD' THEN 'DOWNLOAD_IMPORT_TEMPLATE'
22+
ELSE action
23+
END
24+
WHERE action IN ('IMPORT_DOWNLOAD_GUIDE', 'IMPORT_TEMPLATE_DOWNLOAD');
25+
`);
26+
}
27+
}
Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FailedUsagerImportLogContext } from "./../../../modules/app-logs/app-log-context.types";
21
import { HttpStatus } from "@nestjs/common";
32

43
import { pathExists } from "fs-extra";
@@ -18,7 +17,7 @@ import { ImportController } from "./import.controller";
1817
import { UsagerHistoryStateService } from "../../services/usagerHistoryState.service";
1918
import { UsagersModule } from "../../usagers.module";
2019
import { AppLogsService } from "../../../modules/app-logs/app-logs.service";
21-
import { AppLog } from "../../../_common/model";
20+
import { appLogsRepository } from "../../../database";
2221

2322
const importFilesDir = resolve(
2423
__dirname,
@@ -27,26 +26,15 @@ const importFilesDir = resolve(
2726

2827
describe("Import Controller", () => {
2928
let controller: ImportController;
30-
let appLogService: AppLogsService;
3129

3230
let context: AppTestContext;
3331
let authInfo: TestUserStructure;
3432
beforeAll(async () => {
35-
appLogService = {
36-
create: jest.fn(),
37-
};
3833
context = await AppTestHelper.bootstrapTestApp(
3934
{
4035
controllers: [ImportController],
4136
imports: [UsersModule, StructuresModule, UsagersModule],
42-
providers: [
43-
UsagersService,
44-
UsagerHistoryStateService,
45-
{
46-
provide: AppLogsService,
47-
useValue: appLogService,
48-
},
49-
],
37+
providers: [UsagersService, UsagerHistoryStateService, AppLogsService],
5038
},
5139
{ initApp: true }
5240
);
@@ -58,8 +46,8 @@ describe("Import Controller", () => {
5846
controller = context.module.get<ImportController>(ImportController);
5947
});
6048

61-
beforeEach(() => {
62-
jest.clearAllMocks();
49+
beforeEach(async () => {
50+
await appLogsRepository.clear();
6351
});
6452

6553
afterAll(async () => {
@@ -71,20 +59,6 @@ describe("Import Controller", () => {
7159
});
7260

7361
it(`❌ Import d'un fichier Incorrect`, async () => {
74-
const expectedLogContextEntree: FailedUsagerImportLogContext = {
75-
nombreActifs: 0,
76-
nombreErreurs: 5,
77-
nombreTotal: 2,
78-
};
79-
80-
const expectedLog: AppLog = {
81-
userId: authInfo.id,
82-
structureId: authInfo.structureId,
83-
role: authInfo.role,
84-
context: expectedLogContextEntree,
85-
action: "IMPORT_USAGERS_FAILED",
86-
};
87-
8862
const importFilePath = resolve(importFilesDir, "import_ko_1.xlsx");
8963

9064
expect(await pathExists(importFilePath)).toBeTruthy();
@@ -98,9 +72,21 @@ describe("Import Controller", () => {
9872
context,
9973
});
10074

101-
expect(appLogService.create).toHaveBeenCalledWith(expectedLog);
102-
10375
expect(response.status).toBe(HttpStatus.BAD_REQUEST);
76+
77+
const logs = await appLogsRepository.find({
78+
where: {
79+
userId: authInfo.id,
80+
structureId: authInfo.structureId,
81+
},
82+
});
83+
expect(logs.length).toBe(1);
84+
expect(logs[0].action).toBe("IMPORT_USAGERS_FAILED");
85+
expect(logs[0].context).toEqual({
86+
nombreActifs: 0,
87+
nombreErreurs: 5,
88+
nombreTotal: 2,
89+
});
10490
});
10591

10692
it(`✅ Import d'un fichier Valide 1️⃣`, async () => {
@@ -116,16 +102,6 @@ describe("Import Controller", () => {
116102
attachments: { file: importFilePath },
117103
context,
118104
});
119-
expect(appLogService.create).toHaveBeenCalledWith({
120-
action: "IMPORT_USAGERS_SUCCESS",
121-
context: {
122-
nombreActifs: 19,
123-
nombreTotal: 0,
124-
},
125-
userId: authInfo.id,
126-
structureId: authInfo.structureId,
127-
role: authInfo.role,
128-
});
129105
expect(response.status).toBe(HttpStatus.OK);
130106
expect(JSON.parse(response.text)).toEqual({
131107
importMode: "confirm",
@@ -136,11 +112,23 @@ describe("Import Controller", () => {
136112
totalCount: 0,
137113
},
138114
});
115+
116+
const logs = await appLogsRepository.find({
117+
where: {
118+
userId: authInfo.id,
119+
structureId: authInfo.structureId,
120+
},
121+
});
122+
expect(logs.length).toBe(1);
123+
expect(logs[0].action).toBe("IMPORT_USAGERS_SUCCESS");
124+
expect(logs[0].context).toEqual({
125+
nombreActifs: 19,
126+
nombreTotal: 19,
127+
});
139128
});
140129

141130
it(`✅ Import d'un fichier Valide 2️⃣`, async () => {
142131
const importFilePath = resolve(importFilesDir, "import_ok_2.xlsx");
143-
144132
expect(await pathExists(importFilePath)).toBeTruthy();
145133

146134
const headers: { [name: string]: string } = {};
@@ -151,16 +139,6 @@ describe("Import Controller", () => {
151139
attachments: { file: importFilePath },
152140
context,
153141
});
154-
expect(appLogService.create).toHaveBeenCalledWith({
155-
action: "IMPORT_USAGERS_SUCCESS",
156-
context: {
157-
nombreActifs: 2,
158-
nombreTotal: 0,
159-
},
160-
userId: authInfo.id,
161-
structureId: authInfo.structureId,
162-
role: authInfo.role,
163-
});
164142
expect(response.status).toBe(HttpStatus.OK);
165143
expect(JSON.parse(response.text)).toEqual({
166144
importMode: "confirm",
@@ -171,5 +149,18 @@ describe("Import Controller", () => {
171149
totalCount: 0,
172150
},
173151
});
152+
153+
const logs = await appLogsRepository.find({
154+
where: {
155+
userId: authInfo.id,
156+
structureId: authInfo.structureId,
157+
},
158+
});
159+
expect(logs.length).toBe(1);
160+
expect(logs[0].action).toBe("IMPORT_USAGERS_SUCCESS");
161+
expect(logs[0].context).toEqual({
162+
nombreActifs: 2,
163+
nombreTotal: 4,
164+
});
174165
});
175166
});

packages/backend/src/usagers/controllers/import/import.controller.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ export class ImportController {
218218
totalCount: importPreviewRows.length,
219219
errorsCount: importErrors.length,
220220
// keep only errors, limit to 50 results
221-
rows: importPreviewRows.filter(({ isValid }) => !isValid).slice(0, 50),
221+
rows: [...importPreviewRows]
222+
.filter(({ isValid }) => !isValid)
223+
.slice(0, 50),
222224
};
223225
await this.appLogsService.create<FailedUsagerImportLogContext>({
224226
action: "IMPORT_USAGERS_FAILED",
@@ -302,7 +304,7 @@ export class ImportController {
302304
role: user.role,
303305
context: {
304306
nombreActifs: extractUsagersNumber(usagersRows),
305-
nombreTotal: importPreviewRows.length,
307+
nombreTotal: usagersRows.length,
306308
},
307309
});
308310
return res.status(HttpStatus.OK).json({
@@ -313,18 +315,20 @@ export class ImportController {
313315

314316
@Get("log-document-download/:documentType")
315317
public async logDocumentDownload(
318+
@CurrentUser()
319+
user: UserStructureAuthenticated,
316320
@Param("documentType", new ParseEnumPipe(ImportDocumentType))
317321
documentType: ImportDocumentType
318322
) {
319-
if (documentType === ImportDocumentType.GUIDE)
320-
await this.appLogsService.create({
321-
action: "DOWNLOAD_IMPORT_GUIDE",
322-
});
323-
324-
if (documentType === ImportDocumentType.MODELE)
325-
await this.appLogsService.create({
326-
action: "DOWNLOAD_IMPORT_TEMPLATE",
327-
});
323+
await this.appLogsService.create({
324+
role: user.role,
325+
structureId: user.structureId,
326+
userId: user.id,
327+
action:
328+
documentType === ImportDocumentType.GUIDE
329+
? "IMPORT_DOWNLOAD_GUIDE"
330+
: "IMPORT_TEMPLATE_DOWNLOAD",
331+
});
328332
}
329333
}
330334

packages/frontend/src/app/modules/general/components/faq/faq.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ <h1 class="title">Questions / Réponses sur DomiFa</h1>
66
</div>
77
<div class="text-start text-md-end col-12 col-md-5">
88
<a
9-
(click)="doLogDownloadAction()"
109
href="/assets/files/guide_utilisateur_domifa.pdf"
1110
target="_blank"
1211
class="btn btn-white-primary"

0 commit comments

Comments
 (0)