Skip to content

Commit

Permalink
feat: add communeDeleguee to numero and toponyme
Browse files Browse the repository at this point in the history
  • Loading branch information
fufeck committed Jan 14, 2025
1 parent ddbff61 commit def0dbe
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 6 deletions.
3 changes: 3 additions & 0 deletions apps/api/src/lib/utils/csv.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ function extractData(rows: Row[]): {
? beautifyNomAlt(toponymeRows[0].localizedValues.voie_nom)
: null,
positions: extractPositions(toponymeRows),
communeDeleguee:
toponymeRows[0].rawValues.commune_deleguee_insee || null,
createdAt: date,
updatedAt: date,
};
Expand Down Expand Up @@ -170,6 +172,7 @@ function extractData(rows: Row[]): {
certifie: numeroRows[0].parsedValues.certification_commune,
positions: extractPositions(numeroRows),
parcelles: numeroRows[0].parsedValues.cad_parcelles,
communeDeleguee: numeroRows[0].parsedValues.commune_deleguee_insee,
createdAt: date,
updatedAt: date,
};
Expand Down
6 changes: 6 additions & 0 deletions apps/api/src/modules/numeros/dto/create_numero.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

import { ValidatorBal } from '@/shared/validators/validator_bal.validator';
import { Position } from '@/shared/entities/position.entity';
import { ValidatorCogCommune } from '@/shared/validators/cog.validator';

