[Fix #11071] Fix wrong expert_procedure associated to avis

This commit is contained in:
Mathieu Magnin 2024-11-22 16:53:00 +01:00
parent 998670433e
commit b3672c5526
No known key found for this signature in database
GPG key ID: 8DCAFC82D7BA654E
2 changed files with 39 additions and 2 deletions

View file

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

View file

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