From a9237bf7f1c9dc53e8bc04021f7a053e71c64211 Mon Sep 17 00:00:00 2001 From: mfo Date: Fri, 15 Nov 2024 09:20:40 +0100 Subject: [PATCH 1/3] feat(rnf): also store rnf title --- app/models/champs/rnf_champ.rb | 9 ++++++- spec/models/champs/rnf_champ_spec.rb | 25 ++++++++++--------- .../populate_rnf_json_value_task_spec.rb | 6 +++-- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/models/champs/rnf_champ.rb b/app/models/champs/rnf_champ.rb index 3b9a8fa7a..0dab98707 100644 --- a/app/models/champs/rnf_champ.rb +++ b/app/models/champs/rnf_champ.rb @@ -16,7 +16,7 @@ class Champs::RNFChamp < Champ end def update_with_external_data!(data:) - update!(data:, value_json: APIGeoService.parse_rnf_address(data[:address])) + update!(data:, value_json: extract_value_json(data:)) end def fetch_external_data? @@ -109,4 +109,11 @@ class Champs::RNFChamp < Champ address['label'] end end + + private + + def extract_value_json(data:) + h = APIGeoService.parse_rnf_address(data[:address]) + h.merge(title: data[:title]) + end end diff --git a/spec/models/champs/rnf_champ_spec.rb b/spec/models/champs/rnf_champ_spec.rb index fbca7072a..a55ffa2d6 100644 --- a/spec/models/champs/rnf_champ_spec.rb +++ b/spec/models/champs/rnf_champ_spec.rb @@ -102,18 +102,19 @@ describe Champs::RNFChamp, type: :model do describe 'update_with_external_data!' do it 'works' do value_json = { - :street_number => "16", - :street_name => "Rue du Général de Boissieu", - :street_address => "16 Rue du Général de Boissieu", - :postal_code => "75015", - :city_name => "Paris 15e Arrondissement", - :city_code => "75115", - :departement_code => "75", - :department_code => "75", - :departement_name => "Paris", - :department_name => "Paris", - :region_code => "11", - :region_name => "Île-de-France" + street_number: "16", + street_name: "Rue du Général de Boissieu", + street_address: "16 Rue du Général de Boissieu", + postal_code: "75015", + city_name: "Paris 15e Arrondissement", + city_code: "75115", + departement_code: "75", + department_code: "75", + departement_name: "Paris", + department_name: "Paris", + region_code: "11", + region_name: "Île-de-France", + title: "Fondation SFR" } expect(champ).to receive(:update!).with(data: anything, value_json:) champ.update_with_external_data!(data: subject.value!) diff --git a/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb b/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb index f74ed99a1..1d6b3f810 100644 --- a/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb +++ b/spec/tasks/maintenance/populate_rnf_json_value_task_spec.rb @@ -66,7 +66,8 @@ module Maintenance "departement_name" => "Paris", "department_name" => "Paris", "region_code" => "11", - "region_name" => "Île-de-France" + "region_name" => "Île-de-France", + "title" => "Fondation SFR" }) end end @@ -92,7 +93,8 @@ module Maintenance "departement_name" => "Paris", "department_name" => "Paris", "region_code" => "11", - "region_name" => "Île-de-France" + "region_name" => "Île-de-France", + "title" => "Fondation SFR" }) end end From cfd568b98dcae27f7b5e19bf81212ccd6615cbca Mon Sep 17 00:00:00 2001 From: mfo Date: Fri, 15 Nov 2024 09:33:42 +0100 Subject: [PATCH 2/3] feat(rna): also store rna title --- app/models/champs/rna_champ.rb | 11 +++++++++++ .../rna_champ_association_fetchable_concern.rb | 2 +- app/tasks/maintenance/populate_rna_json_value_task.rb | 2 +- spec/controllers/champs/rna_controller_spec.rb | 3 ++- .../maintenance/populate_rna_json_value_task_spec.rb | 3 ++- 5 files changed, 17 insertions(+), 4 deletions(-) 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 From 9a70a9526de3529f0049b0fd541a52e073f54799 Mon Sep 17 00:00:00 2001 From: mfo Date: Fri, 15 Nov 2024 09:42:44 +0100 Subject: [PATCH 3/3] feat(rna/rnf): expose rnf/rna title --- .../concerns/addressable_column_concern.rb | 6 ++---- app/models/types_de_champ/rna_type_de_champ.rb | 16 ++++++++++++++++ app/models/types_de_champ/rnf_type_de_champ.rb | 16 ++++++++++++++++ app/models/types_de_champ/siret_type_de_champ.rb | 4 ++++ spec/factories/champ.rb | 4 ++-- spec/models/columns/champ_column_spec.rb | 2 ++ 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/addressable_column_concern.rb b/app/models/concerns/addressable_column_concern.rb index 7fdf75a28..b3a6edf73 100644 --- a/app/models/concerns/addressable_column_concern.rb +++ b/app/models/concerns/addressable_column_concern.rb @@ -4,8 +4,8 @@ module AddressableColumnConcern extend ActiveSupport::Concern included do - def columns(procedure:, displayable: true, prefix: nil) - addressable_columns = [ + def addressable_columns(procedure:, displayable: true, prefix: nil) + [ ["code postal (5 chiffres)", '$.postal_code', :text, []], ["commune", '$.city_name', :text, []], ["département", '$.departement_code', :enum, APIGeoService.departement_options], @@ -22,8 +22,6 @@ module AddressableColumnConcern type: ) end - - super.concat(addressable_columns) end end end diff --git a/app/models/types_de_champ/rna_type_de_champ.rb b/app/models/types_de_champ/rna_type_de_champ.rb index e60c34a89..e6c2ca915 100644 --- a/app/models/types_de_champ/rna_type_de_champ.rb +++ b/app/models/types_de_champ/rna_type_de_champ.rb @@ -10,4 +10,20 @@ class TypesDeChamp::RNATypeDeChamp < TypesDeChamp::TypeDeChampBase def champ_value_for_export(champ, path = :value) champ.identifier end + + def columns(procedure:, displayable: true, prefix: nil) + super + .concat(addressable_columns(procedure:, displayable:, prefix:)) + .concat([ + Columns::JSONPathColumn.new( + procedure_id: procedure.id, + stable_id:, + tdc_type: type_champ, + label: "#{libelle_with_prefix(prefix)} – Titre au répertoire national des associations", + type: :text, + jsonpath: '$.title', + displayable: + ) + ]) + end end diff --git a/app/models/types_de_champ/rnf_type_de_champ.rb b/app/models/types_de_champ/rnf_type_de_champ.rb index aacad9bae..e92989af3 100644 --- a/app/models/types_de_champ/rnf_type_de_champ.rb +++ b/app/models/types_de_champ/rnf_type_de_champ.rb @@ -35,6 +35,22 @@ class TypesDeChamp::RNFTypeDeChamp < TypesDeChamp::TextTypeDeChamp def champ_blank?(champ) = champ.external_id.blank? + def columns(procedure:, displayable: true, prefix: nil) + super + .concat(addressable_columns(procedure:, displayable:, prefix:)) + .concat([ + Columns::JSONPathColumn.new( + procedure_id: procedure.id, + stable_id:, + tdc_type: type_champ, + label: "#{libelle_with_prefix(prefix)} – Titre au répertoire national des fondations ", + type: :text, + jsonpath: '$.title', + displayable: + ) + ]) + end + private def paths diff --git a/app/models/types_de_champ/siret_type_de_champ.rb b/app/models/types_de_champ/siret_type_de_champ.rb index a75a091d2..5786a071f 100644 --- a/app/models/types_de_champ/siret_type_de_champ.rb +++ b/app/models/types_de_champ/siret_type_de_champ.rb @@ -8,4 +8,8 @@ class TypesDeChamp::SiretTypeDeChamp < TypesDeChamp::TypeDeChampBase end def champ_blank_or_invalid?(champ) = Siret.new(siret: champ.value).invalid? + + def columns(procedure:, displayable: true, prefix: nil) + super.concat(addressable_columns(procedure:, displayable:, prefix:)) + end end diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 943fc5355..b84c93379 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -170,7 +170,7 @@ FactoryBot.define do factory :champ_do_not_use_rna, class: 'Champs::RNAChamp' do value { 'W173847273' } - value_json { AddressProxy::ADDRESS_PARTS.index_by(&:itself) } + value_json { AddressProxy::ADDRESS_PARTS.index_by(&:itself).merge(title: "LA PRÉVENTION ROUTIERE") } end factory :champ_do_not_use_engagement_juridique, class: 'Champs::EngagementJuridiqueChamp' do @@ -183,7 +183,7 @@ FactoryBot.define do factory :champ_do_not_use_rnf, class: 'Champs::RNFChamp' do value { '075-FDD-00003-01' } external_id { '075-FDD-00003-01' } - value_json { AddressProxy::ADDRESS_PARTS.index_by(&:itself) } + value_json { AddressProxy::ADDRESS_PARTS.index_by(&:itself).merge(title: "Fondation SFR") } end factory :champ_do_not_use_expression_reguliere, class: 'Champs::ExpressionReguliereChamp' do diff --git a/spec/models/columns/champ_column_spec.rb b/spec/models/columns/champ_column_spec.rb index ee5e0615c..a7dfa85bc 100644 --- a/spec/models/columns/champ_column_spec.rb +++ b/spec/models/columns/champ_column_spec.rb @@ -41,6 +41,8 @@ describe Columns::ChampColumn do expect_type_de_champ_values('mesri', eq([nil])) expect_type_de_champ_values('cojo', eq([nil])) expect_type_de_champ_values('expression_reguliere', eq([nil])) + expect_type_de_champ_values('rna', eq(["W173847273", "postal_code", "city_name", "departement_code", "region_name", "LA PRÉVENTION ROUTIERE"])) + expect_type_de_champ_values('rnf', eq(["075-FDD-00003-01", "postal_code", "city_name", "departement_code", "region_name", "Fondation SFR"])) end end