diff --git a/app/controllers/administrateurs/referentiels_controller.rb b/app/controllers/administrateurs/referentiels_controller.rb index c656633b151..f93bc0f7cd1 100644 --- a/app/controllers/administrateurs/referentiels_controller.rb +++ b/app/controllers/administrateurs/referentiels_controller.rb @@ -38,6 +38,15 @@ def mapping_type_de_champ @service.test end + def update_mapping_type_de_champ + flash = if @type_de_champ.update(type_de_champ_mapping_params) + { notice: "ok" } + else + { alert: "ko" } + end + redirect_to mapping_type_de_champ_admin_procedure_referentiel_path(@procedure, @type_de_champ.stable_id, @referentiel), flash: + end + private def type_de_champ_mapping_params diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 3e3c91cbaff..43bffec864e 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -139,7 +139,8 @@ class TypeDeChamp < ApplicationRecord :expression_reguliere_error_message, :collapsible_explanation_enabled, :collapsible_explanation_text, - :header_section_level + :header_section_level, + :referentiel_mapping has_many :revision_types_de_champ, -> { revision_ordered }, class_name: 'ProcedureRevisionTypeDeChamp', dependent: :destroy, inverse_of: :type_de_champ diff --git a/config/routes.rb b/config/routes.rb index 7dc13829e71..02197d03db3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -749,6 +749,7 @@ resources :referentiels, only: [:new, :create, :edit, :update], path: ':stable_id' do member do get :mapping_type_de_champ + patch :update_mapping_type_de_champ end end diff --git a/spec/controllers/administrateurs/referentiels_controller_spec.rb b/spec/controllers/administrateurs/referentiels_controller_spec.rb index 6e3ec4217f4..dcd4edad340 100644 --- a/spec/controllers/administrateurs/referentiels_controller_spec.rb +++ b/spec/controllers/administrateurs/referentiels_controller_spec.rb @@ -126,4 +126,29 @@ end end end + + describe '#update_mapping_type_de_champ' do + let(:type_de_champ) { procedure.draft_revision.types_de_champ.first } + let(:referentiel) { create(:api_referentiel, :configured, types_de_champ: [type_de_champ]) } + let(:referentiel_mapping) do + [ + { + jsonpath: "jsonpath", + type: "type", + prefill: "prefill", + libelle: "libelle" + } + ] + end + it 'update type de champ referentiel_mapping' do + expect do + patch :update_mapping_type_de_champ, params: { + procedure_id: procedure.id, + stable_id: stable_id, + id: referentiel.id, + type_de_champ: { referentiel_mapping: } + } + end.to change { type_de_champ.reload.referentiel_mapping }.from(nil).to(referentiel_mapping) + end + end end