Skip to content

Commit

Permalink
feat: add zipcode island archipelago to export + cache for polynesian…
Browse files Browse the repository at this point in the history
…_cities
  • Loading branch information
seb-by-ouidou committed Nov 9, 2023
1 parent cc04903 commit e0011ba
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 15 deletions.
36 changes: 21 additions & 15 deletions app/lib/api_geo/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions app/models/champs/commune_de_polynesie_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions spec/models/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,61 @@
it { is_expected.to eq(expected) }
end
end

context 'with type_de_champ_communes_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
end

describe "remove_titres_identite!" do
Expand Down
2 changes: 2 additions & 0 deletions spec/services/procedure_export_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
'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 'departements', 'regions', 'pays'
[tdc, "#{tdc} (Code)"]
when 'epci'
Expand Down

0 comments on commit e0011ba

Please sign in to comment.