Skip to content

Commit

Permalink
assign the limit of validation records
Browse files Browse the repository at this point in the history
  • Loading branch information
olegphenomenon committed Oct 24, 2022
1 parent 83f951b commit 37c6239
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
28 changes: 14 additions & 14 deletions app/interactions/actions/email_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def initialize(email:, validation_eventable:, check_level: nil)
def call
result = check_email(email)
save_result(result)
filtering_old_failed_records(result)
result.success ? log_success : log_failure(result)
result.success
end
Expand All @@ -26,25 +25,25 @@ def calculate_check_level
Rails.env.test? && check_level == 'smtp' ? :mx : check_level.to_sym
end

def filtering_old_failed_records(result)
if @check_level == "mx" && !result.success && validation_eventable.validation_events.count > 3
validation_eventable.validation_events.order!(created_at: :asc)
while validation_eventable.validation_events.count > 3
validation_eventable.validation_events.first.destroy
def filtering_old_failed_records(result, contact)
if @check_level == "mx" && !result.success && contact.validation_events.count > 3
contact.validation_events.order!(created_at: :asc)
while contact.validation_events.count > 3
contact.validation_events.first.destroy
end
end

if @check_level == "mx" && result.success && validation_eventable.validation_events.count > 1
validation_eventable.validation_events.order!(created_at: :asc)
while validation_eventable.validation_events.count > 1
validation_eventable.validation_events.first.destroy
if @check_level == "regex" && result.success && contact.validation_events.count > 1
contact.validation_events.order!(created_at: :asc)
while contact.validation_events.count > 1
contact.validation_events.first.destroy
end
end

if @check_level == "smtp" && validation_eventable.validation_events.count > 1
validation_eventable.validation_events.order!(created_at: :asc)
while validation_eventable.validation_events.count > 1
validation_eventable.validation_events.first.destroy
if @check_level == "smtp" && contact.validation_events.count > 1
contact.validation_events.order!(created_at: :asc)
while contact.validation_events.count > 1
contact.validation_events.first.destroy
end
end
end
Expand All @@ -64,6 +63,7 @@ def save_result(result)
contacts.find_in_batches(batch_size: 500) do |contact_batches|
contact_batches.each do |contact|
contact.validation_events.create(validation_event_attrs(result))
filtering_old_failed_records(result, contact)
end
end
rescue ActiveRecord::RecordNotSaved
Expand Down
44 changes: 43 additions & 1 deletion test/interactions/email_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,49 @@ def test_should_remove_valid_validation_record_if_there_count_more_than_one
action.call
end

assert_equal @contact.validation_events.count, 1
assert_equal @contact.validation_events.count, 3
assert @contact.validation_events.last.success
end

def test_should_remove_old_record_if_validation_pass_the_limit
trumail_results = OpenStruct.new(success: false,
email: @contact.email,
domain: "box.tests",
errors: {:mx=>"target host(s) not found"})

action = Actions::EmailCheck.new(email: @contact.email,
validation_eventable: @contact,
check_level: 'mx')

action.stub :check_email, trumail_results do
4.times do
action.call
end
end

assert_equal @contact.validation_events.count, 3
end

def test_should_remove_old_record_if_multiple_contacts_has_the_same_email
contact_two = contacts(:william)
contact_two.update(email: @contact.email)
contact_two.reload
trumail_results = OpenStruct.new(success: false,
email: @contact.email,
domain: "box.tests",
errors: {:mx=>"target host(s) not found"})

action = Actions::EmailCheck.new(email: @contact.email,
validation_eventable: @contact,
check_level: 'mx')

action.stub :check_email, trumail_results do
4.times do
action.call
end
end

assert_equal @contact.validation_events.count, 3
assert_equal contact_two.validation_events.count, 3
end
end

0 comments on commit 37c6239

Please sign in to comment.