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) {
term = encodeURIComponent(term.replace(/\(|\)/g, ''));
if (scope === 'adresse') {
return `${api_adresse_url}/search?q=${term}&limit=${API_ADRESSE_QUERY_LIMIT}`;
} else if (scope === 'annuaire-education') {
return `${api_education_url}/search?dataset=fr-en-annuaire-education&q=${term}&rows=${API_EDUCATION_QUERY_LIMIT}`;
} else if (scope === 'communes') {
const limit = `limit=${API_GEO_COMMUNES_QUERY_LIMIT}`;
const url = extra
? `${api_geo_url}/communes?codeDepartement=${extra}&${limit}&`
: `${api_geo_url}/communes?${limit}&`;
if (isNumeric(term)) {
return `${url}codePostal=${term}`;
const params = new URLSearchParams();
let path = `${api_geo_url}/${scope}`;
if (scope == 'adresse') {
path = `${api_adresse_url}/search`;
params.set('q', term);
params.set('limit', `${API_ADRESSE_QUERY_LIMIT}`);
} else if (scope == 'annuaire-education') {
path = `${api_education_url}/search`;
params.set('q', 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`;
} else if (isNumeric(term)) {
const code = term.padStart(2, '0');
return `${api_geo_url}/${scope}?code=${code}&limit=${API_GEO_QUERY_LIMIT}`;
if (isNumeric(term)) {
params.set('codePostal', term);
} else {
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 ({

View file

@ -35,7 +35,7 @@ class APIGeoService
end
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
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
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.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