diff --git a/app/models/concerns/dossier_prefillable_concern.rb b/app/models/concerns/dossier_prefillable_concern.rb index d0c21a2bf..526d66698 100644 --- a/app/models/concerns/dossier_prefillable_concern.rb +++ b/app/models/concerns/dossier_prefillable_concern.rb @@ -9,6 +9,7 @@ module DossierPrefillableConcern attributes = { prefilled: true } attributes[:champs_attributes] = champs_attributes.map { |h| h.merge(prefilled: true) } attributes[:individual_attributes] = identity_attributes if identity_attributes.present? + reload assign_attributes(attributes) save(validate: false) diff --git a/spec/controllers/api/public/v1/dossiers_controller_spec.rb b/spec/controllers/api/public/v1/dossiers_controller_spec.rb index 323be6f25..0fb8a2dba 100644 --- a/spec/controllers/api/public/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/public/v1/dossiers_controller_spec.rb @@ -72,6 +72,30 @@ RSpec.describe API::Public::V1::DossiersController, type: :controller do expect(dossier.individual.gender).to eq(genre_value) end end + + context 'when prefill given values contains more than one rows for repetitions' do + let(:procedure) { create(:procedure, :published, types_de_champ_public:) } + let(:types_de_champ_public) do + [ + type: :repetition, + children: [ + { type: :text, libelle: 'child of repet text' } + ] + ] + end + let(:prefilled_champs) { TypesDeChamp::PrefillTypeDeChamp.wrap(procedure.published_revision.types_de_champ, procedure.active_revision) } + let(:prefilled_champs_as_params) { prefilled_champs.map { |type_de_champ| ["champ_#{type_de_champ.to_typed_id_for_query}", type_de_champ.example_value] }.to_h } + let(:params) { prefilled_champs_as_params.merge(id: procedure.id) } + + it "updates the champs with the new values and mark them as prefilled" do + expect { create_request }.not_to raise_error(ActiveRecord::RecordNotFound) + dossier = Dossier.last + + first_row = dossier.project_champs_public.first.rows.first + second_row = dossier.project_champs_public.first.rows.last + expect(dossier.project_champs_public.first.rows.flatten.map(&:value)).to match_array(['Texte court', 'Texte court']) + end + end end context 'when the dossier can not be saved' do