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,6 +67,13 @@ module Administrateurs
end
def destroy
coordinate, type_de_champ = draft.coordinate_and_tdc(params[:stable_id])
if coordinate.used_by_routing_rules?
errors = "« #{type_de_champ.libelle} » est utilisé pour le routage, vous ne pouvez pas le supprimer."
@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é"
@ -75,6 +82,7 @@ module Administrateurs
@morphed = champ_components_starting_at(@coordinate)
end
end
end
private

View file

@ -225,6 +225,16 @@ class ProcedureRevision < ApplicationRecord
types_de_champ_public.any?(&:carte?)
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
def compute_estimated_fill_duration
@ -245,16 +255,6 @@ class ProcedureRevision < ApplicationRecord
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)
siblings.to_a.compact.each.with_index do |sibling, 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(morpheds).to eq([['l3', ['l1']]])
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