diff --git a/app/controllers/administrateurs/experts_procedures_controller.rb b/app/controllers/administrateurs/experts_procedures_controller.rb index 454b72730..64abc5c38 100644 --- a/app/controllers/administrateurs/experts_procedures_controller.rb +++ b/app/controllers/administrateurs/experts_procedures_controller.rb @@ -11,15 +11,14 @@ module Administrateurs def create emails = params['emails'].presence || [].to_json emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) } - @maybe_typos, emails = emails + @maybe_typos, no_suggestions = emails .map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] } .partition { _1[1].present? } - errors = if !@maybe_typos.empty? - ["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typos.map(&:first).join(', ')}. Veuillez, #{view_context.link_to(" verifier l'orthographe", "#maybe_typos_errors")} des invitations."] - else - [] - end - emails += [EmailSanitizer.sanitize(params['final_email'])] if params['final_email'].present? + + errors = Array.wrap(generate_emails_suggestions_message(@maybe_typos)) + + emails = no_suggestions.map(&:first) + emails << EmailSanitizer.sanitize(params['final_email']) if params['final_email'].present? valid_users, invalid_users = emails .map { |email| User.create_or_promote_to_expert(email, SecureRandom.hex) } @@ -79,5 +78,14 @@ module Administrateurs def expert_procedure_params params.require(:experts_procedure).permit(:allow_decision_access) end + + def generate_emails_suggestions_message(suggestions) + return if suggestions.empty? + + typo_list = suggestions.map(&:first).join(', ') + verification_link = view_context.link_to("vérifier l’orthographe", "#maybe_typos_errors") + + "Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{typo_list}. Veuillez #{verification_link} des invitations." + end end end diff --git a/spec/controllers/administrateurs/experts_procedures_controller_spec.rb b/spec/controllers/administrateurs/experts_procedures_controller_spec.rb index 4374afb8b..7c99bbbc6 100644 --- a/spec/controllers/administrateurs/experts_procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/experts_procedures_controller_spec.rb @@ -24,14 +24,13 @@ describe Administrateurs::ExpertsProceduresController, type: :controller do let(:procedure) { create :procedure, administrateur: admin, experts_require_administrateur_invitation: true } subject { post :create, params: params } - before { subject } context 'when inviting multiple valid experts' do - let(:params) { { procedure_id: procedure.id, emails: [expert.email, expert2.email].to_json } } + let(:params) { { procedure_id: procedure.id, emails: [expert.email, "new@expert.fr"].to_json } } it 'creates experts' do - expect(procedure.experts.include?(expert)).to be_truthy - expect(procedure.experts.include?(expert2)).to be_truthy + subject + expect(procedure.experts.map(&:email)).to match_array([expert.email, "new@expert.fr"]) expect(flash.notice).to be_present expect(assigns(:maybe_typos)).to eq([]) expect(response).to have_http_status(:success) @@ -42,6 +41,7 @@ describe Administrateurs::ExpertsProceduresController, type: :controller do let(:params) { { procedure_id: procedure.id, emails: ['martin@oraneg.fr'].to_json } } render_views it 'warns' do + subject expect(flash.alert).to be_present expect(assigns(:maybe_typos)).to eq([['martin@oraneg.fr', 'martin@orange.fr']]) expect(response).to have_http_status(:success) @@ -54,6 +54,7 @@ describe Administrateurs::ExpertsProceduresController, type: :controller do let(:params) { { procedure_id: procedure.id, final_email: } } it 'works' do + subject created_user = User.where(email: final_email).first expect(created_user).to be_an_instance_of(User) expect(created_user.expert).to be_an_instance_of(Expert)