feat(routing): server side: prevent tdc deletion if used for routing

This commit is contained in:
simon lehericey 2023-04-05 18:07:32 +02:00
parent 3f55fddcef
commit c1f67499aa
3 changed files with 59 additions and 36 deletions

View file

@ -67,12 +67,20 @@ module Administrateurs
end end
def destroy def destroy
@coordinate = draft.remove_type_de_champ(params[:stable_id]) coordinate, type_de_champ = draft.coordinate_and_tdc(params[:stable_id])
flash.notice = "Formulaire enregistré"
if @coordinate.present? if coordinate.used_by_routing_rules?
@destroyed = @coordinate errors = "« #{type_de_champ.libelle} » est utilisé pour le routage, vous ne pouvez pas le supprimer."
@morphed = champ_components_starting_at(@coordinate) @morphed = [champ_component_from(coordinate, focused: false, errors:)]
flash.alert = errors
else
@coordinate = draft.remove_type_de_champ(params[:stable_id])
flash.notice = "Formulaire enregistré"
if @coordinate.present?
@destroyed = @coordinate
@morphed = champ_components_starting_at(@coordinate)
end
end end
end end
@ -101,27 +109,27 @@ module Administrateurs
def type_de_champ_update_params def type_de_champ_update_params
params.required(:type_de_champ).permit(:type_champ, params.required(:type_de_champ).permit(:type_champ,
:libelle, :libelle,
:description, :description,
:mandatory, :mandatory,
:drop_down_list_value, :drop_down_list_value,
:drop_down_other, :drop_down_other,
:drop_down_secondary_libelle, :drop_down_secondary_libelle,
:drop_down_secondary_description, :drop_down_secondary_description,
:collapsible_explanation_enabled, :collapsible_explanation_enabled,
:collapsible_explanation_text, :collapsible_explanation_text,
editable_options: [ editable_options: [
:cadastres, :cadastres,
:unesco, :unesco,
:arretes_protection, :arretes_protection,
:conservatoire_littoral, :conservatoire_littoral,
:reserves_chasse_faune_sauvage, :reserves_chasse_faune_sauvage,
:reserves_biologiques, :reserves_biologiques,
:reserves_naturelles, :reserves_naturelles,
:natura_2000, :natura_2000,
:zones_humides, :zones_humides,
:znieff :znieff
]) ])
end end
def draft def draft

View file

@ -225,6 +225,16 @@ class ProcedureRevision < ApplicationRecord
types_de_champ_public.any?(&:carte?) types_de_champ_public.any?(&:carte?)
end end
def coordinate_and_tdc(stable_id)
return [nil, nil] if stable_id.blank?
coordinate = revision_types_de_champ
.joins(:type_de_champ)
.find_by(type_de_champ: { stable_id: stable_id })
[coordinate, coordinate&.type_de_champ]
end
private private
def compute_estimated_fill_duration def compute_estimated_fill_duration
@ -245,16 +255,6 @@ class ProcedureRevision < ApplicationRecord
end end
end end
def coordinate_and_tdc(stable_id)
return [nil, nil] if stable_id.blank?
coordinate = revision_types_de_champ
.joins(:type_de_champ)
.find_by(type_de_champ: { stable_id: stable_id })
[coordinate, coordinate&.type_de_champ]
end
def renumber(siblings) def renumber(siblings)
siblings.to_a.compact.each.with_index do |sibling, position| siblings.to_a.compact.each.with_index do |sibling, position|
sibling.update_column(:position, position) sibling.update_column(:position, position)

View file

@ -157,5 +157,20 @@ describe Administrateurs::TypesDeChampController, type: :controller do
expect(assigns(:destroyed).libelle).to eq('l2') expect(assigns(:destroyed).libelle).to eq('l2')
expect(morpheds).to eq([['l3', ['l1']]]) expect(morpheds).to eq([['l3', ['l1']]])
end end
context 'rejected if type changed and routing involved' do
let(:params) do
{ procedure_id: procedure.id, stable_id: third_coordinate.stable_id }
end
before do
allow_any_instance_of(ProcedureRevisionTypeDeChamp).to receive(:used_by_routing_rules?).and_return(true)
end
it do
is_expected.to have_http_status(:ok)
expect(flash.alert).to include("utilisé pour le routage")
end
end
end end
end end