Merge pull request #9798 from demarches-simplifiees/add-new-operators-to-conditional

Etq admin je peux conditionner/router avec les opérateurs "est dans mon département",  "n'est pas dans mon département",  "est dans ma région" et   "n'est pas dans ma région".
This commit is contained in:
Eric Leroy-Terquem 2023-12-19 16:10:59 +00:00 committed by GitHub
commit 954facdaff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 257 additions and 61 deletions

View file

@ -23,7 +23,7 @@
%td.far-left= far_left_tag(row_index)
%td.target= left_operand_tag(targeted_champ, row_index)
%td.operator= operator_tag(operator_name, targeted_champ, row_index)
%td.value= right_operand_tag(targeted_champ, value, row_index)
%td.value= right_operand_tag(targeted_champ, value, row_index, operator_name)
%td.delete-column= delete_condition_tag(row_index)
.flex.justify-end.mt-2= add_condition_tag

View file

@ -97,6 +97,20 @@ class Conditions::ConditionsComponent < ApplicationComponent
[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)
[
[t(InDepartementOperator.name, scope: 'logic.operators'), InDepartementOperator.name],
[t(NotInDepartementOperator.name, scope: 'logic.operators'), NotInDepartementOperator.name],
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name],
[t(NotInRegionOperator.name, scope: 'logic.operators'), NotInRegionOperator.name]
]
when ChampValue::CHAMP_VALUE_TYPE.fetch(:departement_enum)
[
[t('is', scope: 'logic'), Eq.name],
[t('is_not', scope: 'logic'), NotEq.name],
[t(InRegionOperator.name, scope: 'logic.operators'), InRegionOperator.name],
[t(NotInRegionOperator.name, scope: 'logic.operators'), NotInRegionOperator.name]
]
when ChampValue::CHAMP_VALUE_TYPE.fetch(:enums)
[
[t(IncludeOperator.name, scope: 'logic.operators'), IncludeOperator.name],
@ -111,7 +125,7 @@ class Conditions::ConditionsComponent < ApplicationComponent
end
end
def right_operand_tag(left, right, row_index)
def right_operand_tag(left, right, row_index, operator_name)
right_invalid = !current_right_valid?(left, right)
case left.type(@source_tdcs)
@ -135,8 +149,8 @@ class Conditions::ConditionsComponent < ApplicationComponent
id: input_id_for('value', row_index),
class: 'fr-select'
)
when :enum, :enums
enums_for_select = left.options(@source_tdcs)
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum
enums_for_select = left.options(@source_tdcs, operator_name)
if right_invalid
enums_for_select = empty_target_for_select + enums_for_select

View file

@ -26,6 +26,6 @@
%td.far-left= far_left_tag(row_index)
%td.target= left_operand_tag(targeted_champ, row_index)
%td.operator= operator_tag(operator_name, targeted_champ, row_index)
%td.value= right_operand_tag(targeted_champ, value, row_index)
%td.value= right_operand_tag(targeted_champ, value, row_index, operator_name)
%td.delete-column= delete_condition_tag(row_index)
.flex.justify-end.mt-2= add_condition_tag

View file

@ -43,7 +43,7 @@ module Administrateurs
tdc = @procedure.active_revision.routable_types_de_champ.find { |tdc| tdc.stable_id == stable_id }
case tdc.type_champ
when TypeDeChamp.type_champs.fetch(:communes), TypeDeChamp.type_champs.fetch(:departements), TypeDeChamp.type_champs.fetch(:epci)
when TypeDeChamp.type_champs.fetch(:departements)
tdc_options = APIGeoService.departements.map { ["#{_1[:code]} #{_1[:name]}", _1[:code]] }
create_groups_from_territorial_tdc(tdc_options, stable_id)
when TypeDeChamp.type_champs.fetch(:regions)

View file

@ -1,5 +1,5 @@
class Champs::CommuneChamp < Champs::TextChamp
store_accessor :value_json, :code_departement, :code_postal
store_accessor :value_json, :code_departement, :code_postal, :code_region
before_save :on_codes_change, if: :should_refresh_after_code_change?
def for_export
@ -82,6 +82,7 @@ class Champs::CommuneChamp < Champs::TextChamp
if commune.present?
self.code_departement = commune[:departement_code]
self.code_region = commune[:region_code]
self.value = commune[:name]
else
self.code_departement = nil

View file

