diff --git a/apps/api/src/lib/utils/csv.utils.ts b/apps/api/src/lib/utils/csv.utils.ts index 9e67d41f..a0a8a3e1 100644 --- a/apps/api/src/lib/utils/csv.utils.ts +++ b/apps/api/src/lib/utils/csv.utils.ts @@ -17,6 +17,7 @@ export type FromCsvType = { validationError?: string; accepted?: number; rejected?: number; + communeNomsAlt?: Record; voies?: Partial[]; numeros?: Partial[]; toponymes?: Partial[]; @@ -107,7 +108,6 @@ function extractData(rows: Row[]): { .groupBy((r) => normalize(r.parsedValues.voie_nom)) .map((voieRows) => { const dates = compact(voieRows.map((r) => r.parsedValues.date_der_maj)); - return { id: new ObjectId().toHexString(), banId: extractIdBanToponyme(voieRows[0]), @@ -203,10 +203,15 @@ export async function extractFromCsv( accepted.filter((r) => extractCodeCommune(r) === codeCommune), ); + const communeNomsAlt = + rows.find((row) => row.localizedValues?.commune_nom)?.localizedValues + ?.commune_nom || null; + return { isValid: true, accepted: accepted.length, rejected: rejected.length, + communeNomsAlt, voies: communesData.voies, numeros: communesData.numeros, toponymes: communesData.toponymes, diff --git a/apps/api/src/modules/base_locale/base_locale.service.ts b/apps/api/src/modules/base_locale/base_locale.service.ts index f4d5ca8c..83fe3136 100644 --- a/apps/api/src/modules/base_locale/base_locale.service.ts +++ b/apps/api/src/modules/base_locale/base_locale.service.ts @@ -210,6 +210,7 @@ export class BaseLocaleService { voies, numeros, toponymes, + communeNomsAlt, isValid, accepted, rejected, @@ -222,7 +223,12 @@ export class BaseLocaleService { ); } // On populate la Bal avec les infos du fichier - await this.populate(baseLocale, { voies, numeros, toponymes }); + await this.populate(baseLocale, { + voies, + numeros, + toponymes, + communeNomsAlt, + }); // On met a jour le updatedAt de la Bal await this.touch(baseLocale.id); @@ -389,8 +395,13 @@ export class BaseLocaleService { async populate( baseLocale: BaseLocale, - { voies, toponymes, numeros }: FromCsvType, + { voies, toponymes, numeros, communeNomsAlt }: FromCsvType, ): Promise { + if (communeNomsAlt) { + this.basesLocalesRepository.update(baseLocale.id, { + communeNomsAlt, + }); + } // On supprime les numeros, vois et toponymes si il y en a await this.numeroService.deleteMany({ balId: baseLocale.id }); await this.voieService.deleteMany({ balId: baseLocale.id }); diff --git a/apps/api/src/modules/base_locale/dto/update_base_locale.dto.ts b/apps/api/src/modules/base_locale/dto/update_base_locale.dto.ts index 26d6a218..84231287 100644 --- a/apps/api/src/modules/base_locale/dto/update_base_locale.dto.ts +++ b/apps/api/src/modules/base_locale/dto/update_base_locale.dto.ts @@ -1,10 +1,13 @@ +import { ValidatorBal } from '@/shared/validators/validator_bal.validator'; import { ApiProperty } from '@nestjs/swagger'; import { ArrayMaxSize, ArrayNotEmpty, IsEmail, IsNotEmpty, + IsNotEmptyObject, IsOptional, + Validate, } from 'class-validator'; export class UpdateBaseLocaleDTO { @@ -13,6 +16,12 @@ export class UpdateBaseLocaleDTO { @IsNotEmpty() nom?: string; + @IsOptional() + @IsNotEmptyObject() + @Validate(ValidatorBal, ['langAlt']) + @ApiProperty({ required: false, nullable: true }) + communeNomsAlt: Record; + @IsOptional() @ApiProperty({ required: false, nullable: false }) @ArrayNotEmpty() diff --git a/apps/api/test/base_locale.e2e-spec.ts b/apps/api/test/base_locale.e2e-spec.ts index d4ce4ed2..edcae4cb 100644 --- a/apps/api/test/base_locale.e2e-spec.ts +++ b/apps/api/test/base_locale.e2e-spec.ts @@ -37,6 +37,7 @@ const baseLocalePublicProperties = [ 'id', 'banId', 'nom', + 'communeNomsAlt', 'commune', 'nbNumeros', 'nbNumerosCertifies', diff --git a/libs/shared/src/entities/base_locale.entity.ts b/libs/shared/src/entities/base_locale.entity.ts index dbfeed2f..5cdaed83 100644 --- a/libs/shared/src/entities/base_locale.entity.ts +++ b/libs/shared/src/entities/base_locale.entity.ts @@ -38,6 +38,10 @@ export class BaseLocale extends GlobalEntity { @Column('text', { nullable: false }) nom: string; + @ApiProperty() + @Column('json', { name: 'commune_noms_alt', nullable: true }) + communeNomsAlt: Record | null; + @ApiProperty() @Column('varchar', { nullable: false, length: 5 }) commune: string; diff --git a/libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts b/libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts index c15a9e12..9ca7b830 100644 --- a/libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts +++ b/libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts @@ -25,6 +25,7 @@ type BanIdsType = { type RowType = { banIds: BanIdsType; codeCommune: string; + communeNomsAlt: Record; codeVoie: string; numero: number; suffixe?: string; @@ -136,6 +137,12 @@ function createRow(obj: RowType, withComment: boolean): CsvRowType { row.commentaire_voie = obj.commentVoie; } + if (obj.communeNomsAlt) { + Object.keys(obj.communeNomsAlt).forEach((o) => { + row['commune_nom_' + o] = obj.communeNomsAlt[o]; + }); + } + if (obj.nomVoieAlt) { Object.keys(obj.nomVoieAlt).forEach((o) => { row['voie_nom_' + o] = obj.nomVoieAlt[o]; @@ -191,12 +198,13 @@ export async function exportBalToCsv( if (n.positions && n.positions.length > 0) { n.positions.forEach((p) => { rows.push({ - codeCommune: baseLocale.commune, banIds: { commune: baseLocale.banId, toponyme: v.banId, adresse: n.banId, }, + codeCommune: baseLocale.commune, + communeNomsAlt: baseLocale.communeNomsAlt, codeVoie: DEFAULT_CODE_VOIE, numero: n.numero, suffixe: n.suffixe, @@ -219,11 +227,12 @@ export async function exportBalToCsv( if (t.positions.length > 0) { t.positions.forEach((p) => { rows.push({ - codeCommune: baseLocale.commune, banIds: { commune: baseLocale.banId, toponyme: t.banId, }, + codeCommune: baseLocale.commune, + communeNomsAlt: baseLocale.communeNomsAlt, codeVoie: DEFAULT_CODE_VOIE, numero: DEFAULT_NUMERO_TOPONYME, updatedAt: t.updatedAt, @@ -240,6 +249,7 @@ export async function exportBalToCsv( toponyme: t.banId, }, codeCommune: baseLocale.commune, + communeNomsAlt: baseLocale.communeNomsAlt, codeVoie: DEFAULT_CODE_VOIE, numero: DEFAULT_NUMERO_TOPONYME, updatedAt: t.updatedAt, diff --git a/libs/shared/src/validators/validator_bal.validator.ts b/libs/shared/src/validators/validator_bal.validator.ts index ded725d7..80dddee2 100644 --- a/libs/shared/src/validators/validator_bal.validator.ts +++ b/libs/shared/src/validators/validator_bal.validator.ts @@ -35,11 +35,14 @@ export class ValidatorBal implements ValidatorConstraintInterface { if (supportedNomAlt.has(codeISO)) { const nomVoie = value[codeISO]; const { errors } = await validateurBAL(nomVoie, 'voie_nom'); - return errors.length === 0; + if (errors.length > 0) { + return false; + } } else { return false; } } + return true; } } catch { return false; @@ -50,8 +53,8 @@ export class ValidatorBal implements ValidatorConstraintInterface { defaultMessage(args: ValidationArguments) { const field = args.constraints[0]; - const value = - field === 'langAlt' ? Object.values(args.value)[0] : args.value; - return 'Le champ ' + field + ' : ' + value + " n'est pas valide"; + return field === 'langAlt' + ? "Le champ de langue régionale n'est pas valide" + : `Le champ ${field} : ${args.value} n'est pas valide`; } } diff --git a/migrations/1736757828173-commune_add_commune_noms_alt.ts b/migrations/1736757828173-commune_add_commune_noms_alt.ts new file mode 100644 index 00000000..cff778d4 --- /dev/null +++ b/migrations/1736757828173-commune_add_commune_noms_alt.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class CommuneAddCommuneNomsAlt1736757828173 + implements MigrationInterface +{ + name = 'CommuneAddCommuneNomsAlt1736757828173'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "bases_locales" ADD "commune_noms_alt" json`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "bases_locales" DROP COLUMN "commune_noms_alt"`, + ); + } +}