diff --git a/app/models/concerns/dossier_prefillable_concern.rb b/app/models/concerns/dossier_prefillable_concern.rb index 1b716c6b2..bbcfdc8e1 100644 --- a/app/models/concerns/dossier_prefillable_concern.rb +++ b/app/models/concerns/dossier_prefillable_concern.rb @@ -8,7 +8,7 @@ module DossierPrefillableConcern attributes = { prefilled: true } attributes[:champs_attributes] = champs_attributes.map { |h| h.merge(prefilled: true) } - attributes[:individual_attributes] = identity_attributes + attributes[:individual_attributes] = identity_attributes if identity_attributes.present? assign_attributes(attributes) save(validate: false) diff --git a/lib/tasks/deployment/20230918143300_fix_prefilled_empty_individual.rake b/lib/tasks/deployment/20230918143300_fix_prefilled_empty_individual.rake new file mode 100644 index 000000000..68f570f95 --- /dev/null +++ b/lib/tasks/deployment/20230918143300_fix_prefilled_empty_individual.rake @@ -0,0 +1,16 @@ +namespace :after_party do + desc 'Deployment task: fix_prefilled_empty_individual' + task fix_prefilled_empty_individual: :environment do + puts "Running deploy task 'fix_prefilled_empty_individual'" + + Dossier.prefilled.joins(:procedure).joins(:individual).where(procedure: { for_individual: false }).find_each do |dossier| + rake_puts "Destroy Individual of dossier ##{dossier.id}" + dossier.individual.destroy! + end + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord + .create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end diff --git a/spec/models/concern/dossier_prefillable_concern_spec.rb b/spec/models/concern/dossier_prefillable_concern_spec.rb index a2e680368..680688900 100644 --- a/spec/models/concern/dossier_prefillable_concern_spec.rb +++ b/spec/models/concern/dossier_prefillable_concern_spec.rb @@ -101,6 +101,29 @@ RSpec.describe DossierPrefillableConcern do end end + context "when dossier is for etablissement" do + let(:procedure) { create(:procedure, :published, types_de_champ_public:, types_de_champ_private:) } + let(:dossier) { create(:dossier, :brouillon, procedure: procedure) } + + context 'when champs_attributes has values' do + context 'when the champs are valid' do + let(:types_de_champ_public) { [{ type: :text }] } + let(:type_de_champ_1) { procedure.published_revision.types_de_champ_public.first } + let(:value_1) { "any value" } + let(:champ_id_1) { find_champ_by_stable_id(dossier, type_de_champ_1.stable_id).id } + let(:values) { [{ id: champ_id_1, value: value_1 }] } + + it "updates the champs with the new values and mark them as prefilled" do + fill + expect(dossier.champs_public.first.value).to eq(value_1) + expect(dossier.individual).to be_nil # Fix #9486 + end + + it_behaves_like 'a dossier marked as prefilled' + end + end + end + private def find_champ_by_stable_id(dossier, stable_id)