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:
34
spec/jobs/internal/process_stale_contacts_job_spec.rb
Normal file
34
spec/jobs/internal/process_stale_contacts_job_spec.rb
Normal 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
|
||||
20
spec/jobs/internal/remove_stale_contacts_job_spec.rb
Normal file
20
spec/jobs/internal/remove_stale_contacts_job_spec.rb
Normal 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
|
||||
41
spec/services/internal/remove_stale_contacts_service_spec.rb
Normal file
41
spec/services/internal/remove_stale_contacts_service_spec.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Internal::RemoveStaleContactsService do
|
||||
describe '#perform' do
|
||||
let(:account) { create(:account) }
|
||||
|
||||
it 'does not delete contacts with conversations' do
|
||||
# Contact with NULL values and conversation
|
||||
contact1 = create(:contact, account: account, email: nil, phone_number: nil, identifier: nil, created_at: 31.days.ago)
|
||||
create(:conversation, contact: contact1)
|
||||
|
||||
# Contact with empty strings and conversation
|
||||
contact2 = create(:contact, account: account, email: '', phone_number: '', identifier: '', created_at: 31.days.ago)
|
||||
create(:conversation, contact: contact2)
|
||||
|
||||
service = described_class.new(account: account)
|
||||
expect { service.perform }.not_to change(Contact, :count)
|
||||
end
|
||||
|
||||
it 'does not delete contacts with identification' do
|
||||
create(:contact, :with_email, account: account, phone_number: '', identifier: nil, created_at: 31.days.ago)
|
||||
create(:contact, :with_phone_number, account: account, email: nil, identifier: '', created_at: 31.days.ago)
|
||||
create(:contact, account: account, identifier: 'test123', created_at: 31.days.ago)
|
||||
|
||||
create(:contact, :with_email, account: account, phone_number: '', identifier: nil, created_at: 31.days.ago)
|
||||
create(:contact, :with_phone_number, account: account, email: nil, identifier: nil, created_at: 31.days.ago)
|
||||
create(:contact, account: account, email: '', phone_number: nil, identifier: 'test1234', created_at: 31.days.ago)
|
||||
|
||||
service = described_class.new(account: account)
|
||||
expect { service.perform }.not_to change(Contact, :count)
|
||||
end
|
||||
|
||||
it 'deletes stale contacts' do
|
||||
create(:contact, account: account, created_at: 31.days.ago)
|
||||
create(:contact, account: account, created_at: 1.day.ago)
|
||||
|
||||
service = described_class.new(account: account)
|
||||
expect { service.perform }.to change(Contact, :count).by(-1)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user