diff --git a/scripts/data/structures.js b/scripts/data/structures.js index eca896817fa2703f550bb9fd7f44dcec9381e2ce..0a3df8be0ca3ede30a3aff62c01a993a4563524e 100644 --- a/scripts/data/structures.js +++ b/scripts/data/structures.js @@ -301,7 +301,7 @@ module.exports = { address: { numero: '7', street: 'Rue Saint Polycarpe', - commune: 'Lyon 1er Arrondissement', + commune: 'Lyon 1er', }, hours: { monday: { @@ -871,7 +871,7 @@ module.exports = { address: { numero: '172', street: 'Avenue Général Frère', - commune: 'Lyon', + commune: 'Lyon 8e', }, createdAt: '2020-11-13T14:13:00.000Z', updatedAt: '2022-04-13T14:13:00.000Z', 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 a9b923ee20b9e19908382da04cbd3325cd69b29c..36c1ac841897dbfa41d1e980fc4a4ff2777d49c2 100644 --- a/src/structures/services/aptic-structures.service.ts +++ b/src/structures/services/aptic-structures.service.ts @@ -15,6 +15,7 @@ import { ApticStructure } from '../schemas/aptic-structure.schema'; import { Structure, StructureDocument } from '../schemas/structure.schema'; import { StructureTypeService } from './../structure-type/structure-type.service'; import { StructuresSearchService } from './structures-search.service'; +import { StructuresService } from './structures.service'; @Injectable() export class ApticStructuresService { @@ -23,6 +24,7 @@ export class ApticStructuresService { private readonly httpService: HttpService, private readonly userService: UsersService, private readonly categoriesService: CategoriesService, + private structureService: StructuresService, private structuresSearchService: StructuresSearchService, private structureTypeService: StructureTypeService, @InjectModel(Structure.name) private structureModel: Model<StructureDocument> @@ -41,6 +43,7 @@ export class ApticStructuresService { return this.getApticStructures(postalCode).subscribe( (res) => { res.data.presencePoints.forEach((structure) => { + this.logger.debug(`formatApticStructures | postal code: ${postalCode} | received : '${structure.name}'`); // Call aptic api for offer this.getApticStructureOffer(structure.catalogs[0]).subscribe( (serviceData) => { @@ -72,14 +75,17 @@ export class ApticStructuresService { if (!exist) { this.logger.log(`createApticStructures | Create structure : ${structure.name}`); const createdStructure = new this.structureModel(); + createdStructure.categories = {}; // Known fields createdStructure.structureName = structure.name; createdStructure.contactPhone = structure.phone; // Unkown fields (but mandatory) createdStructure.contactMail = 'unknown@unknown.com'; + createdStructure.categories.labelsQualifications = ['passNumerique']; createdStructure.structureType = await this.structureTypeService.findByValue('autre'); createdStructure.pmrAccess = false; createdStructure.remoteAccompaniment = false; + createdStructure.categories.accessModality = ['accesLibre']; createdStructure.accountVerified = true; createdStructure.freeWorkShop = false; createdStructure.nbComputers = 0; @@ -89,7 +95,6 @@ export class ApticStructuresService { createdStructure.coord = [structure.address.gpsLng, structure.address.gpsLat]; createdStructure.address = this.formatAddress(structure); // Set structure offer - createdStructure.categories = {}; createdStructure.categories.selfServiceMaterial = await this.setModules(structure, 'selfServiceMaterial'); createdStructure.categories.solidarityMaterial = await this.setModules(structure, 'solidarityMaterial'); createdStructure.categories.onlineProcedures = await this.setModules(structure, 'onlineProcedures'); @@ -101,8 +106,6 @@ export class ApticStructuresService { createdStructure.categories.languageAndIlliteracy = await this.setModules(structure, 'languageAndIlliteracy'); createdStructure.categories.handicaps = await this.setModules(structure, 'handicaps'); createdStructure.categories.publicOthers = ['toutPublic']; - createdStructure.categories.labelsQualifications = ['passNumerique']; - createdStructure.categories.accessModality = ['accesLibre']; createdStructure.save(); this.structuresSearchService.indexStructure(createdStructure); // Send admin weird structure mail @@ -224,10 +227,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) => { @@ -246,7 +249,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)); } @@ -299,7 +302,8 @@ export class ApticStructuresService { } else { address.street = structure.address.main; } - address.commune = structure.address.city; + + address.commune = this.structureService.getFormattedCity(structure.address.city, structure.address.zip); return address; } } diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index 09c3ed2ee0f3b6dd488d2ebcb4efe4fbf485b244..bf78edcae2240492f5220f3cc2fff92c98771e01 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -469,6 +469,23 @@ export class StructuresService { return false; } + /** + * Get city formatted without "Arrondissement" + * @param {searchQuery} data - Query address + */ + public getFormattedCity(city: string, postcode: string) { + let formattedCity: string = city; + if (city?.startsWith('Lyon')) { + const arrondissement = parseInt(postcode.slice(-2)); + if (arrondissement == 1) { + formattedCity = `Lyon 1er`; + } else { + formattedCity = `Lyon ${arrondissement}e`; + } + } + return formattedCity; + } + /** * Search structure address based on data search WS * @param {searchQuery} data - Query address @@ -522,6 +539,10 @@ export class StructuresService { ); }); + duplicateFreeArray.forEach((features) => { + features.properties.city = this.getFormattedCity(features.properties.city, features.properties.postcode); + }); + return { features: duplicateFreeArray, };