Allow prefill pays type de champs (#8344)

* Allow prefill pays type de champs

* Avoid conditional prefil params for pays champ

* Clean pays data with batch update

* Fix bug and add test batch update pays value

* Improve performance batch_update_pays

* Fix associated country code problem

* Fix after party task name

* Format country name if needed in batch update
This commit is contained in:
Damien Le Thiec 2023-01-18 12:52:38 +01:00 committed by GitHub
parent eea2912ae5
commit 3a8a50a216
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 203 additions and 34 deletions

View file

@ -0,0 +1,92 @@
class Migrations::BatchUpdatePaysValuesJob < ApplicationJob
UNUSUAL_COUNTRY_NAME_MATCHER = {
"ACORES, MADERE" => "Portugal",
"ALASKA" => "États-Unis",
"ANTILLES NEERLANDAISES" => "Pays-Bas",
"BIELORUSSIE" => "Bélarus",
"BONAIRE, SAINT EUSTACHE ET SABA" => "Bonaire, Saint-Eustache et Saba",
"BURKINA" => "Burkina Faso",
"CAIMANES (ILES)" => "îles Caïmans",
"CAMEROUN ET TOGO" => "Cameroun",
"CANARIES (ILES)" => "Espagne",
"CENTRAFRICAINE (REPUBLIQUE)" => "République centrafricaine",
"CHRISTMAS (ILE)" => "Christmas, Île",
"CONGO (REPUBLIQUE DEMOCRATIQUE)" => "République démocratique du Congo",
"COOK (ILES)" => "îles Cook",
"COREE (REPUBLIQUE DE)" => "Corée, République de",
"COREE (REPUBLIQUE POPULAIRE DEMOCRATIQUE DE)" => "Corée, République populaire démocratique de",
"COREE" => "Corée, République de",
"DOMINICAINE (REPUBLIQUE)" => "République dominicaine",
"ETATS MALAIS NON FEDERES" => "Malaisie",
"FEROE (ILES)" => "îles Féroé",
"GUYANE" => "France",
"HAWAII (ILES)" => "États-Unis",
"HEARD ET MACDONALD (ILES)" => "îles Heard-et-MacDonald",
"ILES PORTUGAISES DE L'OCEAN INDIEN" => "Portugal",
"IRLANDE, ou EIRE" => "Irlande",
"KAMTCHATKA" => "Russie, Fédération de",
"LA REUNION" => "Réunion, Île de la",
"LABRADOR" => "Canada",
"MACEDOINE DU NORD (REPUBLIQUE DE)" => "Macédoine du Nord",
"MALOUINES, OU FALKLAND (ILES)" => "Malouines, Îles (Falkland)",
"MAN (ILE)" => "Île de Man",
"MARIANNES DU NORD (ILES)" => "Îles Mariannes du Nord",
"MARSHALL (ILES)" => "Îles Marshall",
"OCEAN INDIEN (TERRITOIRE BRITANNIQUE DE L')" => "Royaume-Uni",
"PALESTINE (Etat de)" => "Palestine, État de",
"POSSESSIONS BRITANNIQUES AU PROCHE-ORIENT" => "Royaume-Uni",
"PROVINCES ESPAGNOLES D'AFRIQUE" => "Espagne",
"REPUBLIQUE DEMOCRATIQUE ALLEMANDE" => "Allemagne",
"REPUBLIQUE FEDERALE D'ALLEMAGNE" => "Allemagne",
"RUSSIE" => "Russie, Fédération de",
"SAINT-MARTIN" => "Saint-Martin (partie française)",
"SAINT-VINCENT-ET-LES GRENADINES" => "Saint-Vincent-et-les-Grenadines",
"SAINTE HELENE, ASCENSION ET TRISTAN DA CUNHA" => "Sainte-Hélène, Ascension et Tristan da Cunha",
"SALOMON (ILES)" => "Salomon, Îles",
"SAMOA OCCIDENTALES" => "Samoa",
"SIBERIE" => "Russie, Fédération de",
"SOUDAN ANGLO-EGYPTIEN, KENYA, OUGANDA" => "Ouganda",
"SYRIE" => "Syrienne, République arabe",
"TANGER" => "Maroc",
"TCHECOSLOVAQUIE" => "Tchéquie",
"TCHEQUE (REPUBLIQUE)" => "Tchéquie",
"TERR. DES ETATS-UNIS D'AMERIQUE EN AMERIQUE" => "États-Unis",
"TERR. DES ETATS-UNIS D'AMERIQUE EN OCEANIE" => "États-Unis",
"TERR. DU ROYAUME-UNI DANS L'ATLANTIQUE SUD" => "Royaume-Uni",
"TERRE-NEUVE" => "Canada",
"TERRITOIRES DU ROYAUME-UNI AUX ANTILLES" => "Royaume-Uni",
"TURKESTAN RUSSE" => "Russie, Fédération de",
"TURKS ET CAIQUES (ILES)" => "îles Turques-et-Caïques",
"TURQUIE D'EUROPE" => "Turquie",
"VATICAN, ou SAINT-SIEGE" => "Saint-Siège (état de la cité du Vatican)",
"VIERGES BRITANNIQUES (ILES)" => "Îles Vierges britanniques",
"VIERGES DES ETATS-UNIS (ILES)" => "Îles Vierges des États-Unis",
"VIET NAM DU NORD" => "Viêt Nam",
"VIET NAM DU SUD" => "Viêt Nam",
"WALLIS-ET-FUTUNA" => "Wallis et Futuna",
"YEMEN (REPUBLIQUE ARABE DU)" => "Yémen",
"YEMEN DEMOCRATIQUE" => "Yémen",
"ZANZIBAR" => "Tanzanie"
}
private_constant :UNUSUAL_COUNTRY_NAME_MATCHER
def perform(ids)
ids.each do |id|
pays_champ = Champs::PaysChamp.find(id)
next if pays_champ.valid?
code = APIGeoService.country_code(pays_champ.value)
value = if code.present?
APIGeoService.country_name(code)
else
UNUSUAL_COUNTRY_NAME_MATCHER[pays_champ.value]
end
if value.present? || !pays_champ.required?
associated_country_code = APIGeoService.country_code(value)
pays_champ.update_columns(value: value, external_id: associated_country_code)
end
end
end
end

View file

@ -21,6 +21,9 @@
# type_de_champ_id :integer
#
class Champs::PaysChamp < Champs::TextChamp
validates :value, inclusion: APIGeoService.countries.pluck(:name), allow_nil: true, allow_blank: false
validates :external_id, inclusion: APIGeoService.countries.pluck(:code), allow_nil: true, allow_blank: false
def for_export
[name, code]
end

View file

@ -38,7 +38,8 @@ class PrefillParams
TypeDeChamp.type_champs.fetch(:datetime),
TypeDeChamp.type_champs.fetch(:civilite),
TypeDeChamp.type_champs.fetch(:yes_no),
TypeDeChamp.type_champs.fetch(:checkbox)
TypeDeChamp.type_champs.fetch(:checkbox),
TypeDeChamp.type_champs.fetch(:pays)
]
attr_reader :champ, :value

