From f0168d147064a78a0696317adfd456e16db28544 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 13 Oct 2023 16:05:26 +0200 Subject: [PATCH 1/3] feat(combobox): add data:string slot --- app/javascript/shared/combobox-ui.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/javascript/shared/combobox-ui.ts b/app/javascript/shared/combobox-ui.ts index 3d4326564..7cd0ccf6b 100644 --- a/app/javascript/shared/combobox-ui.ts +++ b/app/javascript/shared/combobox-ui.ts @@ -296,6 +296,9 @@ export class ComboboxUI implements EventListenerObject { case 'label': input.value = this.#input.value; break; + case 'data:string': + input.value = data ? String(data) : ''; + break; case 'data': input.value = data ? JSON.stringify(data) : ''; break; From b92ccc0a0930c0cb6b314c852ef89340cf5d4d00 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 13 Oct 2023 16:06:00 +0200 Subject: [PATCH 2/3] chore(adresse): improuve adresse data source --- .../data_sources/adresse_controller.rb | 28 +++++++++++++++++-- app/models/type_de_champ.rb | 3 +- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/controllers/data_sources/adresse_controller.rb b/app/controllers/data_sources/adresse_controller.rb index c9cac63ac..a68df5eac 100644 --- a/app/controllers/data_sources/adresse_controller.rb +++ b/app/controllers/data_sources/adresse_controller.rb @@ -1,11 +1,33 @@ class DataSources::AdresseController < ApplicationController def search if params[:q].present? && params[:q].length > 3 - response = Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 }) - result = JSON.parse(response.body, symbolize_names: true) - render json: result[:features].map { { label: _1[:properties][:label], value: _1[:properties][:label] } } + response = fetch_results + + if response.success? + results = JSON.parse(response.body, symbolize_names: true) + + render json: format_results(results) + else + render json: [] + end else render json: [] end end + + private + + def fetch_results + Typhoeus.get("#{API_ADRESSE_URL}/search", params: { q: params[:q], limit: 10 }) + end + + def format_results(results) + results[:features].map do + { + label: _1[:properties][:label], + value: _1[:properties][:label], + data: _1[:properties] + } + end + end end diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 96338b3d1..c420b572b 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -596,8 +596,7 @@ class TypeDeChamp < ApplicationRecord # We should refresh all champs after update except for champs using react or custom refresh # logic (RNA, SIRET, etc.) case type_champ - when type_champs.fetch(:address), - type_champs.fetch(:annuaire_education), + when type_champs.fetch(:annuaire_education), type_champs.fetch(:carte), type_champs.fetch(:piece_justificative), type_champs.fetch(:titre_identite), From 34a76d8afdfbb9eead20e70acb3b0311eead2633 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 13 Oct 2023 16:06:26 +0200 Subject: [PATCH 3/3] feat(dossier): commune champ is an autocomplete now --- .../editable_champ/communes_component.rb | 18 --- .../communes_component.en.yml | 5 - .../communes_component.fr.yml | 5 - .../communes_component.html.haml | 25 +--- .../data_sources/commune_controller.rb | 76 ++++++++++++ app/models/champs/commune_champ.rb | 57 ++++----- .../prefill_commune_type_de_champ.rb | 2 +- .../models/champs/commune_champ/en.yml | 2 +- .../models/champs/commune_champ/fr.yml | 2 +- config/routes.rb | 2 + spec/factories/champ.rb | 5 +- spec/fixtures/cassettes/communes.yml | 113 ++++++++++++++++++ .../migrations/normalize_communes_job_spec.rb | 47 -------- spec/models/champs/commune_champ_spec.rb | 3 +- spec/models/logic/champ_value_spec.rb | 2 +- spec/models/routing_engine_spec.rb | 2 +- .../prefill_commune_type_de_champ_spec.rb | 4 +- .../dossier_projection_service_spec.rb | 3 +- .../shared_examples_for_prefilled_dossier.rb | 5 +- spec/system/users/brouillon_spec.rb | 9 +- spec/system/users/dossier_prefill_get_spec.rb | 2 +- .../system/users/dossier_prefill_post_spec.rb | 2 +- 22 files changed, 231 insertions(+), 160 deletions(-) delete mode 100644 app/components/editable_champ/communes_component/communes_component.en.yml delete mode 100644 app/components/editable_champ/communes_component/communes_component.fr.yml create mode 100644 app/controllers/data_sources/commune_controller.rb create mode 100644 spec/fixtures/cassettes/communes.yml delete mode 100644 spec/jobs/migrations/normalize_communes_job_spec.rb diff --git a/app/components/editable_champ/communes_component.rb b/app/components/editable_champ/communes_component.rb index 1af3b967e..cc5bfee0b 100644 --- a/app/components/editable_champ/communes_component.rb +++ b/app/components/editable_champ/communes_component.rb @@ -1,21 +1,3 @@ class EditableChamp::CommunesComponent < EditableChamp::EditableChampBaseComponent include ApplicationHelper - - def dsfr_champ_container - :fieldset - end - - private - - def commune_options - @champ.communes.map { ["#{_1[:name]} (#{_1[:postal_code]})", _1[:code]] } - end - - def code_postal_input_id - "#{@champ.input_id}-code_postal" - end - - def commune_select_options - { selected: @champ.selected }.merge(@champ.mandatory? ? { prompt: '' } : { include_blank: '' }) - end end diff --git a/app/components/editable_champ/communes_component/communes_component.en.yml b/app/components/editable_champ/communes_component/communes_component.en.yml deleted file mode 100644 index 7bd07cc72..000000000 --- a/app/components/editable_champ/communes_component/communes_component.en.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -en: - postal_code: "Enter the postal code" - commune: "Select the municipality from the list" - not_found: No municipality found for postal code %{postal_code}. Please check that you haven't made any mistakes. diff --git a/app/components/editable_champ/communes_component/communes_component.fr.yml b/app/components/editable_champ/communes_component/communes_component.fr.yml deleted file mode 100644 index 2831fb509..000000000 --- a/app/components/editable_champ/communes_component/communes_component.fr.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -fr: - postal_code: "Renseignez le code postal" - commune: "Sélectionnez la commune dans la liste" - not_found: Aucune commune trouvée pour le code postal %{postal_code}. Verifiez que vous n'avez pas fait d’erreur. diff --git a/app/components/editable_champ/communes_component/communes_component.html.haml b/app/components/editable_champ/communes_component/communes_component.html.haml index 4999a9259..00bb15c6b 100644 --- a/app/components/editable_champ/communes_component/communes_component.html.haml +++ b/app/components/editable_champ/communes_component/communes_component.html.haml @@ -1,23 +1,2 @@ -.fr-fieldset__element.fr-mb-0 - .fr-input-group - = @form.label :code_postal, t('.postal_code').html_safe, class: 'fr-label', for: code_postal_input_id - = @form.text_field :code_postal, required: @champ.required?, id: code_postal_input_id, class: "width-33-desktop width-100-mobile small-margin fr-input" - - if @champ.code_postal? - - if commune_options.empty? - .fr-error-text.mb-4= t('.not_found', postal_code: @champ.code_postal) - -.fr-fieldset__element.fr-mb-0 - - if commune_options.empty? - -# noop - - elsif commune_options.size <= 3 - %fieldset.fr-fieldset - .fr-fieldset__legend--regular.fr-fieldset__legend= t('.commune').html_safe - - - commune_options.each.with_index do |(option, value), index| - .fr-fieldset__element - .fr-radio-group - = @form.radio_button :value, value, checked: @champ.selected == value, id: "#{code_postal_input_id}-#{value.parameterize}" - = @form.label :value, option, for: "#{code_postal_input_id}-#{value.parameterize}", class: 'fr-label' - - else - = @form.label :value, t('.commune').html_safe, for: @champ.input_id, class: 'fr-label' - = @form.select :value, commune_options, commune_select_options, required: @champ.required?, id: @champ.input_id, aria: { describedby: @champ.describedby_id }, class: "width-33-desktop width-100-mobile fr-select" += render Dsfr::ComboboxComponent.new form: @form, name: :external_id, url: data_sources_data_source_commune_path, selected: [@champ.to_s, @champ.selected], id: @champ.input_id, class: 'fr-select', describedby: @champ.describedby_id do + = @form.hidden_field :code_postal, data: { value_slot: 'data:string' } diff --git a/app/controllers/data_sources/commune_controller.rb b/app/controllers/data_sources/commune_controller.rb new file mode 100644 index 000000000..de59f9cd6 --- /dev/null +++ b/app/controllers/data_sources/commune_controller.rb @@ -0,0 +1,76 @@ +class DataSources::CommuneController < ApplicationController + def search + if params[:q].present? && params[:q].length > 1 + response = fetch_results + + if response.success? + results = JSON.parse(response.body, symbolize_names: true) + + render json: format_results(results) + else + render json: [] + end + else + render json: [] + end + end + + private + + def fetch_results + if postal_code?(params[:q]) + fetch_by_postal_code(params[:q]) + else + fetch_by_name(params[:q]) + end + end + + def fetch_by_name(name) + Typhoeus.get("#{API_GEO_URL}/communes", params: { + type: 'commune-actuelle,arrondissement-municipal', + nom: name, + boost: 'population', + limit: 20 + }) + end + + def fetch_by_postal_code(postal_code) + Typhoeus.get("#{API_GEO_URL}/communes", params: { + type: 'commune-actuelle,arrondissement-municipal', + codePostal: postal_code, + boost: 'population', + limit: 10 + }) + end + + def postal_code?(string) + string.match?(/\A[-+]?\d+\z/) ? true : false + end + + def format_results(results) + results.reject(&method(:code_metropole?)).flat_map do |result| + item = { + name: result[:nom].tr("'", '’'), + code: result[:code], + epci_code: result[:codeEpci], + departement_code: result[:codeDepartement] + }.compact + + if result[:codesPostaux].present? + result[:codesPostaux].map { item.merge(postal_code: _1) } + else + [item] + end.map do |item| + { + label: "#{item[:name]} (#{item[:postal_code]})", + value: item[:code], + data: item[:postal_code] + } + end + end + end + + def code_metropole?(result) + result[:code].in?(['75056', '13055', '69123']) + end +end diff --git a/app/models/champs/commune_champ.rb b/app/models/champs/commune_champ.rb index c4aec0c05..cf0512e1c 100644 --- a/app/models/champs/commune_champ.rb +++ b/app/models/champs/commune_champ.rb @@ -1,6 +1,6 @@ class Champs::CommuneChamp < Champs::TextChamp store_accessor :value_json, :code_departement, :code_postal - before_validation :on_code_postal_change + before_save :on_codes_change, if: :should_refresh_after_code_change? def for_export [to_s, code? ? code : '', departement? ? departement_code_and_name : ''] @@ -37,7 +37,7 @@ class Champs::CommuneChamp < Champs::TextChamp alias postal_code code_postal def name - if code? + if departement? && code? APIGeoService.commune_name(code_departement, code).presence || safe_to_s else safe_to_s @@ -45,7 +45,7 @@ class Champs::CommuneChamp < Champs::TextChamp end def to_s - if code? + if departement? && code_postal? && code? name = APIGeoService.commune_name(code_departement, code) name.present? ? "#{name} (#{code_postal})" : safe_to_s else @@ -69,46 +69,29 @@ class Champs::CommuneChamp < Champs::TextChamp end end - def value=(code) - if code.blank? || !code_postal? - self.code_departement = nil - self.external_id = nil - super(nil) - else - commune = communes.find { _1[:code] == code } - if commune.present? - self.code_departement = commune[:departement_code] - self.external_id = commune[:code] - super(commune[:name]) - else - self.code_departement = nil - self.external_id = nil - super(nil) - end - end - end - - def html_label? - false - end - - def legend_label? - true - end - private def safe_to_s value.present? ? value.to_s : '' end - def on_code_postal_change - if code_postal_changed? - if communes.one? - self.value = communes.first[:code] - else - self.value = nil - end + def on_codes_change + return if !code? + + commune = communes.find { _1[:code] == code } + + if commune.present? + self.code_departement = commune[:departement_code] + self.value = commune[:name] + else + self.code_departement = nil + self.code_postal = nil + self.external_id = nil + self.value = nil end end + + def should_refresh_after_code_change? + !departement? || code_postal_changed? || external_id_changed? + end end diff --git a/app/models/types_de_champ/prefill_commune_type_de_champ.rb b/app/models/types_de_champ/prefill_commune_type_de_champ.rb index 860406d39..524fade1b 100644 --- a/app/models/types_de_champ/prefill_commune_type_de_champ.rb +++ b/app/models/types_de_champ/prefill_commune_type_de_champ.rb @@ -32,7 +32,7 @@ class TypesDeChamp::PrefillCommuneTypeDeChamp < TypesDeChamp::PrefillTypeDeChamp end def code_postal_and_commune_attributes(champ, postal_code, commune_code) - code_postal_attributes(champ, postal_code).merge(value: commune_code) + code_postal_attributes(champ, postal_code).merge(external_id: commune_code) end def departements diff --git a/config/locales/models/champs/commune_champ/en.yml b/config/locales/models/champs/commune_champ/en.yml index 552a75a34..9fcc477e7 100644 --- a/config/locales/models/champs/commune_champ/en.yml +++ b/config/locales/models/champs/commune_champ/en.yml @@ -3,4 +3,4 @@ en: attributes: champs/commune_champ: hints: - value: "Enter the municipality's zip code, then select the municipality from the list" + value: "Enter the city name or zip code, then select the municipality from the list" diff --git a/config/locales/models/champs/commune_champ/fr.yml b/config/locales/models/champs/commune_champ/fr.yml index bf5e4954c..f274651ef 100644 --- a/config/locales/models/champs/commune_champ/fr.yml +++ b/config/locales/models/champs/commune_champ/fr.yml @@ -3,4 +3,4 @@ fr: attributes: champs/commune_champ: hints: - value: "Renseignez le code postal de la ville puis, ensuite, sélectionnez la commune dans la liste" + value: "Renseignez le nom ou le code postal de la ville puis, sélectionnez la commune dans la liste" diff --git a/config/routes.rb b/config/routes.rb index 4443b6525..263f2f157 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -224,6 +224,8 @@ Rails.application.routes.draw do namespace :data_sources do get :adresse, to: 'adresse#search', as: :data_source_adresse + get :commune, to: 'commune#search', as: :data_source_commune + get :search_domaine_fonct, to: 'chorus#search_domaine_fonct', as: :search_domaine_fonct get :search_centre_couts, to: 'chorus#search_centre_couts', as: :search_centre_couts get :search_ref_programmation, to: 'chorus#search_ref_programmation', as: :search_ref_programmation diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 304e51c89..91e5b34b0 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -122,9 +122,8 @@ FactoryBot.define do factory :champ_communes, class: 'Champs::CommuneChamp' do type_de_champ { association :type_de_champ_communes, procedure: dossier.procedure } - value { 'Coye-la-Forêt (60580)' } - value_json { { "departement" => "Oise", "code_departement" => "60" } } - external_id { "60172" } + external_id { '60172' } + code_postal { '60580' } end factory :champ_epci, class: 'Champs::EpciChamp' do diff --git a/spec/fixtures/cassettes/communes.yml b/spec/fixtures/cassettes/communes.yml new file mode 100644 index 000000000..d85d393df --- /dev/null +++ b/spec/fixtures/cassettes/communes.yml @@ -0,0 +1,113 @@ +--- +http_interactions: +- request: + method: get + uri: https://geo.api.gouv.fr/communes?boost=population&codePostal=60&limit=10&type=commune-actuelle,arrondissement-municipal + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - demarches-simplifiees.fr + Expect: + - '' + response: + status: + code: 200 + message: '' + headers: + Server: + - nginx/1.10.3 (Ubuntu) + Date: + - Fri, 03 Nov 2023 16:28:05 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '2' + X-Powered-By: + - Express + Vary: + - Origin + Etag: + - W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w" + Strict-Transport-Security: + - max-age=15552000 + body: + encoding: ASCII-8BIT + string: "[]" + recorded_at: Fri, 03 Nov 2023 16:28:05 GMT +- request: + method: get + uri: https://geo.api.gouv.fr/communes?boost=population&codePostal=6040&limit=10&type=commune-actuelle,arrondissement-municipal + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - demarches-simplifiees.fr + Expect: + - '' + response: + status: + code: 200 + message: '' + headers: + Server: + - nginx/1.10.3 (Ubuntu) + Date: + - Fri, 03 Nov 2023 16:28:05 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '2' + X-Powered-By: + - Express + Vary: + - Origin + Etag: + - W/"2-l9Fw4VUO7kr8CvBlt4zaMCqXZ0w" + Strict-Transport-Security: + - max-age=15552000 + body: + encoding: ASCII-8BIT + string: "[]" + recorded_at: Fri, 03 Nov 2023 16:28:05 GMT +- request: + method: get + uri: https://geo.api.gouv.fr/communes?boost=population&codePostal=60400&limit=10&type=commune-actuelle,arrondissement-municipal + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - demarches-simplifiees.fr + Expect: + - '' + response: + status: + code: 200 + message: '' + headers: + Server: + - nginx/1.10.3 (Ubuntu) + Date: + - Fri, 03 Nov 2023 16:28:05 GMT + Content-Type: + - application/json; charset=utf-8 + Content-Length: + - '1608' + Vary: + - Accept-Encoding + - Origin + X-Powered-By: + - Express + Etag: + - W/"648-ePvxexacWlkxvdJ8v6an7VGvHMo" + Strict-Transport-Security: + - max-age=15552000 + body: + encoding: ASCII-8BIT + string: !binary |- + W3sibm9tIjoiQXBwaWxseSIsImNvZGUiOiI2MDAyMSIsImNvZGVEZXBhcnRlbWVudCI6IjYwIiwic2lyZW4iOiIyMTYwMDAyMTYiLCJjb2RlRXBjaSI6IjI0NjAwMDc1NiIsImNvZGVSZWdpb24iOiIzMiIsImNvZGVzUG9zdGF1eCI6WyI2MDQwMCJdLCJwb3B1bGF0aW9uIjo1Mjl9LHsibm9tIjoiQmFixZN1ZiIsImNvZGUiOiI2MDAzNyIsImNvZGVEZXBhcnRlbWVudCI6IjYwIiwic2lyZW4iOiIyMTYwMDAzNjQiLCJjb2RlRXBjaSI6IjI0NjAwMDc1NiIsImNvZGVSZWdpb24iOiIzMiIsImNvZGVzUG9zdGF1eCI6WyI2MDQwMCJdLCJwb3B1bGF0aW9uIjo1MTB9LHsibm9tIjoiQmVhdXJhaW5zLWzDqHMtTm95b24iLCJjb2RlIjoiNjAwNTUiLCJjb2RlRGVwYXJ0ZW1lbnQiOiI2MCIsInNpcmVuIjoiMjE2MDAwNTQ3IiwiY29kZUVwY2kiOiIyNDYwMDA3NTYiLCJjb2RlUmVnaW9uIjoiMzIiLCJjb2Rlc1Bvc3RhdXgiOlsiNjA0MDAiXSwicG9wdWxhdGlvbiI6MzQwfSx7Im5vbSI6IkLDqWjDqXJpY291cnQiLCJjb2RlIjoiNjAwNTkiLCJjb2RlRGVwYXJ0ZW1lbnQiOiI2MCIsInNpcmVuIjoiMjE2MDAwNTg4IiwiY29kZUVwY2kiOiIyNDYwMDA3NTYiLCJjb2RlUmVnaW9uIjoiMzIiLCJjb2Rlc1Bvc3RhdXgiOlsiNjA0MDAiXSwicG9wdWxhdGlvbiI6MjAyfSx7Im5vbSI6IkJyw6l0aWdueSIsImNvZGUiOiI2MDEwNSIsImNvZGVEZXBhcnRlbWVudCI6IjYwIiwic2lyZW4iOiIyMTYwMDEwNTciLCJjb2RlRXBjaSI6IjI0NjAwMDc1NiIsImNvZGVSZWdpb24iOiIzMiIsImNvZGVzUG9zdGF1eCI6WyI2MDQwMCJdLCJwb3B1bGF0aW9uIjo0Mjd9LHsibm9tIjoiQnVzc3kiLCJjb2RlIjoiNjAxMTciLCJjb2RlRGVwYXJ0ZW1lbnQiOiI2MCIsInNpcmVuIjoiMjE2MDAxMTcyIiwiY29kZUVwY2kiOiIyNDYwMDA3NTYiLCJjb2RlUmVnaW9uIjoiMzIiLCJjb2Rlc1Bvc3RhdXgiOlsiNjA0MDAiXSwicG9wdWxhdGlvbiI6MzEwfSx7Im5vbSI6IkNhaXNuZXMiLCJjb2RlIjoiNjAxMTgiLCJjb2RlRGVwYXJ0ZW1lbnQiOiI2MCIsInNpcmVuIjoiMjE2MDAxMTgwIiwiY29kZUVwY2kiOiIyNDYwMDA3NTYiLCJjb2RlUmVnaW9uIjoiMzIiLCJjb2Rlc1Bvc3RhdXgiOlsiNjA0MDAiXSwicG9wdWxhdGlvbiI6NTA3fSx7Im5vbSI6IkNyaXNvbGxlcyIsImNvZGUiOiI2MDE4MSIsImNvZGVEZXBhcnRlbWVudCI6IjYwIiwic2lyZW4iOiIyMTYwMDE4MDAiLCJjb2RlRXBjaSI6IjI0NjAwMDc1NiIsImNvZGVSZWdpb24iOiIzMiIsImNvZGVzUG9zdGF1eCI6WyI2MDQwMCJdLCJwb3B1bGF0aW9uIjo5MDh9LHsibm9tIjoiQ3V0cyIsImNvZGUiOiI2MDE4OSIsImNvZGVEZXBhcnRlbWVudCI6IjYwIiwic2lyZW4iOiIyMTYwMDE4ODMiLCJjb2RlRXBjaSI6IjI0NjAwMDc1NiIsImNvZGVSZWdpb24iOiIzMiIsImNvZGVzUG9zdGF1eCI6WyI2MDQwMCJdLCJwb3B1bGF0aW9uIjo5Njd9LHsibm9tIjoiR2VudnJ5IiwiY29kZSI6IjYwMjcwIiwiY29kZURlcGFydGVtZW50IjoiNjAiLCJzaXJlbiI6IjIxNjAwMjY3NSIsImNvZGVFcGNpIjoiMjQ2MDAwNzU2IiwiY29kZVJlZ2lvbiI6IjMyIiwiY29kZXNQb3N0YXV4IjpbIjYwNDAwIl0sInBvcHVsYXRpb24iOjMzNX1d + recorded_at: Fri, 03 Nov 2023 16:28:05 GMT +recorded_with: VCR 6.1.0 diff --git a/spec/jobs/migrations/normalize_communes_job_spec.rb b/spec/jobs/migrations/normalize_communes_job_spec.rb deleted file mode 100644 index b71421de5..000000000 --- a/spec/jobs/migrations/normalize_communes_job_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -describe Migrations::NormalizeCommunesJob, type: :job do - let(:champ) { create(:champ_communes, external_id: code_insee, code_departement:) } - let(:code_insee) { '97209' } - let(:value) { 'Fort-de-France (97200)' } - let(:code_departement) { nil } - - before { champ.update_column(:value, value) } - - subject { described_class.perform_now([champ.id]) } - - context 'Fort-de-France' do - it 'assign code_departement and code_postal' do - expect(champ.reload.code_postal).to be_nil - expect(champ.reload.code_departement).to be_nil - subject - expect(champ.reload.code_postal).to eq('97200') - expect(champ.reload.code_departement).to eq('972') - end - end - - context 'Ajaccio' do - let(:code_insee) { '2A004' } - let(:value) { 'Ajaccio (20000)' } - - it 'assign code_departement and code_postal' do - expect(champ.reload.code_postal).to be_nil - expect(champ.reload.code_departement).to be_nil - subject - expect(champ.reload.code_postal).to eq('20000') - expect(champ.reload.code_departement).to eq('2A') - end - end - - context 'undefined' do - let(:code_insee) { '2A004' } - let(:value) { 'Ajaccio (20000)' } - let(:code_departement) { 'undefined' } - - it 'assign code_departement and code_postal' do - expect(champ.reload.code_postal).to be_nil - expect(champ.reload.code_departement).to eq('undefined') - subject - expect(champ.reload.code_postal).to eq('20000') - expect(champ.reload.code_departement).to eq('2A') - end - end -end diff --git a/spec/models/champs/commune_champ_spec.rb b/spec/models/champs/commune_champ_spec.rb index 622e6fb67..e74ba5750 100644 --- a/spec/models/champs/commune_champ_spec.rb +++ b/spec/models/champs/commune_champ_spec.rb @@ -2,11 +2,10 @@ describe Champs::CommuneChamp do let(:code_insee) { '63102' } let(:code_postal) { '63290' } let(:code_departement) { '63' } - let(:champ) { create(:champ_communes, code_postal: code_postal) } + let(:champ) { create(:champ_communes, code_postal:, external_id: code_insee) } describe 'value' do it 'with code_postal' do - champ.update(value: code_insee) expect(champ.to_s).to eq('Châteldon (63290)') expect(champ.name).to eq('Châteldon') expect(champ.external_id).to eq(code_insee) diff --git a/spec/models/logic/champ_value_spec.rb b/spec/models/logic/champ_value_spec.rb index 1664165cb..b6da7b574 100644 --- a/spec/models/logic/champ_value_spec.rb +++ b/spec/models/logic/champ_value_spec.rb @@ -89,7 +89,7 @@ describe Logic::ChampValue do end context 'commune tdc' do - let(:champ) { create(:champ_communes, value: 'Rueil-Malmaison', value_json: { code_postal: '92500', code_departement: '92' }) } + let(:champ) { create(:champ_communes, code_postal: '92500', external_id: '92063') } it { is_expected.to eq('92') } end diff --git a/spec/models/routing_engine_spec.rb b/spec/models/routing_engine_spec.rb index 676e7c7de..36ea5bc63 100644 --- a/spec/models/routing_engine_spec.rb +++ b/spec/models/routing_engine_spec.rb @@ -123,7 +123,7 @@ describe RoutingEngine, type: :model do context 'with a matching rule' do before do gi_2.update(routing_rule: ds_eq(champ_value(communes_tdc.stable_id), constant('92'))) - dossier.champs.first.update(value: 'Rueil-Malmaison', value_json: { code_postal: '92500', code_departement: '92' }) + dossier.champs.first.update(code_postal: '92500', external_id: '92063') end it { is_expected.to eq(gi_2) } diff --git a/spec/models/types_de_champ/prefill_commune_type_de_champ_spec.rb b/spec/models/types_de_champ/prefill_commune_type_de_champ_spec.rb index 01dca5dd3..2f02790b2 100644 --- a/spec/models/types_de_champ/prefill_commune_type_de_champ_spec.rb +++ b/spec/models/types_de_champ/prefill_commune_type_de_champ_spec.rb @@ -64,7 +64,7 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do context 'when the first element is a valid postal code' do context 'when the second element is a valid insee code' do let(:value) { ['01540', '01457'] } - it { is_expected.to match({ id: champ.id, code_postal: '01540', value: '01457' }) } + it { is_expected.to match({ id: champ.id, code_postal: '01540', external_id: '01457' }) } end context 'when the second element is not a valid insee code' do @@ -83,7 +83,7 @@ RSpec.describe TypesDeChamp::PrefillCommuneTypeDeChamp do context 'when the first element is a valid postal code' do context 'when the second element is a valid insee code' do let(:value) { ['01540', '01457', 'hello'] } - it { is_expected.to match({ id: champ.id, code_postal: '01540', value: '01457' }) } + it { is_expected.to match({ id: champ.id, code_postal: '01540', external_id: '01457' }) } end context 'when the second element is not a valid insee code' do diff --git a/spec/services/dossier_projection_service_spec.rb b/spec/services/dossier_projection_service_spec.rb index 8b0234e2e..aabaacb6a 100644 --- a/spec/services/dossier_projection_service_spec.rb +++ b/spec/services/dossier_projection_service_spec.rb @@ -62,8 +62,7 @@ describe DossierProjectionService do end before do - dossier.champs_public.first.update(code_postal: '63290') - dossier.champs_public.first.update(value: '63102') + dossier.champs_public.first.update(code_postal: '63290', external_id: '63102') end let(:result) { subject } diff --git a/spec/support/shared_examples_for_prefilled_dossier.rb b/spec/support/shared_examples_for_prefilled_dossier.rb index 8a6b0ef8d..a65ad9029 100644 --- a/spec/support/shared_examples_for_prefilled_dossier.rb +++ b/spec/support/shared_examples_for_prefilled_dossier.rb @@ -23,10 +23,7 @@ shared_examples "the user has got a prefilled dossier, owned by themselves" do expect(page).to have_field("Le département de l’EPCI", with: epci_value.first) expect(page).to have_selector("option[value='#{epci_value.last}'][selected]") expect(page).to have_field(type_de_champ_dossier_link.libelle, with: dossier_link_value) - label_commune = page.find(:label, text: commune_libelle) - radio_commune = page.find("##{label_commune['for']}") - expect(radio_commune).to be_checked - expect(page).to have_field(commune_libelle, with: '01457') + expect(page).to have_field(type_de_champ_commune.libelle, with: commune_libelle) expect(page).to have_content(annuaire_education_value.last) expect(page).to have_content(address_value.last) end diff --git a/spec/system/users/brouillon_spec.rb b/spec/system/users/brouillon_spec.rb index cf8aeb447..13ad2760b 100644 --- a/spec/system/users/brouillon_spec.rb +++ b/spec/system/users/brouillon_spec.rb @@ -6,7 +6,7 @@ describe 'The user' do let(:user_dossier) { user.dossiers.first } let!(:dossier_to_link) { create(:dossier) } - scenario 'fill a dossier', js: true do + scenario 'fill a dossier', js: true, vcr: { cassette_name: 'communes' } do log_in(user, procedure) fill_individual @@ -36,9 +36,8 @@ describe 'The user' do select('Australie', from: form_id_for('pays')) select('Martinique', from: form_id_for('regions')) select('02 – Aisne', from: form_id_for('departements')) - fill_in('Renseignez le code postal', with: '60400') - # wait_until { all('label', text: 'Sélectionnez la commune dans la liste').size == 1 } - select('Brétigny (60400)', from: form_id_for('commune')) + fill_in('communes', with: '60400') + find('li', text: 'Brétigny (60400)').click # communes needs more time to be updated wait_until { champ_value_for('communes') == "Brétigny" } @@ -97,7 +96,7 @@ describe 'The user' do expect(page).to have_button('alpha') expect(page).to have_button('charly') end - expect(page).to have_selected_value('commune', selected: 'Brétigny (60400)') + expect(page).to have_field('communes', with: 'Brétigny (60400)') expect(page).to have_selected_value('pays', selected: 'Australie') expect(page).to have_field('dossier_link', with: '123') expect(page).to have_text('file.pdf') diff --git a/spec/system/users/dossier_prefill_get_spec.rb b/spec/system/users/dossier_prefill_get_spec.rb index 5b4de7c48..a642504ba 100644 --- a/spec/system/users/dossier_prefill_get_spec.rb +++ b/spec/system/users/dossier_prefill_get_spec.rb @@ -30,7 +30,7 @@ describe 'Prefilling a dossier (with a GET request):', js: true do } let(:epci_value) { ['01', '200029999'] } let(:dossier_link_value) { '42' } - let(:commune_value) { ['01540', '01457'] } # Vonnas (01540) + let(:commune_value) { ['01540', '01457'] } let(:commune_libelle) { 'Vonnas (01540)' } let(:address_value) { "20 Avenue de Ségur 75007 Paris" } let(:sub_type_de_champs_repetition) { procedure.active_revision.children_of(type_de_champ_repetition) } diff --git a/spec/system/users/dossier_prefill_post_spec.rb b/spec/system/users/dossier_prefill_post_spec.rb index 687a6b753..ef8102f48 100644 --- a/spec/system/users/dossier_prefill_post_spec.rb +++ b/spec/system/users/dossier_prefill_post_spec.rb @@ -29,7 +29,7 @@ describe 'Prefilling a dossier (with a POST request):', js: true do ] } let(:epci_value) { ['01', '200029999'] } - let(:commune_value) { ['01540', '01457'] } # Vonnas (01540) + let(:commune_value) { ['01540', '01457'] } let(:commune_libelle) { 'Vonnas (01540)' } let(:address_value) { "20 Avenue de Ségur 75007 Paris" } let(:sub_type_de_champs_repetition) { procedure.active_revision.children_of(type_de_champ_repetition) }