diff --git a/app/controllers/data_sources/commune_controller.rb b/app/controllers/data_sources/commune_controller.rb index 0962bd9a0..4ceab5492 100644 --- a/app/controllers/data_sources/commune_controller.rb +++ b/app/controllers/data_sources/commune_controller.rb @@ -3,12 +3,12 @@ class DataSources::CommuneController < ApplicationController def search if params[:q].present? && params[:q].length > 1 - response = fetch_results + response = APIGeoService.commune_by_name_or_postal_code(params[:q]) if response.success? results = JSON.parse(response.body, symbolize_names: true) - render json: format_results(results) + render json: APIGeoService.format_commune_response(results, params[:with_combined_code]) else render json: [] end @@ -16,70 +16,4 @@ class DataSources::CommuneController < ApplicationController render json: [] end end - - private - - def fetch_results - if postal_code?(params[:q]) - fetch_by_postal_code(params[:q]) - else - fetch_by_name(params[:q]) - end - end - - def fetch_by_name(name) - Typhoeus.get("#{API_GEO_URL}/communes", params: { - type: 'commune-actuelle,arrondissement-municipal', - nom: name, - boost: 'population', - limit: 100 - }, timeout: 3) - end - - def fetch_by_postal_code(postal_code) - Typhoeus.get("#{API_GEO_URL}/communes", params: { - type: 'commune-actuelle,arrondissement-municipal', - codePostal: postal_code, - boost: 'population', - limit: 50 - }, timeout: 3) - end - - def postal_code?(string) - string.match?(/\A[-+]?\d+\z/) ? true : false - end - - def format_results(results) - results.reject(&method(:code_metropole?)).flat_map do |result| - item = { - name: result[:nom].tr("'", '’'), - code: result[:code], - epci_code: result[:codeEpci], - departement_code: result[:codeDepartement] - }.compact - - if result[:codesPostaux].present? - result[:codesPostaux].map { item.merge(postal_code: _1) } - else - [item] - end.map do |item| - if params[:with_combined_code].present? - { - label: "#{item[:name]} (#{item[:postal_code]})", - value: "#{item[:code]}-#{item[:postal_code]}" - } - else - { - label: "#{item[:name]} (#{item[:postal_code]})", - value: item[:code], - data: item[:postal_code] - } - end - end - end - end - - def code_metropole?(result) - result[:code].in?(['75056', '13055', '69123']) - end end diff --git a/app/services/api_geo_service.rb b/app/services/api_geo_service.rb index e7bf052a1..5a8e0bcc1 100644 --- a/app/services/api_geo_service.rb +++ b/app/services/api_geo_service.rb @@ -84,6 +84,14 @@ class APIGeoService communes(departement_code).find { _1[:code] == code }&.dig(:name) end + def commune_by_name_or_postal_code(query) + if postal_code?(query) + fetch_by_postal_code(query) + else + fetch_by_name(query) + end + end + def commune_code(departement_code, name) communes(departement_code).find { _1[:name] == name }&.dig(:code) end @@ -215,8 +223,42 @@ class APIGeoService commune_name(department_code, city_code) || fallback end + def format_commune_response(results, with_combined_code) + results.reject(&method(:code_metropole?)).flat_map do |result| + item = { + name: result[:nom].tr("'", '’'), + code: result[:code], + epci_code: result[:codeEpci], + departement_code: result[:codeDepartement] + }.compact + + if result[:codesPostaux].present? + result[:codesPostaux].map { item.merge(postal_code: _1) } + else + [item] + end.map do |item| + if with_combined_code.present? + { + label: "#{item[:name]} (#{item[:postal_code]})", + value: "#{item[:code]}-#{item[:postal_code]}" + } + else + { + label: "#{item[:name]} (#{item[:postal_code]})", + value: item[:code], + data: item[:postal_code] + } + end + end + end + end + private + def code_metropole?(result) + result[:code].in?(['75056', '13055', '69123']) + end + def communes_by_postal_code_map Rails.cache.fetch('api_geo_communes', expires_in: 1.day, version: 3) do departements @@ -251,6 +293,28 @@ class APIGeoService private + def fetch_by_name(name) + Typhoeus.get("#{API_GEO_URL}/communes", params: { + type: 'commune-actuelle,arrondissement-municipal', + nom: name, + boost: 'population', + limit: 100 + }, timeout: 3) + end + + def fetch_by_postal_code(postal_code) + Typhoeus.get("#{API_GEO_URL}/communes", params: { + type: 'commune-actuelle,arrondissement-municipal', + codePostal: postal_code, + boost: 'population', + limit: 50 + }, timeout: 3) + end + + def postal_code?(string) + string.match?(/\A[-+]?\d+\z/) ? true : false + end + def ban_address_schema JSONSchemer.schema(Rails.root.join('app/schemas/adresse-ban.json')) end