@ -1,4 +1,5 @@
class Champs::DepartementChamp < Champs::TextChamp
store_accessor :value_json, :code_region
validate :value_in_departement_names, unless: -> { value.nil? }
validate :external_id_in_departement_codes, unless: -> { external_id.nil? }
@ -39,6 +40,10 @@ class Champs::DepartementChamp < Champs::TextChamp
end
end
def code_region(departement_name)
APIGeoService.region_code_by_departement(name)
end
def value=(code)
if [2, 3].include?(code&.size)
self.external_id = code

View file

@ -1,5 +1,5 @@
class Champs::EpciChamp < Champs::TextChamp
store_accessor :value_json, :code_departement
store_accessor :value_json, :code_departement, :code_region
before_validation :on_departement_change
validate :code_departement_in_departement_codes, unless: -> { code_departement.nil? }
@ -42,6 +42,10 @@ class Champs::EpciChamp < Champs::TextChamp
external_id
end
def code_region
APIGeoService.region_code_by_departement(departement_name)
end
def selected
code
end
@ -74,6 +78,7 @@ class Champs::EpciChamp < Champs::TextChamp
if code_departement_changed?
self.external_id = nil
self.value = nil
self.code_region = code_region
end
end

View file

@ -8,8 +8,26 @@ module Logic
end
def self.class_from_name(name)
[ChampValue, Constant, Empty, LessThan, LessThanEq, Eq, NotEq, GreaterThanEq, GreaterThan, EmptyOperator, IncludeOperator, ExcludeOperator, And, Or]
.find { |c| c.name == name }
[
ChampValue,
Constant,
Empty,
LessThan,
LessThanEq,
Eq,
NotEq,
GreaterThanEq,
GreaterThan,
EmptyOperator,
IncludeOperator,
ExcludeOperator,
And,
Or,
InDepartementOperator,
NotInDepartementOperator,
InRegionOperator,
NotInRegionOperator
].find { |c| c.name == name }
end
def self.ensure_compatibility_from_left(condition, type_de_champs)
@ -24,6 +42,10 @@ module Logic
operator_class = EmptyOperator
in [:enum, _]
operator_class = Eq
in [:commune_enum, _] | [:epci_enum, _]
operator_class = InDepartementOperator
in [:departement_enum, _]
operator_class = Eq
in [:enums, _]
operator_class = IncludeOperator
in [:number, EmptyOperator]
@ -37,7 +59,7 @@ module Logic
Constant.new(true)
when :empty
Empty.new
when :enum, :enums
when :enum, :enums, :commune_enum, :epci_enum, :departement_enum
Constant.new(left.options(type_de_champs).first.second)
when :number
Constant.new(0)
@ -51,7 +73,7 @@ module Logic
case [left.type(type_de_champs), right.type(type_de_champs)]
in [a, ^a] # syntax for same type
true
in [:enum, :string] | [:enums, :string]
in [:enum, :string] | [:enums, :string] | [:commune_enum, :string] | [:epci_enum, :string] | [:departement_enum, :string]
true
else
false
@ -88,6 +110,14 @@ module Logic
def ds_include(left, right) = Logic::IncludeOperator.new(left, right)
def ds_in_departement(left, right) = Logic::InDepartementOperator.new(left, right)
def ds_not_in_departement(left, right) = Logic::NotInDepartementOperator.new(left, right)
def ds_in_region(left, right) = Logic::InRegionOperator.new(left, right)
def ds_not_in_region(left, right) = Logic::NotInRegionOperator.new(left, right)
def ds_exclude(left, right) = Logic::ExcludeOperator.new(left, right)
def constant(value) = Logic::Constant.new(value)

View file

