mirror of
https://github.com/internetee/registry.git
synced 2025-07-23 03:06:14 +02:00
Add email checking with validation event saving
This commit is contained in:
parent
d4fe961e34
commit
c344b91d84
4 changed files with 65 additions and 4 deletions
53
app/interactions/actions/email_check.rb
Normal file
53
app/interactions/actions/email_check.rb
Normal 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
|
|
@ -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
|
||||
|
||||
store_accessor :event_data, :errors, :check_level, :email
|
||||
|
||||
belongs_to :validation_eventable, polymorphic: true
|
||||
|
||||
def event_type
|
||||
@event_type ||= ValidationEvent::EventType.new(read_attribute(:event_kind))
|
||||
@event_type ||= ValidationEvent::EventType.new(self[:event_kind])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,7 +7,7 @@ class CreateValidationEvents < ActiveRecord::Migration[6.1]
|
|||
|
||||
create_table :validation_events do |t|
|
||||
t.jsonb :event_data
|
||||
t.boolean :result
|
||||
t.boolean :success
|
||||
t.references :validation_eventable, polymorphic: true
|
||||
|
||||
t.timestamps
|
||||
|
|
|
@ -2612,7 +2612,7 @@ ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
|
|||
CREATE TABLE public.validation_events (
|
||||
id bigint NOT NULL,
|
||||
event_data jsonb,
|
||||
result boolean,
|
||||
success boolean,
|
||||
validation_eventable_type character varying,
|
||||
validation_eventable_id bigint,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue