diff --git a/app/controllers/concerns/create_avis_concern.rb b/app/controllers/concerns/create_avis_concern.rb index 7f1a51c4a..4c35668f3 100644 --- a/app/controllers/concerns/create_avis_concern.rb +++ b/app/controllers/concerns/create_avis_concern.rb @@ -36,8 +36,9 @@ module CreateAvisConcern create_results = Avis.create( expert_emails.flat_map do |email| user = User.create_or_promote_to_expert(email, SecureRandom.hex) - experts_procedure = user.valid? ? ExpertsProcedure.find_or_create_by(procedure: dossier.procedure, expert: user.expert) : nil + allowed_dossiers.map do |dossier| + experts_procedure = user.valid? ? ExpertsProcedure.find_or_create_by(procedure: dossier.procedure, expert: user.expert) : nil { email: email, introduction: create_avis_params[:introduction], diff --git a/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb b/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb new file mode 100644 index 000000000..a86f5d47d --- /dev/null +++ b/app/tasks/maintenance/fix_avis_experts_procedure_mismatch_task.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Maintenance + class FixAvisExpertsProcedureMismatchTask < MaintenanceTasks::Task + # Some avis have incorrect experts_procedure associations where the procedure_id + # doesn't match the dossier's procedure_id. This task fixes those mismatches. + + def collection + Avis + .joins(:experts_procedure, dossier: :procedure) + .where.not('experts_procedures.procedure_id = procedures.id') + end + + def process(avis) + expert = avis.expert + correct_procedure = avis.dossier.procedure + + correct_experts_procedure = ExpertsProcedure.find_or_create_by( + procedure: correct_procedure, + expert: expert + ) + + avis.update!(experts_procedure: correct_experts_procedure) + end + end +end diff --git a/spec/controllers/experts/avis_controller_spec.rb b/spec/controllers/experts/avis_controller_spec.rb index ed84e994f..13d0cb6f5 100644 --- a/spec/controllers/experts/avis_controller_spec.rb +++ b/spec/controllers/experts/avis_controller_spec.rb @@ -456,7 +456,7 @@ describe Experts::AvisController, type: :controller do end end - context 'when the preivous avis is confidentiel' do + context 'when the previous avis is confidentiel' do let(:previous_avis_confidentiel) { true } context 'when the user asked for a public avis' do @@ -507,6 +507,42 @@ describe Experts::AvisController, type: :controller do end end end + + context 'when creating avis for linked dossiers with different procedures' do + let(:types_de_champ_public) { [{ type: :dossier_link }] } + let(:another_procedure) { create(:procedure, :published) } + let(:dossier) { create(:dossier, :en_construction, procedure: procedure) } + let(:linked_dossier) { create(:dossier, :en_construction, procedure: another_procedure) } + let(:linked_experts_procedure) { create(:experts_procedure, expert: previous_avis.expert, procedure: another_procedure) } + let!(:linked_avis) { create(:avis, dossier: linked_dossier, experts_procedure: linked_experts_procedure, claimant: previous_avis.claimant) } + + let(:invite_linked_dossiers) { true } + + before do + dossier_link_champ = dossier.champs.find_by(type: "Champs::DossierLinkChamp") + dossier_link_champ.update!(value: linked_dossier.id) + end + + it 'creates experts_procedure with correct procedure for each dossier' do + expect { + post :create_avis, params: { + id: previous_avis.id, + procedure_id: procedure.id, + avis: { + emails: ["expert@example.com"], + introduction: "Please review", + confidentiel: false, + invite_linked_dossiers: true + } + } + }.to change(Avis, :count).by(2) + + Avis.all.each do |avis| + # Verify experts_procedures are created with correct procedures + expect(avis.experts_procedure.procedure_id).to eq(avis.dossier.procedure.id) + end + end + end end end diff --git a/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb b/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb new file mode 100644 index 000000000..247c2e4c4 --- /dev/null +++ b/spec/tasks/maintenance/fix_avis_experts_procedure_mismatch_task_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +module Maintenance + RSpec.describe FixAvisExpertsProcedureMismatchTask do + describe "#collection" do + subject(:collection) { described_class.new.collection } + + let!(:mismatched_avis) do + expert = create(:expert) + procedure1 = create(:procedure) + procedure2 = create(:procedure) + dossier = create(:dossier, procedure: procedure2) + wrong_experts_procedure = create(:experts_procedure, expert: expert, procedure: procedure1) + + create(:avis, + dossier: dossier, + expert: expert, + experts_procedure: wrong_experts_procedure) + end + + let!(:correct_avis) do + expert = create(:expert) + procedure = create(:procedure) + dossier = create(:dossier, procedure: procedure) + experts_procedure = create(:experts_procedure, expert: expert, procedure: procedure) + + create(:avis, + dossier: dossier, + expert: expert, + experts_procedure: experts_procedure) + end + + it "only returns avis with mismatched procedure ids" do + expect(collection).to include(mismatched_avis) + expect(collection).not_to include(correct_avis) + end + end + + describe "#process" do + subject(:process) { described_class.process(avis) } + + let(:expert) { create(:expert) } + let(:procedure1) { create(:procedure) } + let(:procedure2) { create(:procedure) } + let(:dossier) { create(:dossier, procedure: procedure2) } + + let!(:wrong_experts_procedure) do + create(:experts_procedure, expert: expert, procedure: procedure1) + end + + let(:avis) do + create(:avis, + dossier: dossier, + expert: expert, + experts_procedure: wrong_experts_procedure) + end + + it "fixes the experts_procedure association" do + expect(avis.experts_procedure.procedure_id).to eq(procedure1.id) + expect(avis.dossier.procedure.id).to eq(procedure2.id) + + process + + avis.reload + expect(avis.experts_procedure.procedure_id).to eq(procedure2.id) + expect(avis.dossier.procedure.id).to eq(procedure2.id) + end + + context "when the correct experts_procedure already exists" do + let!(:correct_experts_procedure) do + create(:experts_procedure, expert: expert, procedure: procedure2) + end + + it "uses the existing experts_procedure" do + process + + avis.reload + expect(avis.experts_procedure).to eq(correct_experts_procedure) + end + end + end + end +end