diff --git a/src/structures/common/regex.ts b/src/structures/common/regex.ts new file mode 100644 index 0000000000000000000000000000000000000000..3cdf549441a5c22a8937e91c2fe585160ff16a7b --- /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 b58185d9d840c3c2e5ce30963d07fb5647ac879f..d1bb442cade9565dd040ffc852a4da2a2652ae13 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 8d313a822ff90e89b1cf4cb8ddd9f68373424e99..0de71acb333cecc431c4f85786235dc4efe89b15 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')