diff --git a/app/controllers/administrateurs/experts_procedures_controller.rb b/app/controllers/administrateurs/experts_procedures_controller.rb index 64abc5c38..d86f00ca2 100644 --- a/app/controllers/administrateurs/experts_procedures_controller.rb +++ b/app/controllers/administrateurs/experts_procedures_controller.rb @@ -10,16 +10,10 @@ module Administrateurs def create emails = params['emails'].presence || [].to_json - emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) } - @maybe_typos, no_suggestions = emails - .map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] } - .partition { _1[1].present? } + emails = check_if_typo(emails) 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) } .partition(&:valid?) @@ -78,14 +72,5 @@ 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/app/models/concerns/email_sanitizable_concern.rb b/app/models/concerns/email_sanitizable_concern.rb index d143c7c7d..b8818b1d2 100644 --- a/app/models/concerns/email_sanitizable_concern.rb +++ b/app/models/concerns/email_sanitizable_concern.rb @@ -8,6 +8,26 @@ module EmailSanitizableConcern end 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 + + def check_if_typo(emails) + emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) } + @maybe_typos, no_suggestions = emails + .map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] } + .partition { _1[1].present? } + + emails = no_suggestions.map(&:first) + emails << EmailSanitizer.sanitize(params['final_email']) if params['final_email'].present? + emails + end + class EmailSanitizer def self.sanitize(value) value.gsub(/[[:space:]]/, ' ').strip.downcase