Merge pull request #10338 from demarches-simplifiees/do_not_suggest_unconfirmed_expert

ETQ instructeur: lorsque je demande des avis externes, ds suggere uniquement des utilisateurs s'étant connectée au moins 1 fois
This commit is contained in:
LeSim 2024-04-16 16:06:57 +00:00 committed by GitHub
commit 1de945b25d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 58 additions and 31 deletions

View file

@ -83,11 +83,7 @@ module Experts
def avis_new
@new_avis = Avis.new
if @dossier.procedure.experts_require_administrateur_invitation?
@experts_emails = @dossier.procedure.experts_procedures.where(revoked_at: nil).map { _1.expert.email }.sort
else
@experts_emails = @dossier.procedure.experts.map(&:email).sort
end
@experts_emails = Expert.autocomplete_mails(@dossier.procedure)
end
def create_avis

View file

@ -64,21 +64,13 @@ module Instructeurs
def avis
@avis_seen_at = current_instructeur.follows.find_by(dossier: dossier)&.avis_seen_at
@avis = Avis.new
if @dossier.procedure.experts_require_administrateur_invitation?
@experts_emails = dossier.procedure.experts_procedures.where(revoked_at: nil).map(&:expert).map(&:email).sort
else
@experts_emails = @dossier.procedure.experts.map(&:email).sort
end
@experts_emails = Expert.autocomplete_mails(@dossier.procedure)
end
def avis_new
@avis_seen_at = current_instructeur.follows.find_by(dossier: dossier)&.avis_seen_at
@avis = Avis.new
if @dossier.procedure.experts_require_administrateur_invitation?
@experts_emails = dossier.procedure.experts_procedures.where(revoked_at: nil).map(&:expert).map(&:email).sort
else
@experts_emails = @dossier.procedure.experts.map(&:email).sort
end
@experts_emails = Expert.autocomplete_mails(@dossier.procedure)
end
def personnes_impliquees

View file

@ -29,6 +29,27 @@ class Expert < ApplicationRecord
end
end
def self.autocomplete_mails(procedure)
procedure_experts = Expert
.joins(:experts_procedures, :user)
.where(experts_procedures: { procedure: procedure })
new_or_confirmed_experts = procedure_experts
.where.not(users: { last_sign_in_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
suggested_expert
.pluck('users.email')
.sort
end
def merge(old_expert)
return if old_expert.nil?

View file

@ -355,26 +355,12 @@ describe Experts::AvisController, type: :controller do
end
describe '#avis_new' do
let!(:revoked_expert) { create(:experts_procedure, revoked_at: 2.days.ago, procedure: procedure, expert: create(:expert)).expert }
before do
allow(Expert).to receive(:autocomplete_mails).and_return([])
get :avis_new, params: { procedure_id: procedure.id, id: avis_without_answer.id }
end
context 'when procedure experts need administrateur invitation' do
let!(:procedure) { create(:procedure, experts_require_administrateur_invitation: true) }
it 'limit invited email list to not revoked experts' do
expect(assigns(:experts_emails)).to include(experts_procedure.expert.user.email)
expect(assigns(:experts_emails)).not_to include(revoked_expert.user.email)
end
end
context 'when procedure experts can be anyone' do
let!(:procedure) { create(:procedure, experts_require_administrateur_invitation: false) }
it 'prefill autocomplete with all experts in the procedure' do
expect(assigns(:experts_emails)).to include(experts_procedure.expert.user.email)
expect(assigns(:experts_emails)).to include(revoked_expert.user.email)
end
end
it { expect(Expert).to have_received(:autocomplete_mails).with(procedure) }
end
describe '#create_avis' do

View file

@ -94,4 +94,36 @@ RSpec.describe Expert, type: :model do
end
end
end
describe '.autocomplete_mails' do
subject { Expert.autocomplete_mails(procedure) }
let(:procedure) { create(:procedure, experts_require_administrateur_invitation: true) }
let(:expert) { create(:expert) }
let(:revoked_expert) { create(:expert) }
let(:unsigned_expert) { create(:expert) }
let(:new_unsigned_expert) { create(:expert) }
before do
procedure.experts << expert << revoked_expert << unsigned_expert << new_unsigned_expert
ExpertsProcedure.find_by(expert: revoked_expert, procedure: procedure)
.update!(revoked_at: 1.day.ago)
unsigned_expert.user.update!(last_sign_in_at: nil, created_at: 2.days.ago)
new_unsigned_expert.user.update!(last_sign_in_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, new_unsigned_expert.user.email].sort)
end
end
context 'when procedure experts can be anyone' 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, new_unsigned_expert.user.email].sort)
end
end
end
end