diff --git a/app/controllers/new_administrateur/procedures_controller.rb b/app/controllers/new_administrateur/procedures_controller.rb index bebb3a255..7a2169656 100644 --- a/app/controllers/new_administrateur/procedures_controller.rb +++ b/app/controllers/new_administrateur/procedures_controller.rb @@ -186,7 +186,7 @@ module NewAdministrateur end def invited_expert_list - @invited_expert_emails = ExpertsProcedure.invited_expert_emails(@procedure) + @experts_procedure = @procedure.experts_procedures.sort_by { |expert_procedure| expert_procedure.expert.email } end private diff --git a/app/models/experts_procedure.rb b/app/models/experts_procedure.rb index d874fdb36..203f927fc 100644 --- a/app/models/experts_procedure.rb +++ b/app/models/experts_procedure.rb @@ -14,15 +14,4 @@ class ExpertsProcedure < ApplicationRecord belongs_to :procedure has_many :avis, dependent: :destroy - - def email_to_display - expert&.email - end - - def self.invited_expert_emails(procedure) - joins(:expert) - .where(procedure: procedure) - .map(&:email_to_display) - .sort - end end diff --git a/spec/models/experts_procedure_spec.rb b/spec/models/experts_procedure_spec.rb index efd227593..b6dd1a7b8 100644 --- a/spec/models/experts_procedure_spec.rb +++ b/spec/models/experts_procedure_spec.rb @@ -7,7 +7,7 @@ RSpec.describe ExpertsProcedure, type: :model do let(:experts_procedure) { ExpertsProcedure.create(expert: expert, procedure: procedure) } let(:experts_procedure2) { ExpertsProcedure.create(expert: expert2, procedure: procedure) } let(:experts_procedure3) { ExpertsProcedure.create(expert: expert3, procedure: procedure) } - subject { ExpertsProcedure.invited_expert_emails(procedure) } + subject { procedure.experts_procedures } context 'when there is one dossier' do let!(:dossier) { create(:dossier, procedure: procedure) } @@ -15,14 +15,18 @@ RSpec.describe ExpertsProcedure, type: :model do context 'when a procedure has one avis and known instructeur' do let!(:avis) { create(:avis, dossier: dossier, instructeur: create(:instructeur, email: expert.email), experts_procedure: experts_procedure) } - it { is_expected.to eq([expert.email]) } + it { is_expected.to eq([experts_procedure]) } + it { expect(procedure.experts.count).to eq(1) } + it { expect(procedure.experts.first.email).to eq(expert.email) } end context 'when a dossier has 2 avis from the same expert' do let!(:avis) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) } let!(:avis2) { create(:avis, dossier: dossier, experts_procedure: experts_procedure) } - it { is_expected.to eq([expert.email]) } + it { is_expected.to eq([experts_procedure]) } + it { expect(procedure.experts.count).to eq(1) } + it { expect(procedure.experts.first).to eq(expert) } end end @@ -35,7 +39,9 @@ RSpec.describe ExpertsProcedure, type: :model do let!(:avis2) { create(:avis, dossier: dossier2, experts_procedure: experts_procedure2) } let!(:avis3) { create(:avis, dossier: dossier2, experts_procedure: experts_procedure3) } - it { is_expected.to eq([expert.email, expert2.email, expert3.email].sort) } + it { is_expected.to match_array([experts_procedure, experts_procedure2, experts_procedure3]) } + it { expect(procedure.experts.count).to eq(3) } + it { expect(procedure.experts).to match_array([expert, expert2, expert3]) } end end end