Add email checking with validation event saving

This commit is contained in:
Alex Sherman 2021-06-29 16:13:40 +05:00
parent d4fe961e34
commit c344b91d84
4 changed files with 65 additions and 4 deletions

View file

@ -0,0 +1,53 @@
module Actions
class EmailCheck
attr_reader :email, :validation_eventable, :check_level
def initialize(email:, validation_eventable:, check_level: nil)
@email = email
@validation_eventable = validation_eventable
@check_level = check_level || :regex
end
def call
result = check_email
save_result(result)
log_failure(result) unless result.success
result.success
end
private
def check_email
Truemail.validate(email, with: check_level).result
end
def save_result(result)
validation_eventable.validation_events.create(validation_event_attrs(result))
end
def validation_event_attrs(result)
{
event_data: event_data(result),
event_type: ValidationEvent::EventType::TYPES[:email_validation],
success: result.success,
}
end
def logger
@logger ||= Rails.logger
end
def event_data(result)
result.to_h.merge(check_level: check_level)
end
def log_failure(result)
logger.info "Failed to validate email #{email} for the #{log_object_id}."
logger.info "Validation level #{check_level}, the result was #{result}"
end
def log_object_id
"#{validation_eventable.class}: #{validation_eventable.id}"
end
end
end

View file

@ -1,9 +1,17 @@
class ValidationEvent < ActiveRecord::Base # frozen_string_literal: true
# Class to store validation events. Need to include boolean `success` field - was validation event
# successful or not.
# Types of events supported so far stored in ValidationEvent::EventType::TYPES
# For email_validation event kind also check_level (regex/mx/smtp) is stored in the event_data
class ValidationEvent < ApplicationRecord
enum event_type: ValidationEvent::EventType::TYPES, _suffix: true enum event_type: ValidationEvent::EventType::TYPES, _suffix: true
store_accessor :event_data, :errors, :check_level, :email
belongs_to :validation_eventable, polymorphic: true belongs_to :validation_eventable, polymorphic: true
def event_type def event_type
@event_type ||= ValidationEvent::EventType.new(read_attribute(:event_kind)) @event_type ||= ValidationEvent::EventType.new(self[:event_kind])
end end
end end

View file

@ -7,7 +7,7 @@ class CreateValidationEvents < ActiveRecord::Migration[6.1]
create_table :validation_events do |t| create_table :validation_events do |t|
t.jsonb :event_data t.jsonb :event_data
t.boolean :result t.boolean :success
t.references :validation_eventable, polymorphic: true t.references :validation_eventable, polymorphic: true
t.timestamps t.timestamps

View file

@ -2612,7 +2612,7 @@ ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
CREATE TABLE public.validation_events ( CREATE TABLE public.validation_events (
id bigint NOT NULL, id bigint NOT NULL,
event_data jsonb, event_data jsonb,
result boolean, success boolean,
validation_eventable_type character varying, validation_eventable_type character varying,
validation_eventable_id bigint, validation_eventable_id bigint,
created_at timestamp(6) without time zone NOT NULL, created_at timestamp(6) without time zone NOT NULL,