Move functionality from job to interactor

This commit is contained in:
Alex Sherman 2020-11-26 15:01:09 +05:00
parent 31bfe19d77
commit 4b7cffbb57
5 changed files with 77 additions and 32 deletions

View file

@ -0,0 +1,22 @@
module DomainUpdateConfirmInteraction
class Base < ActiveInteraction::Base
object :domain,
class: Domain,
description: 'Domain to confirm update'
string :action
string :initiator,
default: nil
validates :domain, :action, presence: true
validates :action, inclusion: { in: [RegistrantVerification::CONFIRMED,
RegistrantVerification::REJECTED] }
def raise_errors!(domain)
if domain.errors.any?
message = "domain #{domain.name} failed with errors #{domain.errors.full_messages}"
throw message
end
end
end
end

View file

@ -0,0 +1,15 @@
module DomainUpdateConfirmInteraction
class ProcessAction < Base
def execute
::PaperTrail.request.whodunnit = "interaction - #{self.class.name} - #{action} by"\
" #{initiator}"
case action
when RegistrantVerification::CONFIRMED
compose(ProcessUpdateConfirmed, inputs)
when RegistrantVerification::REJECTED
compose(ProcessUpdateRejected, inputs)
end
end
end
end

View file

@ -0,0 +1,20 @@
module DomainUpdateConfirmInteraction
class ProcessUpdateConfirmed < Base
def execute
ActiveRecord::Base.transaction do
domain.is_admin = true
old_registrant = domain.registrant
domain.notify_registrar(:poll_pending_update_confirmed_by_registrant)
domain.apply_pending_update!
raise_errors!(domain)
domain.clean_pendings!
raise_errors!(domain)
RegistrantChange.new(domain: domain, old_registrant: old_registrant).confirm
end
end
end
end

View file

@ -0,0 +1,16 @@
module DomainUpdateConfirmInteraction
class ProcessUpdateRejected < Base
def execute
ActiveRecord::Base.transaction do
RegistrantChangeMailer.rejected(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant).deliver_now
domain.notify_registrar(:poll_pending_update_rejected_by_registrant)
domain.preclean_pendings
domain.clean_pendings!
end
end
end
end

View file

@ -2,37 +2,9 @@ class DomainUpdateConfirmJob < ApplicationJob
queue_as :default
def perform(domain_id, action, initiator = nil)
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{action} by #{initiator}"
# it's recommended to keep transaction against job table as short as possible.
ActiveRecord::Base.transaction do
domain = Epp::Domain.find(domain_id)
domain.is_admin = true
case action
when RegistrantVerification::CONFIRMED
old_registrant = domain.registrant
domain.notify_registrar(:poll_pending_update_confirmed_by_registrant)
raise_errors!(domain)
domain.apply_pending_update!
raise_errors!(domain)
domain.clean_pendings!
raise_errors!(domain)
RegistrantChange.new(domain: domain, old_registrant: old_registrant).confirm
when RegistrantVerification::REJECTED
RegistrantChangeMailer.rejected(domain: domain,
registrar: domain.registrar,
registrant: domain.registrant).deliver_now
domain.notify_registrar(:poll_pending_update_rejected_by_registrant)
domain.preclean_pendings
domain.clean_pendings!
end
end
end
def raise_errors!(domain)
throw "domain #{domain.name} failed with errors #{domain.errors.full_messages}" if domain.errors.any?
domain = Epp::Domain.find(domain_id)
DomainUpdateConfirmInteraction::ProcessAction.run(domain: domain,
action: action,
initiator: initiator)
end
end