chore(conditions): update controllers and routes

This commit is contained in:
Eric Leroy-Terquem 2023-10-13 11:31:41 +02:00
parent 979b5101ae
commit 4d95f49c82
6 changed files with 241 additions and 172 deletions

View file

@ -42,7 +42,7 @@ module Administrateurs
private private
def build_condition_component def build_condition_component
TypesDeChampEditor::ConditionsComponent.new( Conditions::ChampsConditionsComponent.new(
tdc: @tdc, tdc: @tdc,
upper_tdcs: @upper_tdcs, upper_tdcs: @upper_tdcs,
procedure_id: @procedure.id procedure_id: @procedure.id
@ -50,7 +50,7 @@ module Administrateurs
end end
def condition_form def condition_form
ConditionForm.new(condition_params.merge({ upper_tdcs: @upper_tdcs })) ConditionForm.new(condition_params.merge({ source_tdcs: @upper_tdcs }))
end end
def retrieve_coordinate_and_uppers def retrieve_coordinate_and_uppers

View file

@ -1,56 +0,0 @@
module Administrateurs
class RoutingController < AdministrateurController
include Logic
before_action :retrieve_procedure
def update
left = targeted_champ
right = targeted_champ_changed? ? empty : value
new_routing_rule = case operator_name
when Eq.name
ds_eq(left, right)
when NotEq.name
ds_not_eq(left, right)
end
groupe_instructeur.update!(routing_rule: new_routing_rule)
end
def update_defaut_groupe_instructeur
new_defaut = @procedure.groupe_instructeurs.find(defaut_groupe_instructeur_id)
@procedure.update!(defaut_groupe_instructeur: new_defaut)
end
private
def targeted_champ_changed?
targeted_champ != groupe_instructeur.routing_rule&.left
end
def targeted_champ
Logic.from_json(params[:targeted_champ])
end
def operator_name
params[:operator_name]
end
def value
Logic.from_json(params[:value])
end
def groupe_instructeur
@groupe_instructeur ||= @procedure.groupe_instructeurs.find(groupe_instructeur_id)
end
def groupe_instructeur_id
params[:groupe_instructeur_id]
end
def defaut_groupe_instructeur_id
params[:defaut_groupe_instructeur_id]
end
end
end

View file

@ -0,0 +1,79 @@
module Administrateurs
class RoutingRulesController < AdministrateurController
include Logic
before_action :retrieve_procedure, :retrieve_tdcs
before_action :retrieve_groupe_instructeur, except: [:update_defaut_groupe_instructeur]
def update
condition = condition_form.to_condition
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def add_row
condition = Logic.add_empty_condition_to(@groupe_instructeur.routing_rule)
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def delete_row
condition = condition_form.delete_row(row_index).to_condition
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def change_targeted_champ
condition = condition_form.change_champ(row_index).to_condition
@groupe_instructeur.update!(routing_rule: condition)
@routing_rule_component = build_routing_rule_component
end
def update_defaut_groupe_instructeur
new_defaut = @procedure.groupe_instructeurs.find(defaut_groupe_instructeur_id)
@procedure.update!(defaut_groupe_instructeur: new_defaut)
end
private
def groupe_instructeur_id
params[:groupe_instructeur_id]
end
def defaut_groupe_instructeur_id
params[:defaut_groupe_instructeur_id]
end
def build_routing_rule_component
Conditions::RoutingRulesComponent.new(
groupe_instructeur: @groupe_instructeur
)
end
def condition_form
ConditionForm.new(routing_rule_params.merge({ source_tdcs: @source_tdcs }))
end
def retrieve_tdcs
@source_tdcs = @procedure.active_revision.types_de_champ
end
def retrieve_groupe_instructeur
@groupe_instructeur = @procedure.groupe_instructeurs.find(groupe_instructeur_id)
end
def routing_rule_params
params
.require(:groupe_instructeur)
.require(:condition_form)
.permit(:top_operator_name, rows: [:targeted_champ, :operator_name, :value])
end
def row_index
params[:row_index].to_i
end
end
end

View file

@ -546,8 +546,13 @@ Rails.application.routes.draw do
delete :delete_row, on: :member delete :delete_row, on: :member
end end
patch :update, controller: 'routing', as: :routing_rules resources :routing_rules, only: [:update, :destroy], param: :groupe_instructeur_id do
patch :update_defaut_groupe_instructeur, controller: 'routing', as: :update_defaut_groupe_instructeur patch :add_row, on: :member
patch :change_targeted_champ, on: :member
delete :delete_row, on: :member
end
patch :update_defaut_groupe_instructeur, controller: 'routing_rules', as: :update_defaut_groupe_instructeur
put 'clone' put 'clone'
put 'archive' put 'archive'

View file

@ -1,112 +0,0 @@
describe Administrateurs::RoutingController, type: :controller do
include Logic
before { sign_in(procedure.administrateurs.first.user) }
describe '#update targeted champ' do
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }, { type: :text, libelle: 'Un champ texte' }]) }
let(:gi_2) { create(:groupe_instructeur, label: 'groupe 2', procedure: procedure) }
let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first }
let(:params) do
{
procedure_id: procedure.id,
targeted_champ: champ_value(drop_down_tdc.stable_id).to_json,
operator_name: operator_name,
value: empty.to_json,
groupe_instructeur_id: gi_2.id
}
end
context 'with Eq operator' do
let(:operator_name) { Logic::Eq.name }
before do
post :update, params: params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), empty))
end
context '#update value' do
let(:value_updated_params) { params.merge(value: constant('Lyon').to_json) }
before do
post :update, params: value_updated_params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
end
context 'targeted champ changed' do
let(:last_tdc) { procedure.draft_revision.types_de_champ.last }
before do
targeted_champ_updated_params = value_updated_params.merge(targeted_champ: champ_value(last_tdc.stable_id).to_json)
post :update, params: targeted_champ_updated_params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(last_tdc.stable_id), empty))
end
end
end
end
context 'with NotEq operator' do
let(:operator_name) { Logic::NotEq.name }
before do
post :update, params: params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_not_eq(champ_value(drop_down_tdc.stable_id), empty))
end
context '#update value' do
let(:value_updated_params) { params.merge(value: constant('Lyon').to_json) }
before do
post :update, params: value_updated_params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_not_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
end
context 'targeted champ changed' do
let(:last_tdc) { procedure.draft_revision.types_de_champ.last }
before do
targeted_champ_updated_params = value_updated_params.merge(targeted_champ: champ_value(last_tdc.stable_id).to_json)
post :update, params: targeted_champ_updated_params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_not_eq(champ_value(last_tdc.stable_id), empty))
end
end
end
end
end
describe "#update_defaut_groupe_instructeur" do
let(:procedure) { create(:procedure) }
let(:gi_2) { create(:groupe_instructeur, label: 'groupe 2', procedure: procedure) }
let(:params) do
{
procedure_id: procedure.id,
defaut_groupe_instructeur_id: gi_2.id
}
end
before do
post :update_defaut_groupe_instructeur, params: params, format: :turbo_stream
procedure.reload
end
it { expect(procedure.defaut_groupe_instructeur.id).to eq(gi_2.id) }
end
end

