add reusable code in concern for typo detection email
This commit is contained in:
parent
68cf6b58a3
commit
c8906d20b8
2 changed files with 21 additions and 16 deletions
|
@ -10,16 +10,10 @@ module Administrateurs
|
||||||
|
|
||||||
def create
|
def create
|
||||||
emails = params['emails'].presence || [].to_json
|
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))
|
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
|
valid_users, invalid_users = emails
|
||||||
.map { |email| User.create_or_promote_to_expert(email, SecureRandom.hex) }
|
.map { |email| User.create_or_promote_to_expert(email, SecureRandom.hex) }
|
||||||
.partition(&:valid?)
|
.partition(&:valid?)
|
||||||
|
@ -78,14 +72,5 @@ module Administrateurs
|
||||||
def expert_procedure_params
|
def expert_procedure_params
|
||||||
params.require(:experts_procedure).permit(:allow_decision_access)
|
params.require(:experts_procedure).permit(:allow_decision_access)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,26 @@ module EmailSanitizableConcern
|
||||||
end
|
end
|
||||||
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
|
class EmailSanitizer
|
||||||
def self.sanitize(value)
|
def self.sanitize(value)
|
||||||
value.gsub(/[[:space:]]/, ' ').strip.downcase
|
value.gsub(/[[:space:]]/, ' ').strip.downcase
|
||||||
|
|
Loading…
Reference in a new issue