Merge pull request #9406 from demarches-simplifiees/can-route-from-departements-champ
ETQ admin je peux router depuis un champ département
This commit is contained in:
commit
69eafa4dda
11 changed files with 153 additions and 67 deletions
|
@ -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: |
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -58,8 +58,18 @@ class Procedure::OneGroupeManagementComponent < ApplicationComponent
|
|||
|
||||
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
|
||||
|
|
|
@ -40,14 +40,26 @@ module Administrateurs
|
|||
|
||||
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?
|
||||
|
|
|
@ -105,7 +105,13 @@ class GroupeInstructeur < ApplicationRecord
|
|||
|
||||
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
|
||||
|
||||
|
|
|
@ -44,6 +44,8 @@ class Logic::ChampValue < Logic::Term
|
|||
targeted_champ.selected
|
||||
when "Champs::MultipleDropDownListChamp"
|
||||
targeted_champ.selected_options
|
||||
when "Champs::DepartementChamp"
|
||||
targeted_champ.code
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -224,7 +224,7 @@ class ProcedureRevision < ApplicationRecord
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -719,25 +719,47 @@ describe Administrateurs::GroupeInstructeursController, type: :controller do
|
|||
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
|
||||
|
||||
before { post :create_simple_routing, params: { procedure_id: procedure3.id, create_simple_routing: { stable_id: drop_down_tdc.stable_id } } }
|
||||
let!(:departements_tdc) { procedure3.draft_revision.types_de_champ.first }
|
||||
|
||||
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
|
||||
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 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
|
||||
|
||||
|
|
|
@ -941,4 +941,16 @@ describe ProcedureRevision do
|
|||
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
|
||||
|
|
|
@ -2,16 +2,6 @@ describe RoutingEngine, type: :model do
|
|||
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') }
|
||||
|
@ -21,43 +11,75 @@ describe RoutingEngine, type: :model do
|
|||
dossier.groupe_instructeur
|
||||
end
|
||||
|
||||
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))
|
||||
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
|
||||
|
||||
it { is_expected.to eq(defaut_groupe) }
|
||||
let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first }
|
||||
|
||||
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
|
||||
|
||||
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 rule' do
|
||||
before { gi_2.update(routing_rule: constant(true), closed: true) }
|
||||
|
||||
it { is_expected.to eq(defaut_groupe) }
|
||||
end
|
||||
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 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(defaut_groupe) }
|
||||
end
|
||||
let(:departements_tdc) { procedure.draft_revision.types_de_champ.first }
|
||||
|
||||
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 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(gi_2) }
|
||||
end
|
||||
|
||||
it { is_expected.to eq(gi_2) }
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue