From 77d1aad411b6657ca2500bb1d8c512653be658d0 Mon Sep 17 00:00:00 2001 From: seb-by-ouidou Date: Thu, 9 Nov 2023 16:59:28 +0000 Subject: [PATCH] feat: add zipcode island archipelago to export + cache for polynesian_cities --- .github/workflows/ci.yml | 4 +- app/lib/api_geo/api.rb | 40 ++++--- .../champs/code_postal_de_polynesie_champ.rb | 8 ++ .../champs/commune_de_polynesie_champ.rb | 8 ++ .../code_postal_de_polynesie_type_de_champ.rb | 3 + .../commune_de_polynesie_type_de_champ.rb | 3 + spec/models/dossier_spec.rb | 110 ++++++++++++++++++ .../services/procedure_export_service_spec.rb | 4 + 8 files changed, 163 insertions(+), 17 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5d799d2cc0..7dfecdac44d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,9 +1,9 @@ name: Continuous Integration on: push: - branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*', 'hotfix/*'] + branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*', 'feature-ouidou/*', 'hotfix/*'] pull_request: - branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*'] + branches: ['main', 'devpf', 'masterpf', 'issue/*', 'feature/*', 'feature-ouidou/*'] merge_group: branches: ['main', 'devpf'] diff --git a/app/lib/api_geo/api.rb b/app/lib/api_geo/api.rb index ae6ece4ec80..e81486e6912 100644 --- a/app/lib/api_geo/api.rb +++ b/app/lib/api_geo/api.rb @@ -11,24 +11,26 @@ def self.nationalites end def self.polynesian_cities - result = [] - headers = [] - File.foreach('app/lib/api_geo/polynesian_postal_codes.txt', encoding: "windows-1252").with_index do |l, i| - fields = l.split("\t") - if i == 0 - headers = fields.map { |f| f.gsub(/\s+/, "_").downcase.to_sym } - else - entry = {} - [1, 6, 7].each do |f| - entry[headers[f]] = fields[f] + Rails.cache.fetch('api_polynesian_cities', expires_in: 1.week) do + result = [] + headers = [] + File.foreach('app/lib/api_geo/polynesian_postal_codes.txt', encoding: "windows-1252").with_index do |l, i| + fields = l.split("\t") + if i == 0 + headers = fields.map { |f| f.gsub(/\s+/, "_").downcase.to_sym } + else + entry = {} + [1, 6, 7].each do |f| + entry[headers[f]] = fields[f] + end + [2, 3].each do |f| + entry[headers[f]] = fields[f].to_i + end + result << entry end - [2, 3].each do |f| - entry[headers[f]] = fields[f].to_i - end - result << entry end + result end - result end def self.codes_postaux_de_polynesie @@ -45,6 +47,14 @@ def self.communes_de_polynesie big_cities + ['------------------------'] + small_cities end + def self.commune_by_city_postal_code(value) + polynesian_cities.find { city_postal_code(_1) == value } + end + + def self.commune_by_postal_code_city_label(value) + polynesian_cities.find { postal_code_city_label(_1) == value } + end + private def self.postal_code_city_label(e) diff --git a/app/models/champs/code_postal_de_polynesie_champ.rb b/app/models/champs/code_postal_de_polynesie_champ.rb index 780ef9f60be..9e3612c7aaf 100644 --- a/app/models/champs/code_postal_de_polynesie_champ.rb +++ b/app/models/champs/code_postal_de_polynesie_champ.rb @@ -21,6 +21,14 @@ # type_de_champ_id :integer # class Champs::CodePostalDePolynesieChamp < Champs::TextChamp + def for_export + if value.present? && (postal_code_city_label = APIGeo::API.commune_by_postal_code_city_label(value)) + [postal_code_city_label[:code_postal].to_s, postal_code_city_label[:commune], postal_code_city_label[:ile], postal_code_city_label[:archipel]] + else + ['', '', '', ''] + end + end + def self.options APIGeo::API.codes_postaux_de_polynesie end diff --git a/app/models/champs/commune_de_polynesie_champ.rb b/app/models/champs/commune_de_polynesie_champ.rb index be5ea471608..71084b9c7ad 100644 --- a/app/models/champs/commune_de_polynesie_champ.rb +++ b/app/models/champs/commune_de_polynesie_champ.rb @@ -21,6 +21,14 @@ # type_de_champ_id :integer # class Champs::CommuneDePolynesieChamp < Champs::TextChamp + def for_export + if value.present? && (city_postal_code = APIGeo::API.commune_by_city_postal_code(value)) + [city_postal_code[:commune], city_postal_code[:code_postal].to_s, city_postal_code[:ile], city_postal_code[:archipel]] + else + ['', '', '', ''] + end + end + def self.options APIGeo::API.communes_de_polynesie end diff --git a/app/models/types_de_champ/code_postal_de_polynesie_type_de_champ.rb b/app/models/types_de_champ/code_postal_de_polynesie_type_de_champ.rb index 5eff5d1000d..f2fe986fdaa 100644 --- a/app/models/types_de_champ/code_postal_de_polynesie_type_de_champ.rb +++ b/app/models/types_de_champ/code_postal_de_polynesie_type_de_champ.rb @@ -1,2 +1,5 @@ class TypesDeChamp::CodePostalDePolynesieTypeDeChamp < TypesDeChamp::TextTypeDeChamp + def libelle_for_export(index) + [libelle, "#{libelle} (Commune)", "#{libelle} (Ile)", "#{libelle} (Archipel)"][index] + end end diff --git a/app/models/types_de_champ/commune_de_polynesie_type_de_champ.rb b/app/models/types_de_champ/commune_de_polynesie_type_de_champ.rb index 27974f52dff..19ee6b32091 100644 --- a/app/models/types_de_champ/commune_de_polynesie_type_de_champ.rb +++ b/app/models/types_de_champ/commune_de_polynesie_type_de_champ.rb @@ -1,2 +1,5 @@ class TypesDeChamp::CommuneDePolynesieTypeDeChamp < TypesDeChamp::TextTypeDeChamp + def libelle_for_export(index) + [libelle, "#{libelle} (Code postal)", "#{libelle} (Ile)", "#{libelle} (Archipel)"][index] + end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 58dd6c5cc23..4d8e6a625a5 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1809,6 +1809,116 @@ it { is_expected.to eq(expected) } end end + + context 'with type_de_champ_commune_de_polynesie' do + let(:types_de_champ) { [{ type: :yes_no }, { type: :text }, { type: :commune_de_polynesie, libelle: 'commune' }] } + let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } + let(:dossier) { create(:dossier, procedure:) } + let(:yes_no_tdc) { procedure.active_revision.types_de_champ_public.first } + let(:text_tdc) { procedure.active_revision.types_de_champ_public.second } + let(:tdcs) { dossier.champs_public.map(&:type_de_champ) } + context 'with no value for commune_de_polynesie' do + let(:expected) do + [ + [yes_no_tdc.libelle, "Oui"], + [text_tdc.libelle, "text"], + ["commune", ''], + ["commune (Code postal)", ''], + ["commune (Ile)", ""], + ["commune (Archipel)", ""] + ] + end + + subject { Dossier.champs_for_export(dossier.champs_public, tdcs) } + + before do + yes_no, text, commune_de_polynesie = dossier.champs_public + yes_no.update(value: 'true') + text.update(value: 'text') + end + + it { is_expected.to eq(expected) } + end + + context 'with value for commune_de_polynesie' do + let(:expected) do + [ + [yes_no_tdc.libelle, "Oui"], + [text_tdc.libelle, "text"], + ["commune", 'Avera'], + ["commune (Code postal)", '98736'], + ["commune (Ile)", "Raiatea"], + ["commune (Archipel)", "Iles Sous Le Vent"] + ] + end + + subject { Dossier.champs_for_export(dossier.champs_public, tdcs) } + + before do + yes_no, text, commune_de_polynesie = dossier.champs_public + yes_no.update(value: 'true') + commune_de_polynesie.update(value: 'Avera - Raiatea - 98736') + text.update(value: 'text') + end + + it { is_expected.to eq(expected) } + end + end + + context 'with type_de_champ_code_postal_de_polynesie' do + let(:types_de_champ) { [{ type: :yes_no }, { type: :text }, { type: :code_postal_de_polynesie, libelle: 'code postal' }] } + let(:procedure) { create(:procedure, types_de_champ_public: types_de_champ) } + let(:dossier) { create(:dossier, procedure:) } + let(:yes_no_tdc) { procedure.active_revision.types_de_champ_public.first } + let(:text_tdc) { procedure.active_revision.types_de_champ_public.second } + let(:tdcs) { dossier.champs_public.map(&:type_de_champ) } + context 'with no value for code_postal_de_polynesie' do + let(:expected) do + [ + [yes_no_tdc.libelle, "Oui"], + [text_tdc.libelle, "text"], + ["code postal", ''], + ["code postal (Commune)", ''], + ["code postal (Ile)", ""], + ["code postal (Archipel)", ""] + ] + end + + subject { Dossier.champs_for_export(dossier.champs_public, tdcs) } + + before do + yes_no, text, code_postal_de_polynesie = dossier.champs_public + yes_no.update(value: 'true') + text.update(value: 'text') + end + + it { is_expected.to eq(expected) } + end + + context 'with value for code_postal_de_polynesie' do + let(:expected) do + [ + [yes_no_tdc.libelle, "Oui"], + [text_tdc.libelle, "text"], + ["code postal", '98736'], + ["code postal (Commune)", 'Avera'], + ["code postal (Ile)", "Raiatea"], + ["code postal (Archipel)", "Iles Sous Le Vent"] + ] + end + + subject { Dossier.champs_for_export(dossier.champs_public, tdcs) } + + before do + yes_no, text, code_postal_de_polynesie = dossier.champs_public + yes_no.update(value: 'true') + code_postal_de_polynesie.update(value: '98736 - Avera - Raiatea') + text.update(value: 'text') + end + + it { is_expected.to eq(expected) } + end + end end describe "remove_titres_identite!" do diff --git a/spec/services/procedure_export_service_spec.rb b/spec/services/procedure_export_service_spec.rb index f436d97b877..c90b569ad80 100644 --- a/spec/services/procedure_export_service_spec.rb +++ b/spec/services/procedure_export_service_spec.rb @@ -12,6 +12,10 @@ 'simple_drop_down_list' when 'communes' ['communes', "communes (Code insee)", "communes (Département)"] + when 'commune_de_polynesie' + ["commune_de_polynesie", "commune_de_polynesie (Archipel)", "commune_de_polynesie (Code postal)", "commune_de_polynesie (Ile)"] + when 'code_postal_de_polynesie' + ["code_postal_de_polynesie", "code_postal_de_polynesie (Archipel)", "code_postal_de_polynesie (Commune)", "code_postal_de_polynesie (Ile)"] when 'departements', 'regions', 'pays' [tdc, "#{tdc} (Code)"] when 'epci'