diff --git a/app/jobs/api_entreprise/service_job.rb b/app/jobs/api_entreprise/service_job.rb index d1e70f71f..01e5c62c3 100644 --- a/app/jobs/api_entreprise/service_job.rb +++ b/app/jobs/api_entreprise/service_job.rb @@ -5,16 +5,21 @@ class APIEntreprise::ServiceJob < APIEntreprise::Job service_params = APIEntreprise::ServiceAdapter.new(service.siret, service_id).to_params service.etablissement_infos = service_params - point = Geocoder.search(service_params[:adresse]).first - - service.etablissement_lat = point&.latitude - service.etablissement_lng = point&.longitude - code_insee = service.etablissement_infos['code_insee_localite'] - if code_insee + if code_insee.present? service.departement = CodeInsee.new(code_insee).to_departement end + if service_params[:adresse].present? + point = Geocoder.search(service_params[:adresse], params: { citycode: code_insee, limit: 1 }).first + + service.etablissement_lat = point&.latitude + service.etablissement_lng = point&.longitude + else + service.etablissement_lat = nil + service.etablissement_lng = nil + end + service.save! end end diff --git a/spec/jobs/api_entreprise/service_job_spec.rb b/spec/jobs/api_entreprise/service_job_spec.rb index 6a5fbc704..1831c1f55 100644 --- a/spec/jobs/api_entreprise/service_job_spec.rb +++ b/spec/jobs/api_entreprise/service_job_spec.rb @@ -14,7 +14,7 @@ RSpec.describe APIEntreprise::ServiceJob, type: :job do Geocoder.configure(lookup: :ban_data_gouv_fr, use_https: true) - stub_request(:get, "https://api-adresse.data.gouv.fr/search/?q=#{adresse}") + stub_request(:get, "https://api-adresse.data.gouv.fr/search/?citycode=75112&limit=1&q=#{adresse}") .to_return(body: geocoder_body, status: status) end @@ -67,7 +67,7 @@ RSpec.describe APIEntreprise::ServiceJob, type: :job do geocoder_response = JSON.parse(geocoder_body) geocoder_response["features"] = [] - stub_request(:get, "https://api-adresse.data.gouv.fr/search/?q=#{adresse}") + stub_request(:get, "https://api-adresse.data.gouv.fr/search/?citycode=75112&limit=1&q=#{adresse}") .to_return(body: JSON.generate(geocoder_response), status: status) subject