feat: add job to remove stale contacts and contact_inboxes (#11186)

- Add a job to remove stale contacts and contact_inboxes across all accounts

Stale anonymous contact is defined as 
- have no identification (email, phone_number, and identifier are NULL)
- have no conversations
- are older than 30 days

---------

Co-authored-by: Pranav <pranav@chatwoot.com>
Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
Vishnu Narayanan
2025-03-28 12:18:39 +05:30
committed by GitHub
parent 001b25c92a
commit 0175714d65
8 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
require 'rails_helper'
RSpec.describe Internal::ProcessStaleContactsJob do
subject(:job) { described_class.perform_later }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.on_queue('scheduled_jobs')
end
it 'enqueues RemoveStaleContactsJob for each account' do
account1 = create(:account)
account2 = create(:account)
account3 = create(:account)
expect { described_class.perform_now }.to have_enqueued_job(Internal::RemoveStaleContactsJob)
.with(account1)
.on_queue('low')
expect { described_class.perform_now }.to have_enqueued_job(Internal::RemoveStaleContactsJob)
.with(account2)
.on_queue('low')
expect { described_class.perform_now }.to have_enqueued_job(Internal::RemoveStaleContactsJob)
.with(account3)
.on_queue('low')
end
it 'processes accounts in batches' do
account = create(:account)
allow(Account).to receive(:find_in_batches).with(batch_size: 100).and_yield([account])
expect(Internal::RemoveStaleContactsJob).to receive(:perform_later).with(account)
described_class.perform_now
end
end

View File

@@ -0,0 +1,20 @@
require 'rails_helper'
RSpec.describe Internal::RemoveStaleContactsJob do
subject(:job) { described_class.perform_later(account) }
let(:account) { create(:account) }
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(account)
.on_queue('low')
end
it 'calls the RemoveStaleContactsService' do
service = instance_double(Internal::RemoveStaleContactsService)
expect(Internal::RemoveStaleContactsService).to receive(:new).with(account: account).and_return(service)
expect(service).to receive(:perform)
described_class.perform_now(account)
end
end