Skip to content

Commit ad1b5c5

Browse files
committed
chore(api): utilisation des enums
1 parent b073b2b commit ad1b5c5

19 files changed

+293
-120
lines changed
-334 Bytes
Binary file not shown.

packages/backend/src/_common/mocks/POST_USER_STRUCTURE_BODY.mock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
UserStructureRole,
33
StructureType,
44
UserStructure,
5+
UserFonction,
56
} from "@domifa/common";
67

78
export const POST_USER_STRUCTURE_BODY: Partial<UserStructure> = {
@@ -11,7 +12,7 @@ export const POST_USER_STRUCTURE_BODY: Partial<UserStructure> = {
1112
nom: "TEST",
1213
prenom: "TEST",
1314
role: "admin" as UserStructureRole,
14-
fonction: "Président",
15+
fonction: UserFonction.PRESIDENT,
1516
fonctionDetail: null,
1617
structure: {
1718
id: 100,

packages/backend/src/_common/mocks/STRUCTURE_MOCK.const.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const STRUCTURE_MOCK: StructureCommon = {
2424
nom: "Jean",
2525
prenom: "Thomson",
2626
fonction: "PDG",
27-
fonctionDetail: null,
2827
},
2928
structureType: "ccas",
3029
ville: "Asnieres-sur-seine",

packages/backend/src/_common/model/jwt/user-structure-jwt-payload.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UserStructureRole } from "@domifa/common";
1+
import { UserFonction, UserStructureRole } from "@domifa/common";
22
import { UseBaseJwtPayload } from "./user-base-jwt-payload.type";
33

44
export type UserStructureJwtPayload = UseBaseJwtPayload<"structure"> & {
@@ -7,7 +7,7 @@ export type UserStructureJwtPayload = UseBaseJwtPayload<"structure"> & {
77
nom: string;
88
prenom: string;
99
role: UserStructureRole;
10-
fonction: string;
10+
fonction: UserFonction;
1111
fonctionDetail: string;
1212
lastLogin: Date;
1313
acceptTerms: Date | null;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
import { domifaConfig } from "../config";
3+
import { matchFonctionUtilisateur } from "@domifa/common/src/structure/functions/matchFonctionUtilisateur";
4+
import { UserFonction } from "@domifa/common";
5+
6+
export class ManualMigration1751927912971 implements MigrationInterface {
7+
name = "ManualMigration1751927912971";
8+
9+
public async up(queryRunner: QueryRunner): Promise<void> {
10+
if (
11+
domifaConfig().envId === "prod" ||
12+
domifaConfig().envId === "preprod" ||
13+
domifaConfig().envId === "local"
14+
) {
15+
console.log(
16+
"Starting migration to standardize user_structure fonction values..."
17+
);
18+
19+
// Get all user_structure records with their current fonction values
20+
const users = await queryRunner.query(`
21+
SELECT id, fonction
22+
FROM user_structure
23+
WHERE fonction IS NOT NULL
24+
`);
25+
26+
console.log(
27+
`Found ${users.length} user records with fonction values to process`
28+
);
29+
30+
let updatedCount = 0;
31+
let unchangedCount = 0;
32+
let nullifiedCount = 0;
33+
34+
// Process each user record
35+
for (const user of users) {
36+
const currentFonction = user.fonction;
37+
const matchedFonction = matchFonctionUtilisateur(currentFonction);
38+
39+
// Determine the new fonction value
40+
let newFonction: string;
41+
if (matchedFonction === null) {
42+
// If no match found, set to AUTRE
43+
newFonction = UserFonction.AUTRE;
44+
nullifiedCount++;
45+
} else {
46+
newFonction = matchedFonction;
47+
}
48+
49+
// Only update if the value has changed
50+
if (newFonction !== currentFonction) {
51+
await queryRunner.query(
52+
`
53+
UPDATE user_structure
54+
SET fonction = $1
55+
WHERE id = $2
56+
`,
57+
[newFonction, user.id]
58+
);
59+
60+
console.log(
61+
`Updated user ${user.id}: "${currentFonction}" -> "${newFonction}"`
62+
);
63+
updatedCount++;
64+
} else {
65+
unchangedCount++;
66+
}
67+
}
68+
69+
// Handle users with null fonction values
70+
const nullFonctionUsers = await queryRunner.query(`
71+
SELECT id
72+
FROM user_structure
73+
WHERE fonction IS NULL
74+
`);
75+
76+
if (nullFonctionUsers.length > 0) {
77+
await queryRunner.query(
78+
`
79+
UPDATE user_structure
80+
SET fonction = $1
81+
WHERE fonction IS NULL
82+
`,
83+
[UserFonction.AUTRE]
84+
);
85+
86+
console.log(
87+
`Set ${nullFonctionUsers.length} null fonction values to AUTRE`
88+
);
89+
updatedCount += nullFonctionUsers.length;
90+
}
91+
92+
console.log(`Migration completed:`);
93+
console.log(`- Updated records: ${updatedCount}`);
94+
console.log(`- Unchanged records: ${unchangedCount}`);
95+
console.log(`- Records set to AUTRE (no match): ${nullifiedCount}`);
96+
}
97+
}
98+
99+
public async down(): Promise<void> {
100+
if (
101+
domifaConfig().envId === "prod" ||
102+
domifaConfig().envId === "preprod" ||
103+
domifaConfig().envId === "local"
104+
) {
105+
console.log(
106+
"WARNING: This migration cannot be fully reversed as the original non-standardized values were not preserved."
107+
);
108+
console.log(
109+
"The down migration will not perform any changes to maintain data integrity."
110+
);
111+
}
112+
}
113+
}

packages/backend/src/auth/guards/AppUserGuard.guard.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { UserStructureRole, UserSupervisorRole } from "@domifa/common";
1+
import {
2+
UserFonction,
3+
UserStructureRole,
4+
UserSupervisorRole,
5+
} from "@domifa/common";
26
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
37
import { Reflector } from "@nestjs/core";
48
import { getCurrentScope } from "@sentry/node";
@@ -35,7 +39,7 @@ export class AppUserGuard implements CanActivate {
3539
structureId?: number | null;
3640
role?: UserSupervisorRole | UserStructureRole;
3741
email?: string | null;
38-
fonction?: string | null;
42+
fonction?: UserFonction | null;
3943
fonctionDetail?: string | null;
4044
} = {
4145
id: user._userId,

packages/backend/src/database/entities/user-structure/UserStructureTable.typeorm.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
UserStructureRole,
1313
UserStructureMails,
1414
UserStructure,
15+
UserFonction,
1516
} from "@domifa/common";
1617

1718
// https://typeorm.io/#/entities/column-types-for-postgres
@@ -25,7 +26,7 @@ export class UserStructureTable
2526
email: string;
2627

2728
@Column({ type: "text", nullable: true })
28-
fonction: string;
29+
fonction: UserFonction;
2930

3031
@Column({ type: "text", nullable: true })
3132
fonctionDetail: string;

packages/backend/src/database/entities/user-supervisor/UserSupervisorTable.typeorm.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Column, Entity, Generated, Index } from "typeorm";
22
import { AppTypeormTable } from "../_core/AppTypeormTable.typeorm";
3-
import { UserSupervisor, UserSupervisorRole } from "@domifa/common";
3+
import {
4+
UserFonction,
5+
UserSupervisor,
6+
UserSupervisorRole,
7+
} from "@domifa/common";
48

59
// https://typeorm.io/#/entities/column-types-for-postgres
610
@Entity({ name: "user_supervisor" })
@@ -13,7 +17,7 @@ export class UserSupervisorTable
1317
email: string;
1418

1519
@Column({ type: "text", nullable: true })
16-
fonction: string;
20+
fonction: UserFonction;
1721

1822
@Index()
1923
@Column({ type: "integer", unique: true })

packages/backend/src/modules/structures/controllers/structures.public.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export class StructuresPublicController {
2626
);
2727
return res.status(HttpStatus.OK).json("OK");
2828
} catch (e) {
29-
console.log(e);
3029
return res.status(HttpStatus.BAD_REQUEST).json("CREATE_STRUCTURE_FAIL");
3130
}
3231
}

packages/backend/src/modules/structures/structures.creation-full.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { structureDeletorService } from "./services/structure-deletor.service";
1919
import { StructuresModule } from "./structure.module";
2020
import { PortailAdminModule } from "../portail-admin";
2121
import { MailsModule } from "../mails/mails.module";
22+
import { UserFonction } from "@domifa/common";
2223
const structureDto: StructureDto = {
2324
adresse: "1 rue de Pessac",
2425
adresseCourrier: {
@@ -137,7 +138,7 @@ describe("Stuctures creation full", () => {
137138
password: "Y67xc6D7XBibZ6r",
138139
prenom: "Ben",
139140
structure: undefined,
140-
fonction: "Autre",
141+
fonction: UserFonction.PRESIDENT,
141142
fonctionDetail: "Président",
142143
};
143144
const structureWithUser: StructureWithUserDto = {

0 commit comments

Comments
 (0)