Skip to content

Commit e5f1cc4

Browse files
committed
fix(backend): adding userId and structureId to user create log
1 parent cd92224 commit e5f1cc4

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

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

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,24 @@ import {
1313
} from "../../../util/test";
1414
import { UsersController } from "./users.controller";
1515
import { POST_USER_STRUCTURE_BODY } from "../../../_common/mocks";
16-
import { TESTS_USERS_STRUCTURE } from "../../../_tests";
16+
import { TESTS_USERS_STRUCTURE, TestUserStructure } from "../../../_tests";
1717
import { usersDeletor } from "../services/users-deletor.service";
1818
import { MailsModule } from "../../mails/mails.module";
1919
import { AppLogsService } from "../../app-logs/app-logs.service";
20-
import { userStructureRepository } from "../../../database";
20+
import { appLogsRepository } from "../../../database";
2121

2222
describe("Users Controller", () => {
2323
let controller: UsersController;
2424
let context: AppTestContext;
25-
let appLogService: AppLogsService;
25+
let authInfo: TestUserStructure;
2626
beforeAll(async () => {
27-
appLogService = {
28-
create: jest.fn(),
29-
};
3027
context = await AppTestHelper.bootstrapTestApp({
3128
controllers: [UsersController],
3229
imports: [MailsModule, StructuresModule, UsagersModule, HttpModule],
33-
providers: [
34-
{
35-
provide: AppLogsService,
36-
useValue: appLogService,
37-
},
38-
],
30+
providers: [AppLogsService],
3931
});
4032

41-
const authInfo =
33+
authInfo =
4234
TESTS_USERS_STRUCTURE.BY_EMAIL["preprod.domifa@fabrique.social.gouv.fr"];
4335
await AppTestHelper.authenticateStructure(authInfo, { context });
4436
controller = context.module.get<UsersController>(UsersController);
@@ -49,6 +41,10 @@ describe("Users Controller", () => {
4941
await AppTestHelper.tearDownTestApp(context);
5042
});
5143

44+
beforeEach(async () => {
45+
await appLogsRepository.clear();
46+
});
47+
5248
describe("> Register user", () => {
5349
describe("Nominal case", () => {
5450
it("should be defined", async () => {
@@ -65,19 +61,16 @@ describe("Users Controller", () => {
6561
structure: { ...POST_USER_STRUCTURE_BODY.structure, id: 1 },
6662
},
6763
});
68-
69-
const user = await userStructureRepository.findOneByOrFail({
70-
email: "test@test.com",
71-
});
72-
expect(appLogService.create).toHaveBeenCalledWith({
73-
action: "USER_CREATE",
74-
userId: context.user.userId,
75-
context: {
76-
role: POST_USER_STRUCTURE_BODY.role,
77-
structureId: 1,
78-
userId: user.id,
64+
const logs = await appLogsRepository.find({
65+
where: {
66+
userId: authInfo.id,
67+
action: "USER_CREATE",
7968
},
8069
});
70+
expect(logs.length).toEqual(1);
71+
expect(logs[0].action).toEqual("USER_CREATE");
72+
expect(logs[0].role).toEqual(authInfo.role);
73+
expect(logs[0].userId).toEqual(authInfo.id);
8174
expect(response.status).toBe(200);
8275
expect(response.text).toBe('{"message":"OK"}');
8376
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ export class UsersController {
299299
await this.appLogService.create<UserStructureCreateLogContext>({
300300
action: "USER_CREATE",
301301
userId: user.id,
302+
structureId: "structureId" in user ? user.structureId : null,
303+
role: user.role,
302304
context: {
303305
role: newUser.role,
304306
userId: newUser.id,

0 commit comments

Comments
 (0)