diff --git a/src/migrations/scripts/1653396510972-format-lyon-arrondissement.ts b/src/migrations/scripts/1653396510972-format-lyon-arrondissement.ts new file mode 100644 index 0000000000000000000000000000000000000000..b3438f886687ca21fa900278980813fd8a09f65a --- /dev/null +++ b/src/migrations/scripts/1653396510972-format-lyon-arrondissement.ts @@ -0,0 +1,49 @@ +import { Db, Cursor } from 'mongodb'; +import { StructureDocument } from '../../structures/schemas/structure.schema'; +import { getDb } from '../migrations-utils/db'; + +export const up = async () => { + const db: Db = await getDb(); + const cursor: Cursor<StructureDocument> = db.collection('structures').find({}); + let document: StructureDocument; + while ((document = await cursor.next())) { + const newDoc = updateStructure(document); + await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + console.log("Updated : removed 'Arrondissement' from structure's commune "); +}; + +export const down = async () => { + const db: Db = await getDb(); + const cursor: Cursor<StructureDocument> = db.collection('structures').find({}); + let document: StructureDocument; + while ((document = await cursor.next())) { + const newDoc = downgradeStructure(document); + await db.collection('structures').updateOne({ _id: document._id }, [{ $set: newDoc }]); + } + console.log("Downgraded : added 'Arrondissement' to structure's commune"); +}; + +function updateStructure(doc: StructureDocument): StructureDocument { + if (doc?.address?.commune && doc.address.commune.startsWith('Lyon')) { + doc.address.commune = doc.address.commune.replace('er Arrondissement', 'er'); + doc.address.commune = doc.address.commune.replace('ème Arrondissement', 'e'); + } + return doc; +} + +function downgradeStructure(doc: StructureDocument) { + if (doc?.address?.commune && doc.address.commune.startsWith('Lyon')) { + // First arrondissement + if (doc.address.commune.endsWith('er')) { + doc.address.commune = doc.address.commune + ' Arrondissement'; + return doc; + } + // Other arrondissement + if (doc.address.commune.endsWith('e')) { + doc.address.commune = doc.address.commune.slice(0, -1) + 'ème Arrondissement'; + return doc; + } + } + return doc; +} diff --git a/src/structures/services/aptic-structures.service.ts b/src/structures/services/aptic-structures.service.ts index 24a26bdbd87c0e041fcca54fa6dbaf0e45a10d12..1b198c1596395e51f51077100c9efc010731edc2 100644 --- a/src/structures/services/aptic-structures.service.ts +++ b/src/structures/services/aptic-structures.service.ts @@ -1,5 +1,4 @@ -import { HttpService, Injectable } from '@nestjs/common'; -import { Logger } from '@nestjs/common'; +import { HttpService, Injectable, Logger } from '@nestjs/common'; import { Observable } from 'rxjs'; import { AxiosResponse } from 'axios'; import { Cron, CronExpression } from '@nestjs/schedule'; @@ -183,10 +182,10 @@ export class ApticStructuresService { * Get Metropole new aptic structure evey week. For testing, please change the expression */ @Cron(CronExpression.EVERY_WEEK) - public getMetopoleMunicipality(): void { + public getMetropoleMunicipality(): void { const req = 'https://download.data.grandlyon.com/ws/grandlyon/adr_voie_lieu.adrcomgl/all.json?maxfeatures=-1&start=1'; - this.logger.log(`getMetopoleMunicipality | Request : ${req}`, ''); + this.logger.log(`getMetropoleMunicipality | Request : ${req}`, ''); this.httpService.get(encodeURI(req)).subscribe( (data) => { const inseeArray = data.data.values.map((municipality) => { @@ -205,7 +204,7 @@ export class ApticStructuresService { public getPostalCodeWithINSEE(inseeCode: string): Observable<AxiosResponse<any>> { const req = `https://geo.api.gouv.fr/communes/${inseeCode}?fields=codesPostaux&format=json`; - this.logger.debug(`getMetopoleMunicipality | Request : ${req}`); + this.logger.debug(`getMetropoleMunicipality | Request : ${req}`); return this.httpService.get(encodeURI(req)); } @@ -258,7 +257,23 @@ export class ApticStructuresService { } else { address.street = structure.address.main; } - address.commune = structure.address.city; + + if (structure.address.city === 'Lyon') { + const arrondissement = parseInt(structure.address.zip.slice(-2)); + switch (arrondissement) { + case 0: + address.commune = structure.address.city; + break; + case 1: + address.commune = `${structure.address.city} 1er`; + break; + default: + address.commune = `${structure.address.city} ${arrondissement}e`; + break; + } + } else { + address.commune = structure.address.city; + } return address; } }