mirror of
https://github.com/internetee/registry.git
synced 2025-08-04 17:01:44 +02:00
Merge pull request #160 from internetee/story/116761157-contact-dyn-states
Story/116761157 contact dyn states
This commit is contained in:
commit
f2f9377cd9
9 changed files with 72 additions and 108 deletions
|
@ -36,12 +36,10 @@ class Contact < ActiveRecord::Base
|
|||
|
||||
validate :val_ident_type
|
||||
validate :val_ident_valid_format?
|
||||
validate :uniq_statuses?
|
||||
validate :validate_html
|
||||
validate :val_country_code
|
||||
|
||||
after_initialize do
|
||||
self.statuses = [] if statuses.nil?
|
||||
self.status_notes = {} if status_notes.nil?
|
||||
self.ident_updated_at = Time.zone.now if new_record? && ident_updated_at.blank?
|
||||
end
|
||||
|
@ -64,16 +62,6 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
before_save :manage_statuses
|
||||
def manage_statuses
|
||||
if domain_transfer # very ugly but need better workflow
|
||||
self.statuses = statuses | [OK, LINKED]
|
||||
return
|
||||
end
|
||||
|
||||
manage_linked
|
||||
manage_ok
|
||||
end
|
||||
|
||||
after_save :update_related_whois_records
|
||||
|
||||
|
@ -176,7 +164,7 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def find_orphans
|
||||
Contact.where('
|
||||
where('
|
||||
NOT EXISTS(
|
||||
select 1 from domains d where d.registrant_id = contacts.id
|
||||
) AND NOT EXISTS(
|
||||
|
@ -185,20 +173,49 @@ class Contact < ActiveRecord::Base
|
|||
')
|
||||
end
|
||||
|
||||
def find_linked
|
||||
where('
|
||||
EXISTS(
|
||||
select 1 from domains d where d.registrant_id = contacts.id
|
||||
) OR EXISTS(
|
||||
select 1 from domain_contacts dc where dc.contact_id = contacts.id
|
||||
)
|
||||
')
|
||||
end
|
||||
|
||||
def filter_by_states in_states
|
||||
states = Array(in_states).dup
|
||||
scope = all
|
||||
|
||||
# all contacts has state ok, so no need to filter by it
|
||||
scope = scope.where("NOT contacts.statuses && ?::varchar[]", "{#{(STATUSES - [OK, LINKED]).join(',')}}") if states.delete(OK)
|
||||
scope = scope.find_linked if states.delete(LINKED)
|
||||
scope = scope.where("contacts.statuses @> ?::varchar[]", "{#{states.join(',')}}") if states.any?
|
||||
scope
|
||||
end
|
||||
|
||||
# To leave only new ones we need to check
|
||||
# if contact was at any time used in domain.
|
||||
# This can be checked by domain history.
|
||||
# This can be checked by saved relations in children attribute
|
||||
def destroy_orphans
|
||||
STDOUT << "#{Time.zone.now.utc} - Destroying orphaned contacts\n" unless Rails.env.test?
|
||||
|
||||
orphans = find_orphans
|
||||
|
||||
unless Rails.env.test?
|
||||
orphans.each do |m|
|
||||
STDOUT << "#{Time.zone.now.utc} Contact.destroy_orphans: ##{m.id} (#{m.name})\n"
|
||||
counter = Counter.new
|
||||
find_orphans.find_each do |contact|
|
||||
ver_scope = []
|
||||
%w(admin_contacts tech_contacts registrant).each do |type|
|
||||
ver_scope << "(children->'#{type}')::jsonb <@ json_build_array(#{contact.id})::jsonb"
|
||||
end
|
||||
next if DomainVersion.where("created_at > ?", Time.now - Setting.orphans_contacts_in_months.to_i.months).where(ver_scope.join(" OR ")).any?
|
||||
next if contact.domains_present?
|
||||
|
||||
contact.destroy
|
||||
counter.next
|
||||
STDOUT << "#{Time.zone.now.utc} Contact.destroy_orphans: ##{contact.id} (#{contact.name})\n"
|
||||
end
|
||||
|
||||
count = orphans.destroy_all.count
|
||||
|
||||
STDOUT << "#{Time.zone.now.utc} - Successfully destroyed #{count} orphaned contacts\n" unless Rails.env.test?
|
||||
STDOUT << "#{Time.zone.now.utc} - Successfully destroyed #{counter} orphaned contacts\n" unless Rails.env.test?
|
||||
end
|
||||
|
||||
def privs
|
||||
|
@ -239,6 +256,23 @@ class Contact < ActiveRecord::Base
|
|||
"EIS-#{id}"
|
||||
end
|
||||
|
||||
# kind of decorator in order to always return statuses
|
||||
# if we use separate decorator, then we should add it
|
||||
# to too many places
|
||||
def statuses
|
||||
calculated = Array(read_attribute(:statuses))
|
||||
calculated.delete(Contact::OK)
|
||||
calculated.delete(Contact::LINKED)
|
||||
calculated << Contact::OK if calculated.empty?# && valid?
|
||||
calculated << Contact::LINKED if domains_present?
|
||||
|
||||
calculated.uniq
|
||||
end
|
||||
|
||||
def statuses= arr
|
||||
write_attribute(:statuses, Array(arr).uniq)
|
||||
end
|
||||
|
||||
def to_s
|
||||
name || '[no name]'
|
||||
end
|
||||
|
@ -278,11 +312,6 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def uniq_statuses?
|
||||
return true unless statuses.detect { |s| statuses.count(s) > 1 }
|
||||
errors.add(:statuses, :not_uniq)
|
||||
false
|
||||
end
|
||||
|
||||
def org?
|
||||
ident_type == ORG
|
||||
|
@ -412,13 +441,6 @@ class Contact < ActiveRecord::Base
|
|||
domain_contacts.present? || registrant_domains.present?
|
||||
end
|
||||
|
||||
def manage_linked
|
||||
if domains_present?
|
||||
set_linked
|
||||
else
|
||||
unset_linked
|
||||
end
|
||||
end
|
||||
|
||||
def search_name
|
||||
"#{code} #{name}"
|
||||
|
@ -497,43 +519,6 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def set_linked
|
||||
statuses << LINKED if statuses.detect { |s| s == LINKED }.blank?
|
||||
end
|
||||
|
||||
def unset_linked
|
||||
statuses.delete_if { |s| s == LINKED }
|
||||
end
|
||||
|
||||
# rubocop:disable Metrics/CyclomaticComplexity
|
||||
def manage_ok
|
||||
return unset_ok unless valid?
|
||||
|
||||
case statuses.size
|
||||
when 0
|
||||
set_ok
|
||||
when 1
|
||||
set_ok if statuses == [LINKED]
|
||||
when 2
|
||||
return if statuses.sort == [LINKED, OK]
|
||||
unset_ok
|
||||
else
|
||||
unset_ok
|
||||
end
|
||||
end
|
||||
# rubocop:enable Metrics/CyclomaticComplexity
|
||||
|
||||
def unset_ok
|
||||
statuses.delete_if { |s| s == OK }
|
||||
end
|
||||
|
||||
def set_ok
|
||||
statuses << OK if statuses.detect { |s| s == OK }.blank?
|
||||
end
|
||||
|
||||
def linked?
|
||||
statuses.include?(LINKED)
|
||||
end
|
||||
|
||||
def update_prohibited?
|
||||
(statuses & [
|
||||
|
|
|
@ -39,29 +39,12 @@ class Epp::Domain < Domain
|
|||
|
||||
before_save :link_contacts
|
||||
def link_contacts
|
||||
# Based on bullet report
|
||||
if new_record?
|
||||
# new record does not have correct instance contacts entries thanks to epp
|
||||
unlinked_contacts = [registrant]
|
||||
unlinked_contacts << admin_domain_contacts.map(&:contact)
|
||||
unlinked_contacts << tech_domain_contacts.map(&:contact)
|
||||
unlinked_contacts.flatten!
|
||||
else
|
||||
unlinked_contacts = contacts.select { |c| !c.linked? } # speed up a bit
|
||||
end
|
||||
|
||||
unlinked_contacts.each do |uc|
|
||||
uc.domains_present = true # no need to fetch domains again
|
||||
uc.save(validate: false)
|
||||
end
|
||||
#TODO: cleanup cache if we think to cache dynamic statuses
|
||||
end
|
||||
|
||||
after_destroy :unlink_contacts
|
||||
def unlink_contacts
|
||||
contacts.each do |c|
|
||||
c.domains_present = false
|
||||
c.save(validate: false)
|
||||
end
|
||||
#TODO: cleanup cache if we think to cache dynamic statuses
|
||||
end
|
||||
|
||||
class << self
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue