From fdff699d513c31051bd454dcb0c6ef14f95e2658 Mon Sep 17 00:00:00 2001 From: mfo Date: Wed, 21 Aug 2024 11:01:40 +0200 Subject: [PATCH 1/3] feat(champ.rnf/rna/siret): render normalized address the same way --- app/models/address_proxy.rb | 55 +++++++++++++++++++ app/services/api_geo_service.rb | 4 ++ app/views/shared/champs/rna/_show.html.haml | 11 +--- app/views/shared/champs/rnf/_show.html.haml | 10 +--- .../dossiers/_identite_entreprise.html.haml | 7 +-- .../dossiers/_normalized_address.html.haml | 23 ++++++++ .../models/champs/normalized_address/en.yml | 9 +++ .../models/champs/normalized_address/fr.yml | 9 +++ config/locales/models/champs/rna_champ/en.yml | 3 - config/locales/models/champs/rna_champ/fr.yml | 3 - config/locales/models/champs/rnf_champ/en.yml | 4 -- config/locales/models/champs/rnf_champ/fr.yml | 4 -- 12 files changed, 103 insertions(+), 39 deletions(-) create mode 100644 app/models/address_proxy.rb create mode 100644 app/views/shared/dossiers/_normalized_address.html.haml create mode 100644 config/locales/models/champs/normalized_address/en.yml create mode 100644 config/locales/models/champs/normalized_address/fr.yml diff --git a/app/models/address_proxy.rb b/app/models/address_proxy.rb new file mode 100644 index 000000000..13eebd2e6 --- /dev/null +++ b/app/models/address_proxy.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +class AddressProxy + ADDRESS_PARTS = [ + :street_address, + :city_name, + :postal_code, + :city_code, + :departement_name, + :departement_code, + :region_name, + :region_code + ] + + class ChampAddressPresenter + ADDRESS_PARTS.each do |address_part| + define_method(address_part) do + @data[address_part] + end + end + + def initialize(champ) + @data = champ.value_json.with_indifferent_access + end + end + + class EtablissementAddressPresenter + attr_reader(*ADDRESS_PARTS) + + def initialize(etablissement) + @street_address = [etablissement.numero_voie, etablissement.type_voie, etablissement.nom_voie].compact.join(" ") + @city_name = etablissement.localite + @postal_code = etablissement.code_postal + @city_code = etablissement.code_insee_localite + @departement_name = APIGeoService.departement_name_by_postal_code(@postal_code) + @departement_code = APIGeoService.departement_code(@departement_name) + @region_code = APIGeoService.region_code_by_departement(@departement_code) + @region_name = APIGeoService.region_name(@region_code) + end + end + + delegate(*ADDRESS_PARTS, to: :@presenter) + + def initialize(champs_or_etablissement) + @presenter = make(champ_or_etablissement) + end + + def make(champ_or_etablissement) + case champ_or_etablissement + when Champ then ChampAddressPresenter.new(champ_or_etablissement) + when Etablissement then EtablissementAddressPresenter.new(champ_or_etablissement) + else raise NotImplementedError("Unsupported address from #{champ_or_etablissement.class.name}") + end + end +end diff --git a/app/services/api_geo_service.rb b/app/services/api_geo_service.rb index 167bae36b..ee96af7b4 100644 --- a/app/services/api_geo_service.rb +++ b/app/services/api_geo_service.rb @@ -47,6 +47,10 @@ class APIGeoService departements.find { _1[:code] == code }&.dig(:name) end + def departement_name_by_postal_code(postal_code) + APIGeoService.departement_name(postal_code[0..2]) || APIGeoService.departement_name(postal_code[0..1]) + end + def departement_code(name) return if name.nil? departements.find { _1[:name] == name }&.dig(:code) diff --git a/app/views/shared/champs/rna/_show.html.haml b/app/views/shared/champs/rna/_show.html.haml index ce989b8e7..588ff8206 100644 --- a/app/views/shared/champs/rna/_show.html.haml +++ b/app/views/shared/champs/rna/_show.html.haml @@ -22,13 +22,4 @@ - c.with_value do %p= l(champ.data[scope].to_date) - - if champ.data['address'].present? - = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.rna_champ.data.address")) do |c| - - c.with_value do - %p= champ.full_address - - - ['code_insee', 'code_postal'].each do |scope| - - if champ.data['address'][scope].present? - = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.rna_champ.data.#{scope}")) do |c| - - c.with_value do - %p= champ.data['address'][scope] + = render partial: "shared/dossiers/normalized_address", locals: { address: AddressProxy.new(champ) } diff --git a/app/views/shared/champs/rnf/_show.html.haml b/app/views/shared/champs/rnf/_show.html.haml index da251eff6..32f8e14e2 100644 --- a/app/views/shared/champs/rnf/_show.html.haml +++ b/app/views/shared/champs/rnf/_show.html.haml @@ -19,12 +19,4 @@ = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.rnf_champ.data.#{scope}")) do |c| - c.with_value do %p= l(champ.data[scope].to_date) - - - if champ.data['address'].present? - - ['label', 'cityCode', 'postalCode'].each do |scope| - = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.rnf_champ.data.#{scope}")) do |c| - - c.with_value do - %p= champ.data['address'][scope] - = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.rnf_champ.data.department")) do |c| - - c.with_value do - %p= "#{champ.data['address']['departmentCode']} – #{champ.data['address']['departmentName']}" + = render partial: "shared/dossiers/normalized_address", locals: { address: AddressProxy.new(champ) } diff --git a/app/views/shared/dossiers/_identite_entreprise.html.haml b/app/views/shared/dossiers/_identite_entreprise.html.haml index a7b88fc7f..c918e2f51 100644 --- a/app/views/shared/dossiers/_identite_entreprise.html.haml +++ b/app/views/shared/dossiers/_identite_entreprise.html.haml @@ -73,12 +73,7 @@ - c.with_value do %p= etablissement.entreprise.numero_tva_intracommunautaire - = render Dossiers::RowShowComponent.new(label: "Adresse") do |c| - - c.with_value do - %p - - etablissement.adresse.split("\n").compact_blank.each do |line| - = line - %br + = render partial: "shared/dossiers/normalized_address", locals: { address: AddressProxy.new(etablissement)} = render Dossiers::RowShowComponent.new(label: "Capital social") do |c| - c.with_value do diff --git a/app/views/shared/dossiers/_normalized_address.html.haml b/app/views/shared/dossiers/_normalized_address.html.haml new file mode 100644 index 000000000..698a01cfa --- /dev/null +++ b/app/views/shared/dossiers/_normalized_address.html.haml @@ -0,0 +1,23 @@ += render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.normalized_address.full_address")) do |c| + - c.with_value do + %p + = address.street_address + %br + = [address.city_name, address.postal_code].join(" ") + + +- ['city_code', 'postal_code'].each do |scope| + - if address.public_send(scope).present? + = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.normalized_address.#{scope}")) do |c| + - c.with_value do + %p= address.public_send(scope) + +- if address.departement_name.present? + = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.normalized_address.department")) do |c| + - c.with_value do + %p= "#{address.departement_name} – #{address.departement_code}" + +- if address.region_name.present? + = render Dossiers::RowShowComponent.new(label: t("activemodel.attributes.normalized_address.region")) do |c| + - c.with_value do + %p= "#{address.region_name} – #{address.region_code}" diff --git a/config/locales/models/champs/normalized_address/en.yml b/config/locales/models/champs/normalized_address/en.yml new file mode 100644 index 000000000..3c1b50706 --- /dev/null +++ b/config/locales/models/champs/normalized_address/en.yml @@ -0,0 +1,9 @@ +en: + activemodel: + attributes: + normalized_address: + full_address: Address + city_code: INSEE code + postal_code: Postal code + department: Department + region: Region & region code diff --git a/config/locales/models/champs/normalized_address/fr.yml b/config/locales/models/champs/normalized_address/fr.yml new file mode 100644 index 000000000..3290e2ced --- /dev/null +++ b/config/locales/models/champs/normalized_address/fr.yml @@ -0,0 +1,9 @@ +fr: + activemodel: + attributes: + normalized_address: + full_address: Adresse + city_code: Code INSEE + postal_code: Code postal + department: Département + region: Région & code région diff --git a/config/locales/models/champs/rna_champ/en.yml b/config/locales/models/champs/rna_champ/en.yml index aa6ee7bb5..d40042092 100644 --- a/config/locales/models/champs/rna_champ/en.yml +++ b/config/locales/models/champs/rna_champ/en.yml @@ -9,9 +9,6 @@ en: association_date_creation: Creation date association_date_declaration: Declaration date association_date_publication: Publication date - address: Address - code_insee: INSEE code - code_postal: Postal code paste: Copy the RNA to the clipboard paste_success: The RNA has been copied to the clipboard activerecord: diff --git a/config/locales/models/champs/rna_champ/fr.yml b/config/locales/models/champs/rna_champ/fr.yml index edb8736a5..42d3ecfc2 100644 --- a/config/locales/models/champs/rna_champ/fr.yml +++ b/config/locales/models/champs/rna_champ/fr.yml @@ -9,9 +9,6 @@ fr: association_date_creation: Date de création association_date_declaration: Date de déclaration association_date_publication: Date de publication - address: Adresse - code_insee: Code INSEE - code_postal: Code postal paste: Copier le RNA dans le presse-papier paste_success: Le RNA a été copié dans le presse-papier activerecord: diff --git a/config/locales/models/champs/rnf_champ/en.yml b/config/locales/models/champs/rnf_champ/en.yml index 977d9c4ab..8050acc4b 100644 --- a/config/locales/models/champs/rnf_champ/en.yml +++ b/config/locales/models/champs/rnf_champ/en.yml @@ -12,10 +12,6 @@ en: dissolvedAt: Dissolved at address: Address status: Status - cityCode: City code - postalCode: Postal code - department: Department - label: Address paste: Copy the RNF to the clipboard paste_success: The RNF has been copied to the clipboard activerecord: diff --git a/config/locales/models/champs/rnf_champ/fr.yml b/config/locales/models/champs/rnf_champ/fr.yml index 8e4ae7812..a847d6e52 100644 --- a/config/locales/models/champs/rnf_champ/fr.yml +++ b/config/locales/models/champs/rnf_champ/fr.yml @@ -12,10 +12,6 @@ fr: dissolvedAt: Dissoute le address: Adresse status: Statut - cityCode: Code INSEE - postalCode: Code postal - department: Département - label: Adresse paste: Copier le RNF dans le presse-papier paste_success: Le RNF a été copié dans le presse-papier activerecord: From 7182a4e8d0811712d20b794c5f7769f24a431f21 Mon Sep 17 00:00:00 2001 From: mfo Date: Thu, 22 Aug 2024 09:16:46 +0200 Subject: [PATCH 2/3] feat(identite_entreprise): prefer champ.value_json in order to ensure good department --- app/views/shared/champs/siret/_show.html.haml | 2 +- app/views/shared/dossiers/_identite_entreprise.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/champs/siret/_show.html.haml b/app/views/shared/champs/siret/_show.html.haml index 7f819c6f8..44e31d87f 100644 --- a/app/views/shared/champs/siret/_show.html.haml +++ b/app/views/shared/champs/siret/_show.html.haml @@ -1,2 +1,2 @@ - if champ.etablissement.present? - = render partial: "shared/dossiers/identite_entreprise", locals: { etablissement: champ.etablissement, profile: profile } + = render partial: "shared/dossiers/identite_entreprise", locals: { etablissement: champ.etablissement, profile: profile, champ: } diff --git a/app/views/shared/dossiers/_identite_entreprise.html.haml b/app/views/shared/dossiers/_identite_entreprise.html.haml index c918e2f51..7d2bd5dbc 100644 --- a/app/views/shared/dossiers/_identite_entreprise.html.haml +++ b/app/views/shared/dossiers/_identite_entreprise.html.haml @@ -73,7 +73,7 @@ - c.with_value do %p= etablissement.entreprise.numero_tva_intracommunautaire - = render partial: "shared/dossiers/normalized_address", locals: { address: AddressProxy.new(etablissement)} + = render partial: "shared/dossiers/normalized_address", locals: { address: AddressProxy.new(defined?(champ) ? champ : etablissement)} = render Dossiers::RowShowComponent.new(label: "Capital social") do |c| - c.with_value do From 70983c50f973eeff0f90fccbe3631392d96f5193 Mon Sep 17 00:00:00 2001 From: mfo Date: Thu, 22 Aug 2024 10:18:50 +0200 Subject: [PATCH 3/3] spec(normalized_address): add simple spec --- app/models/address_proxy.rb | 2 +- spec/factories/champ.rb | 1 + .../populate_siret_value_json_task_spec.rb | 2 +- .../_normalized_address.html.haml_spec.rb | 31 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 spec/views/shared/dossiers/_normalized_address.html.haml_spec.rb diff --git a/app/models/address_proxy.rb b/app/models/address_proxy.rb index 13eebd2e6..0a2fb3392 100644 --- a/app/models/address_proxy.rb +++ b/app/models/address_proxy.rb @@ -41,7 +41,7 @@ class AddressProxy delegate(*ADDRESS_PARTS, to: :@presenter) - def initialize(champs_or_etablissement) + def initialize(champ_or_etablissement) @presenter = make(champ_or_etablissement) end diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 6bb952dfb..f88e13a67 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -163,6 +163,7 @@ FactoryBot.define do factory :champ_do_not_use_siret, class: 'Champs::SiretChamp' do association :etablissement, factory: [:etablissement] value { '44011762001530' } + value_json { AddressProxy::ADDRESS_PARTS.index_by(&:itself) } end factory :champ_do_not_use_rna, class: 'Champs::RNAChamp' do diff --git a/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb b/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb index 4fca8f9ce..76759ed40 100644 --- a/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb +++ b/spec/tasks/maintenance/populate_siret_value_json_task_spec.rb @@ -12,7 +12,7 @@ module Maintenance it 'updates value_json' do expect { subject }.to change { element.reload.value_json } - .from(nil) + .from(anything) .to({ "city_code" => "92009", "city_name" => "Bois-Colombes", diff --git a/spec/views/shared/dossiers/_normalized_address.html.haml_spec.rb b/spec/views/shared/dossiers/_normalized_address.html.haml_spec.rb new file mode 100644 index 000000000..1ce482ca4 --- /dev/null +++ b/spec/views/shared/dossiers/_normalized_address.html.haml_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +describe 'shared/dossiers/normalized_address', type: :view do + before { render 'shared/dossiers/normalized_address', address: } + + context 'given an champ' do + let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :siret }]) } + let(:dossier) { create(:dossier, :with_populated_champs, procedure:) } + let(:address) { AddressProxy.new(dossier.champs.first) } + + it 'render address' do + AddressProxy::ADDRESS_PARTS.each do |address_part| + expect(rendered).to have_text(address_part) + end + end + end + + context 'given an etablissement' do + let(:etablissement) { create(:etablissement) } + let(:address) { AddressProxy.new(etablissement) } + + it 'render address' do + expect(rendered).to have_text("6 RUE RAOUL NORDLING") + expect(rendered).to have_text("BOIS COLOMBES 92270") + expect(rendered).to have_text("92009") + expect(rendered).to have_text("92270") + expect(rendered).to have_text("Hauts-de-Seine – 92") + expect(rendered).to have_text("Île-de-France – 11") + end + end +end