Created
June 19, 2019 16:06
-
-
Save pom421/8bc773c2fa2919857c63745a6ebe94fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { of, throwError } from 'rxjs'; | |
import { Company, CompanySearchResult, CompanyFromAddok, Feature } from '../model/Company'; | |
import { HttpClient, HttpParams } from '@angular/common/http'; | |
import { Api, ServiceUtils } from './service.utils'; | |
import { catchError, map, pluck, filter, flatMap } from 'rxjs/operators'; | |
import { deserialize } from 'json-typescript-mapper'; | |
export const MaxCompanyResult = 20; | |
export const Radius = 0.2; | |
// see https://wiki.openstreetmap.org/wiki/Map_Features | |
export const UNTAKE_POI_LIST = [ | |
'bicycle_parking', | |
'bus_stop', | |
'cathedral', | |
'chapel', | |
'church', | |
'clinic', | |
'clothes', | |
'hospital', | |
'military', | |
'mosque', | |
'place_of_worship', | |
'police', | |
'pitch', | |
'religious', | |
//'residential', | |
'school', | |
'shrine', | |
'social_facility', | |
'sports_centre', | |
'stadium', | |
'synagogue', | |
'temple', | |
'toilets', | |
'townhall', | |
'yes', | |
] | |
// see https://sirene.fr/sirene/public/variable/activnat | |
export const UNTAKE_NATURE_ACTIVITE_LIST = [ | |
'03', // Extraction | |
'04', // Fabrication, production | |
'07', // Transport | |
'08', // Import, export | |
'09', // Commerce de gros ou intermédiaire du commerce | |
'11', // Profession libérale | |
'13', // Location de meublés | |
'14', // Bâtiments, travaux publics | |
'15', // Services aux entreprises | |
'20', // Donneur d'ordre | |
] | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class CompanyService { | |
constructor(private http: HttpClient, | |
private serviceUtils: ServiceUtils) { | |
} | |
searchCompanies(search: string, searchPostalCode: string = "") { | |
let httpParams = new HttpParams(); | |
httpParams = httpParams.append('postalCode', searchPostalCode.toString()); | |
httpParams = httpParams.append('maxCount', MaxCompanyResult.toString()); | |
return this.http.get( | |
this.serviceUtils.getUrl(Api.Report, ['api', 'companies', search]), | |
{ | |
params: httpParams | |
} | |
).pipe( | |
map(result => deserialize(CompanySearchResult, result)), | |
map(result => result.companies), | |
catchError(err => { | |
if (err.status === 404) { | |
// return of(deserialize(CompanySearchResult, {total_results: 0})); | |
return of([] as Company[]); | |
} else { | |
return throwError(err); | |
} | |
}) | |
); | |
} | |
searchCompaniesFromAddok(search: string) { | |
let httpParams = new HttpParams(); | |
httpParams = httpParams.append('limit', '' + 20); | |
httpParams = httpParams.append('q', search); | |
return this.http.get( | |
this.serviceUtils.getUrl(Api.PoiAddok, ['search']), | |
{ | |
params: httpParams | |
} | |
).pipe( | |
map(result => deserialize(CompanyFromAddok, result)), | |
flatMap(result => result.features), | |
filter(feature => feature.properties && | |
! UNTAKE_POI_LIST.includes(feature.properties.poi) && | |
feature.properties.score >= 0.66), | |
catchError(err => { | |
if (err.status === 404) { | |
return of([] as Feature[]); | |
} else { | |
return throwError(err); | |
} | |
}) | |
); | |
} | |
searchCompaniesBySiret(siret: string) { | |
let httpParams = new HttpParams(); | |
httpParams = httpParams.append('siret', siret); | |
httpParams = httpParams.append('maxCount', MaxCompanyResult.toString()); | |
return this.http.get( | |
this.serviceUtils.getUrl(Api.Report, ['api', 'companies', 'siret', siret]), | |
{ | |
params: httpParams | |
} | |
).pipe( | |
map(result => deserialize(Company, result['etablissement'])), | |
catchError(err => { | |
if (err.status === 404) { | |
return of(undefined); | |
} else { | |
return throwError(err); | |
} | |
}) | |
); | |
} | |
getNearbyCompanies(lat: number, long: number, radius: number = Radius) { | |
let httpParams = new HttpParams(); | |
httpParams = httpParams.append('lat', lat.toString()); | |
httpParams = httpParams.append('long', long.toString()); | |
httpParams = httpParams.append('radius', radius.toString()); | |
httpParams = httpParams.append('maxCount', MaxCompanyResult.toString()); | |
return this.http.get( | |
this.serviceUtils.getUrl(Api.Report, ['api', 'companies', 'nearby', '']), | |
{ | |
params: httpParams | |
} | |
).pipe( | |
map(result => deserialize(CompanySearchResult, result)), | |
map(result => result.companies), | |
catchError(err => { | |
if (err.status === 404) { | |
return of([] as Company[]); | |
} else { | |
return throwError(err); | |
} | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment