Skip to content

Commit

Permalink
Merge pull request #9406 from demarches-simplifiees/can-route-from-de…
Browse files Browse the repository at this point in the history
…partements-champ

ETQ admin je peux router depuis un champ département
  • Loading branch information
E-L-T authored Aug 30, 2023
2 parents afed0df + b1c28e1 commit 69eafa4
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fr:
Le routage permet d’acheminer les dossiers vers différents groupes d’instructeurs.
routing_configuration_notice_2_html: |
<p>Pour le configurer, votre formulaire doit comporter
au moins un champ « choix simple ».</p>
au moins un champ de type « choix simple » ou « départements ».</p>
<p>Ajoutez ce champ dans la page <a href="%{path}">« Configuration des champs »</a>.</p>
delete_title: Aucun dossier ne sera supprimé. Les groupes d'instructeurs vont être supprimés. Seuls les instructeurs du groupe « %{defaut_label} » resteront affectés à la démarche.
delete_confirmation: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class: 'fr-btn',
method: :delete,
title: t('.delete_title', defaut_label: @procedure.defaut_groupe_instructeur.label),
data: ( @procedure.publiee? ? { disable_with: "Suppression...", confirm: t('.delete_confirmation', defaut_label: @procedure.defaut_groupe_instructeur.label) } : nil)
data: ( @procedure.publiee? ? { disable_with: "Suppression", confirm: t('.delete_confirmation', defaut_label: @procedure.defaut_groupe_instructeur.label) } : nil)

- elsif @state == 'choix'
= form_for :choice,
Expand Down
16 changes: 13 additions & 3 deletions app/components/procedure/one_groupe_management_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ def values_for_select(targeted_champ)

def available_values_for_select(targeted_champ)
return [] if targeted_champ.is_a?(Logic::Empty)
targeted_champ
.options(@revision.types_de_champ_public)
.map { |(label, value)| [label, constant(value).to_json] }

case @revision.types_de_champ_public.find_by(stable_id: targeted_champ.stable_id).type_champ
when TypeDeChamp.type_champs.fetch(:departements)
departements_for_select
when TypeDeChamp.type_champs.fetch(:drop_down_list)
targeted_champ
.options(@revision.types_de_champ_public)
.map { |(label, value)| [label, constant(value).to_json] }
end
end

def departements_for_select
APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", constant(_1[:code]).to_json] }
end
end
26 changes: 19 additions & 7 deletions app/controllers/administrateurs/groupe_instructeurs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,26 @@ def create_simple_routing

tdc = @procedure.active_revision.routable_types_de_champ.find { |tdc| tdc.stable_id == stable_id }

tdc_options = tdc.drop_down_options.reject(&:empty?)
case tdc.type_champ
when TypeDeChamp.type_champs.fetch(:departements)
tdc_options = APIGeoService.departements.map { ["#{_1[:code]}#{_1[:name]}", _1[:code]] }
tdc_options.each do |code_and_name, code|
routing_rule = ds_eq(champ_value(stable_id), constant(code))
@procedure
.groupe_instructeurs
.find_or_create_by(label: code_and_name)
.update(instructeurs: [current_administrateur.instructeur], routing_rule:)
end

tdc_options.each do |option_label|
routing_rule = ds_eq(champ_value(stable_id), constant(option_label))
@procedure
.groupe_instructeurs
.find_or_create_by(label: option_label)
.update(instructeurs: [current_administrateur.instructeur], routing_rule:)
when TypeDeChamp.type_champs.fetch(:drop_down_list)
tdc_options = tdc.drop_down_options.reject(&:empty?)
tdc_options.each do |option_label|
routing_rule = ds_eq(champ_value(stable_id), constant(option_label))
@procedure
.groupe_instructeurs
.find_or_create_by(label: option_label)
.update(instructeurs: [current_administrateur.instructeur], routing_rule:)
end
end

if tdc.drop_down_other?
Expand Down
8 changes: 7 additions & 1 deletion app/models/groupe_instructeur.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ def other_groupe_instructeurs

def routing_rule_matches_tdc?
routing_tdc = procedure.active_revision.types_de_champ.find_by(stable_id: routing_rule.left.stable_id)
options = routing_tdc.options_with_drop_down_other

options = case routing_tdc.type_champ
when TypeDeChamp.type_champs.fetch(:departements)
APIGeoService.departements.map { _1[:code] }
when TypeDeChamp.type_champs.fetch(:drop_down_list)
routing_tdc.options_with_drop_down_other
end
routing_rule.right.value.in?(options)
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/logic/champ_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def compute(champs)
targeted_champ.selected
when "Champs::MultipleDropDownListChamp"
targeted_champ.selected_options
when "Champs::DepartementChamp"
targeted_champ.code
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/procedure_revision.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def coordinate_and_tdc(stable_id)
end

