Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/backend/src/_common/model/app-log/LogAction.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FailedUsagerImportLogContext } from "./../../../modules/app-logs/app-log-context.types";
import { HttpStatus } from "@nestjs/common";

import { pathExists } from "fs-extra";
Expand All @@ -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,
Expand All @@ -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 }
);
Expand All @@ -58,8 +46,8 @@ describe("Import Controller", () => {
controller = context.module.get<ImportController>(ImportController);
});

beforeEach(() => {
jest.clearAllMocks();
beforeEach(async () => {
await appLogsRepository.clear();
});

afterAll(async () => {
Expand All @@ -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();
Expand All @@ -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 () => {
Expand All @@ -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",
Expand All @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated assertion nombreTotal: 19 correctly reflects the fix where total count now represents processed users rather than all preview rows. This aligns with the controller change that uses usagersRows.length instead of importPreviewRows.length.

});
});

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 } = {};
Expand All @@ -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",
Expand All @@ -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,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<FailedUsagerImportLogContext>({
action: "IMPORT_USAGERS_FAILED",
Expand Down Expand Up @@ -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({
Expand All @@ -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",
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ <h1 class="title">Questions / Réponses sur DomiFa</h1>
</div>
<div class="text-start text-md-end col-12 col-md-5">
<a
(click)="doLogDownloadAction()"
href="/assets/files/guide_utilisateur_domifa.pdf"
target="_blank"
class="btn btn-white-primary"
Expand Down