Skip to content

Commit

Permalink
feat: add nomAlt to commune (#498)
Browse files Browse the repository at this point in the history
* feat: add nomAlt to commune

* correct test

* return gfay

* corect validateur
  • Loading branch information
fufeck authored Jan 15, 2025
1 parent 0f94b10 commit 1db9ef4
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 9 deletions.
7 changes: 6 additions & 1 deletion apps/api/src/lib/utils/csv.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type FromCsvType = {
validationError?: string;
accepted?: number;
rejected?: number;
communeNomsAlt?: Record<string, string>;
voies?: Partial<Voie>[];
numeros?: Partial<Numero>[];
toponymes?: Partial<Toponyme>[];
Expand Down Expand Up @@ -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]),
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 13 additions & 2 deletions apps/api/src/modules/base_locale/base_locale.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ export class BaseLocaleService {
voies,
numeros,
toponymes,
communeNomsAlt,
isValid,
accepted,
rejected,
Expand All @@ -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);

Expand Down Expand Up @@ -389,8 +395,13 @@ export class BaseLocaleService {

async populate(
baseLocale: BaseLocale,
{ voies, toponymes, numeros }: FromCsvType,
{ voies, toponymes, numeros, communeNomsAlt }: FromCsvType,
): Promise<BaseLocale> {
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 });
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -13,6 +16,12 @@ export class UpdateBaseLocaleDTO {
@IsNotEmpty()
nom?: string;

@IsOptional()
@IsNotEmptyObject()
@Validate(ValidatorBal, ['langAlt'])
@ApiProperty({ required: false, nullable: true })
communeNomsAlt: Record<string, string>;

@IsOptional()
@ApiProperty({ required: false, nullable: false })
@ArrayNotEmpty()
Expand Down
1 change: 1 addition & 0 deletions apps/api/test/base_locale.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const baseLocalePublicProperties = [
'id',
'banId',
'nom',
'communeNomsAlt',
'commune',
'nbNumeros',
'nbNumerosCertifies',
Expand Down
4 changes: 4 additions & 0 deletions libs/shared/src/entities/base_locale.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> | null;

@ApiProperty()
@Column('varchar', { nullable: false, length: 5 })
commune: string;
Expand Down
14 changes: 12 additions & 2 deletions libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type BanIdsType = {
type RowType = {
banIds: BanIdsType;
codeCommune: string;
communeNomsAlt: Record<string, string>;
codeVoie: string;
numero: number;
suffixe?: string;
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions libs/shared/src/validators/validator_bal.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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`;
}
}
19 changes: 19 additions & 0 deletions migrations/1736757828173-commune_add_commune_noms_alt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CommuneAddCommuneNomsAlt1736757828173
implements MigrationInterface
{
name = 'CommuneAddCommuneNomsAlt1736757828173';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "bases_locales" ADD "commune_noms_alt" json`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "bases_locales" DROP COLUMN "commune_noms_alt"`,
);
}
}

0 comments on commit 1db9ef4

Please sign in to comment.