From b29893a843ebc138ec0fd1ae185c09051d3cee43 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Mon, 4 Nov 2024 10:54:00 +0100 Subject: [PATCH 1/2] feat(conditional): can condition and route with pays tdc --- app/models/logic/champ_value.rb | 9 +++++--- .../champs_conditions_component_spec.rb | 12 ++++++++++ spec/models/routing_engine_spec.rb | 23 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/models/logic/champ_value.rb b/app/models/logic/champ_value.rb index f4f655a5e..0d0015198 100644 --- a/app/models/logic/champ_value.rb +++ b/app/models/logic/champ_value.rb @@ -12,7 +12,8 @@ class Logic::ChampValue < Logic::Term :epci, :departements, :regions, - :address + :address, + :pays ) CHAMP_VALUE_TYPE = { @@ -56,7 +57,7 @@ class Logic::ChampValue < Logic::Term targeted_champ.selected when "Champs::MultipleDropDownListChamp" targeted_champ.selected_options - when "Champs::RegionChamp" + when "Champs::RegionChamp", "Champs::PaysChamp" targeted_champ.code when "Champs::DepartementChamp" { @@ -81,7 +82,7 @@ class Logic::ChampValue < Logic::Term when MANAGED_TYPE_DE_CHAMP.fetch(:integer_number), MANAGED_TYPE_DE_CHAMP.fetch(:decimal_number) CHAMP_VALUE_TYPE.fetch(:number) when MANAGED_TYPE_DE_CHAMP.fetch(:drop_down_list), - MANAGED_TYPE_DE_CHAMP.fetch(:regions) + MANAGED_TYPE_DE_CHAMP.fetch(:regions), MANAGED_TYPE_DE_CHAMP.fetch(:pays) CHAMP_VALUE_TYPE.fetch(:enum) when MANAGED_TYPE_DE_CHAMP.fetch(:communes) CHAMP_VALUE_TYPE.fetch(:commune_enum) @@ -128,6 +129,8 @@ class Logic::ChampValue < Logic::Term APIGeoService.regions.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } elsif operator_name.in?([Logic::InDepartementOperator.name, Logic::NotInDepartementOperator.name]) || tdc.type_champ.in?([MANAGED_TYPE_DE_CHAMP.fetch(:communes), MANAGED_TYPE_DE_CHAMP.fetch(:epci), MANAGED_TYPE_DE_CHAMP.fetch(:departements), MANAGED_TYPE_DE_CHAMP.fetch(:address)]) APIGeoService.departements.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } + elsif tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:pays) + APIGeoService.countries.map { ["#{_1[:name]} – #{_1[:code]}", _1[:code]] } else tdc.drop_down_options_with_other.map { _1.is_a?(Array) ? _1 : [_1, _1] } end diff --git a/spec/components/conditions/champs_conditions_component_spec.rb b/spec/components/conditions/champs_conditions_component_spec.rb index b24abd8ae..e6e826cba 100644 --- a/spec/components/conditions/champs_conditions_component_spec.rb +++ b/spec/components/conditions/champs_conditions_component_spec.rb @@ -131,6 +131,18 @@ describe Conditions::ChampsConditionsComponent, type: :component do end end + context 'pays' do + let(:pays) { create(:type_de_champ_pays) } + let(:upper_tdcs) { [pays] } + let(:condition) { empty_operator(champ_value(pays.stable_id), constant(true)) } + let(:pays_options) { APIGeoService.countries.map { "#{_1[:name]} – #{_1[:code]}" } } + + it do + expect(page).to have_select('type_de_champ[condition_form][rows][][operator_name]', with_options: ['Est']) + expect(page).to have_select('type_de_champ[condition_form][rows][][value]', options: (['Sélectionner'] + pays_options)) + end + end + context 'address' do let(:address) { create(:type_de_champ_address) } let(:upper_tdcs) { [address] } diff --git a/spec/models/routing_engine_spec.rb b/spec/models/routing_engine_spec.rb index b929844f5..9d8a986a4 100644 --- a/spec/models/routing_engine_spec.rb +++ b/spec/models/routing_engine_spec.rb @@ -181,6 +181,29 @@ describe RoutingEngine, type: :model do end end + context 'with a pays type de champ' do + let(:procedure) do + create(:procedure, types_de_champ_public: [{ type: :pays }]).tap do |p| + p.groupe_instructeurs.create(label: 'a third group') + end + end + + let(:pays_tdc) { procedure.draft_revision.types_de_champ.first } + + context 'with a matching rule' do + before do + gi_2.update(routing_rule: ds_eq(champ_value(pays_tdc.stable_id), constant('BE'))) + dossier.champs.first.update_columns( + value: "Belgique" + ) + end + + it do + is_expected.to eq(gi_2) + end + end + end + context 'routing rules priorities' do let(:procedure) do create(:procedure, From 30aa0b71d0ebbcd276920e171d1212f096dfc9f4 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Mon, 4 Nov 2024 11:08:14 +0100 Subject: [PATCH 2/2] feat(routing): can create simple routing with pays tdc --- .../groupe_instructeurs_controller.rb | 4 ++++ app/models/type_de_champ.rb | 1 + .../groupe_instructeurs_controller_spec.rb | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/app/controllers/administrateurs/groupe_instructeurs_controller.rb b/app/controllers/administrateurs/groupe_instructeurs_controller.rb index c9ecd1563..4984e601e 100644 --- a/app/controllers/administrateurs/groupe_instructeurs_controller.rb +++ b/app/controllers/administrateurs/groupe_instructeurs_controller.rb @@ -60,6 +60,10 @@ module Administrateurs rule_operator = :ds_eq tdc_options = APIGeoService.regions.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator) + when TypeDeChamp.type_champs.fetch(:pays) + rule_operator = :ds_eq + tdc_options = APIGeoService.countries.map { ["#{_1[:code]} – #{_1[:name]}", _1[:code]] } + create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator) when TypeDeChamp.type_champs.fetch(:drop_down_list) tdc_options = tdc.drop_down_options.reject(&:empty?) create_groups_from_drop_down_list_tdc(tdc_options, stable_id) diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 79bfa7088..2c8e83406 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -115,6 +115,7 @@ class TypeDeChamp < ApplicationRecord type_champs.fetch(:communes), type_champs.fetch(:departements), type_champs.fetch(:regions), + type_champs.fetch(:pays), type_champs.fetch(:epci), type_champs.fetch(:address) ] diff --git a/spec/controllers/administrateurs/groupe_instructeurs_controller_spec.rb b/spec/controllers/administrateurs/groupe_instructeurs_controller_spec.rb index 6e4b823dc..0c6790606 100644 --- a/spec/controllers/administrateurs/groupe_instructeurs_controller_spec.rb +++ b/spec/controllers/administrateurs/groupe_instructeurs_controller_spec.rb @@ -959,6 +959,26 @@ describe Administrateurs::GroupeInstructeursController, type: :controller do end end + context 'with a pays type de champ' do + let!(:procedure3) do + create(:procedure, + types_de_champ_public: [{ type: :pays }], + administrateurs: [admin]) + end + + let!(:pays_tdc) { procedure3.draft_revision.types_de_champ.first } + + before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: pays_tdc.stable_id } } } + + it do + expect(response).to redirect_to(admin_procedure_groupe_instructeurs_path(procedure3)) + expect(flash.notice).to eq 'Les groupes instructeurs ont été ajoutés' + expect(procedure3.groupe_instructeurs.pluck(:label)).to include("AD – Andorre") + expect(procedure3.reload.defaut_groupe_instructeur.routing_rule).to eq(ds_eq(champ_value(pays_tdc.stable_id), constant('AD'))) + expect(procedure3.routing_enabled).to be_truthy + end + end + context 'with a communes type de champ' do let!(:procedure3) do create(:procedure,