View file

@ -261,6 +261,7 @@ class TypeDeChamp < ApplicationRecord
TypeDeChamp.type_champs.fetch(:phone),
TypeDeChamp.type_champs.fetch(:iban),
TypeDeChamp.type_champs.fetch(:civilite),
TypeDeChamp.type_champs.fetch(:pays),
TypeDeChamp.type_champs.fetch(:date),
TypeDeChamp.type_champs.fetch(:datetime),
TypeDeChamp.type_champs.fetch(:yes_no),

View file

@ -3,7 +3,7 @@ class TypesDeChamp::PrefillDropDownListTypeDeChamp < TypesDeChamp::PrefillTypeDe
if drop_down_other?
drop_down_list_enabled_non_empty_options.insert(
0,
I18n.t("views.prefill_descriptions.edit.possible_values.drop_down_list_other")
I18n.t("views.prefill_descriptions.edit.possible_values.drop_down_list_other_html")
)
else
drop_down_list_enabled_non_empty_options

View file

@ -17,7 +17,7 @@ class TypesDeChamp::PrefillTypeDeChamp < SimpleDelegator
def possible_values
return [] unless prefillable?
[I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}")]
[I18n.t("views.prefill_descriptions.edit.possible_values.#{type_champ}_html")]
end
def example_value

View file

@ -20,7 +20,8 @@
%button.fr-btn.fr-btn--secondary{ disabled: true }
= t("views.prefill_descriptions.edit.champ_unavailable")
= type_de_champ.description
%p
= type_de_champ.description
%table.table.vertical
%tbody

View file

