feat(apigeo): enable all zones on departements api

This commit is contained in:
Paul Chavard 2023-02-22 17:22:00 +01:00
parent d943e34b6d
commit bdb15aaa4a
5 changed files with 51 additions and 33 deletions

View file

@ -27,24 +27,42 @@ type QueryKey = readonly [
function buildURL(scope: string, term: string, extra?: string) { function buildURL(scope: string, term: string, extra?: string) {
term = encodeURIComponent(term.replace(/\(|\)/g, '')); term = encodeURIComponent(term.replace(/\(|\)/g, ''));
if (scope === 'adresse') { const params = new URLSearchParams();
return `${api_adresse_url}/search?q=${term}&limit=${API_ADRESSE_QUERY_LIMIT}`; let path = `${api_geo_url}/${scope}`;
} else if (scope === 'annuaire-education') {
return `${api_education_url}/search?dataset=fr-en-annuaire-education&q=${term}&rows=${API_EDUCATION_QUERY_LIMIT}`; if (scope == 'adresse') {
} else if (scope === 'communes') { path = `${api_adresse_url}/search`;
const limit = `limit=${API_GEO_COMMUNES_QUERY_LIMIT}`; params.set('q', term);
const url = extra params.set('limit', `${API_ADRESSE_QUERY_LIMIT}`);
? `${api_geo_url}/communes?codeDepartement=${extra}&${limit}&` } else if (scope == 'annuaire-education') {
: `${api_geo_url}/communes?${limit}&`; path = `${api_education_url}/search`;
if (isNumeric(term)) { params.set('q', term);
return `${url}codePostal=${term}`; params.set('rows', `${API_EDUCATION_QUERY_LIMIT}`);
params.set('dataset', 'fr-en-annuaire-education');
} else if (scope == 'communes') {
if (extra) {
params.set('codeDepartement', extra);
} }
return `${url}nom=${term}&boost=population`; if (isNumeric(term)) {
} else if (isNumeric(term)) { params.set('codePostal', term);
const code = term.padStart(2, '0'); } else {
return `${api_geo_url}/${scope}?code=${code}&limit=${API_GEO_QUERY_LIMIT}`; params.set('nom', term);
params.set('boost', 'population');
}
params.set('limit', `${API_GEO_COMMUNES_QUERY_LIMIT}`);
} else {
if (isNumeric(term)) {
params.set('code', term.padStart(2, '0'));
} else {
params.set('nom', term);
}
if (scope == 'departements') {
params.set('zone', 'metro,drom,com');
}
params.set('limit', `${API_GEO_QUERY_LIMIT}`);
} }
return `${api_geo_url}/${scope}?nom=${term}&limit=${API_GEO_QUERY_LIMIT}`;
return `${path}?${params}`;
} }
const defaultQueryFn: QueryFunction<unknown, QueryKey> = async ({ const defaultQueryFn: QueryFunction<unknown, QueryKey> = async ({

View file

@ -35,7 +35,7 @@ class APIGeoService
end end
def departements def departements
[{ code: '99', name: 'Etranger' }] + get_from_api_geo(:departements).sort_by { _1[:code] } [{ code: '99', name: 'Etranger' }] + get_from_api_geo('departements?zone=metro,drom,com').sort_by { _1[:code] }
end end
def departement_name(code) def departement_name(code)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -36,10 +36,10 @@ describe APIGeoService do
describe 'departements', vcr: { cassette_name: 'api_geo_departements' } do describe 'departements', vcr: { cassette_name: 'api_geo_departements' } do
it 'return sorted results' do it 'return sorted results' do
expect(APIGeoService.departements.size).to eq(102) expect(APIGeoService.departements.size).to eq(110)
expect(APIGeoService.departements.first).to eq(code: '99', name: 'Etranger') expect(APIGeoService.departements.first).to eq(code: '99', name: 'Etranger')
expect(APIGeoService.departements.second).to eq(code: '01', name: 'Ain') expect(APIGeoService.departements.second).to eq(code: '01', name: 'Ain')
expect(APIGeoService.departements.last).to eq(code: '976', name: 'Mayotte') expect(APIGeoService.departements.last).to eq(code: '989', name: 'Île de Clipperton')
end end
end end