Skip to content

Commit

Permalink
Merge pull request #9944 from demarches-simplifiees/can-route-by-address
Browse files Browse the repository at this point in the history
ETQ admin je peux conditionner / router depuis un champ adresse
  • Loading branch information
E-L-T authored Aug 26, 2024
2 parents 340828c + fade633 commit 8ec2b38
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/components/conditions/conditions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def compatibles_operators_for_select(left)
[t('is', scope: 'logic'), Eq.name],
[t('is_not', scope: 'logic'), NotEq.name]
]
when ChampValue::CHAMP_VALUE_TYPE.fetch(:commune_enum), ChampValue::CHAMP_VALUE_TYPE.fetch(:epci_enum)
when ChampValue::CHAMP_VALUE_TYPE.fetch(:commune_enum), ChampValue::CHAMP_VALUE_TYPE.fetch(:epci_enum), ChampValue::CHAMP_VALUE_TYPE.fetch(:address)
[
[t(InDepartementOperator.name, scope: 'logic.operators'), InDepartementOperator.name],
[t(NotInDepartementOperator.name, scope: 'logic.operators'), NotInDepartementOperator.name],
Expand Down Expand Up @@ -151,7 +151,7 @@ def right_operand_tag(left, right, row_index, operator_name)
id: input_id_for('value', row_index),
class: 'fr-select'
)
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum, :address
enums_for_select = left.options(@source_tdcs, operator_name)

if right_invalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create_simple_routing
tdc_options = APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
rule_operator = :ds_eq
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
when TypeDeChamp.type_champs.fetch(:communes), TypeDeChamp.type_champs.fetch(:epci)
when TypeDeChamp.type_champs.fetch(:communes), TypeDeChamp.type_champs.fetch(:epci), TypeDeChamp.type_champs.fetch(:address)
tdc_options = APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
rule_operator = :ds_in_departement
create_groups_from_territorial_tdc(tdc_options, stable_id, rule_operator)
Expand Down
6 changes: 3 additions & 3 deletions app/models/logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.ensure_compatibility_from_left(condition, type_de_champs)
operator_class = EmptyOperator
in [:enum, _]
operator_class = Eq
in [:commune_enum, _] | [:epci_enum, _]
in [:commune_enum, _] | [:epci_enum, _] | [:address, _]
operator_class = InDepartementOperator
in [:departement_enum, _]
operator_class = Eq
Expand All @@ -61,7 +61,7 @@ def self.ensure_compatibility_from_left(condition, type_de_champs)
Constant.new(true)
when :empty
Empty.new
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum, :address
Constant.new(left.options(type_de_champs).first.second)
when :number
Constant.new(0)
Expand All @@ -75,7 +75,7 @@ def self.compatible_type?(left, right, type_de_champs)
case [left.type(type_de_champs), right.type(type_de_champs)]
in [a, ^a] # syntax for same type
true
in [:enum, :string] | [:enums, :string] | [:commune_enum, :string] | [:epci_enum, :string] | [:departement_enum, :string]
in [:enum, :string] | [:enums, :string] | [:commune_enum, :string] | [:epci_enum, :string] | [:departement_enum, :string] | [:address, :string]
true
else
false
Expand Down
10 changes: 7 additions & 3 deletions app/models/logic/champ_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class Logic::ChampValue < Logic::Term
:communes,
:epci,
:departements,
:regions
:regions,
:address
)