@ -108,6 +108,7 @@ ignore_unused:
- 'views.pagination.*'
- 'time.formats.default'
- 'instructeurs.dossiers.filterable_state.*'
- 'views.prefill_descriptions.edit.possible_values.*'
# - '{devise,kaminari,will_paginate}.*'
# - 'simple_form.{yes,no}'
# - 'simple_form.{placeholders,hints,labels}.*'

View file

@ -113,18 +113,20 @@ en:
champ_unavailable: Unavailable
possible_values:
title: Values
text: A short text
textarea: A long text
decimal_number: A decimal number
integer_number: An integer number
email: An email address
phone: A phone number
iban: An Iban number
yes_no: '"true" for Yes, "false" pour No'
date: ISO8601 date
datetime: ISO8601 datetime
checkbox: '"true" to check, "false" to uncheck'
drop_down_list_other: Any value
text_html: A short text
textarea_html: A long text
decimal_number_html: A decimal number
integer_number_html: An integer number
email_html: An email address
civilite_html: '"M." for Mister, "Mme" for Madam'
phone_html: A phone number
iban_html: An Iban number
yes_no_html: '"true" for Yes, "false" pour No'
checkbox_html: '"true" to check, "false" to uncheck'
pays_html: An <a href="https://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">ISO 3166-2 country code</a>
date_html: ISO8601 date
datetime_html: ISO8601 datetime
drop_down_list_other_html: Any value
examples:
title: Example
text: Short text
@ -135,6 +137,7 @@ en:
phone: 0612345678
iban: FR7611315000011234567890138
yes_no: "true"
pays: "FR"
date: "2023-02-01"
datetime: "2023-02-01T10:30"
checkbox: "true"

View file

@ -104,19 +104,20 @@ fr:
champ_unavailable: Indisponible
possible_values:
title: Valeurs
text: Un texte court
textarea: Un texte long
decimal_number: Un nombre décimal
integer_number: Un nombre entier
email: Une adresse email
civilite: '"M." pour Monsieur, "Mme" pour Madame'
phone: Un numéro de téléphone
iban: Un numéro Iban
yes_no: '"true" pour Oui, "false" pour Non'
date: Date au format ISO8601
datetime: Datetime au format ISO8601
checkbox: '"true" pour coché, "false" pour décoché'
drop_down_list_other: Toute valeur
text_html: Un texte court
textarea_html: Un texte long
decimal_number_html: Un nombre décimal
integer_number_html: Un nombre entier
email_html: Une adresse email
civilite_html: '"M." pour Monsieur, "Mme" pour Madame'
phone_html: Un numéro de téléphone
iban_html: Un numéro Iban
yes_no_html: '"true" pour Oui, "false" pour Non'
checkbox_html: '"true" pour coché, "false" pour décoché'
pays_html: Un <a href="https://en.wikipedia.org/wiki/ISO_3166-2" target="_blank">code pays ISO 3166-2</a>
datetime_html: Datetime au format ISO8601
date_html: Date au format ISO8601
drop_down_list_other_html: Toute valeur
examples:
title: Exemple
text: Texte court
@ -128,6 +129,7 @@ fr:
iban: FR7611315000011234567890138
yes_no: "true"
civilite: "M."
pays: "FR"
date: "2023-02-01"
datetime: "2023-02-01T10:30"
checkbox: "true"

View file

@ -1,4 +1,4 @@
fr:
en:
activerecord:
models:
type_de_champ: 'Field type'

View file

@ -0,0 +1,16 @@
namespace :after_party do
desc 'Deployment task: normalize_pays_values'
task normalize_pays_values: :environment do
puts "Running deploy task 'normalize_pays_values'"
# Put your task implementation HERE.
Champs::PaysChamp.in_batches do |pays_champs|
Migrations::BatchUpdatePaysValuesJob.perform_later(pays_champs.pluck(:id))
end
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end

View file

