Skip to content

Commit

Permalink
Change implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Beljajev committed Apr 22, 2019
1 parent c2d76a1 commit 2893bff
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 75 deletions.
26 changes: 12 additions & 14 deletions app/models/concerns/contact/usable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ def in_use
contacts.id)')
end

def not_in_use_during_last(period)
not_in_use.where("NOT EXISTS(SELECT 1 FROM log_domains WHERE created_at >= ?
AND (
((children->'registrant') @> (contacts.id::text)::jsonb)
OR
((children->'admin_contacts') @> (contacts.id::text)::jsonb)
OR
((children->'tech_contacts') @> (contacts.id::text)::jsonb)
))",
period.public_send(:ago))
end

private

def not_in_use
where('NOT EXISTS(SELECT 1 FROM domains WHERE domains.registrant_id = contacts.id)
AND
Expand All @@ -37,6 +23,18 @@ def in_use?
# https://github.com/internetee/registry/pull/1162/files is merged
registrant_domains.any? || domain_contacts.any?
end

def in_use_during_last(period)
in_use? && DomainVersion.count_by_sql("SELECT COUNT(*) FROM log_domains WHERE
created_at <= '#{period.public_send(:ago)}'
AND (
(children->'registrant') @> '#{id}'
OR
(children->'admin_contacts') @> '#{id}'
OR
(children->'tech_contacts') @> '#{id}'
)").nonzero?
end
end
end
end
4 changes: 3 additions & 1 deletion app/services/contact_archiver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def initialize(timeout: Setting.orphans_contacts_in_months&.months)

def archive
archivable_contacts.each do |contact|
next if contact.in_use_during_last(timeout)

contact.archive
yield contact if block_given?
end
Expand All @@ -15,6 +17,6 @@ def archive
private

def archivable_contacts
Contact.not_in_use_during_last(timeout)
Contact.not_in_use
end
end
122 changes: 63 additions & 59 deletions test/models/contact/usable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,111 +29,115 @@ def test_in_use_scope_skips_contact_that_is_not_in_use
assert_not Contact.in_use.include?(@contact), 'Contact should be excluded'
end

def test_not_in_use_during_last_scope_returns_contact_that_is_currently_not_in_use
assert Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be included'
def test_not_in_use_scope_returns_contact_that_is_not_in_use
assert Contact.not_in_use.include?(@contact), 'Contact should be included'
end

def test_not_in_use_during_last_scope_returns_contact_that_was_never_in_use
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [],
tech_contacts: [],
registrant: [] })
travel_to Time.zone.parse('2010-07-05 00:00:01')

assert Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be included'
def test_not_in_use_scope_skips_contact_that_is_in_use_as_a_registrant
domains(:shop).update!(registrant: @contact.becomes(Registrant))
assert_not Contact.not_in_use.include?(@contact), 'Contact should be excluded'
end

def test_not_in_use_during_last_scope_returns_contact_that_was_in_use_as_a_registrant_longer_than_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [],
tech_contacts: [],
registrant: [@contact.id] })
travel_to Time.zone.parse('2010-07-05 00:00:01')
def test_not_in_use_scope_skips_contact_that_is_in_use_as_an_admin_contact
domains(:shop).admin_contacts = [@contact]
assert_not Contact.not_in_use.include?(@contact), 'Contact should be excluded'
end

assert Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be included'
def test_not_in_use_scope_skips_contact_that_is_in_use_as_a_tech_contact
domains(:shop).tech_contacts = [@contact]
assert_not Contact.not_in_use.include?(@contact), 'Contact should be excluded'
end

def test_not_in_use_during_last_scope_returns_contact_that_was_in_use_as_an_admin_contact_longer_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [1, @contact.id],
tech_contacts: [],
registrant: [] })
travel_to Time.zone.parse('2010-07-05 00:00:01')
def test_in_use_when_acts_as_a_registrant
domains(:shop).update!(registrant: @contact.becomes(Registrant))
assert @contact.in_use?
end

assert Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be included'
def test_in_use_when_acts_as_an_admin_contact
domains(:shop).admin_contacts = [@contact]
assert @contact.in_use?
end

def test_not_in_use_during_last_scope_returns_contact_that_was_in_use_as_a_tech_contact_longer_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [],
tech_contacts: [1, @contact.id],
registrant: [] })
travel_to Time.zone.parse('2010-07-05 00:00:01')
def test_in_use_when_acts_as_a_tech_contact
domains(:shop).tech_contacts = [@contact]
assert @contact.in_use?
end

