Commit 1383c132 authored by Hugo SUBTIL's avatar Hugo SUBTIL
Browse files

Merge branch 'feat/us34-department-address' into 'dev'

feat/US34-department-address

See merge request !109
parents 32ccd5e4 ed4e89a8
Pipeline #20184 passed with stages
in 6 minutes and 55 seconds
export const depRegex = /69[0-9]{3}/g;
......@@ -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));
}
......
......@@ -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')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment