Apply dispute statusses via DisputeStatusUpdateJob

This commit is contained in:
Karl Erik Õunapuu 2020-04-29 15:44:52 +03:00
parent cca7745c64
commit 26e7fd870c
3 changed files with 99 additions and 12 deletions

View file

@ -0,0 +1,67 @@
class DisputeStatusUpdateJob < Que::Job
def run
@backlog = { activated: 0, closed: 0, active_fail: [], close_fail: [] }
close_disputes
activate_disputes
Rails.logger.info "DisputeStatusCloseJob - All done. Closed #{@backlog[:closed]} and " \
"activated #{@backlog[:closed]} disputes."
show_failed_disputes unless @backlog[:active_fail].empty? && @backlog[:close_fail].empty?
end
def close_disputes
disputes = Dispute.where(closed: false).where('expires_at < ?', Date.today).all
Rails.logger.info "DisputeStatusCloseJob - Found #{disputes.count} closable disputes"
disputes.each do |dispute|
puts "attempnt"
close_dispute(dispute)
end
end
def activate_disputes
disputes = Dispute.where(closed: false, starts_at: Date.today).all
Rails.logger.info "DisputeStatusCloseJob - Found #{disputes.count} activatable disputes"
disputes.each do |dispute|
activate_dispute(dispute)
end
end
def close_dispute(dispute)
if dispute.close
Rails.logger.info 'DisputeStatusCloseJob - Closed dispute ' \
"##{dispute.id} for '#{dispute.domain_name}'"
@backlog[:closed] += 1
else
Rails.logger.info 'DisputeStatusCloseJob - Failed to close dispute ' \
"##{dispute.id} for '#{dispute.domain_name}'"
@backlog[:close_fail] << dispute.id
end
end
def activate_dispute(dispute)
if dispute.generate_data
Rails.logger.info 'DisputeStatusCloseJob - Activated dispute ' \
"##{dispute.id} for '#{dispute.domain_name}'"
@backlog[:activated] += 1
else
Rails.logger.info 'DisputeStatusCloseJob - Failed to activate dispute ' \
"##{dispute.id} for '#{dispute.domain_name}'"
@backlog[:active_fail] << dispute.id
end
end
def show_failed_disputes
if @backlog[:close_fail].any?
Rails.logger.info('DisputeStatuseCloseJob - Failed to close disputes with Ids:' \
"#{@backlog[:close_fail]}")
end
return unless @backlog[:active_fail].any?
Rails.logger.info('DisputeStatuseCloseJob - Failed to activate disputes with Ids:' \
"#{@backlog[:active_fail]}")
end
end

View file

@ -60,7 +60,7 @@ class UpdateWhoisRecordJob < Que::Job
Whois::Record.where(name: name).each do |r| Whois::Record.where(name: name).each do |r|
r.json['status'] = r.json['status'].delete_if { |status| status == 'disputed' } r.json['status'] = r.json['status'].delete_if { |status| status == 'disputed' }
r.save! r.json['status'].blank? ? r.destroy : r.save
end end
end end
end end

View file

@ -15,8 +15,8 @@ class Dispute < ApplicationRecord
before_save :generate_data 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 < ?', Date.today) }
scope :active, -> { where('expires_at > ? AND closed = false', Time.zone.today) } scope :active, -> { where('expires_at > ? AND closed = false', Date.today) }
scope :closed, -> { where(closed: true) } scope :closed, -> { where(closed: true) }
alias_attribute :name, :domain_name alias_attribute :name, :domain_name
@ -26,6 +26,10 @@ class Dispute < ApplicationRecord
dispute.update(closed: true) if dispute.present? dispute.update(closed: true) if dispute.present?
end end
def for_active_domain?
Domain.where(name: domain_name).any?
end
def set_expiry_date def set_expiry_date
return if starts_at.blank? return if starts_at.blank?
@ -37,27 +41,43 @@ class Dispute < ApplicationRecord
end end
def generate_data def generate_data
return if starts_at > Date.today
wr = Whois::Record.find_or_initialize_by(name: domain_name) wr = Whois::Record.find_or_initialize_by(name: domain_name)
if Domain.where(name: domain_name).any? if for_active_domain?
@json = wr.json.with_indifferent_access wr.json['status'] << 'disputed' unless wr.json['status'].include? 'disputed'
@json[:status] << 'disputed' unless @json[:status].include? 'disputed'
wr.json = @json
else else
wr.json = @json = generate_json(wr) # we need @json to bind to class wr.json = generate_json(wr) # we need @json to bind to class
end end
wr.save! wr.save
end end
alias_method :update_whois_record, :generate_data alias_method :update_whois_record, :generate_data
def close def close
self.closed = true return false unless update(closed: true)
save! return if Dispute.active.where(domain_name: domain_name).any?
puts "PASS"
whois_record = Whois::Record.find_or_initialize_by(name: domain_name)
return true if remove_whois_data(whois_record)
false
end
def remove_whois_data(record)
record.json['status'] = record.json['status'].delete_if { |status| status == 'disputed' }
if record.json['status'].blank?
return true if record.destroy
return false
end
record.save
end end
def generate_json(record) def generate_json(record)
h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed']) h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed'])
return h if record.json.empty? return h if record.json.blank?
status_arr = (record.json['status'] ||= []) status_arr = (record.json['status'] ||= [])
status_arr.push('disputed') unless status_arr.include? 'disputed' status_arr.push('disputed') unless status_arr.include? 'disputed'