@ -0,0 +1,47 @@
describe Migrations::BatchUpdatePaysValuesJob, type: :job do
before do
pays_champ.save(validate: false)
end
subject { described_class.perform_now([pays_champ.id]) }
context "the value is correct" do
let(:pays_champ) { build(:champ_pays, value: 'France', external_id: 'FR') }
it 'does not change it' do
subject
expect(pays_champ.reload.value).to eq('France')
expect(pays_champ.reload.external_id).to eq('FR')
end
end
context "the value is incorrect" do
let(:pays_champ) { build(:champ_pays, value: 'Incorrect') }
it 'updates value to nil' do
subject
expect(pays_champ.reload.value).to be_nil
expect(pays_champ.reload.external_id).to be_nil
end
end
context "the value is easily cleanable" do
let(:pays_champ) { build(:champ_pays, value: 'Vietnam') }
it 'cleans the value' do
subject
expect(pays_champ.reload.value).to eq('Viêt Nam')
expect(pays_champ.reload.external_id).to eq('VN')
end
end
context "the value is hard to clean" do
let(:pays_champ) { build(:champ_pays, value: 'CHRISTMAS (ILE)') }
it 'cleans the value' do
subject
expect(pays_champ.reload.value).to eq('Christmas, Île')
expect(pays_champ.reload.external_id).to eq('CX')
end
end
end

View file

@ -110,6 +110,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ public value that is authorized", :phone, "value"
it_behaves_like "a champ public value that is authorized", :iban, "value"
it_behaves_like "a champ public value that is authorized", :civilite, "M."
it_behaves_like "a champ public value that is authorized", :pays, "FR"
it_behaves_like "a champ public value that is authorized", :date, "2022-12-22"
it_behaves_like "a champ public value that is authorized", :datetime, "2022-12-22T10:30"
it_behaves_like "a champ public value that is authorized", :yes_no, "true"
@ -126,6 +127,7 @@ RSpec.describe PrefillParams do
it_behaves_like "a champ private value that is authorized", :phone, "value"
it_behaves_like "a champ private value that is authorized", :iban, "value"
it_behaves_like "a champ private value that is authorized", :civilite, "M."
it_behaves_like "a champ private value that is authorized", :pays, "FR"
it_behaves_like "a champ private value that is authorized", :date, "2022-12-22"
it_behaves_like "a champ private value that is authorized", :datetime, "2022-12-22T10:30"
it_behaves_like "a champ private value that is authorized", :yes_no, "true"

View file

@ -245,6 +245,7 @@ describe TypeDeChamp do
it_behaves_like "a prefillable type de champ", :type_de_champ_date
it_behaves_like "a prefillable type de champ", :type_de_champ_datetime
it_behaves_like "a prefillable type de champ", :type_de_champ_civilite
it_behaves_like "a prefillable type de champ", :type_de_champ_pays
it_behaves_like "a prefillable type de champ", :type_de_champ_yes_no
it_behaves_like "a prefillable type de champ", :type_de_champ_checkbox
it_behaves_like "a prefillable type de champ", :type_de_champ_drop_down_list
@ -265,7 +266,6 @@ describe TypeDeChamp do
it_behaves_like "a non-prefillable type de champ", :type_de_champ_mesri
it_behaves_like "a non-prefillable type de champ", :type_de_champ_carte
it_behaves_like "a non-prefillable type de champ", :type_de_champ_address
it_behaves_like "a non-prefillable type de champ", :type_de_champ_pays
it_behaves_like "a non-prefillable type de champ", :type_de_champ_regions
it_behaves_like "a non-prefillable type de champ", :type_de_champ_departements
it_behaves_like "a non-prefillable type de champ", :type_de_champ_siret

View file

@ -9,7 +9,7 @@ RSpec.describe TypesDeChamp::PrefillDropDownListTypeDeChamp do
it {
expect(possible_values).to match(
[I18n.t("views.prefill_descriptions.edit.possible_values.drop_down_list_other")] + type_de_champ.drop_down_list_enabled_non_empty_options
[I18n.t("views.prefill_descriptions.edit.possible_values.drop_down_list_other_html")] + type_de_champ.drop_down_list_enabled_non_empty_options
)
}
end

View file

@ -38,7 +38,7 @@ RSpec.describe TypesDeChamp::PrefillTypeDeChamp, type: :model do
context 'when the type de champ is prefillable' do
let(:type_de_champ) { build(:type_de_champ_email) }
it { expect(possible_values).to match([I18n.t("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}")]) }
it { expect(possible_values).to match([I18n.t("views.prefill_descriptions.edit.possible_values.#{type_de_champ.type_champ}_html")]) }
end
end

View file

@ -215,7 +215,7 @@ describe DossierProjectionService do
dossier.champs_public.first.update(value: "qu'il est beau mon pays")
end
it { is_expected.to eq("qu'il est beau mon pays") }
it { is_expected.to eq("") }
end
end
end