assert Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be included'
def test_not_in_use_when_acts_as_neither_registrant_nor_domain_contact
assert_not @contact.in_use?
end

def test_not_in_use_during_last_scope_skips_contact_that_was_in_use_as_a_registrant_shorter_than_given_period
def test_in_use_as_a_registrant_shorter_than_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-05'),
children: { admin_contacts: [],
tech_contacts: [],
registrant: [@contact.id] })
travel_to Time.zone.parse('2010-07-05')

assert_not Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be excluded'
assert @contact.in_use_during_last(1.day)
end

def test_not_in_use_during_last_scope_skips_contact_that_was_in_use_as_an_admin_contact_shorter_than_given_period
def test_in_use_as_an_admin_contact_shorter_than_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-05'),
children: { admin_contacts: [1, @contact.id],
tech_contacts: [],
registrant: [] })
travel_to Time.zone.parse('2010-07-05')

assert_not Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be excluded'
assert @contact.in_use_during_last(1.day)
end

def test_not_in_use_during_last_scope_skips_contact_that_was_in_use_as_a_tech_contact_shorter_than_given_period
def test_in_use_as_a_tech_contact_shorter_than_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-05'),
children: { admin_contacts: [],
tech_contacts: [1, @contact.id],
registrant: [] })
travel_to Time.zone.parse('2010-07-05')

assert_not Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be excluded'
assert @contact.in_use_during_last(1.day)
end

def test_not_in_use_during_last_scope_skips_contact_that_is_currently_in_use_as_a_registrant
domains(:shop).update!(registrant: @contact.becomes(Registrant))
assert_not Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be excluded'
end
def test_in_use_as_a_registrant_longer_than_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [],
tech_contacts: [],
registrant: [@contact.id] })
travel_to Time.zone.parse('2010-07-05 00:00:01')

def test_not_in_use_during_last_scope_skips_contact_that_is_currently_in_use_as_an_admin_contact
domains(:shop).admin_contacts = [@contact]
assert_not Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be excluded'
assert_not @contact.in_use_during_last(1.day)
end

def test_not_in_use_during_last_scope_skips_contact_that_is_currently_in_use_as_a_tech_contact
domains(:shop).tech_contacts = [@contact]
assert_not Contact.not_in_use_during_last(1.day).include?(@contact), 'Contact should be excluded'
end
def test_in_use_as_an_admin_contact_longer_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [1, @contact.id],
tech_contacts: [],
registrant: [] })
travel_to Time.zone.parse('2010-07-05 00:00:01')

def test_in_use_when_acts_as_a_registrant
domains(:shop).update!(registrant: @contact.becomes(Registrant))
assert @contact.in_use?
assert_not @contact.in_use_during_last(1.day)
end

def test_in_use_when_acts_as_an_admin_contact
domains(:shop).admin_contacts = [@contact]
assert @contact.in_use?
def test_in_use_as_a_tech_contact_longer_given_period
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [],
tech_contacts: [1, @contact.id],
registrant: [] })
travel_to Time.zone.parse('2010-07-05 00:00:01')

assert_not @contact.in_use_during_last(1.day)
end

def test_in_use_when_acts_as_a_tech_contact
domains(:shop).tech_contacts = [@contact]
assert @contact.in_use?
def test_currently_not_in_use
assert_not @contact.in_use_during_last(1.day)
end

def test_not_in_use_when_acts_as_neither_registrant_nor_domain_contact
assert_not @contact.in_use?
def test_never_in_use
domain_versions(:one).update!(created_at: Time.zone.parse('2010-07-04'),
children: { admin_contacts: [],
tech_contacts: [],
registrant: [] })
travel_to Time.zone.parse('2010-07-05 00:00:01')

assert_not @contact.in_use_during_last(1.day)
end
end
2 changes: 1 addition & 1 deletion test/services/contact_archiver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_archives_contacts_that_were_not_in_use_for_the_period_specified_by_time
contact_mock = Minitest::Mock.new
contact_mock.expect(:archive, nil)

Contact.stub(:not_in_use_during_last, [contact_mock]) do
Contact.stub(:not_in_use, [contact_mock]) do
archiver = ContactArchiver.new
archiver.archive
end
Expand Down

0 comments on commit 2893bff

Please sign in to comment.