@ -16,6 +16,9 @@ class Logic::ChampValue < Logic::Term
boolean: :boolean, # from yes_no or checkbox champ
number: :number, # from integer or decimal number champ
enum: :enum, # a choice from a dropdownlist
commune_enum: :commune_enum,
epci_enum: :epci_enum,
departement_enum: :departement_enum,
enums: :enums, # multiple choice from a dropdownlist (multipledropdownlist)
empty: :empty,
unmanaged: :unmanaged
@ -55,6 +58,17 @@ class Logic::ChampValue < Logic::Term
end
end
def compute_value_json(champs)
targeted_champ = champ(champs)
return nil if !targeted_champ.visible?
return nil if targeted_champ.blank? & !targeted_champ.drop_down_other?
if targeted_champ.type.in?(["Champs::CommuneChamp", "Champs::EpciChamp", "Champs::DepartementChamp"])
targeted_champ.value_json
end
end
def to_s(type_de_champs) = type_de_champ(type_de_champs)&.libelle # TODO: gerer le cas ou un tdc est supprimé
def type(type_de_champs)
@ -65,11 +79,14 @@ 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(:communes),
MANAGED_TYPE_DE_CHAMP.fetch(:epci),
MANAGED_TYPE_DE_CHAMP.fetch(:departements),
MANAGED_TYPE_DE_CHAMP.fetch(:regions)
CHAMP_VALUE_TYPE.fetch(:enum)
when MANAGED_TYPE_DE_CHAMP.fetch(:communes)
CHAMP_VALUE_TYPE.fetch(:commune_enum)
when MANAGED_TYPE_DE_CHAMP.fetch(:epci)
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(:multiple_drop_down_list)
CHAMP_VALUE_TYPE.fetch(:enums)
else
@ -100,14 +117,13 @@ class Logic::ChampValue < Logic::Term
self.class == other.class && @stable_id == other.stable_id
end
def options(type_de_champs)
def options(type_de_champs, operator_name = nil)
tdc = type_de_champ(type_de_champs)
case tdc.type_champ
when MANAGED_TYPE_DE_CHAMP.fetch(:communes), MANAGED_TYPE_DE_CHAMP.fetch(:epci), MANAGED_TYPE_DE_CHAMP.fetch(:departements)
APIGeoService.departements.map { ["#{_1[:code]} #{_1[:name]}", _1[:code]] }
when MANAGED_TYPE_DE_CHAMP.fetch(:regions)
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)])
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] }
end

View file

@ -0,0 +1,18 @@
class Logic::InDepartementOperator < Logic::BinaryOperator
def operation
:est_dans_le_departement
end
def compute(champs = [])
l = @left.compute_value_json(champs)
r = @right.compute(champs)
return false if l.nil?
l.fetch("code_departement") == r
end
def errors(type_de_champs = [])
@left.errors(type_de_champs) + @right.errors(type_de_champs)
end
end

View file

@ -0,0 +1,18 @@
class Logic::InRegionOperator < Logic::BinaryOperator
def operation
:est_dans_la_region
end
def compute(champs)
l = @left.compute_value_json(champs)
r = @right.compute(champs)
return false if l.nil?
l.fetch("code_region") == r
end
def errors(type_de_champs = [])
@left.errors(type_de_champs) + @right.errors(type_de_champs)
end
end

View file

@ -0,0 +1,14 @@
class Logic::NotInDepartementOperator < Logic::InDepartementOperator
def operation
:n_est_pas_dans_le_departement
end
def compute(champs = [])
l = @left.compute_value_json(champs)
r = @right.compute(champs)
return false if l.nil?
l.fetch("code_departement") != r
end
end

View file

@ -0,0 +1,14 @@
class Logic::NotInRegionOperator < Logic::InRegionOperator
def operation
:n_est_pas_dans_la_region
end
def compute(champs)
l = @left.compute_value_json(champs)
r = @right.compute(champs)
return false if l.nil?
l.fetch("code_region") != r
end
end

View file

@ -15,3 +15,7 @@ fr:
'Logic::NotEq': Nest pas
'Logic::IncludeOperator': Contient
'Logic::ExcludeOperator': Ne contient pas
'Logic::InDepartementOperator': Est dans le département
'Logic::NotInDepartementOperator': N'est pas dans le département
'Logic::InRegionOperator': Est dans la région
'Logic::NotInRegionOperator': N'est pas dans la région

View file

@ -734,46 +734,6 @@ describe Administrateurs::GroupeInstructeursController, type: :controller do
end
end
context 'with a communes type de champ' do
let!(:procedure3) do
create(:procedure,
types_de_champ_public: [{ type: :communes }],
administrateurs: [admin])
end
let!(:communes_tdc) { procedure3.draft_revision.types_de_champ.first }
before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: communes_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_eq(champ_value(communes_tdc.stable_id), constant('01')))
expect(procedure3.routing_enabled).to be_truthy
end
end
context 'with a epci type de champ' do
let!(:procedure3) do
create(:procedure,
types_de_champ_public: [{ type: :epci }],
administrateurs: [admin])
end
let!(:epci_tdc) { procedure3.draft_revision.types_de_champ.first }
before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: epci_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_eq(champ_value(epci_tdc.stable_id), constant('01')))
expect(procedure3.routing_enabled).to be_truthy
end
end
context 'with a departements type de champ' do
let!(:procedure3) do
create(:procedure,

View file

@ -110,7 +110,7 @@ describe Logic::ChampValue do
context 'departement tdc' do
let(:champ) { create(:champ_departements, value: '02') }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:enum) }
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:departement_enum) }
it { is_expected.to eq('02') }
end

