feat(dossier): prefill dossier from query params

This commit is contained in:
sebastiencarceles 2022-11-28 15:42:39 +01:00
parent 7a1325df60
commit 24802985a2
4 changed files with 72 additions and 1 deletions

View file

@ -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)

View file

@ -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

View file

@ -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) }

View file

@ -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 }