Merge pull request #2201 from internetee/optimize-validation-event-model

Optimize verify email
This commit is contained in:
Timo Võhmar 2021-11-19 13:08:28 +02:00 committed by GitHub
commit 9bdcc9e6b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 177 additions and 104 deletions

View file

@ -10,25 +10,26 @@ module EmailVerifable
end
def validate_email_data(level:, count:)
validation_events.recent.order(id: :desc).limit(count).all? do |event|
validation_events.order(created_at: :desc).limit(count).all? do |event|
event.check_level == level.to_s && event.failed?
end
end
def need_to_start_force_delete?
flag = false
ValidationEvent::INVALID_EVENTS_COUNT_BY_LEVEL.each do |level, count|
if validation_events.recent.count >= count && validate_email_data(level: level, count: count)
return true
if validation_events.count >= count && validate_email_data(level: level, count: count)
flag = true
end
end
false
flag
end
def need_to_lift_force_delete?
validation_events.recent.failed.empty? ||
validation_events.failed.empty? ||
ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL.any? do |level, count|
validation_events.recent.order(id: :desc).limit(count).all? do |event|
validation_events.order(created_at: :desc).limit(count).all? do |event|
event.check_level == level.to_s && event.successful?
end
end

View file

@ -6,13 +6,13 @@
# 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.month.freeze
VALIDATION_PERIOD = 1.year.freeze
VALID_CHECK_LEVELS = %w[regex mx smtp].freeze
VALID_EVENTS_COUNT_THRESHOLD = 5
INVALID_EVENTS_COUNT_BY_LEVEL = {
regex: 1,
mx: 5,
mx: 3,
smtp: 1,
}.freeze
@ -26,7 +26,7 @@ class ValidationEvent < ApplicationRecord
belongs_to :validation_eventable, polymorphic: true
scope :recent, -> { where('created_at > ?', Time.zone.now - VALIDATION_PERIOD) }
scope :recent, -> { 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) }
@ -69,5 +69,15 @@ class ValidationEvent < ApplicationRecord
Domains::ForceDeleteEmail::Base.run(email: email)
end
def lift_force_delete; end
def lift_force_delete
domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten
registrant_ids = Registrant.where(email: email).pluck(:id)
domains = domain_contacts.map(&:domain).flatten +
Domain.where(registrant_id: registrant_ids)
domains.each do |domain|
Domains::ForceDeleteLift::Base.run(domain: domain)
end
end
end