View file

@ -0,0 +1,19 @@
describe Logic::InDepartementOperator do
include Logic
let(:champ_commune) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
let(:champ_epci) { create(:champ_epci, code_departement: '02', code_region: "32") }
describe '#compute' do
context 'commune' do
it { expect(ds_in_departement(champ_value(champ_commune.stable_id), constant('92')).compute([champ_commune])).to be(true) }
end
context 'epci' do
it do
champ_epci.update_columns(external_id: "200071991", value: "CC Retz en Valois")
expect(ds_in_departement(champ_value(champ_epci.stable_id), constant('02')).compute([champ_epci])).to be(true)
end
end
end
end

View file

@ -0,0 +1,24 @@
describe Logic::InRegionOperator do
include Logic
let(:champ_commune) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
let(:champ_epci) { create(:champ_epci, code_departement: '02', code_region: "32") }
let(:champ_departement) { create(:champ_departements, value: '01', code_region: '84') }
describe '#compute' do
context 'commune' do
it { expect(ds_in_region(champ_value(champ_commune.stable_id), constant('11')).compute([champ_commune])).to be(true) }
end
context 'epci' do
it do
champ_epci.update_columns(external_id: "200071991", value: "CC Retz en Valois")
expect(ds_in_region(champ_value(champ_epci.stable_id), constant('32')).compute([champ_epci])).to be(true)
end
end
context 'departement' do
it { expect(ds_in_region(champ_value(champ_departement.stable_id), constant('84')).compute([champ_departement])).to be(true) }
end
end
end

View file

@ -0,0 +1,23 @@
describe Logic::NotInDepartementOperator do
include Logic
let(:champ_commune) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
let(:champ_epci) { create(:champ_epci, code_departement: '02', code_region: "32") }
describe '#compute' do
context 'commune' do
it do
expect(ds_not_in_departement(champ_value(champ_commune.stable_id), constant('93')).compute([champ_commune])).to be(true)
expect(ds_not_in_departement(champ_value(champ_commune.stable_id), constant('92')).compute([champ_commune])).to be(false)
end
end
context 'epci' do
it do
champ_epci.update_columns(external_id: "200071991", value: "CC Retz en Valois")
expect(ds_not_in_departement(champ_value(champ_epci.stable_id), constant('02')).compute([champ_epci])).to be(false)
expect(ds_not_in_departement(champ_value(champ_epci.stable_id), constant('03')).compute([champ_epci])).to be(true)
end
end
end
end

View file

@ -0,0 +1,31 @@
describe Logic::NotInRegionOperator do
include Logic
let(:champ_commune) { create(:champ_communes, code_postal: '92500', external_id: '92063') }
let(:champ_epci) { create(:champ_epci, code_departement: '02', code_region: "32") }
let(:champ_departement) { create(:champ_departements, value: '01', code_region: '84') }
describe '#compute' do
context 'commune' do
it do
expect(ds_not_in_region(champ_value(champ_commune.stable_id), constant('11')).compute([champ_commune])).to be(false)
expect(ds_not_in_region(champ_value(champ_commune.stable_id), constant('32')).compute([champ_commune])).to be(true)
end
end
context 'epci' do
it do
champ_epci.update_columns(external_id: "200071991", value: "CC Retz en Valois")
expect(ds_not_in_region(champ_value(champ_epci.stable_id), constant('32')).compute([champ_epci])).to be(false)
expect(ds_not_in_region(champ_value(champ_epci.stable_id), constant('11')).compute([champ_epci])).to be(true)
end
end
context 'departement' do
it do
expect(ds_not_in_region(champ_value(champ_departement.stable_id), constant('84')).compute([champ_departement])).to be(false)
expect(ds_not_in_region(champ_value(champ_departement.stable_id), constant('32')).compute([champ_departement])).to be(true)
end
end
end
end