diff --git a/app/jobs/dispute_status_update_job.rb b/app/jobs/dispute_status_update_job.rb new file mode 100644 index 000000000..5f8f1c6b1 --- /dev/null +++ b/app/jobs/dispute_status_update_job.rb @@ -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 diff --git a/app/jobs/update_whois_record_job.rb b/app/jobs/update_whois_record_job.rb index 0642aed8d..4740a12cb 100644 --- a/app/jobs/update_whois_record_job.rb +++ b/app/jobs/update_whois_record_job.rb @@ -60,7 +60,7 @@ class UpdateWhoisRecordJob < Que::Job Whois::Record.where(name: name).each do |r| r.json['status'] = r.json['status'].delete_if { |status| status == 'disputed' } - r.save! + r.json['status'].blank? ? r.destroy : r.save end end end diff --git a/app/models/dispute.rb b/app/models/dispute.rb index 42ea9ff79..a548ba7b5 100644 --- a/app/models/dispute.rb +++ b/app/models/dispute.rb @@ -15,8 +15,8 @@ class Dispute < ApplicationRecord before_save :generate_data after_destroy :remove_data - scope :expired, -> { where('expires_at < ?', Time.zone.today) } - scope :active, -> { where('expires_at > ? AND closed = false', Time.zone.today) } + scope :expired, -> { where('expires_at < ?', Date.today) } + scope :active, -> { where('expires_at > ? AND closed = false', Date.today) } scope :closed, -> { where(closed: true) } alias_attribute :name, :domain_name @@ -26,6 +26,10 @@ class Dispute < ApplicationRecord dispute.update(closed: true) if dispute.present? end + def for_active_domain? + Domain.where(name: domain_name).any? + end + def set_expiry_date return if starts_at.blank? @@ -37,27 +41,43 @@ class Dispute < ApplicationRecord end def generate_data + return if starts_at > Date.today + wr = Whois::Record.find_or_initialize_by(name: domain_name) - if Domain.where(name: domain_name).any? - @json = wr.json.with_indifferent_access - @json[:status] << 'disputed' unless @json[:status].include? 'disputed' - wr.json = @json + if for_active_domain? + wr.json['status'] << 'disputed' unless wr.json['status'].include? 'disputed' 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 - wr.save! + wr.save end alias_method :update_whois_record, :generate_data def close - self.closed = true - save! + return false unless update(closed: true) + 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 def generate_json(record) 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.push('disputed') unless status_arr.include? 'disputed'