contacts:archive: delete orphaned once it's discovered / allow to start query from specific contact_id

This commit is contained in:
Karl Erik Õunapuu 2020-09-04 11:24:00 +03:00
parent bbbb3d5367
commit fa6ebd9cda
No known key found for this signature in database
GPG key ID: C9DD647298A34764
2 changed files with 23 additions and 5 deletions

View file

@ -1,10 +1,27 @@
namespace :contacts do
desc 'Archives inactive contacts'
task archive: :environment do
inactive_contacts = InactiveContacts.new
archived_contacts = inactive_contacts.archive(verified: true)
task :archive, [:track_id] => [:environment] do |_t, args|
unlinked_contacts = contacts_start_point(args[:track_id])
puts "Archived total: #{archived_contacts.count}"
counter = 0
puts "Found #{unlinked_contacts.count} unlinked contacts. Starting to archive."
unlinked_contacts.each do |contact|
next unless contact.archivable?
puts "Archiving contact: id(#{contact.id}), code(#{contact.code})"
contact.archive(verified: true)
counter += 1
end
puts "Archived total: #{counter}"
end
def contacts_start_point(track_id = nil)
puts "Starting to find archivable contacts WHERE CONTACT_ID > #{track_id}" if track_id
return Contact.unlinked unless track_id
Contact.unlinked.where("id > #{track_id}")
end
end

View file

@ -13,7 +13,8 @@ class ArchiveContactsTaskTest < ActiveSupport::TestCase
contact = archivable_contact
eliminate_effect_of_all_contacts_except(contact)
expected_output = "Found archivable contact id(#{contact.id}), code (#{contact.code})\n" \
expected_output = "Found 1 unlinked contacts. Starting to archive.\n" \
"Found archivable contact id(#{contact.id}), code (#{contact.code})\n" \
"Archiving contact: id(#{contact.id}), code(#{contact.code})\n" \
"Archived total: 1\n"
assert_output(expected_output) { run_task }