mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
Create check force delete rake task
This commit is contained in:
parent
c246bbaa6d
commit
e159821a86
5 changed files with 93 additions and 47 deletions
|
@ -1,12 +1,42 @@
|
|||
class ValidationEventCheckForceDeleteJob < ApplicationJob
|
||||
def perform(event_id, contact_id)
|
||||
event = ValidationEvent.find(event_id)
|
||||
def perform(contact_id)
|
||||
contact = Contact.find(contact_id)
|
||||
email = contact.email
|
||||
|
||||
if contact.need_to_start_force_delete?
|
||||
event.start_force_delete
|
||||
Domains::ForceDeleteEmail::Base.run(email: email)
|
||||
elsif contact.need_to_lift_force_delete?
|
||||
event.refresh_status_notes
|
||||
refresh_status_notes(domain_list(email))
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def refresh_status_notes(domain_list)
|
||||
domain_list.each do |domain|
|
||||
force_delete_emails = domain.status_notes[DomainStatus::FORCE_DELETE]
|
||||
next unless force_delete_emails
|
||||
|
||||
force_delete_emails.slice!(object.email_history)
|
||||
force_delete_emails.lstrip!
|
||||
domain.save(validate: false)
|
||||
|
||||
notify_registrar(domain) unless domain.status_notes[DomainStatus::FORCE_DELETE].empty?
|
||||
end
|
||||
end
|
||||
|
||||
def domain_list(email)
|
||||
domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten
|
||||
registrant_ids = Registrant.where(email: email).pluck(:id)
|
||||
|
||||
(domain_contacts.map(&:domain).flatten + Domain.where(registrant_id: registrant_ids)).uniq
|
||||
end
|
||||
|
||||
def notify_registrar(domain)
|
||||
domain.registrar.notifications.create!(text: I18n.t('force_delete_auto_email',
|
||||
domain_name: domain.name,
|
||||
outzone_date: domain.outzone_date,
|
||||
purge_date: domain.purge_date,
|
||||
email: domain.status_notes[DomainStatus::FORCE_DELETE]))
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,16 +12,11 @@ module EmailVerifable
|
|||
end
|
||||
|
||||
def need_to_start_force_delete?
|
||||
flag = false
|
||||
ValidationEvent::INVALID_EVENTS_COUNT_BY_LEVEL.each do |level, count|
|
||||
if validation_events.count >= count && validate_email_data(level: level, count: count)
|
||||
flag = true
|
||||
validation_events.count >= count && validate_email_data(level: level, count: count)
|
||||
end
|
||||
end
|
||||
|
||||
flag
|
||||
end
|
||||
|
||||
def need_to_lift_force_delete?
|
||||
validation_events.failed.empty? ||
|
||||
ValidationEvent::REDEEM_EVENTS_COUNT_BY_LEVEL.any? do |level, count|
|
||||
|
|
|
@ -35,8 +35,6 @@ class ValidationEvent < ApplicationRecord
|
|||
scope :smtp, -> { where('event_data @> ?', { 'check_level': 'smtp' }.to_json) }
|
||||
scope :by_object, ->(object) { where(validation_eventable: object) }
|
||||
|
||||
after_create :check_for_force_delete
|
||||
|
||||
def self.validated_ids_by(klass)
|
||||
recent.successful.where('validation_eventable_type = ?', klass)
|
||||
.pluck(:validation_eventable_id)
|
||||
|
@ -57,39 +55,4 @@ class ValidationEvent < ApplicationRecord
|
|||
def object
|
||||
validation_eventable
|
||||
end
|
||||
|
||||
def check_for_force_delete
|
||||
ValidationEventCheckForceDeleteJob.perform_later(id, object.id)
|
||||
end
|
||||
|
||||
def start_force_delete
|
||||
Domains::ForceDeleteEmail::Base.run(email: email)
|
||||
end
|
||||
|
||||
def refresh_status_notes
|
||||
domain_list.each do |domain|
|
||||
next unless domain.status_notes[DomainStatus::FORCE_DELETE]
|
||||
|
||||
domain.status_notes[DomainStatus::FORCE_DELETE].slice!(object.email_history)
|
||||
domain.status_notes[DomainStatus::FORCE_DELETE].lstrip!
|
||||
domain.save(validate: false)
|
||||
|
||||
notify_registrar(domain) unless domain.status_notes[DomainStatus::FORCE_DELETE].empty?
|
||||
end
|
||||
end
|
||||
|
||||
def domain_list
|
||||
domain_contacts = Contact.where(email: email).map(&:domain_contacts).flatten
|
||||
registrant_ids = Registrant.where(email: email).pluck(:id)
|
||||
|
||||
(domain_contacts.map(&:domain).flatten + Domain.where(registrant_id: registrant_ids)).uniq
|
||||
end
|
||||
|
||||
def notify_registrar(domain)
|
||||
domain.registrar.notifications.create!(text: I18n.t('force_delete_auto_email',
|
||||
domain_name: domain.name,
|
||||
outzone_date: domain.outzone_date,
|
||||
purge_date: domain.purge_date,
|
||||
email: domain.status_notes[DomainStatus::FORCE_DELETE]))
|
||||
end
|
||||
end
|
||||
|
|
12
lib/tasks/check_force_delete.rake
Normal file
12
lib/tasks/check_force_delete.rake
Normal file
|
@ -0,0 +1,12 @@
|
|||
desc 'Check Force Delete'
|
||||
task :check_force_delete do
|
||||
|
||||
invalid_contacts = Contact.joins(:validation_events).select do |contact|
|
||||
events = contact.validation_events
|
||||
events.mx.count >= 3 || events.regex.present?
|
||||
end.uniq(&:id)
|
||||
|
||||
invalid_contacts.each do |contact|
|
||||
ValidationEventCheckForceDeleteJob.perform_later(contact.id)
|
||||
end
|
||||
end
|
46
test/tasks/check_force_delete_test.rb
Normal file
46
test/tasks/check_force_delete_test.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
require 'test_helper'
|
||||
|
||||
class CheckForceDeleteTaskTest < ActiveSupport::TestCase
|
||||
include ActiveJob::TestHelper
|
||||
|
||||
def setup
|
||||
@contact = contacts(:john)
|
||||
@invalid_contact = contacts(:invalid_email)
|
||||
end
|
||||
|
||||
def test_enque_force_delete_when_three_invalid_records_by_mx
|
||||
trumail_results = OpenStruct.new(success: false,
|
||||
email: @contact.email,
|
||||
domain: 'box.tests',
|
||||
errors: { mx: 'target host(s) not found' })
|
||||
|
||||
Spy.on_instance_method(Actions::EmailCheck, :check_email).and_return(trumail_results)
|
||||
Spy.on_instance_method(Actions::AAndAaaaEmailValidation, :call).and_return([])
|
||||
|
||||
action = Actions::EmailCheck.new(email: @contact.email,
|
||||
validation_eventable: @contact,
|
||||
check_level: 'mx')
|
||||
3.times do
|
||||
action.call
|
||||
end
|
||||
|
||||
run_task
|
||||
|
||||
assert_enqueued_jobs 1
|
||||
assert_enqueued_with(job: ValidationEventCheckForceDeleteJob, args: [@contact.id])
|
||||
end
|
||||
|
||||
def test_enque_force_delete_when_invalid_record_by_regex
|
||||
@invalid_contact.verify_email
|
||||
run_task
|
||||
|
||||
assert_enqueued_jobs 1
|
||||
assert_enqueued_with(job: ValidationEventCheckForceDeleteJob, args: [@invalid_contact.id])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def run_task
|
||||
Rake::Task['check_force_delete'].execute
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue