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
def run(names, type)
::PaperTrail.whodunnit = "job - #{self.class.name} - #{type}"
::PaperTrail.request.whodunnit = "job - #{self.class.name} - #{type}"
klass = case type
when 'reserved' then ReservedDomain
when 'blocked' then BlockedDomain
when 'domain' then Domain
when 'disputed' then Dispute
when 'disputed' then Dispute.active
end
Array(names).each do |name|
@ -56,6 +56,11 @@ class UpdateWhoisRecordJob < Que::Job
end
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

View file

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