CHAMP_VALUE_TYPE = {
Expand All @@ -21,6 +22,7 @@ class Logic::ChampValue < Logic::Term
commune_enum: :commune_enum,
epci_enum: :epci_enum,
departement_enum: :departement_enum,
address: :address,
enums: :enums, # multiple choice from a dropdownlist (multipledropdownlist)
empty: :empty,
unmanaged: :unmanaged
Expand Down Expand Up @@ -60,7 +62,7 @@ def compute(champs)
value: targeted_champ.code,
code_region: targeted_champ.code_region
}
when "Champs::CommuneChamp", "Champs::EpciChamp"
when "Champs::CommuneChamp", "Champs::EpciChamp", "Champs::AddressChamp"
{
code_departement: targeted_champ.code_departement,
code_region: targeted_champ.code_region
Expand All @@ -86,6 +88,8 @@ def type(type_de_champs)
CHAMP_VALUE_TYPE.fetch(:epci_enum)
when MANAGED_TYPE_DE_CHAMP.fetch(:departements)
CHAMP_VALUE_TYPE.fetch(:departement_enum)
when MANAGED_TYPE_DE_CHAMP.fetch(:address)
CHAMP_VALUE_TYPE.fetch(:address)
when MANAGED_TYPE_DE_CHAMP.fetch(:multiple_drop_down_list)
CHAMP_VALUE_TYPE.fetch(:enums)
else
Expand Down Expand Up @@ -121,7 +125,7 @@ def options(type_de_champs, operator_name = nil)

if operator_name.in?([Logic::InRegionOperator.name, Logic::NotInRegionOperator.name]) || tdc.type_champ == MANAGED_TYPE_DE_CHAMP.fetch(:regions)
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)])
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]] }
else
tdc.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1 : [_1, _1] }
Expand Down
3 changes: 2 additions & 1 deletion app/models/type_de_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ class TypeDeChamp < ApplicationRecord
type_champs.fetch(:communes),
type_champs.fetch(:departements),
type_champs.fetch(:regions),
type_champs.fetch(:epci)
type_champs.fetch(:epci),
type_champs.fetch(:address)
]

PRIVATE_ONLY_TYPES = [
Expand Down
14 changes: 13 additions & 1 deletion spec/components/conditions/champs_conditions_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
end

context 'when there are upper tdcs but not managed' do
let(:upper_tdcs) { [build(:type_de_champ_address)] }
let(:upper_tdcs) { [build(:type_de_champ_email)] }

it { expect(page).not_to have_text('Logique conditionnelle') }
end
Expand Down Expand Up @@ -130,6 +130,18 @@
expect(page).to have_select('type_de_champ[condition_form][rows][][value]', options: (['Sélectionner'] + region_options))
end
end

context 'address' do
let(:address) { create(:type_de_champ_address) }
let(:upper_tdcs) { [address] }
let(:condition) { empty_operator(champ_value(address.stable_id), constant(true)) }
let(:departement_options) { APIGeoService.departements.map { "#{_1[:code]}#{_1[:name]}" } }

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'] + departement_options))
end
end
end

context 'and 2 conditions' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
end

context 'when the targeted_champ is unmanaged' do
let(:tdc) { create(:type_de_champ_address) }
let(:tdc) { create(:type_de_champ_email) }
let(:source_tdcs) { [tdc] }
let(:conditions) { [ds_eq(champ_value(tdc.stable_id), constant(1))] }

it do
expect(page).to have_css('.errors-summary')
expect(page).to have_content("Le champ « #{tdc.libelle} » est de type « adresse » et ne peut pas être utilisé comme champ cible.")
expect(page).to have_content("Le champ « #{tdc.libelle} » est de type « adresse électronique » et ne peut pas être utilisé comme champ cible.")
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,26 @@ def remove_instructeur(instructeur)
expect(procedure3.routing_enabled).to be_truthy
end
end

context 'with an address type de champ' do
let!(:procedure3) do
create(:procedure,
types_de_champ_public: [{ type: :address }],
administrateurs: [admin])
end

let!(:address_tdc) { procedure3.draft_revision.types_de_champ.first }

before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: address_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("01 – Ain")
expect(procedure3.reload.defaut_groupe_instructeur.routing_rule).to eq(ds_in_departement(champ_value(address_tdc.stable_id), constant('01')))
expect(procedure3.routing_enabled).to be_truthy
end
end
end

describe '#wizard' do
Expand Down
24 changes: 24 additions & 0 deletions spec/models/routing_engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@
end
end

context 'with an address type de champ' do
let(:procedure) do
create(:procedure, types_de_champ_public: [{ type: :address }]).tap do |p|
p.groupe_instructeurs.create(label: 'a third group')
end
end

let(:address_tdc) { procedure.draft_revision.types_de_champ.first }

context 'with a matching rule' do
before do
gi_2.update(routing_rule: ds_in_departement(champ_value(address_tdc.stable_id), constant('42')))
dossier.champs.first.update_columns(
value: "2 rue de l'Hôtel de Ville 42000 Saint-Étienne",
data: { department_code: '42', region_code: '83' }
)
end

it do
is_expected.to eq(gi_2)
end
end
end

context 'routing rules priorities' do
let(:procedure) do
create(:procedure,
Expand Down

0 comments on commit 8ec2b38

Please sign in to comment.