Migrate refresh status notes to CheckForceDeleteLift job

This commit is contained in:
Thiago Youssef 2022-09-25 10:31:31 -03:00
parent c6ff1ad300
commit 71d1e9bf47
5 changed files with 75 additions and 56 deletions

View file

@ -0,0 +1,53 @@
class CheckForceDeleteLift < ApplicationJob
queue_as :default
def perform
domains = Domain.where("(status_notes->'serverForceDelete') is not null")
.select { |d| d.registrant.need_to_lift_force_delete? }
handle_refresh_status(domains) if domains.present?
domains = Domain.where("force_delete_data->'template_name' = ?", 'invalid_email')
.where("force_delete_data->'force_delete_type' = ?", 'soft')
domains.each do |domain|
Domains::ForceDeleteLift::Base.run(domain: domain)
end
end
private
def handle_refresh_status(domains)
domains.each do |domain|
registrant = domain.registrant
event = registrant.validation_events.last
next if event.blank?
domain_list(event).each { |d| refresh_status_notes(d, registrant) }
end
end
def domain_list(event)
domain_contacts = Contact.where(email: event.email).map(&:domain_contacts).flatten
registrant_ids = Registrant.where(email: event.email).pluck(:id)
(domain_contacts.map(&:domain).flatten + Domain.where(registrant_id: registrant_ids)).uniq
end
def refresh_status_notes(domain, registrant)
return unless domain.status_notes[DomainStatus::FORCE_DELETE]
domain.status_notes[DomainStatus::FORCE_DELETE].slice!(registrant.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
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

View file

@ -1,35 +0,0 @@
class CheckForceDeleteLiftJob < ApplicationJob
def perform(validation_event_id, contact_id)
@event = ValidationEvent.find(validation_event_id)
@contact = Contact.find(contact_id)
return unless @contact.need_to_lift_force_delete?
domain_list.each { |domain| refresh_status_notes(domain) }
end
def domain_list
domain_contacts = Contact.where(email: @event.email).map(&:domain_contacts).flatten
registrant_ids = Registrant.where(email: @event.email).pluck(:id)
(domain_contacts.map(&:domain).flatten + Domain.where(registrant_id: registrant_ids)).uniq
end
def refresh_status_notes(domain)
return unless domain.status_notes[DomainStatus::FORCE_DELETE]
domain.status_notes[DomainStatus::FORCE_DELETE].slice!(@contact.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
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

View file

@ -1,12 +0,0 @@
class ForceDeleteLiftJob < ApplicationJob
queue_as :default
def perform
domains = Domain.where("force_delete_data->'template_name' = ?", 'invalid_email')
.where("force_delete_data->'force_delete_type' = ?", 'soft')
domains.each do |domain|
Domains::ForceDeleteLift::Base.run(domain: domain)
end
end
end

View file

@ -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_force_delete_lift
def self.validated_ids_by(klass)
old_records
.successful
@ -59,10 +57,4 @@ class ValidationEvent < ApplicationRecord
def object
validation_eventable
end
private
def check_force_delete_lift
CheckForceDeleteLiftJob.perform_later(id, object.id) if object.need_to_lift_force_delete?
end
end

View file

@ -512,12 +512,33 @@ class ForceDeleteTest < ActionMailer::TestCase
@domain.registrant.update(email: 'aaa@bbb.com', email_history: email)
@domain.registrant.verify_email
assert @domain.registrant.need_to_lift_force_delete?
ForceDeleteLiftJob.perform_now
CheckForceDeleteLift.perform_now
@domain.reload
assert_not @domain.force_delete_scheduled?
end
def test_lifts_force_delete_after_changing_to_valid_email
@domain.update(valid_to: Time.zone.parse('2012-08-05'))
assert_not @domain.force_delete_scheduled?
travel_to Time.zone.parse('2010-07-05')
@domain.registrant.update_attribute(:email, '`@internet.ee')
3.times { @domain.registrant.verify_email }
perform_check_force_delete_job(@domain.registrant.id)
@domain.reload
assert @domain.force_delete_scheduled?
@domain.registrant.update(email: 'aaa@bbb.ee')
@domain.registrant.verify_email
Spy.on_instance_method(Registrant, :need_to_lift_force_delete?).and_return(true)
perform_enqueued_jobs { CheckForceDeleteLift.perform_now }
@domain.reload
assert_not @domain.force_delete_scheduled?
end
def prepare_bounced_email_address(email)
@bounced_mail = BouncedMailAddress.new
@bounced_mail.email = email