Improve WhoisRecord create/update for disputed domain

This commit is contained in:
Karl Erik Õunapuu 2020-04-27 18:24:08 +03:00
parent 39791f5755
commit cca7745c64
2 changed files with 28 additions and 12 deletions

View file

@ -1,13 +1,13 @@
class UpdateWhoisRecordJob < Que::Job class UpdateWhoisRecordJob < Que::Job
def run(names, type) def run(names, type)
::PaperTrail.whodunnit = "job - #{self.class.name} - #{type}" ::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{type}"
klass = case type klass = case type
when 'reserved' then ReservedDomain when 'reserved' then ReservedDomain
when 'blocked' then BlockedDomain when 'blocked' then BlockedDomain
when 'domain' then Domain when 'domain' then Domain
when 'disputed' then Dispute when 'disputed' then Dispute.active
end end
Array(names).each do |name| Array(names).each do |name|
@ -56,6 +56,11 @@ class UpdateWhoisRecordJob < Que::Job
end end
def delete_disputed(name) def delete_disputed(name)
delete_reserved(name) return if Dispute.active.find_by(domain_name: name).present?
Whois::Record.where(name: name).each do |r|
r.json['status'] = r.json['status'].delete_if { |status| status == 'disputed' }
r.save!
end
end end
end end

View file

@ -12,6 +12,7 @@ class Dispute < ApplicationRecord
end end
before_save :set_expiry_date before_save :set_expiry_date
before_save :sync_reserved_password before_save :sync_reserved_password
before_save :generate_data
after_destroy :remove_data after_destroy :remove_data
scope :expired, -> { where('expires_at < ?', Time.zone.today) } scope :expired, -> { where('expires_at < ?', Time.zone.today) }
@ -36,23 +37,33 @@ class Dispute < ApplicationRecord
end end
def generate_data def generate_data
return if Domain.where(name: domain_name).any?
wr = Whois::Record.find_or_initialize_by(name: domain_name) wr = Whois::Record.find_or_initialize_by(name: domain_name)
wr.json = @json = generate_json # we need @json to bind to class if Domain.where(name: domain_name).any?
wr.save @json = wr.json.with_indifferent_access
@json[:status] << 'disputed' unless @json[:status].include? 'disputed'
wr.json = @json
else
wr.json = @json = generate_json(wr) # we need @json to bind to class
end
wr.save!
end end
alias_method :update_whois_record, :generate_data
def close def close
self.closed = true self.closed = true
save! save!
end end
def generate_json def generate_json(record)
h = HashWithIndifferentAccess.new h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed'])
h[:name] = domain_name return h if record.json.empty?
h[:status] = ['Disputed']
h status_arr = (record.json['status'] ||= [])
status_arr.push('disputed') unless status_arr.include? 'disputed'
record.json['status'] = status_arr
record.json
end end
def remove_data def remove_data