diff --git a/app/controllers/administrateurs/experts_procedures_controller.rb b/app/controllers/administrateurs/experts_procedures_controller.rb index c11a04deb..ce3f92cc9 100644 --- a/app/controllers/administrateurs/experts_procedures_controller.rb +++ b/app/controllers/administrateurs/experts_procedures_controller.rb @@ -9,17 +9,11 @@ module Administrateurs end def create - email_checker = EmailChecker.new emails = params['emails'].presence || [].to_json emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) } - @maybe_typo, emails = emails.map do |email| - result = email_checker.check(email: email) - if result[:email_suggestions].present? - [email, result[:email_suggestions].first] - else - [email, nil] - end - end.partition { _1[1].present? } + @maybe_typo, emails = emails + .map { |email| [email, EmailChecker.check(email:)[:suggestions]&.first] } + .partition { _1[1].present? } errors = if !@maybe_typo.empty? ["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typo.map(&:first).join(', ')}"] else diff --git a/app/controllers/email_checker_controller.rb b/app/controllers/email_checker_controller.rb index 19cd0493b..b794b4d7a 100644 --- a/app/controllers/email_checker_controller.rb +++ b/app/controllers/email_checker_controller.rb @@ -1,5 +1,5 @@ class EmailCheckerController < ApplicationController def show - render json: EmailChecker.new.check(email: params[:email]) + render json: EmailChecker.check(email: params[:email]) end end diff --git a/app/javascript/controllers/email_input_controller.ts b/app/javascript/controllers/email_input_controller.ts index f8442e1d3..d0afb3168 100644 --- a/app/javascript/controllers/email_input_controller.ts +++ b/app/javascript/controllers/email_input_controller.ts @@ -4,7 +4,7 @@ import { ApplicationController } from './application_controller'; type checkEmailResponse = { success: boolean; - email_suggestions: string[]; + suggestions: string[]; }; export class EmailInputController extends ApplicationController { @@ -36,8 +36,8 @@ export class EmailInputController extends ApplicationController { url.toString() ).json(); - if (data && data.email_suggestions && data.email_suggestions.length > 0) { - this.suggestionTarget.innerHTML = data.email_suggestions[0]; + if (data && data.suggestions && data.suggestions.length > 0) { + this.suggestionTarget.innerHTML = data.suggestions[0]; show(this.ariaRegionTarget); this.ariaRegionTarget.setAttribute('aria-live', 'assertive'); } diff --git a/app/lib/email_checker.rb b/app/lib/email_checker.rb index e2d2d5222..bf1a2d871 100644 --- a/app/lib/email_checker.rb +++ b/app/lib/email_checker.rb @@ -615,7 +615,7 @@ class EmailChecker 'ac-toulous.fr' ].freeze - def check(email:) + def self.check(email:) return { success: false } if email.blank? parsed_email = Mail::Address.new(EmailSanitizableConcern::EmailSanitizer.sanitize(email)) @@ -626,29 +626,29 @@ class EmailChecker similar_domains = closest_domains(domain: parsed_email.domain) return { success: true } if similar_domains.empty? - { success: true, email_suggestions: email_suggestions(parsed_email:, similar_domains:) } + { success: true, suggestions: suggestions(parsed_email:, similar_domains:) } rescue Mail::Field::IncompleteParseError return { success: false } end private - def closest_domains(domain:) + def self.closest_domains(domain:) KNOWN_DOMAINS.filter do |known_domain| close_by_distance_of(domain, known_domain, distance: 1) || with_same_chars_and_close_by_distance_of(domain, known_domain, distance: 2) end end - def close_by_distance_of(a, b, distance:) + def self.close_by_distance_of(a, b, distance:) String::Similarity.levenshtein_distance(a, b) == distance end - def with_same_chars_and_close_by_distance_of(a, b, distance:) + def self.with_same_chars_and_close_by_distance_of(a, b, distance:) close_by_distance_of(a, b, distance: 2) && a.chars.sort == b.chars.sort end - def email_suggestions(parsed_email:, similar_domains:) + def self.suggestions(parsed_email:, similar_domains:) similar_domains.map { Mail::Address.new("#{parsed_email.local}@#{_1}").to_s } end end diff --git a/spec/controllers/email_checker_controller_spec.rb b/spec/controllers/email_checker_controller_spec.rb index 685bd5de9..bcb232301 100644 --- a/spec/controllers/email_checker_controller_spec.rb +++ b/spec/controllers/email_checker_controller_spec.rb @@ -16,7 +16,7 @@ describe EmailCheckerController, type: :controller do let(:params) { { email: 'martin@orane.fr' } } it do expect(response).to have_http_status(:success) - expect(body).to eq({ success: true, email_suggestions: ['martin@orange.fr'] }) + expect(body).to eq({ success: true, suggestions: ['martin@orange.fr'] }) end end diff --git a/spec/lib/email_checker_spec.rb b/spec/lib/email_checker_spec.rb index f9c35ea91..fd650ff31 100644 --- a/spec/lib/email_checker_spec.rb +++ b/spec/lib/email_checker_spec.rb @@ -1,6 +1,6 @@ describe EmailChecker do describe 'check' do - subject { described_class.new } + subject { described_class } it 'works with identified use cases' do expect(subject.check(email: nil)).to eq({ success: false }) @@ -10,22 +10,22 @@ describe EmailChecker do # allow same domain expect(subject.check(email: "martin@orange.fr")).to eq({ success: true }) # find difference of 1 lev distance - expect(subject.check(email: "martin@orane.fr")).to eq({ success: true, email_suggestions: ['martin@orange.fr'] }) + expect(subject.check(email: "martin@orane.fr")).to eq({ success: true, suggestions: ['martin@orange.fr'] }) # find difference of 2 lev distance, only with same chars - expect(subject.check(email: "martin@oragne.fr")).to eq({ success: true, email_suggestions: ['martin@orange.fr'] }) + expect(subject.check(email: "martin@oragne.fr")).to eq({ success: true, suggestions: ['martin@orange.fr'] }) # ignore unknown domain expect(subject.check(email: "martin@ore.fr")).to eq({ success: true }) end it 'passes through real use cases, with levenshtein_distance 1' do - expect(subject.check(email: "martin@asn.com")).to eq({ success: true, email_suggestions: ['martin@msn.com'] }) - expect(subject.check(email: "martin@gamail.com")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] }) - expect(subject.check(email: "martin@glail.com")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] }) - expect(subject.check(email: "martin@gmail.coml")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] }) - expect(subject.check(email: "martin@gmail.con")).to eq({ success: true, email_suggestions: ['martin@gmail.com'] }) - expect(subject.check(email: "martin@hotmil.fr")).to eq({ success: true, email_suggestions: ['martin@hotmail.fr'] }) - expect(subject.check(email: "martin@mail.com")).to eq({ success: true, email_suggestions: ["martin@gmail.com", "martin@ymail.com", "martin@mailo.com"] }) - expect(subject.check(email: "martin@msc.com")).to eq({ success: true, email_suggestions: ["martin@msn.com", "martin@mac.com"] }) + expect(subject.check(email: "martin@asn.com")).to eq({ success: true, suggestions: ['martin@msn.com'] }) + expect(subject.check(email: "martin@gamail.com")).to eq({ success: true, suggestions: ['martin@gmail.com'] }) + expect(subject.check(email: "martin@glail.com")).to eq({ success: true, suggestions: ['martin@gmail.com'] }) + expect(subject.check(email: "martin@gmail.coml")).to eq({ success: true, suggestions: ['martin@gmail.com'] }) + expect(subject.check(email: "martin@gmail.con")).to eq({ success: true, suggestions: ['martin@gmail.com'] }) + expect(subject.check(email: "martin@hotmil.fr")).to eq({ success: true, suggestions: ['martin@hotmail.fr'] }) + expect(subject.check(email: "martin@mail.com")).to eq({ success: true, suggestions: ["martin@gmail.com", "martin@ymail.com", "martin@mailo.com"] }) + expect(subject.check(email: "martin@msc.com")).to eq({ success: true, suggestions: ["martin@msn.com", "martin@mac.com"] }) expect(subject.check(email: "martin@ymail.com")).to eq({ success: true }) end