diff --git a/app/models/champs/rna_champ.rb b/app/models/champs/rna_champ.rb index 1f79cfdbd..b738922d8 100644 --- a/app/models/champs/rna_champ.rb +++ b/app/models/champs/rna_champ.rb @@ -13,6 +13,10 @@ class Champs::RNAChamp < Champ data&.dig("association_titre") end + def update_with_external_data!(data:) + update!(data:, value_json: extract_value_json(data:)) + end + def identifier title.present? ? "#{value} (#{title})" : value end @@ -41,4 +45,11 @@ class Champs::RNAChamp < Champ city_code: address["code_insee"] }.with_indifferent_access end + + private + + def extract_value_json(data:) + h = APIGeoService.parse_rna_address(data['adresse']) + h.merge(title: data['association_titre']) + end end diff --git a/app/models/concerns/rna_champ_association_fetchable_concern.rb b/app/models/concerns/rna_champ_association_fetchable_concern.rb index b1e95c97e..8a5f322e8 100644 --- a/app/models/concerns/rna_champ_association_fetchable_concern.rb +++ b/app/models/concerns/rna_champ_association_fetchable_concern.rb @@ -12,7 +12,7 @@ module RNAChampAssociationFetchableConcern return clear_association!(:invalid) unless valid_champ_value? return clear_association!(:not_found) if (data = APIEntreprise::RNAAdapter.new(rna, procedure_id).to_params).blank? - update!(data:, value_json: APIGeoService.parse_rna_address(data['adresse'])) + update_with_external_data!(data:) rescue APIEntreprise::API::Error, APIEntrepriseToken::TokenError => error if APIEntrepriseService.service_unavailable_error?(error, target: :djepva) clear_association!(:network_error) diff --git a/app/tasks/maintenance/populate_rna_json_value_task.rb b/app/tasks/maintenance/populate_rna_json_value_task.rb index 1998dea15..e366d3aee 100644 --- a/app/tasks/maintenance/populate_rna_json_value_task.rb +++ b/app/tasks/maintenance/populate_rna_json_value_task.rb @@ -14,7 +14,7 @@ module Maintenance return if champ&.dossier&.procedure&.id.blank? data = APIEntreprise::RNAAdapter.new(champ.value, champ&.dossier&.procedure&.id).to_params return if data.blank? - champ.update(value_json: APIGeoService.parse_rna_address(data['adresse'])) + champ.update_with_external_data!(data:) rescue URI::InvalidURIError # some Champs::RNAChamp contain spaces which raise this error rescue ActiveRecord::RecordNotFound diff --git a/spec/controllers/champs/rna_controller_spec.rb b/spec/controllers/champs/rna_controller_spec.rb index 9b149e8dc..3f5c59daf 100644 --- a/spec/controllers/champs/rna_controller_spec.rb +++ b/spec/controllers/champs/rna_controller_spec.rb @@ -134,7 +134,8 @@ describe Champs::RNAController, type: :controller do "region_name" => nil, "street_address" => "33 rue de Modagor", "street_name" => "de Modagor", - "street_number" => "33" + "street_number" => "33", + "title" => "LA PRÉVENTION ROUTIERE" }) end end diff --git a/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb b/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb index df727227c..e83600037 100644 --- a/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb +++ b/spec/tasks/maintenance/populate_rna_json_value_task_spec.rb @@ -33,7 +33,8 @@ module Maintenance "departement_name" => nil, "department_name" => nil, "region_code" => nil, - "region_name" => nil + "region_name" => nil, + "title" => "LA PRÉVENTION ROUTIERE" }) end end