From e8d687f5e7542b6185b64c4c3bd1f39344e4b555 Mon Sep 17 00:00:00 2001 From: Eric Leroy-Terquem Date: Thu, 25 May 2023 14:53:51 +0200 Subject: [PATCH] fix(routing): routing engine works even with unconfigured groups --- app/models/routing_engine.rb | 2 +- spec/models/routing_engine_spec.rb | 30 ++++++++++++++----- .../routing/rules_full_scenario_spec.rb | 21 +++++++++---- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/app/models/routing_engine.rb b/app/models/routing_engine.rb index ca3e9e5ff..b08cc4bf8 100644 --- a/app/models/routing_engine.rb +++ b/app/models/routing_engine.rb @@ -2,7 +2,7 @@ module RoutingEngine def self.compute(dossier) return if !dossier.procedure.feature_enabled?(:routing_rules) - matching_groupe = dossier.procedure.groupe_instructeurs.active.find do |gi| + matching_groupe = dossier.procedure.groupe_instructeurs.active.reject(&:routing_to_configure?).find do |gi| gi.routing_rule&.compute(dossier.champs) end matching_groupe ||= dossier.procedure.defaut_groupe_instructeur diff --git a/spec/models/routing_engine_spec.rb b/spec/models/routing_engine_spec.rb index 36a072720..cf90dddc8 100644 --- a/spec/models/routing_engine_spec.rb +++ b/spec/models/routing_engine_spec.rb @@ -5,12 +5,15 @@ describe RoutingEngine, type: :model do describe '.compute' do let(:procedure) do - create(:procedure).tap do |p| - p.groupe_instructeurs.create(label: 'a second group') - p.groupe_instructeurs.create(label: 'a third group') - end + create(:procedure, + types_de_champ_public: [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }]).tap do |p| + p.groupe_instructeurs.create(label: 'a second group') + p.groupe_instructeurs.create(label: 'a third group') + end end + let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first } + let(:dossier) { create(:dossier, procedure:) } let(:defaut_groupe) { procedure.defaut_groupe_instructeur } let(:gi_2) { procedure.groupe_instructeurs.find_by(label: 'a second group') } @@ -34,13 +37,26 @@ describe RoutingEngine, type: :model do it { is_expected.to eq(defaut_groupe) } end - context 'with a matching rules' do - before { gi_2.update(routing_rule: constant(true)) } + context 'with rules not configured yet' do + before do + procedure.groupe_instructeurs.each do |gi| + gi.update(routing_rule: ds_eq(empty, empty)) + end + end + + it { is_expected.to eq(defaut_groupe) } + end + + context 'with a matching rule' do + before do + gi_2.update(routing_rule: ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon'))) + dossier.champs.first.update(value: 'Lyon') + end it { is_expected.to eq(gi_2) } end - context 'with a closed gi with a matching rules' do + context 'with a closed gi with a matching rule' do before { gi_2.update(routing_rule: constant(true), closed: true) } it { is_expected.to eq(defaut_groupe) } diff --git a/spec/system/routing/rules_full_scenario_spec.rb b/spec/system/routing/rules_full_scenario_spec.rb index 9abd380d3..93b487e10 100644 --- a/spec/system/routing/rules_full_scenario_spec.rb +++ b/spec/system/routing/rules_full_scenario_spec.rb @@ -10,13 +10,14 @@ describe 'The routing with rules', js: true do p.draft_revision.add_type_de_champ( type_champ: :drop_down_list, libelle: 'Spécialité', - options: { "drop_down_other" => "0", "drop_down_options" => ["", "littéraire", "scientifique"] } + options: { "drop_down_other" => "0", "drop_down_options" => ["", "littéraire", "scientifique", "artistique"] } ) end end let(:administrateur) { create(:administrateur, procedures: [procedure]) } let(:scientifique_user) { create(:user, password: password) } let(:litteraire_user) { create(:user, password: password) } + let(:artistique_user) { create(:user, password: password) } before do Flipper.enable(:routing_rules, procedure) @@ -35,14 +36,14 @@ describe 'The routing with rules', js: true do click_on 'Créer les groupes' expect(page).to have_text('Gestion des groupes') - expect(page).to have_text('2 groupes') + expect(page).to have_text('3 groupes') expect(page).not_to have_text('À configurer') click_on 'littéraire' expect(page).to have_select("targeted_champ", selected: "Spécialité") expect(page).to have_select("value", selected: "littéraire") - click_on '2 groupes' + click_on '3 groupes' click_on 'scientifique' expect(page).to have_select("targeted_champ", selected: "Spécialité") @@ -91,7 +92,7 @@ describe 'The routing with rules', js: true do fill_in 'Nom du groupe', with: 'scientifique' click_on 'Renommer' expect(page).to have_text('Le nom est à présent « scientifique ». ') - # + # add marie to scientifique groupe fill_in 'Emails', with: 'marie@inst.com' perform_enqueued_jobs { click_on 'Affecter' } @@ -103,7 +104,7 @@ describe 'The routing with rules', js: true do fill_in 'Emails', with: 'superwoman@inst.com' perform_enqueued_jobs { click_on 'Affecter' } expect(page).to have_text("L’instructeur superwoman@inst.com a été affecté") - # + # add routing rules within('.target') { select('Spécialité') } within('.value') { select('scientifique') } @@ -117,13 +118,21 @@ describe 'The routing with rules', js: true do procedure.groupe_instructeurs.where(closed: false).each { |gi| wait_until { gi.reload.routing_rule.present? } } + # add a group without routing rules + click_on 'Ajout de groupes' + fill_in 'Nouveau groupe', with: 'artistique' + click_on 'Ajouter' + expect(page).to have_text('Le groupe d’instructeurs « artistique » a été créé. ') + expect(procedure.groupe_instructeurs.count).to eq(4) + # publish publish_procedure(procedure) log_out - # 2 users fill a dossier in each group + # 3 users fill a dossier in each group user_send_dossier(scientifique_user, 'scientifique') user_send_dossier(litteraire_user, 'littéraire') + user_send_dossier(artistique_user, 'artistique') # the litteraires instructeurs only manage the litteraires dossiers register_instructeur_and_log_in(victor.email)