diff --git a/app/models/concerns/contact/archivable.rb b/app/models/concerns/contact/archivable.rb index 842efa775..656e86a6a 100644 --- a/app/models/concerns/contact/archivable.rb +++ b/app/models/concerns/contact/archivable.rb @@ -9,12 +9,16 @@ module Concerns end end - def archivable? - inactive? + def archivable?(post: false) + inactive = inactive? + + log("Found archivable contact id(#{id}), code (#{code})") if inactive && !post + + inactive end def archive - raise 'Contact cannot be archived' unless archivable? + raise 'Contact cannot be archived' unless archivable?(post: true) destroy! end @@ -32,6 +36,11 @@ module Concerns def inactivity_period Setting.orphans_contacts_in_months.months end + + def log(msg) + @logger ||= Logger.new(STDOUT) + @logger.info(msg) + end end end end diff --git a/app/models/inactive_contacts.rb b/app/models/inactive_contacts.rb index a4123c8de..948b1f0bf 100644 --- a/app/models/inactive_contacts.rb +++ b/app/models/inactive_contacts.rb @@ -7,8 +7,15 @@ class InactiveContacts def archive contacts.each do |contact| + log("Archiving contact: id(#{contact.id}), code(#{contact.code})") + contact.archive yield contact if block_given? end end + + def log(msg) + @logger ||= Logger.new(STDOUT) + @logger.info(msg) + end end diff --git a/lib/tasks/contacts/archive.rake b/lib/tasks/contacts/archive.rake index 80ace6ce0..6ed29969f 100644 --- a/lib/tasks/contacts/archive.rake +++ b/lib/tasks/contacts/archive.rake @@ -2,13 +2,10 @@ namespace :contacts do desc 'Archives inactive contacts' task archive: :environment do + puts 'Starting to gather archivable contacts' inactive_contacts = InactiveContacts.new archived_contacts = inactive_contacts.archive - archived_contacts.each do |contact| - puts "Contact ##{contact.id} (code: #{contact.code}) is archived" - end - puts "Archived total: #{archived_contacts.count}" end end