Refactor inactive contact archivation

Fixes #956
This commit is contained in:
Artur Beljajev 2019-03-28 22:08:16 +02:00
parent 296442e330
commit 487613db1e
14 changed files with 424 additions and 84 deletions

View file

@ -5,4 +5,41 @@ class DomainVersion < PaperTrail::Version
self.sequence_name = :log_domains_id_seq
scope :deleted, -> { where(event: 'destroy') }
end
def self.was_contact_linked?(contact)
sql = <<-SQL
SELECT
COUNT(*)
FROM
#{table_name}
WHERE
(children->'registrant') @> '#{contact.id}'
OR
(children->'admin_contacts') @> '#{contact.id}'
OR
(children->'tech_contacts') @> '#{contact.id}'
SQL
count_by_sql(sql).nonzero?
end
def self.contact_unlinked_more_than?(contact:, period:)
sql = <<-SQL
SELECT
COUNT(*)
FROM
#{table_name}
WHERE
created_at < TIMESTAMP WITH TIME ZONE '#{period.ago}'
AND (
(children->'registrant') @> '#{contact.id}'
OR
(children->'admin_contacts') @> '#{contact.id}'
OR
(children->'tech_contacts') @> '#{contact.id}'
)
SQL
count_by_sql(sql).nonzero?
end
end