def routable_types_de_champ
types_de_champ_public.filter { |tdc| [:drop_down_list].include?(tdc.type_champ.to_sym) }
types_de_champ_public.filter { |tdc| [:drop_down_list, :departements].include?(tdc.type_champ.to_sym) }
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
%li
= link_to 'Retour', options_admin_procedure_groupe_instructeurs_path(@procedure, state: :choix), class: 'fr-btn fr-btn--secondary'
%li
%button.fr-btn{ disabled: true, data: { 'radio-enabled-submit-target': 'submit' } } Créer les groupes
%button.fr-btn{ disabled: true, data: { disable_with: 'Création des groupes…', 'radio-enabled-submit-target': 'submit' } } Créer les groupes
Original file line number Diff line number Diff line change
Expand Up @@ -719,25 +719,47 @@ def remove_instructeur(instructeur)
end

describe '#create_simple_routing' do
let!(:procedure3) do
create(:procedure,
types_de_champ_public: [
{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] },
{ type: :text, libelle: 'Un champ texte' }
],
administrateurs: [admin])
context 'with a drop_down_list type de champ' do
let!(:procedure3) do
create(:procedure,
types_de_champ_public: [
{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] },
{ type: :text, libelle: 'Un champ texte' }
],
administrateurs: [admin])
end

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

before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: drop_down_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 match_array(['Paris', 'Lyon', 'Marseille'])
expect(procedure3.reload.defaut_groupe_instructeur.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
expect(procedure3.routing_enabled).to be_truthy
end
end

let!(:drop_down_tdc) { procedure3.draft_revision.types_de_champ.first }
context 'with a departements type de champ' do
let!(:procedure3) do
create(:procedure,
types_de_champ_public: [{ type: :departements }],
administrateurs: [admin])
end

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

before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: drop_down_tdc.stable_id } } }
before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: departements_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 match_array(['Paris', 'Lyon', 'Marseille'])
expect(procedure3.reload.defaut_groupe_instructeur.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
expect(procedure3.routing_enabled).to be_truthy
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(departements_tdc.stable_id), constant('01')))
expect(procedure3.routing_enabled).to be_truthy
end
end
end

Expand Down
12 changes: 12 additions & 0 deletions spec/models/procedure_revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -941,4 +941,16 @@ def second_champ = procedure.draft_revision.types_de_champ_public.second
expect(type_de_champ.revisions.count).to eq(1)
}
end

describe '#routable_types_de_champ' do
let(:procedure) do
create(:procedure).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :text, libelle: 'l1')
p.draft_revision.add_type_de_champ(type_champ: :drop_down_list, libelle: 'l2')
p.draft_revision.add_type_de_champ(type_champ: :departements, libelle: 'l3')
end
end

it { expect(draft.routable_types_de_champ.pluck(:libelle)).to eq(['l2', 'l3']) }
end
end
88 changes: 55 additions & 33 deletions spec/models/routing_engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
include Logic

describe '.compute' do
let(:procedure) do
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') }
Expand All @@ -21,43 +11,75 @@
dossier.groupe_instructeur
end

context 'without any rules' do
it { is_expected.to eq(defaut_groupe) }
end
context 'with a drop down list type de champ' do
let(:procedure) do
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 }

context 'without any matching rules' do
before do
procedure.groupe_instructeurs.each do |gi|
gi.update(routing_rule: constant(false))
context 'without any rules' do
it { is_expected.to eq(defaut_groupe) }
end

context 'without any matching rules' do
before do
procedure.groupe_instructeurs.each do |gi|
gi.update(routing_rule: constant(false))
end
end

it { is_expected.to eq(defaut_groupe) }
end

it { is_expected.to eq(defaut_groupe) }
end
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 rules not configured yet' do
before do
procedure.groupe_instructeurs.each do |gi|
gi.update(routing_rule: ds_eq(empty, empty))
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

it { is_expected.to eq(defaut_groupe) }
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) }
end
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')
context 'with a departements type de champ' do
let(:procedure) do
create(:procedure, types_de_champ_public: [{ type: :departements }]).tap do |p|
p.groupe_instructeurs.create(label: 'a second group')
p.groupe_instructeurs.create(label: 'a third group')
end
end

it { is_expected.to eq(gi_2) }
end
let(:departements_tdc) { procedure.draft_revision.types_de_champ.first }

context 'with a closed gi with a matching rule' do
before { gi_2.update(routing_rule: constant(true), closed: true) }
context 'with a matching rule' do
before do
gi_2.update(routing_rule: ds_eq(champ_value(departements_tdc.stable_id), constant('43')))
dossier.champs.first.update(value: 'Haute-Loire')
end

it { is_expected.to eq(defaut_groupe) }
it { is_expected.to eq(gi_2) }
end
end
end
end

0 comments on commit 69eafa4

Please sign in to comment.