chore(routing): update routing_rule validations

This commit is contained in:
Eric Leroy-Terquem 2023-10-13 15:00:07 +02:00
parent 22feb48c29
commit cf80fd03e1
2 changed files with 26 additions and 7 deletions

View file

@ -4,7 +4,11 @@
data: { turbo: true, controller: 'autosave' },
class: 'form width-100' do
.conditionnel.mt-2.width-100
.flex
- if @groupe_instructeur.invalid_rule?
%p.fr-mb-1w.fr-badge.fr-badge--warning.fr-badge--sm règle invalide
- elsif @groupe_instructeur.non_unique_rule?
%p.fr-mb-1w.fr-badge.fr-badge--warning.fr-badge--sm règle déjà attribuée à #{@groupe_instructeur.groups_with_same_rule}
%table.condition-table.mt-2
%thead
%tr

View file

@ -84,7 +84,15 @@ class GroupeInstructeur < ApplicationRecord
def valid_rule?
return false if routing_rule.nil?
([routing_rule.left, routing_rule, routing_rule.right] in [ChampValue, Eq | NotEq, Constant]) && routing_rule_matches_tdc?
if [And, Or].include?(routing_rule.class)
routing_rule.operands.all? { |rule_line| valid_rule_line?(rule_line) }
else
valid_rule_line?(routing_rule)
end
end
def valid_rule_line?(rule)
([rule.left, rule, rule.right] in [ChampValue, (LessThan | LessThanEq | Eq | NotEq | GreaterThanEq | GreaterThan | IncludeOperator), Constant]) && routing_rule_matches_tdc?(rule)
end
def non_unique_rule?
@ -95,7 +103,8 @@ class GroupeInstructeur < ApplicationRecord
def groups_with_same_rule
return if routing_rule.nil?
other_groupe_instructeurs
.filter { |gi| !gi.routing_rule.nil? && gi.routing_rule.right != empty && gi.routing_rule == routing_rule }
.filter { _1.routing_rule.present? }
.filter { _1.routing_rule == routing_rule }
.map(&:label)
.join(', ')
end
@ -106,18 +115,24 @@ class GroupeInstructeur < ApplicationRecord
private
def routing_rule_matches_tdc?
routing_tdc = procedure.active_revision.types_de_champ.find_by(stable_id: routing_rule.left.stable_id)
def routing_rule_matches_tdc?(rule)
routing_tdc = procedure.active_revision.types_de_champ.find_by(stable_id: rule.left.stable_id)
options = case routing_tdc.type_champ
when TypeDeChamp.type_champs.fetch(:communes), TypeDeChamp.type_champs.fetch(:departements), TypeDeChamp.type_champs.fetch(:epci)
APIGeoService.departements.map { _1[:code] }
when TypeDeChamp.type_champs.fetch(:regions)
APIGeoService.regions.map { _1[:code] }
when TypeDeChamp.type_champs.fetch(:drop_down_list)
when TypeDeChamp.type_champs.fetch(:drop_down_list), TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)
routing_tdc.drop_down_list_enabled_non_empty_options(other: true).map { _1.is_a?(Array) ? _1.last : _1 }
when TypeDeChamp.type_champs.fetch(:checkbox), TypeDeChamp.type_champs.fetch(:yes_no)
[true, false]
when TypeDeChamp.type_champs.fetch(:integer_number)
return rule.right.value.is_a? Integer
when TypeDeChamp.type_champs.fetch(:decimal_number)
return rule.right.value.is_a? Float
end
routing_rule.right.value.in?(options)
rule.right.value.in?(options)
end
serialize :routing_rule, LogicSerializer