feat: add global method without account scope
This commit is contained in:
@@ -137,7 +137,7 @@ class Contact < ApplicationRecord
|
||||
.where('contacts.phone_number IS NULL OR contacts.phone_number = ?', '')
|
||||
.where('contacts.identifier IS NULL OR contacts.identifier = ?', '')
|
||||
.where('contacts.created_at < ?', time_period)
|
||||
.where('NOT EXISTS (SELECT 1 FROM conversations WHERE conversations.contact_id = contacts.id)')
|
||||
.where.missing(:conversations)
|
||||
}
|
||||
|
||||
def get_source_id(inbox_id)
|
||||
|
||||
@@ -4,21 +4,15 @@ class Internal::RemoveStaleContactsService
|
||||
def perform(batch_size = 1000)
|
||||
Rails.logger.info "[Internal::RemoveStaleContactsService] Starting removal of stale contacts for account #{@account.id}"
|
||||
|
||||
# Get the stale contacts query
|
||||
stale_contacts = @account.contacts.stale_without_conversations(30.days.ago)
|
||||
|
||||
total_deleted = 0
|
||||
|
||||
# Get only IDs in batches without loading full records
|
||||
stale_contacts.select(:id).in_batches(of: batch_size) do |relation|
|
||||
# Use pluck to get only the IDs
|
||||
contact_ids = relation.pluck(:id)
|
||||
next if contact_ids.empty?
|
||||
|
||||
# Delete associated contact_inboxes first
|
||||
ContactInbox.where(contact_id: contact_ids).delete_all
|
||||
|
||||
# Then delete the contacts
|
||||
Contact.where(id: contact_ids).delete_all
|
||||
|
||||
total_deleted += contact_ids.size
|
||||
@@ -29,4 +23,26 @@ class Internal::RemoveStaleContactsService
|
||||
Rails.logger.info "[Internal::RemoveStaleContactsService] Completed removal of #{total_deleted} stale contacts " \
|
||||
"for account #{@account.id}"
|
||||
end
|
||||
|
||||
# Process stale contacts across all accounts
|
||||
def perform_global(batch_size = 1000)
|
||||
Rails.logger.info '[Internal::RemoveStaleContactsService] Starting global removal of stale contacts'
|
||||
|
||||
stale_contacts = Contact.stale_without_conversations(30.days.ago)
|
||||
total_deleted = 0
|
||||
|
||||
stale_contacts.select(:id).in_batches(of: batch_size) do |relation|
|
||||
contact_ids = relation.pluck(:id)
|
||||
next if contact_ids.empty?
|
||||
|
||||
ContactInbox.where(contact_id: contact_ids).delete_all
|
||||
Contact.where(id: contact_ids).delete_all
|
||||
|
||||
total_deleted += contact_ids.size
|
||||
Rails.logger.info "[Internal::RemoveStaleContactsService] Deleted #{contact_ids.size} contacts " \
|
||||
"(#{total_deleted} total)"
|
||||
end
|
||||
|
||||
Rails.logger.info "[Internal::RemoveStaleContactsService] Completed global removal of #{total_deleted} stale contacts"
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user