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 authored and maatinito committed Nov 27, 2023
1 parent 1850eef commit c1835f6
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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']

Expand Down
40 changes: 25 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,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)
Expand Down
8 changes: 8 additions & 0 deletions app/models/champs/code_postal_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::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
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::CodePostalDePolynesieTypeDeChamp < TypesDeChamp::TextTypeDeChamp
def libelle_for_export(index)
[libelle, "#{libelle} (Commune)", "#{libelle} (Ile)", "#{libelle} (Archipel)"][index]
end
end
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
110 changes: 110 additions & 0 deletions spec/models/dossier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 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,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'
Expand Down

0 comments on commit c1835f6

Please sign in to comment.