From 456de1946ac456e9c421f46f280ba7bdde19f8ea 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 --- app/lib/api_geo/api.rb | 36 +++++++----- .../champs/commune_de_polynesie_champ.rb | 8 +++ .../commune_de_polynesie_type_de_champ.rb | 3 + spec/models/dossier_spec.rb | 55 +++++++++++++++++++ 4 files changed, 87 insertions(+), 15 deletions(-) diff --git a/app/lib/api_geo/api.rb b/app/lib/api_geo/api.rb index ae6ece4ec80..7eaf3ad86cd 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,10 @@ 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 + private def self.postal_code_city_label(e) 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/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..224bf489c85 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1809,6 +1809,61 @@ it { is_expected.to eq(expected) } end end + + context 'with type_de_champ_communes_de_polynesieyyyy' 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 end describe "remove_titres_identite!" do