Skip to content

Commit

Permalink
fix(frontend): fix select boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Nov 27, 2024
1 parent b697eab commit 5e18746
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 648 deletions.
9 changes: 4 additions & 5 deletions packages/backend/src/_common/decorators/StripTagsDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ export function StripTagsTransform(
return null;
}

const trimmedValue = sourceData.value.trim();
if (trimmedValue === "") {
if (sourceData.value.trim() === "") {
return null;
}

const sanitized = sanitizeHtml(trimmedValue);
const sanitized = sanitizeHtml(sourceData.value);

return striptags(sanitized)
.replace(/[\\$~*<>{}]/gi, "")
.replace(/\s+/g, " ")
.trim();
.replace(/\s+/g, " ");
}, transformOptions);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MigrationInterface, QueryRunner } from "typeorm";

import { domifaConfig } from "../config";
import { dataCompare } from "../util";
import { appLogger, normalizeString } from "../util";

const batchSize = 5000;
export class ManualMigration1731349672897 implements MigrationInterface {
Expand Down Expand Up @@ -39,12 +39,12 @@ export class ManualMigration1731349672897 implements MigrationInterface {
usager?.customRef ?? usager?.ref,
]
.filter(Boolean)
.map((part) => dataCompare.cleanString(part.toString()));
.map((part) => normalizeString(part.toString()));

const nom_prenom_surnom_ref = parts.join(" ");

await queryRunner.query(
`UPDATE usager set nom_prenom_surnom_ref = $1 where uuid=$2`,
"UPDATE usager set nom_prenom_surnom_ref = $1 where uuid=$2",
[nom_prenom_surnom_ref, usager.uuid]
);
}
Expand All @@ -54,7 +54,7 @@ export class ManualMigration1731349672897 implements MigrationInterface {
await queryRunner.commitTransaction();

processedRecords += usagers.length;
console.log(
appLogger.warn(
`Progression: ${Math.min(
processedRecords,
totalRecords
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BeforeInsert, Column, Entity } from "typeorm";
import { Column, Entity } from "typeorm";
import { ContactSupport } from "../../../_common/model";
import { MessageEmailAttachment } from "../message-email";

Expand Down Expand Up @@ -34,10 +34,6 @@ export class ContactSupportTable
@Column({ type: "text", nullable: true })
public structureName: string;

@BeforeInsert()
nameToUpperCase() {
this.email = this.email.toLowerCase().trim();
}
public constructor(entity?: Partial<ContactSupportTable>) {
super(entity);
Object.assign(this, entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
EventSubscriber,
EntitySubscriberInterface,
InsertEvent,
UpdateEvent,
} from "typeorm";
import { StructureTable } from "./StructureTable.typeorm";

@EventSubscriber()
export class StructureSubscriber
implements EntitySubscriberInterface<StructureTable>
{
listenTo() {
return StructureTable;
}
public lowerAndTrim(entity: StructureTable) {
entity.email = entity.email.toLowerCase().trim();
entity.adresse = entity.adresse.trim();
entity.nom = entity.nom.trim();
entity.ville = entity.ville.trim();
}

beforeInsert(event: InsertEvent<StructureTable>) {
this.lowerAndTrim(event.entity);
}

beforeUpdate(event: UpdateEvent<StructureTable>) {
if (event.entity) {
this.lowerAndTrim(event.entity as StructureTable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BeforeInsert, Column, Entity, Generated, Index } from "typeorm";
import { Column, Entity, Generated, Index } from "typeorm";

import { AppTypeormTable } from "../_core/AppTypeormTable.typeorm";
import {
Expand Down Expand Up @@ -155,14 +155,6 @@ export class StructureTable
@Column({ type: "text", nullable: true })
organismeType: StructureOrganismeType;

@BeforeInsert()
lowerAndTrim() {
this.email = this.email.toLowerCase().trim();
this.adresse = this.adresse.trim();
this.nom = this.nom.trim();
this.ville = this.ville.trim();
}

public constructor(entity?: Partial<StructureTable>) {
super(entity);
Object.assign(this, entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
EventSubscriber,
EntitySubscriberInterface,
InsertEvent,
UpdateEvent,
} from "typeorm";
import { normalizeString } from "../../../util";
import { UsagerTable } from "./UsagerTable.typeorm";

@EventSubscriber()
export class UsagerSubscriber
implements EntitySubscriberInterface<UsagerTable>
{
listenTo() {
return UsagerTable;
}

private processName(entity: UsagerTable) {
if (!entity?.nom || !entity?.prenom) {
return;
}

try {
entity.nom = entity.nom.trim();
entity.prenom = entity.prenom.trim();

const parts = [
entity.nom,
entity.prenom,
entity.surnom,
entity?.customRef ?? entity?.ref,
].filter(Boolean);

entity.nom_prenom_surnom_ref = normalizeString(parts.join(" "));
} catch (error) {
console.error("Erreur lors du traitement du nom:", error);
}
}

beforeInsert(event: InsertEvent<UsagerTable>) {
this.processName(event.entity);
}

beforeUpdate(event: UpdateEvent<UsagerTable>) {
if (event.entity) {
this.processName(event.entity as UsagerTable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { UsagerEntretienTable } from "./UsagerEntretienTable.typeorm";
import {
BeforeInsert,
BeforeUpdate,
Column,
Entity,
Index,
Expand Down Expand Up @@ -30,7 +28,6 @@ import {
UsagerDecisionStatut,
UsagerImport,
} from "@domifa/common";
import { dataCompare } from "../../../util";

// https://typeorm.io/#/entities/column-types-for-postgres
@Entity({ name: "usager" })
Expand Down Expand Up @@ -156,26 +153,6 @@ export class UsagerTable
"usagerRef" | "createdAt" | "createdBy" | "message"
> | null;

@BeforeInsert()
@BeforeUpdate()
nameToUpperCase() {
this.nom = this.nom.trim();
this.prenom = this.prenom.trim();

const parts = [
this.nom,
this.prenom,
this.surnom,
this?.customRef ?? this?.ref,
]
.filter(Boolean)
.map((part) => dataCompare.cleanString(part.toString()));

this.nom_prenom_surnom_ref = parts.join(" ");

console.table({ ta: this.nom_prenom_surnom_ref });
}

public constructor(entity?: Partial<UsagerTable>) {
super(entity);
Object.assign(this, entity);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
EventSubscriber,
EntitySubscriberInterface,
InsertEvent,
UpdateEvent,
} from "typeorm";
import { UserStructureTable } from "./UserStructureTable.typeorm";

@EventSubscriber()
export class StructureSubscriber
implements EntitySubscriberInterface<UserStructureTable>
{
listenTo() {
return UserStructureTable;
}
public lowerAndTrim(entity: UserStructureTable) {
entity.email = entity.email.toLowerCase().trim();
entity.nom = entity.nom.trim();
entity.prenom = entity.prenom.trim();
}

beforeInsert(event: InsertEvent<UserStructureTable>) {
this.lowerAndTrim(event.entity);
}

beforeUpdate(event: UpdateEvent<UserStructureTable>) {
if (event.entity) {
this.lowerAndTrim(event.entity as UserStructureTable);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {
BeforeInsert,
Column,
Entity,
Generated,
Index,
JoinColumn,
ManyToOne,
} from "typeorm";
import { titleCase } from "typeorm/util/StringUtils";
import { StructureTable } from "..";
import { AppTypeormTable } from "../_core/AppTypeormTable.typeorm";
import {
Expand Down Expand Up @@ -76,13 +74,6 @@ export class UserStructureTable
@Column({ type: "text", default: "structure" })
userRightStatus: UserRightStatus;

@BeforeInsert()
nameToUpperCase() {
this.email = this.email.toLowerCase().trim();
this.nom = titleCase(this.nom).trim();
this.prenom = titleCase(this.prenom).trim();
}

public constructor(entity?: Partial<UserStructureTable>) {
super(entity);
Object.assign(this, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ if (isTypescriptMode) {
connectOptionsPaths = {
migrations: ["src/_migrations/**/*.ts"],
entities: ["src/database/entities/**/*Table.typeorm.ts"],
subscribers: ["src/database/entities/**/*Subscriber.typeorm.ts"],
};
} else {
appLogger.warn("[appTypeormManager] Running in javascript DIST mode");
Expand All @@ -22,6 +23,9 @@ if (isTypescriptMode) {
entities: [
"/app/packages/backend/dist/database/entities/**/*Table.typeorm.js",
],
subscribers: [
"/app/packages/backend/dist/database/entities/**/*Subscriber.typeorm.js",
],
};
}

Expand Down
25 changes: 12 additions & 13 deletions packages/backend/src/usagers/controllers/usagers.controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { usagerEntretienRepository } from "./../../database/services/usager/usagerEntretienRepository.service";
import { usagerDocsRepository } from "./../../database/services/usager/usagerDocsRepository.service";

import { messageSmsRepository } from "./../../database/services/message-sms/messageSmsRepository.service";
import {
BadRequestException,
Body,
Expand Down Expand Up @@ -31,10 +27,12 @@ import {
usagerRepository,
USAGER_LIGHT_ATTRIBUTES,
joinSelectFields,
messageSmsRepository,
usagerDocsRepository,
usagerEntretienRepository,
} from "../../database";

import { cleanPath } from "../../util";
import { dataCompare } from "../../util/search/dataCompare.service";
import { cleanPath, normalizeString } from "../../util";
import {
UserStructureAuthenticated,
USER_STRUCTURE_ROLE_ALL,
Expand Down Expand Up @@ -138,7 +136,7 @@ export class UsagersController {
@Get("update-manage")
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL)
public async updateManage(@CurrentUser() user: UserStructureAuthenticated) {
return usagerRepository
return await usagerRepository
.createQueryBuilder()
.select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES))
.where(
Expand All @@ -157,6 +155,7 @@ export class UsagersController {
@Body() search: SearchUsagerDto,
@CurrentUser() user: UserStructureAuthenticated
) {
console.log({ x: normalizeString(search.searchString) });
const query = usagerRepository
.createQueryBuilder("usager")
.select(joinSelectFields(USAGER_LIGHT_ATTRIBUTES))
Expand All @@ -165,8 +164,8 @@ export class UsagersController {
});

if (search.searchString && search.searchStringField === "DEFAULT") {
query.andWhere(`nom_prenom_surnom_ref ILIKE :str`, {
str: `%${dataCompare.cleanString(search.searchString)}%`,
query.andWhere("nom_prenom_surnom_ref ILIKE :str", {
str: `%${normalizeString(search.searchString)}%`,
});
}

Expand Down Expand Up @@ -224,7 +223,7 @@ export class UsagersController {
query.take(100);
}

return query.getRawMany();
return await query.getRawMany();
}

@Post()
Expand Down Expand Up @@ -438,7 +437,7 @@ export class UsagersController {
const key =
join(
domifaConfig().upload.bucketRootDir,
"usager-documents",
`usager-documents`,
cleanPath(user.structure.uuid),
cleanPath(usager.uuid)
) + "/";
Expand Down Expand Up @@ -483,10 +482,10 @@ export class UsagersController {
@UseGuards(UsagerAccessGuard)
@AllowUserStructureRoles(...USER_STRUCTURE_ROLE_ALL)
@Get(":usagerRef")
public async findOne(
public findOne(
@Param("usagerRef", new ParseIntPipe()) _usagerRef: number,
@CurrentUsager() currentUsager: Usager
): Promise<Usager> {
): Usager {
return currentUsager;
}
}
2 changes: 0 additions & 2 deletions packages/backend/src/usagers/dto/search-usager.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IsIn, IsOptional, IsString, MinLength } from "class-validator";
import {
LowerCaseTransform,
StripTagsTransform,
Trim,
} from "../../_common/decorators";
import {
UsagersFilterCriteriaDernierPassage,
Expand All @@ -17,7 +16,6 @@ export class SearchUsagerDto {
})
@IsOptional()
@IsString()
@Trim()
@MinLength(1)
@StripTagsTransform()
@LowerCaseTransform()
Expand Down
Loading

0 comments on commit 5e18746

Please sign in to comment.