-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(backend): add new rules for search
- Loading branch information
Showing
32 changed files
with
1,203 additions
and
653 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
packages/backend/src/_migrations/1731347018702-auto-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
import { domifaConfig } from "../config"; | ||
|
||
export class AutoMigration1731347018702 implements MigrationInterface { | ||
name = "AutoMigration1731347018702"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
if ( | ||
domifaConfig().envId === "prod" || | ||
domifaConfig().envId === "preprod" || | ||
domifaConfig().envId === "local" | ||
) { | ||
await queryRunner.query( | ||
`CREATE TABLE typeorm_metadata ( type varchar(255), schema varchar(255), name varchar(255), value text );` | ||
); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_57133463f2311234ecf27157fa"` | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "usager" DROP COLUMN "nom_prenom_surnom_ref"` | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "usager" ADD "nom_prenom_surnom_ref" character varying NOT NULL` | ||
); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/backend/src/_migrations/1731349553247-auto-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
import { domifaConfig } from "../config"; | ||
|
||
export class AutoMigration1731349553247 implements MigrationInterface { | ||
name = "AutoMigration1731349553247"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
if ( | ||
domifaConfig().envId === "prod" || | ||
domifaConfig().envId === "preprod" || | ||
domifaConfig().envId === "local" | ||
) { | ||
await queryRunner.query( | ||
`ALTER TABLE "usager" DROP COLUMN "nom_prenom_ref"` | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "usager" ADD "nom_prenom_surnom_ref" character varying NULL` | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_57133463f2311234ecf27157fa" ON "usager" ("nom_prenom_surnom_ref") ` | ||
); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_57133463f2311234ecf27157fa"` | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "usager" DROP COLUMN "nom_prenom_surnom_ref"` | ||
); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
packages/backend/src/_migrations/1731349672897-manual-migration.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
import { domifaConfig } from "../config"; | ||
import { dataCompare } from "../util"; | ||
|
||
const batchSize = 5000; | ||
export class ManualMigration1731349672897 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
if ( | ||
domifaConfig().envId === "prod" || | ||
domifaConfig().envId === "preprod" || | ||
domifaConfig().envId === "local" | ||
) { | ||
const totalCount = await queryRunner.query(` | ||
SELECT COUNT(*) as count | ||
FROM usager | ||
WHERE nom_prenom_surnom_ref IS NULL OR nom_prenom_surnom_ref = '' | ||
`); | ||
|
||
const totalRecords = parseInt(totalCount[0].count); | ||
let processedRecords = 0; | ||
|
||
while ((await this.getUsagersToUpdate(queryRunner)) > 0) { | ||
await queryRunner.startTransaction(); | ||
|
||
try { | ||
// Récupère un lot d'usagers | ||
const usagers = await queryRunner.query( | ||
`SELECT uuid, ref, nom, prenom, surnom FROM usager WHERE usager_nom_prenom_surnom_ref IS NULL OR usager_nom_prenom_surnom_ref = '' LIMIT ${batchSize}` | ||
); | ||
|
||
// Exécute les updates en une seule requête | ||
if (usagers.length > 0) { | ||
for (const usager of usagers) { | ||
const parts = [ | ||
usager.nom, | ||
usager.prenom, | ||
usager.surnom, | ||
usager.ref, | ||
] | ||
.filter(Boolean) | ||
.map((part) => dataCompare.cleanString(part.toString())); | ||
|
||
const nom_prenom_surnom_ref = parts.join(" "); | ||
|
||
await queryRunner.query( | ||
`UPDATE usager set nom_prenom_surnom_ref = $1 where uuid=$2`, | ||
[nom_prenom_surnom_ref, usager.uuid] | ||
); | ||
} | ||
} | ||
|
||
// Valide la transaction | ||
await queryRunner.commitTransaction(); | ||
|
||
processedRecords += usagers.length; | ||
console.log( | ||
`Progression: ${Math.min( | ||
processedRecords, | ||
totalRecords | ||
)}/${totalRecords} enregistrements traités` | ||
); | ||
} catch (error) { | ||
await queryRunner.rollbackTransaction(); | ||
console.error( | ||
`Erreur lors du traitement du lot ${processedRecords}-${ | ||
processedRecords + batchSize | ||
}:`, | ||
error | ||
); | ||
throw error; | ||
} | ||
|
||
// Petite pause entre les lots | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
} | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.startTransaction(); | ||
|
||
try { | ||
await queryRunner.query(` | ||
UPDATE usager | ||
SET nom_prenom_surnom_ref = NULL | ||
`); | ||
|
||
await queryRunner.commitTransaction(); | ||
} catch (error) { | ||
await queryRunner.rollbackTransaction(); | ||
throw error; | ||
} | ||
} | ||
|
||
private async getUsagersToUpdate(queryRunner: QueryRunner): Promise<number> { | ||
const total = await queryRunner.query(` | ||
SELECT COUNT(*) as count | ||
FROM usager | ||
WHERE nom_prenom_surnom_ref IS NULL OR nom_prenom_surnom_ref = '' | ||
`); | ||
return parseInt(total[0].count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
import { ApiProperty } from "@nestjs/swagger"; | ||
import { IsNotEmpty, IsString, MinLength } from "class-validator"; | ||
import { IsIn, IsOptional, IsString, MinLength } from "class-validator"; | ||
import { | ||
LowerCaseTransform, | ||
StripTagsTransform, | ||
Trim, | ||
} from "../../_common/decorators"; | ||
import { | ||
UsagersFilterCriteriaDernierPassage, | ||
UsagersFilterCriteriaEcheance, | ||
} from "@domifa/common"; | ||
|
||
export class SearchUsagerDto { | ||
@ApiProperty({ | ||
example: "dupuis", | ||
description: "Nom ou prénom", | ||
}) | ||
@IsNotEmpty() | ||
@IsOptional() | ||
@IsString() | ||
@Trim() | ||
@MinLength(3) | ||
@MinLength(2) | ||
@StripTagsTransform() | ||
@LowerCaseTransform() | ||
public searchString!: string; | ||
|
||
@IsOptional() | ||
@IsIn(["DEFAULT", "DATE_NAISSANCE"]) | ||
public readonly searchStringField: "DEFAULT" | "DATE_NAISSANCE"; | ||
|
||
@IsIn([ | ||
"EXCEEDED", | ||
"NEXT_TWO_WEEKS", | ||
"NEXT_TWO_MONTHS", | ||
"PREVIOUS_YEAR", | ||
"PREVIOUS_TWO_YEARS", | ||
]) | ||
@IsOptional() | ||
public readonly echeance: UsagersFilterCriteriaEcheance; | ||
|
||
@IsIn(["PREVIOUS_TWO_MONTHS", "PREVIOUS_THREE_MONTHS"]) | ||
@IsOptional() | ||
public readonly lastInteractionDate: UsagersFilterCriteriaDernierPassage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.