fix(admin): invitation for experts not having an account

This commit is contained in:
Colin Darie 2024-07-02 16:10:06 +02:00
parent b2514dd16c
commit a53fc0fac9
No known key found for this signature in database
GPG key ID: 4FB865FDBCA4BCC4
2 changed files with 20 additions and 11 deletions

View file

@ -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 lorthographe", "#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

View file

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