export class CreateNumeroDTO {
@Validate(ValidatorBal, ['numero'])
Expand Down Expand Up @@ -41,6 +42,11 @@ export class CreateNumeroDTO {
@ApiProperty({ required: false, nullable: false })
certifie?: boolean;

@IsOptional()
@Validate(ValidatorCogCommune, ['commune_deleguee'])
@ApiProperty({ required: false, nullable: false })
communeDeleguee: string | null;

@ValidateNested({ each: true, message: 'positions must be an array' })
@ArrayNotEmpty()
@Type(() => Position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import {
IsNotEmpty,
IsMongoId,
ValidateIf,
Validate,
} from 'class-validator';

import { PositionTypeEnum } from '@/shared/entities/position.entity';
import { ValidatorCogCommune } from '@/shared/validators/cog.validator';

export class UpdateBatchNumeroChangeDTO {
@IsOptional()
Expand Down Expand Up @@ -38,4 +40,9 @@ export class UpdateBatchNumeroChangeDTO {
@IsNotEmpty()
@ApiProperty({ required: false, nullable: false })
certifie?: boolean;

@IsOptional()
@Validate(ValidatorCogCommune, ['commune_deleguee'])
@ApiProperty({ required: false, nullable: false })
communeDeleguee: string | null;
}
6 changes: 6 additions & 0 deletions apps/api/src/modules/numeros/dto/update_numero.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

import { Position } from '@/shared/entities/position.entity';
import { ValidatorBal } from '@/shared/validators/validator_bal.validator';
import { ValidatorCogCommune } from '@/shared/validators/cog.validator';

export class UpdateNumeroDTO {
@IsOptional()
Expand Down Expand Up @@ -47,6 +48,11 @@ export class UpdateNumeroDTO {
@ApiProperty({ required: false, nullable: false })
certifie?: boolean;

@IsOptional()
@Validate(ValidatorCogCommune, ['commune_deleguee'])
@ApiProperty({ required: false, nullable: false })
communeDeleguee: string | null;

@IsOptional()
@ValidateNested({ each: true, message: 'positions must be an array' })
@ArrayNotEmpty()
Expand Down
3 changes: 2 additions & 1 deletion apps/api/src/modules/numeros/numero.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export class NumeroService {
comment: createNumeroDto.comment || null,
parcelles: createNumeroDto.parcelles || [],
certifie: createNumeroDto.certifie || false,
communeDeleguee: createNumeroDto.communeDeleguee || null,
};
// Créer l'entité typeorm
const entityToSave: Numero = this.numerosRepository.create(numero);
Expand Down Expand Up @@ -435,7 +436,7 @@ export class NumeroService {
...(changes.toponymeId !== undefined && {
toponymeId: changes.toponymeId,
}),
...pick(changes, ['comment', 'certifie']),
...pick(changes, ['comment', 'certifie', 'communeDeleguee']),
};
// Si le positionType est changé, on change le type de la première position dans le batch
let positionTypeAffected: number = 0;
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/modules/toponyme/dto/create_toponyme.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

import { ValidatorBal } from '@/shared/validators/validator_bal.validator';
import { Position } from '@/shared/entities/position.entity';
import { ValidatorCogCommune } from '@/shared/validators/cog.validator';

export class CreateToponymeDTO {
@Validate(ValidatorBal, ['nom'])
Expand All @@ -21,6 +22,10 @@ export class CreateToponymeDTO {
@ApiProperty({ required: false, nullable: true })
nomAlt: Record<string, string>;

@Validate(ValidatorCogCommune, ['commune_deleguee'])
@ApiProperty({ required: false, nullable: true })
communeDeleguee?: string;

@IsOptional()
@Validate(ValidatorBal, ['cad_parcelles'])
@ApiProperty({ required: false, nullable: true })
Expand Down
5 changes: 5 additions & 0 deletions apps/api/src/modules/toponyme/dto/update_toponyme.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {

import { ValidatorBal } from '@/shared/validators/validator_bal.validator';
import { Position } from '@/shared/entities/position.entity';
import { ValidatorCogCommune } from '@/shared/validators/cog.validator';

export class UpdateToponymeDTO {
@IsOptional()
Expand All @@ -22,6 +23,10 @@ export class UpdateToponymeDTO {
@ApiProperty({ required: false, nullable: true })
nomAlt: Record<string, string>;

@Validate(ValidatorCogCommune, ['commune_deleguee'])
@ApiProperty({ required: false, nullable: true })
communeDeleguee?: string;

@IsOptional()
@Validate(ValidatorBal, ['cad_parcelles'])
@ApiProperty({ required: false, nullable: true })
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/modules/toponyme/toponyme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class ToponymeService {
nomAlt: createToponymeDto.nomAlt
? cleanNomAlt(createToponymeDto.nomAlt)
: null,
communeDeleguee: createToponymeDto.communeDeleguee || null,
positions: createToponymeDto.positions || [],
parcelles: createToponymeDto.parcelles || [],
};
Expand Down Expand Up @@ -228,6 +229,7 @@ export class ToponymeService {
nom: cleanNom(rawToponyme.nom),
parcelles: rawToponyme.parcelles || [],
nomAlt: getNomAltDefault(rawToponyme.nomAlt),
communeDeleguee: rawToponyme.communeDeleguee,
...(rawToponyme.updatedAt && { updatedAt: rawToponyme.updatedAt }),
...(rawToponyme.createdAt && { createdAt: rawToponyme.createdAt }),
}));
Expand Down
9 changes: 9 additions & 0 deletions libs/shared/src/entities/numero.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export class Numero extends GlobalEntity {
@Column('boolean', { nullable: false, default: false })
certifie?: boolean;

@ApiProperty()
@Column('varchar', {
name: 'commune_deleguee',
default: null,
nullable: true,
length: 5,
})
communeDeleguee: string | null;

@ApiProperty({ type: () => Position, isArray: true })
@OneToMany(() => Position, (position) => position.numero, {
eager: true,
Expand Down
9 changes: 9 additions & 0 deletions libs/shared/src/entities/toponyme.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export class Toponyme extends GlobalEntity {
@Column('json', { name: 'nom_alt', nullable: true })
nomAlt: Record<string, string> | null;

@ApiProperty()
@Column('varchar', {
name: 'commune_deleguee',
default: null,
nullable: true,
length: 5,
})
communeDeleguee: string | null;

@ApiProperty()
@Column('text', { nullable: true, array: true })
parcelles?: string[] | null;
Expand Down
11 changes: 10 additions & 1 deletion libs/shared/src/modules/export_csv/utils/export_csv_bal.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as proj from '@etalab/project-legal';
import { Toponyme } from '@/shared/entities/toponyme.entity';
import { Numero } from '@/shared/entities/numero.entity';
import { Voie } from '@/shared/entities/voie.entity';
import { getCommune } from '@/shared/utils/cog.utils';
import { getCommune, getOldCommune } from '@/shared/utils/cog.utils';
import { roundCoordinate } from '@/shared/utils/coor.utils';
import { BaseLocale } from '@/shared/entities/base_locale.entity';

Expand All @@ -25,6 +25,7 @@ type BanIdsType = {
type RowType = {
banIds: BanIdsType;
codeCommune: string;
communeDeleguee?: string;
codeVoie: string;
numero: number;
suffixe?: string;
Expand Down Expand Up @@ -52,6 +53,8 @@ type CsvRowType = {
certification_commune: string;
commune_insee: string;
commune_nom: string;
commune_deleguee_insee: string;
commune_deleguee_nom: string;
position: string;
long: string;
lat: string;
Expand Down Expand Up @@ -121,6 +124,10 @@ function createRow(obj: RowType, withComment: boolean): CsvRowType {
certification_commune: toCsvBoolean(obj.certifie),
commune_insee: obj.codeCommune,
commune_nom: getCommune(obj.codeCommune).nom,
commune_deleguee_insee: obj.communeDeleguee || null,
commune_deleguee_nom: obj.communeDeleguee
? getOldCommune(obj.communeDeleguee).nom
: null,
position: '',
long: '',
lat: '',
Expand Down Expand Up @@ -192,6 +199,7 @@ export async function exportBalToCsv(
n.positions.forEach((p) => {
rows.push({
codeCommune: baseLocale.commune,
communeDeleguee: n.communeDeleguee,
banIds: {
commune: baseLocale.banId,
toponyme: v.banId,
Expand Down Expand Up @@ -220,6 +228,7 @@ export async function exportBalToCsv(
t.positions.forEach((p) => {
rows.push({
codeCommune: baseLocale.commune,
communeDeleguee: t.communeDeleguee,
banIds: {
commune: baseLocale.banId,
toponyme: t.banId,
Expand Down
23 changes: 22 additions & 1 deletion libs/shared/src/utils/cog.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ import * as communes from '@etalab/decoupage-administratif/data/communes.json';
import * as departements from '@etalab/decoupage-administratif/data/departements.json';
import { CommuneCOG } from '../types/cog.type';

export enum CommuneTypeEnum {
COMMUNE_ACTUELLE = 'commune-actuelle',
ARRONDISSEMENT_MUNICIPAL = 'arrondissement-municipal',
COMMUNE_DELEGUEE = 'commune-deleguee',
}

const filteredCommunes: CommuneCOG[] = (communes as Array<any>).filter((c) =>
['commune-actuelle', 'arrondissement-municipal'].includes(c.type),
[
CommuneTypeEnum.COMMUNE_ACTUELLE,
CommuneTypeEnum.ARRONDISSEMENT_MUNICIPAL,
].includes(c.type),
);

const communesIndex: Record<string, CommuneCOG> = keyBy(
filteredCommunes,
'code',
);

const oldCommunes: CommuneCOG[] = (communes as Array<any>).filter((c) =>
[CommuneTypeEnum.COMMUNE_DELEGUEE].includes(c.type),
);

const oldCommunesIndex: Record<string, CommuneCOG> = keyBy(oldCommunes, 'code');

const departementsIndex = keyBy(departements, 'code');

const communesByDepartementIndex = groupBy(communes, 'departement');
Expand All @@ -18,6 +35,10 @@ export function getCommunesByDepartement(codeDepartement) {
return communesByDepartementIndex[codeDepartement] || [];
}

export function getOldCommune(codeCommune) {
return oldCommunesIndex[codeCommune];
}

export function getCommune(codeCommune) {
return communesIndex[codeCommune];
}
Expand Down
11 changes: 8 additions & 3 deletions libs/shared/src/validators/cog.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import {
ValidatorConstraintInterface,
ValidationArguments,
} from 'class-validator';
import { getCommune } from '../utils/cog.utils';
import { getCommune, getOldCommune } from '../utils/cog.utils';

@ValidatorConstraint({ name: 'validatorCogCommune' })
export class ValidatorCogCommune implements ValidatorConstraintInterface {
constructor() {}

validate(commune: string) {
return Boolean(getCommune(commune));
validate(commune: string, args: ValidationArguments) {
const field = args.constraints[0];
if (field === 'commune') {
return Boolean(getCommune(commune));
} else if (field === 'commune_deleguee') {
return commune ? Boolean(getOldCommune(commune)) : true;
}
}

defaultMessage(args: ValidationArguments) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

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

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "toponymes" ADD "commune_deleguee" character varying(5)`,
);
await queryRunner.query(
`ALTER TABLE "numeros" ADD "commune_deleguee" character varying(5)`,
);
}

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

0 comments on commit def0dbe

Please sign in to comment.