diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index f4c247b79..7369eaf78 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -148,6 +148,7 @@ module Users def brouillon @dossier = dossier_with_champs + # TODO: SEB show validation errors # TODO: remove when the champs are unifed if !@dossier.autorisation_donnees @@ -303,6 +304,7 @@ module Users ) dossier.build_default_individual dossier.save! + dossier.prefill!(PrefillParams.new(dossier, params).to_a) if dossier.procedure.for_individual redirect_to identite_dossier_path(dossier) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fc30676df..71c2aba8c 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1226,6 +1226,12 @@ class Dossier < ApplicationRecord cloned_dossier end + def find_champ_by_stable_id(stable_id) + return nil if stable_id.blank? + + champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id }) + end + private def create_missing_traitemets diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index a458f4d1f..2f2a4d0b5 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -1054,8 +1054,9 @@ describe Users::DossiersController, type: :controller do describe '#new' do let(:procedure) { create(:procedure, :published) } let(:procedure_id) { procedure.id } + let(:params) { { procedure_id: procedure_id } } - subject { get :new, params: { procedure_id: procedure_id } } + subject { get :new, params: params } it 'clears the stored procedure context' do subject @@ -1085,6 +1086,34 @@ describe Users::DossiersController, type: :controller do it { is_expected.to redirect_to dossiers_path } end + + context 'when prefill values are given' do + let!(:type_de_champ_1) { create(:type_de_champ_text, procedure: procedure) } + let(:value_1) { "any value" } + + let!(:type_de_champ_2) { create(:type_de_champ_textarea, procedure: procedure) } + let(:value_2) { "another value" } + + let(:params) { + { + procedure_id: procedure_id, + type_de_champ_1.to_typed_id => value_1, + type_de_champ_2.to_typed_id => value_2 + } + } + + it { expect { subject }.to change { Dossier.count }.by(1) } + + it "prefills the dossier's champs with the given values" do + subject + + dossier = Dossier.last + expect(dossier.find_champ_by_stable_id(type_de_champ_1.stable_id).value).to eq(value_1) + expect(dossier.find_champ_by_stable_id(type_de_champ_2.stable_id).value).to eq(value_2) + end + + it { is_expected.to redirect_to siret_dossier_path(id: Dossier.last) } + end end context 'when user is not logged' do it { is_expected.to have_http_status(302) } diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index cba307b3e..f22296cee 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1855,6 +1855,40 @@ describe Dossier do end end + describe "#find_champ_by_stable_id" do + let(:procedure) { create(:procedure, :published) } + let(:dossier) { create(:dossier, :brouillon, procedure: procedure) } + + subject { dossier.find_champ_by_stable_id(stable_id) } + + context 'when the stable id is missing' do + let(:stable_id) { nil } + + it { expect(subject).to be_nil } + end + + context 'when the stable id is blank' do + let(:stable_id) { "" } + + it { expect(subject).to be_nil } + end + + context 'when the stable id is present' do + context 'when the dossier has no champ with the given stable id' do + let(:stable_id) { 'My Neighbor Totoro is the best film ever' } + + it { expect(subject).to be_nil } + end + + context 'when the dossier has a champ with the given stable id' do + let!(:type_de_champ) { create(:type_de_champ_text, procedure: procedure) } + let(:stable_id) { type_de_champ.stable_id } + + it { expect(subject).to eq(dossier.champs_public.joins(:type_de_champ).find_by(types_de_champ: { stable_id: stable_id })) } + end + end + end + describe 'BatchOperation' do subject { build(:dossier) } it { is_expected.to belong_to(:batch_operation).optional }