mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 06:34:46 +02:00
Move WhoisUpdate job to interactor
This commit is contained in:
parent
6841703349
commit
a54249f6ff
4 changed files with 120 additions and 83 deletions
48
app/interactions/whois/delete_record.rb
Normal file
48
app/interactions/whois/delete_record.rb
Normal file
|
@ -0,0 +1,48 @@
|
|||
module Whois
|
||||
class DeleteRecord < ActiveInteraction::Base
|
||||
string :name
|
||||
string :type
|
||||
|
||||
validates :type, inclusion: { in: %w[reserved blocked domain disputed zone] }
|
||||
|
||||
def execute
|
||||
send "delete_#{type}", name
|
||||
end
|
||||
|
||||
# 1. deleting own
|
||||
# 2. trying to regenerate reserved in order domain is still in the list
|
||||
def delete_domain(name)
|
||||
WhoisRecord.where(name: name).destroy_all
|
||||
|
||||
BlockedDomain.find_by(name: name).try(:generate_data)
|
||||
ReservedDomain.find_by(name: name).try(:generate_data)
|
||||
Dispute.active.find_by(domain_name: name).try(:generate_data)
|
||||
end
|
||||
|
||||
def delete_reserved(name)
|
||||
remove_status_from_whois(domain_name: name, domain_status: 'Reserved')
|
||||
end
|
||||
|
||||
def delete_blocked(name)
|
||||
delete_reserved(name)
|
||||
end
|
||||
|
||||
def delete_disputed(name)
|
||||
return if Dispute.active.find_by(domain_name: name).present?
|
||||
|
||||
remove_status_from_whois(domain_name: name, domain_status: 'disputed')
|
||||
end
|
||||
|
||||
def delete_zone(name)
|
||||
WhoisRecord.where(name: name).destroy_all
|
||||
Whois::Record.where(name: name).destroy_all
|
||||
end
|
||||
|
||||
def remove_status_from_whois(domain_name:, domain_status:)
|
||||
Whois::Record.where(name: domain_name).each do |r|
|
||||
r.json['status'] = r.json['status'].delete_if { |status| status == domain_status }
|
||||
r.json['status'].blank? ? r.destroy : r.save
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
39
app/interactions/whois/update.rb
Normal file
39
app/interactions/whois/update.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
module Whois
|
||||
class Update < ActiveInteraction::Base
|
||||
array :names
|
||||
string :type
|
||||
|
||||
validates :type, inclusion: { in: %w[reserved blocked domain disputed zone] }
|
||||
|
||||
def execute
|
||||
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{type}"
|
||||
|
||||
klass = determine_class
|
||||
|
||||
Array(names).each do |name|
|
||||
record = find_record(klass, name)
|
||||
if record
|
||||
Whois::UpdateRecord.run(record: record, type: type)
|
||||
else
|
||||
Whois::DeleteRecord.run(name: name, type: type)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def determine_class
|
||||
case type
|
||||
when 'reserved' then ReservedDomain
|
||||
when 'blocked' then BlockedDomain
|
||||
when 'domain' then Domain
|
||||
when 'disputed' then Dispute.active
|
||||
else DNS::Zone
|
||||
end
|
||||
end
|
||||
|
||||
def find_record(klass, name)
|
||||
klass == DNS::Zone ? klass.find_by(origin: name) : klass.find_by(name: name)
|
||||
end
|
||||
end
|
||||
end
|
32
app/interactions/whois/update_record.rb
Normal file
32
app/interactions/whois/update_record.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module Whois
|
||||
class UpdateRecord < ActiveInteraction::Base
|
||||
interface :record
|
||||
string :type
|
||||
|
||||
validates :type, inclusion: { in: %w[reserved blocked domain disputed zone] }
|
||||
|
||||
def execute
|
||||
send "update_#{type}", record
|
||||
end
|
||||
|
||||
def update_domain(domain)
|
||||
domain.whois_record ? domain.whois_record.save : domain.create_whois_record
|
||||
end
|
||||
|
||||
def update_reserved(record)
|
||||
record.generate_data
|
||||
end
|
||||
|
||||
def update_blocked(record)
|
||||
update_reserved(record)
|
||||
end
|
||||
|
||||
def update_disputed(record)
|
||||
update_reserved(record)
|
||||
end
|
||||
|
||||
def update_zone(record)
|
||||
update_reserved(record)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,87 +1,5 @@
|
|||
class UpdateWhoisRecordJob < Que::Job
|
||||
|
||||
def run(names, type)
|
||||
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{type}"
|
||||
|
||||
klass = determine_class(type)
|
||||
|
||||
Array(names).each do |name|
|
||||
record = find_record(klass, name)
|
||||
if record
|
||||
send "update_#{type}", record
|
||||
else
|
||||
send "delete_#{type}", name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def find_record(klass, name)
|
||||
klass == DNS::Zone ? klass.find_by(origin: name) : klass.find_by(name: name)
|
||||
end
|
||||
|
||||
def determine_class(type)
|
||||
case type
|
||||
when 'reserved' then ReservedDomain
|
||||
when 'blocked' then BlockedDomain
|
||||
when 'domain' then Domain
|
||||
when 'disputed' then Dispute.active
|
||||
when 'zone' then DNS::Zone
|
||||
end
|
||||
end
|
||||
|
||||
def update_domain(domain)
|
||||
domain.whois_record ? domain.whois_record.save : domain.create_whois_record
|
||||
end
|
||||
|
||||
def update_reserved(record)
|
||||
record.generate_data
|
||||
end
|
||||
|
||||
def update_blocked(record)
|
||||
update_reserved(record)
|
||||
end
|
||||
|
||||
def update_disputed(record)
|
||||
update_reserved(record)
|
||||
end
|
||||
|
||||
def update_zone(record)
|
||||
update_reserved(record)
|
||||
end
|
||||
|
||||
# 1. deleting own
|
||||
# 2. trying to regenerate reserved in order domain is still in the list
|
||||
def delete_domain(name)
|
||||
WhoisRecord.where(name: name).destroy_all
|
||||
|
||||
BlockedDomain.find_by(name: name).try(:generate_data)
|
||||
ReservedDomain.find_by(name: name).try(:generate_data)
|
||||
Dispute.active.find_by(domain_name: name).try(:generate_data)
|
||||
end
|
||||
|
||||
def delete_reserved(name)
|
||||
remove_status_from_whois(domain_name: name, domain_status: 'Reserved')
|
||||
end
|
||||
|
||||
def delete_blocked(name)
|
||||
delete_reserved(name)
|
||||
end
|
||||
|
||||
def delete_disputed(name)
|
||||
return if Dispute.active.find_by(domain_name: name).present?
|
||||
|
||||
remove_status_from_whois(domain_name: name, domain_status: 'disputed')
|
||||
end
|
||||
|
||||
def delete_zone(name)
|
||||
WhoisRecord.where(name: name).destroy_all
|
||||
Whois::Record.where(name: name).destroy_all
|
||||
end
|
||||
|
||||
def remove_status_from_whois(domain_name:, domain_status:)
|
||||
Whois::Record.where(name: domain_name).each do |r|
|
||||
r.json['status'] = r.json['status'].delete_if { |status| status == domain_status }
|
||||
r.json['status'].blank? ? r.destroy : r.save
|
||||
end
|
||||
Whois::Update.run(names: [names].flatten, type: type)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue