feat: integrate LeadSquared CRM (#11284)

This commit is contained in:
Shivam Mishra
2025-04-29 09:14:00 +05:30
committed by GitHub
parent c63b583f90
commit 1a2e6dc4ee
36 changed files with 2577 additions and 7 deletions

View File

@@ -0,0 +1,85 @@
require 'rails_helper'
RSpec.describe Crm::SetupJob do
subject(:job) { described_class.perform_later(hook.id) }
let(:account) { create(:account) }
let(:hook) do
create(:integrations_hook,
account: account,
app_id: 'leadsquared',
settings: {
access_key: 'test_key',
secret_key: 'test_token',
endpoint_url: 'https://api.leadsquared.com'
})
end
before do
account.enable_features('crm_integration')
end
it 'enqueues the job' do
expect { job }.to have_enqueued_job(described_class)
.with(hook.id)
.on_queue('default')
end
describe '#perform' do
context 'when hook is not found' do
it 'returns without processing' do
allow(Integrations::Hook).to receive(:find_by).and_return(nil)
expect(described_class.new.perform(0)).to be_nil
end
end
context 'when hook is disabled' do
it 'returns without processing' do
disabled_hook = create(:integrations_hook,
account: account,
app_id: 'leadsquared',
status: 'disabled',
settings: {
access_key: 'test_key',
secret_key: 'test_token',
endpoint_url: 'https://api.leadsquared.com'
})
expect(described_class.new.perform(disabled_hook.id)).to be_nil
end
end
context 'when hook is not a CRM integration' do
it 'returns without processing' do
non_crm_hook = create(:integrations_hook,
account: account,
app_id: 'slack',
settings: { webhook_url: 'https://slack.com/webhook' })
expect(described_class.new.perform(non_crm_hook.id)).to be_nil
end
end
context 'when hook is valid' do
let(:setup_service) { instance_double(Crm::Leadsquared::SetupService) }
before do
allow(Crm::Leadsquared::SetupService).to receive(:new).with(hook).and_return(setup_service)
end
context 'when setup raises an error' do
it 'captures exception and logs error' do
error = StandardError.new('Test error')
allow(setup_service).to receive(:setup).and_raise(error)
allow(Rails.logger).to receive(:error)
allow(ChatwootExceptionTracker).to receive(:new)
.with(error, account: hook.account)
.and_return(instance_double(ChatwootExceptionTracker, capture_exception: true))
described_class.new.perform(hook.id)
expect(Rails.logger).to have_received(:error)
.with("Error in CRM setup for hook ##{hook.id} (#{hook.app_id}): Test error")
end
end
end
end
end

View File

@@ -66,4 +66,110 @@ RSpec.describe HookJob do
described_class.perform_now(hook, event_name, event_data)
end
end
context 'when processing leadsquared integration' do
let(:contact) { create(:contact, account: account) }
let(:conversation) { create(:conversation, account: account, contact: contact) }
let(:processor_service) { instance_double(Crm::Leadsquared::ProcessorService) }
let(:leadsquared_hook) { instance_double(Integrations::Hook, id: 123, app_id: 'leadsquared', account: account) }
before do
allow(Crm::Leadsquared::ProcessorService).to receive(:new).with(leadsquared_hook).and_return(processor_service)
end
context 'when processing contact.updated event' do
let(:event_name) { 'contact.updated' }
let(:event_data) { { contact: contact } }
it 'uses a lock when processing' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
allow(processor_service).to receive(:handle_contact).with(contact)
# Mock the with_lock method directly on the job instance
job_instance = described_class.new
allow(job_instance).to receive(:with_lock).and_yield
allow(described_class).to receive(:new).and_return(job_instance)
expect(job_instance).to receive(:with_lock).with(
format(Redis::Alfred::CRM_PROCESS_MUTEX, hook_id: leadsquared_hook.id)
)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
it 'does not process when feature is not allowed' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(false)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock)
expect(job_instance).not_to receive(:with_lock)
expect(processor_service).not_to receive(:handle_contact)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
context 'when processing conversation.created event' do
let(:event_name) { 'conversation.created' }
let(:event_data) { { conversation: conversation } }
it 'uses a lock when processing' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
allow(processor_service).to receive(:handle_conversation_created).with(conversation)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock).and_yield
allow(described_class).to receive(:new).and_return(job_instance)
expect(job_instance).to receive(:with_lock).with(
format(Redis::Alfred::CRM_PROCESS_MUTEX, hook_id: leadsquared_hook.id)
)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
context 'when processing conversation.resolved event' do
let(:event_name) { 'conversation.resolved' }
let(:event_data) { { conversation: conversation } }
it 'uses a lock when processing' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
allow(processor_service).to receive(:handle_conversation_resolved).with(conversation)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock).and_yield
allow(described_class).to receive(:new).and_return(job_instance)
expect(job_instance).to receive(:with_lock).with(
format(Redis::Alfred::CRM_PROCESS_MUTEX, hook_id: leadsquared_hook.id)
)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
context 'when processing invalid event' do
let(:event_name) { 'invalid.event' }
let(:event_data) { { contact: contact } }
it 'does not process for invalid event names' do
allow(leadsquared_hook).to receive(:disabled?).and_return(false)
allow(leadsquared_hook).to receive(:feature_allowed?).and_return(true)
job_instance = described_class.new
allow(job_instance).to receive(:with_lock)
expect(job_instance).not_to receive(:with_lock)
expect(processor_service).not_to receive(:handle_contact)
job_instance.perform(leadsquared_hook, event_name, event_data)
end
end
end
end