View file

@ -0,0 +1,153 @@
describe Administrateurs::RoutingRulesController, type: :controller do
include Logic
before { sign_in(procedure.administrateurs.first.user) }
let(:procedure) { create(:procedure, types_de_champ_public: [{ type: :drop_down_list, libelle: 'Votre ville', options: ['Paris', 'Lyon', 'Marseille'] }, { type: :text, libelle: 'Un champ texte' }]) }
let(:gi_2) { create(:groupe_instructeur, label: 'groupe 2', procedure: procedure) }
let(:drop_down_tdc) { procedure.draft_revision.types_de_champ.first }
let(:default_params) do
{
procedure_id: procedure.id,
groupe_instructeur_id: gi_2.id
}
end
describe '#update' do
let(:value) { empty.to_json }
let(:targeted_champ) { champ_value(drop_down_tdc.stable_id).to_json }
before { post :update, params: params, format: :turbo_stream }
let(:params) { default_params.merge(groupe_instructeur: { condition_form: condition_form }) }
let(:condition_form) do
{
rows: [
{
targeted_champ: targeted_champ,
operator_name: operator_name,
value: value
}
]
}
end
context 'with Eq operator' do
let(:operator_name) { Logic::Eq.name }
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), empty))
end
context '#update value' do
let(:value) { constant('Lyon').to_json }
before { post :update, params: params, format: :turbo_stream }
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
end
context 'targeted champ changed' do
let(:last_tdc) { procedure.draft_revision.types_de_champ.last }
let(:targeted_champ) { champ_value(last_tdc.stable_id).to_json }
let(:value) { empty.to_json }
before { post :update, params: params, format: :turbo_stream }
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(last_tdc.stable_id), empty))
end
end
end
end
context 'with NotEq operator' do
let(:operator_name) { Logic::NotEq.name }
it do
expect(gi_2.reload.routing_rule).to eq(ds_not_eq(champ_value(drop_down_tdc.stable_id), empty))
end
context '#update value' do
let(:value) { constant('Lyon').to_json }
before { post :update, params: params, format: :turbo_stream }
it do
expect(gi_2.reload.routing_rule).to eq(ds_not_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
end
context 'targeted champ changed' do
let(:last_tdc) { procedure.draft_revision.types_de_champ.last }
let(:targeted_champ) { champ_value(last_tdc.stable_id).to_json }
let(:value) { empty.to_json }
before { post :update, params: params, format: :turbo_stream }
it do
expect(gi_2.reload.routing_rule).to eq(ds_not_eq(champ_value(last_tdc.stable_id), empty))
end
end
end
end
end
describe '#add_row' do
before do
gi_2.update(routing_rule: ds_eq(champ_value(drop_down_tdc.stable_id), empty))
post :add_row, params: default_params, format: :turbo_stream
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_and([ds_eq(champ_value(drop_down_tdc.stable_id), empty), empty_operator(empty, empty)]))
end
end
describe '#delete_row' do
before do
gi_2.update(routing_rule: ds_and([ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')), empty_operator(empty, empty)]))
post :delete_row, params: params.merge(row_index: 1), format: :turbo_stream
end
let(:params) { default_params.merge(groupe_instructeur: { condition_form: condition_form }) }
let(:condition_form) do
{
rows: [
{
targeted_champ: champ_value(drop_down_tdc.stable_id).to_json,
operator_name: Logic::Eq.name,
value: constant('Lyon')
},
{
targeted_champ: empty,
operator_name: Logic::Eq.name,
value: empty
}
]
}
end
it do
expect(gi_2.reload.routing_rule).to eq(ds_eq(champ_value(drop_down_tdc.stable_id), constant('Lyon')))
end
end
describe "#update_defaut_groupe_instructeur" do
let(:procedure) { create(:procedure) }
let(:gi_2) { create(:groupe_instructeur, label: 'groupe 2', procedure: procedure) }
let(:params) do
{
procedure_id: procedure.id,
defaut_groupe_instructeur_id: gi_2.id
}
end
before do
post :update_defaut_groupe_instructeur, params: params, format: :turbo_stream
procedure.reload
end
it { expect(procedure.defaut_groupe_instructeur.id).to eq(gi_2.id) }
end
end