diff --git a/app/models/expert.rb b/app/models/expert.rb index 906e0e6c3..a0bcc13a5 100644 --- a/app/models/expert.rb +++ b/app/models/expert.rb @@ -30,17 +30,22 @@ class Expert < ApplicationRecord end def self.autocomplete_mails(procedure) - experts = Expert + procedure_experts = Expert .joins(:experts_procedures, :user) .where(experts_procedures: { procedure: procedure }) - .where.not(users: { confirmed_at: nil }) - if procedure.experts_require_administrateur_invitation? - experts = experts + new_or_confirmed_experts = procedure_experts + .where.not(users: { confirmed_at: nil }) + .or(procedure_experts.where(users: { created_at: 1.day.ago.. })) + + suggested_expert = if procedure.experts_require_administrateur_invitation? + new_or_confirmed_experts .where(experts_procedures: { revoked_at: nil }) + else + new_or_confirmed_experts end - experts + suggested_expert .pluck('users.email') .sort end diff --git a/spec/models/expert_spec.rb b/spec/models/expert_spec.rb index 89f53f82a..98aef6b9d 100644 --- a/spec/models/expert_spec.rb +++ b/spec/models/expert_spec.rb @@ -102,17 +102,19 @@ RSpec.describe Expert, type: :model do let(:expert) { create(:expert) } let(:revoked_expert) { create(:expert) } let(:unconfirmed_expert) { create(:expert) } + let(:new_unconfirmed_expert) { create(:expert) } before do - procedure.experts << expert << revoked_expert << unconfirmed_expert + procedure.experts << expert << revoked_expert << unconfirmed_expert << new_unconfirmed_expert ExpertsProcedure.find_by(expert: revoked_expert, procedure: procedure) .update!(revoked_at: 1.day.ago) - unconfirmed_expert.user.update!(confirmed_at: nil) + unconfirmed_expert.user.update!(confirmed_at: nil, created_at: 2.days.ago) + new_unconfirmed_expert.user.update!(confirmed_at: nil) end context 'when procedure experts need administrateur invitation' do it 'returns only confirmed not revoked experts' do - expect(subject).to eq([expert.user.email]) + expect(subject).to eq([expert.user.email, new_unconfirmed_expert.user.email]) end end @@ -120,7 +122,7 @@ RSpec.describe Expert, type: :model do let(:procedure) { create(:procedure, experts_require_administrateur_invitation: false) } it 'prefill autocomplete with all confirmed experts in the procedure' do - expect(subject).to eq([expert.user.email, revoked_expert.user.email]) + expect(subject).to eq([expert.user.email, revoked_expert.user.email, new_unconfirmed_expert.user.email]) end end end