From ed4e89a8854ba5f882b22a84daffcc8100394cca Mon Sep 17 00:00:00 2001 From: Hugo SUBTIL <ext.sopra.husubtil@grandlyon.com> Date: Thu, 20 Jan 2022 09:59:18 +0000 Subject: [PATCH] feat(structure): change addresse api in order to cover the all department. /photon is now used instead of /photon-bal --- src/structures/common/regex.ts | 1 + src/structures/services/structures.service.ts | 27 ++++++++++++------- src/structures/structures.controller.ts | 17 ++++++++---- 3 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 src/structures/common/regex.ts diff --git a/src/structures/common/regex.ts b/src/structures/common/regex.ts new file mode 100644 index 000000000..3cdf54944 --- /dev/null +++ b/src/structures/common/regex.ts @@ -0,0 +1 @@ +export const depRegex = /69[0-9]{3}/g; diff --git a/src/structures/services/structures.service.ts b/src/structures/services/structures.service.ts index b58185d9d..d1bb442ca 100644 --- a/src/structures/services/structures.service.ts +++ b/src/structures/services/structures.service.ts @@ -21,6 +21,7 @@ import { CategoriesAccompagnement } from '../../categories/schemas/categoriesAcc import { CategoriesFormations } from '../../categories/schemas/categoriesFormations.schema'; import { CategoriesOthers } from '../../categories/schemas/categoriesOthers.schema'; import { UnclaimedStructureDto } from '../../admin/dto/unclaimed-structure-dto'; +import { depRegex } from '../common/regex'; @Injectable() export class StructuresService { @@ -340,12 +341,13 @@ export class StructuresService { /** * Search structure address based on data search WS + * @param {searchQuery} data - Query address */ - public async searchAddress(data: { searchQuery: string }): Promise<AxiosResponse<any>> { - const req = - 'https://download.data.grandlyon.com/geocoding/photon-bal/api?q=' + - data.searchQuery + - '&lat=45.75&lon=4.85&lang=fr&limit=50&osm_tag=:!construction&osm_tag=:!bus_stop'; + public async searchAddress(data: { + searchQuery: string; + }): Promise<AxiosResponse<{ features: { geometry: {}; type: string; properties: {} }[] }>> { + const req = `https://download.data.grandlyon.com/geocoding/photon/api?q=${data.searchQuery}&lang=fr&limit=500&osm_tag=:!construction&osm_tag=:!bus_stop`; + Logger.debug(`Search request: ${encodeURI(req)}`, 'StructureService'); return new Promise((resolve, reject) => { this.httpService .request({ @@ -355,10 +357,16 @@ export class StructuresService { }) .subscribe( (reply) => { + Logger.debug(`Search request response length : ${reply.data.features.length}`, 'StructureService'); + reply.data.features = reply.data.features + .filter((doc) => doc.properties.postcode && doc.properties.postcode.match(depRegex)) + .sort((a, b) => { + return b.properties.housenumber ? 1 : -1; + }); return resolve(reply.data); }, (err) => { - Logger.error(`Request error: ${err.config.url}`, 'StructureService - search'); + Logger.error(`Search - Request error: ${err.config.url}`, 'StructureService'); Logger.error(err); } ); @@ -372,7 +380,7 @@ export class StructuresService { */ public async countByStructureKey(key: string, selected: { id: string; text: string }[]): Promise<any> { const uniqueElements = await this.structureModel.distinct(key).exec(); - return await Promise.all( + return Promise.all( uniqueElements.map(async (value) => { const keyList: FilterQuery<DocumentDefinition<StructureDocument>>[] = []; keyList.push({ @@ -402,9 +410,8 @@ export class StructuresService { } public getCoord(numero: string, address: string, zipcode: string): Observable<AxiosResponse<any>> { - const req = - 'https://download.data.grandlyon.com/geocoding/photon-bal/api' + '?q=' + numero + ' ' + address + ' ' + zipcode; - Logger.log(`Request : ${req}`, 'StructureService - getCoord'); + const req = 'https://download.data.grandlyon.com/geocoding/photon/api?q=' + numero + ' ' + address + ' ' + zipcode; + Logger.log(`getCoord - Request : ${req}`, 'StructureService'); return this.httpService.get(encodeURI(req)); } diff --git a/src/structures/structures.controller.ts b/src/structures/structures.controller.ts index 8d313a822..0de71acb3 100644 --- a/src/structures/structures.controller.ts +++ b/src/structures/structures.controller.ts @@ -30,6 +30,7 @@ import { structureDto } from './dto/structure.dto'; import { Structure, StructureDocument } from './schemas/structure.schema'; import { StructuresService } from './services/structures.service'; import { RolesGuard } from '../users/guards/roles.guard'; +import { depRegex } from './common/regex'; @Controller('structures') export class StructuresController { @@ -51,11 +52,17 @@ export class StructuresController { @Get('coordinates/:zipcode') @ApiParam({ name: 'zipcode', type: String, required: true }) public async getCoordinates(@Param('zipcode') city: string): Promise<any> { - return await this.httpService - .get(encodeURI('https://download.data.grandlyon.com/geocoding/photon-bal/api?q=' + city)) + return this.httpService + .get(encodeURI(`https://download.data.grandlyon.com/geocoding/photon/api?q=${city}`)) .toPromise() .then(async (res) => res.data.features) - .then((data) => data.filter((cityPoint) => cityPoint.properties.city.toLowerCase().includes(city.toLowerCase()))) + .then((data) => + data.filter( + (cityPoint) => + cityPoint.properties.city?.toLowerCase().includes(city.toLowerCase()) && + cityPoint.properties.postcode.match(depRegex) + ) + ) .then((data) => data.map((filteredCityPoint) => filteredCityPoint.geometry.coordinates)); } @@ -66,7 +73,7 @@ export class StructuresController { @Post('search') public async search(@Query() query: QueryStructure, @Body() body): Promise<Structure[]> { - return await this.structureService.searchForStructures(query.query, body ? body.filters : null); + return this.structureService.searchForStructures(query.query, body ? body.filters : null); } @Post('resetSearchIndex') @@ -138,7 +145,7 @@ export class StructuresController { @Post('address') public async searchAddress(@Body() data: { searchQuery: string }) { - return await this.structureService.searchAddress(data); + return this.structureService.searchAddress(data); } @Get(':id') -- GitLab