From 294b60ebdc912bddb15dae85b5d8e5d7a92372cd Mon Sep 17 00:00:00 2001 From: dinsmol Date: Sat, 19 Feb 2022 22:43:05 +0300 Subject: [PATCH] Add removing old records for validation events --- app/interactions/actions/email_check.rb | 5 +++++ app/models/validation_event.rb | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/interactions/actions/email_check.rb b/app/interactions/actions/email_check.rb index 4b026ec2e..248952a47 100644 --- a/app/interactions/actions/email_check.rb +++ b/app/interactions/actions/email_check.rb @@ -12,12 +12,17 @@ module Actions result = check_email(email) save_result(result) filtering_old_failed_records(result) + remove_old_records! result.success ? log_success : log_failure(result) result.success end private + def remove_old_records! + validation_eventable.validation_events.old_records.destroy_all + end + def check_email(parsed_email) Truemail.validate(parsed_email, with: calculate_check_level).result end diff --git a/app/models/validation_event.rb b/app/models/validation_event.rb index 621fec030..1c0ec155d 100644 --- a/app/models/validation_event.rb +++ b/app/models/validation_event.rb @@ -6,7 +6,7 @@ # 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 - VALIDATION_PERIOD = 1.year.freeze + VALIDATION_PERIOD = 4.month.freeze VALID_CHECK_LEVELS = %w[regex mx smtp].freeze VALID_EVENTS_COUNT_THRESHOLD = 5 MX_CHECK = 3 @@ -27,7 +27,7 @@ class ValidationEvent < ApplicationRecord belongs_to :validation_eventable, polymorphic: true - scope :recent, -> { where('created_at < ?', Time.zone.now - VALIDATION_PERIOD) } + scope :old_records, -> { where('created_at < ?', Time.zone.now - VALIDATION_PERIOD) } scope :successful, -> { where(success: true) } scope :failed, -> { where(success: false) } scope :regex, -> { where('event_data @> ?', { 'check_level': 'regex' }.to_json) } @@ -38,7 +38,7 @@ class ValidationEvent < ApplicationRecord after_create :check_for_force_delete def self.validated_ids_by(klass) - recent.successful.where('validation_eventable_type = ?', klass) + old_records.successful.where('validation_eventable_type = ?', klass) .pluck(:validation_eventable_id) end