Restructure omni services and add Chatwoot research snapshot
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::Api::ActivityClient do
|
||||
let(:credentials) do
|
||||
{
|
||||
access_key: SecureRandom.hex,
|
||||
secret_key: SecureRandom.hex,
|
||||
endpoint_url: 'https://api.leadsquared.com/'
|
||||
}
|
||||
end
|
||||
|
||||
let(:headers) do
|
||||
{
|
||||
'Content-Type': 'application/json',
|
||||
'x-LSQ-AccessKey': credentials[:access_key],
|
||||
'x-LSQ-SecretKey': credentials[:secret_key]
|
||||
}
|
||||
end
|
||||
let(:client) { described_class.new(credentials[:access_key], credentials[:secret_key], credentials[:endpoint_url]) }
|
||||
let(:prospect_id) { SecureRandom.uuid }
|
||||
let(:activity_event) { 1001 } # Example activity event code
|
||||
let(:activity_note) { 'Test activity note' }
|
||||
let(:activity_date_time) { '2025-04-11 14:15:00' }
|
||||
|
||||
describe '#post_activity' do
|
||||
let(:path) { '/ProspectActivity.svc/Create' }
|
||||
let(:full_url) { URI.join(credentials[:endpoint_url], path).to_s }
|
||||
|
||||
context 'with missing required parameters' do
|
||||
it 'raises ArgumentError when prospect_id is missing' do
|
||||
expect { client.post_activity(nil, activity_event, activity_note) }
|
||||
.to raise_error(ArgumentError, 'Prospect ID is required')
|
||||
end
|
||||
|
||||
it 'raises ArgumentError when activity_event is missing' do
|
||||
expect { client.post_activity(prospect_id, nil, activity_note) }
|
||||
.to raise_error(ArgumentError, 'Activity event code is required')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request is successful' do
|
||||
let(:activity_id) { SecureRandom.uuid }
|
||||
let(:success_response) do
|
||||
{
|
||||
'Status' => 'Success',
|
||||
'Message' => {
|
||||
'Id' => activity_id,
|
||||
'Message' => 'Activity created successfully'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: {
|
||||
'RelatedProspectId' => prospect_id,
|
||||
'ActivityEvent' => activity_event,
|
||||
'ActivityNote' => activity_note
|
||||
}.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: success_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns activity ID directly' do
|
||||
response = client.post_activity(prospect_id, activity_event, activity_note)
|
||||
expect(response).to eq(activity_id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when response indicates failure' do
|
||||
let(:error_response) do
|
||||
{
|
||||
'Status' => 'Error',
|
||||
'ExceptionType' => 'NullReferenceException',
|
||||
'ExceptionMessage' => 'There was an error processing the request.'
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: {
|
||||
'RelatedProspectId' => prospect_id,
|
||||
'ActivityEvent' => activity_event,
|
||||
'ActivityNote' => activity_note
|
||||
}.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: error_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError when activity creation fails' do
|
||||
expect { client.post_activity(prospect_id, activity_event, activity_note) }
|
||||
.to raise_error do |error|
|
||||
expect(error.class.name).to eq('Crm::Leadsquared::Api::BaseClient::ApiError')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create_activity_type' do
|
||||
let(:path) { 'ProspectActivity.svc/CreateType' }
|
||||
let(:full_url) { URI.join(credentials[:endpoint_url], path).to_s }
|
||||
let(:activity_params) do
|
||||
{
|
||||
name: 'Test Activity Type',
|
||||
score: 10,
|
||||
direction: 0
|
||||
}
|
||||
end
|
||||
|
||||
context 'with missing required parameters' do
|
||||
it 'raises ArgumentError when name is missing' do
|
||||
expect { client.create_activity_type(name: nil, score: 10) }
|
||||
.to raise_error(ArgumentError, 'Activity name is required')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request is successful' do
|
||||
let(:activity_event_id) { 1001 }
|
||||
let(:success_response) do
|
||||
{
|
||||
'Status' => 'Success',
|
||||
'Message' => {
|
||||
'Id' => activity_event_id
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: {
|
||||
'ActivityEventName' => activity_params[:name],
|
||||
'Score' => activity_params[:score],
|
||||
'Direction' => activity_params[:direction]
|
||||
}.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: success_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns activity ID directly' do
|
||||
response = client.create_activity_type(**activity_params)
|
||||
expect(response).to eq(activity_event_id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when response indicates failure' do
|
||||
let(:error_response) do
|
||||
{
|
||||
'Status' => 'Error',
|
||||
'ExceptionType' => 'MXInvalidInputException',
|
||||
'ExceptionMessage' => 'Invalid Input! Parameter Name: activity'
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: {
|
||||
'ActivityEventName' => activity_params[:name],
|
||||
'Score' => activity_params[:score],
|
||||
'Direction' => activity_params[:direction]
|
||||
}.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: error_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError when activity type creation fails' do
|
||||
expect { client.create_activity_type(**activity_params) }
|
||||
.to raise_error do |error|
|
||||
expect(error.class.name).to eq('Crm::Leadsquared::Api::BaseClient::ApiError')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when API request fails' do
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: {
|
||||
'ActivityEventName' => activity_params[:name],
|
||||
'Score' => activity_params[:score],
|
||||
'Direction' => activity_params[:direction]
|
||||
}.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 500,
|
||||
body: 'Internal Server Error',
|
||||
headers: { 'Content-Type' => 'text/plain' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError when the request fails' do
|
||||
expect { client.create_activity_type(**activity_params) }
|
||||
.to raise_error do |error|
|
||||
expect(error.class.name).to eq('Crm::Leadsquared::Api::BaseClient::ApiError')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,187 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::Api::BaseClient do
|
||||
let(:access_key) { SecureRandom.hex }
|
||||
let(:secret_key) { SecureRandom.hex }
|
||||
let(:headers) do
|
||||
{
|
||||
'Content-Type': 'application/json',
|
||||
'x-LSQ-AccessKey': access_key,
|
||||
'x-LSQ-SecretKey': secret_key
|
||||
}
|
||||
end
|
||||
|
||||
let(:endpoint_url) { 'https://api.leadsquared.com/v2' }
|
||||
let(:client) { described_class.new(access_key, secret_key, endpoint_url) }
|
||||
|
||||
describe '#initialize' do
|
||||
it 'creates a client with valid credentials' do
|
||||
expect(client.instance_variable_get(:@access_key)).to eq(access_key)
|
||||
expect(client.instance_variable_get(:@secret_key)).to eq(secret_key)
|
||||
expect(client.instance_variable_get(:@base_uri)).to eq(endpoint_url)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#get' do
|
||||
let(:path) { 'LeadManagement.svc/Leads.Get' }
|
||||
let(:params) { { leadId: '123' } }
|
||||
let(:full_url) { URI.join(endpoint_url, path).to_s }
|
||||
|
||||
context 'when request is successful' do
|
||||
before do
|
||||
stub_request(:get, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: { Message: 'Success', Status: 'Success' }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns parsed response data directly' do
|
||||
response = client.get(path, params)
|
||||
expect(response).to include('Message' => 'Success')
|
||||
expect(response).to include('Status' => 'Success')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request returns error status' do
|
||||
before do
|
||||
stub_request(:get, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: { Status: 'Error', ExceptionMessage: 'Invalid lead ID' }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError with error message' do
|
||||
expect { client.get(path, params) }.to raise_error do |error|
|
||||
expect(error.class.name).to eq(described_class::ApiError.name)
|
||||
expect(error.message).to eq('Invalid lead ID')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request fails with non-200 status' do
|
||||
before do
|
||||
stub_request(:get, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(status: 404, body: 'Not Found')
|
||||
end
|
||||
|
||||
it 'raises ApiError with status code' do
|
||||
expect { client.get(path, params) }.to raise_error do |error|
|
||||
expect(error.class.name).to eq(described_class::ApiError.name)
|
||||
expect(error.message).to include('Not Found')
|
||||
expect(error.code).to eq(404)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#post' do
|
||||
let(:path) { 'LeadManagement.svc/Lead.Create' }
|
||||
let(:params) { {} }
|
||||
let(:body) { { FirstName: 'John', LastName: 'Doe' } }
|
||||
let(:full_url) { URI.join(endpoint_url, path).to_s }
|
||||
|
||||
context 'when request is successful' do
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
body: body.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: { Message: 'Lead created', Status: 'Success' }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns parsed response data directly' do
|
||||
response = client.post(path, params, body)
|
||||
expect(response).to include('Message' => 'Lead created')
|
||||
expect(response).to include('Status' => 'Success')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request returns error status' do
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
body: body.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: { Status: 'Error', ExceptionMessage: 'Invalid data' }.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError with error message' do
|
||||
expect { client.post(path, params, body) }.to raise_error do |error|
|
||||
expect(error.class.name).to eq(described_class::ApiError.name)
|
||||
expect(error.message).to eq('Invalid data')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when response cannot be parsed' do
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
body: body.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: 'Invalid JSON',
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError for invalid JSON' do
|
||||
expect { client.post(path, params, body) }.to raise_error do |error|
|
||||
expect(error.class.name).to eq(described_class::ApiError.name)
|
||||
expect(error.message).to include('Failed to parse')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request fails with server error' do
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
query: params,
|
||||
body: body.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(status: 500, body: 'Internal Server Error')
|
||||
end
|
||||
|
||||
it 'raises ApiError with status code' do
|
||||
expect { client.post(path, params, body) }.to raise_error do |error|
|
||||
expect(error.class.name).to eq(described_class::ApiError.name)
|
||||
expect(error.message).to include('Internal Server Error')
|
||||
expect(error.code).to eq(500)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,231 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::Api::LeadClient do
|
||||
let(:access_key) { SecureRandom.hex }
|
||||
let(:secret_key) { SecureRandom.hex }
|
||||
let(:headers) do
|
||||
{
|
||||
'Content-Type': 'application/json',
|
||||
'x-LSQ-AccessKey': access_key,
|
||||
'x-LSQ-SecretKey': secret_key
|
||||
}
|
||||
end
|
||||
|
||||
let(:endpoint_url) { 'https://api.leadsquared.com/v2' }
|
||||
let(:client) { described_class.new(access_key, secret_key, endpoint_url) }
|
||||
|
||||
describe '#search_lead' do
|
||||
let(:path) { 'LeadManagement.svc/Leads.GetByQuickSearch' }
|
||||
let(:search_key) { 'test@example.com' }
|
||||
let(:full_url) { URI.join(endpoint_url, path).to_s }
|
||||
|
||||
context 'when search key is missing' do
|
||||
it 'raises ArgumentError' do
|
||||
expect { client.search_lead(nil) }
|
||||
.to raise_error(ArgumentError, 'Search key is required')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when no leads are found' do
|
||||
before do
|
||||
stub_request(:get, full_url)
|
||||
.with(query: { key: search_key }, headers: headers)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: [].to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns empty array directly' do
|
||||
response = client.search_lead(search_key)
|
||||
expect(response).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
context 'when leads are found' do
|
||||
let(:lead_data) do
|
||||
[{
|
||||
'ProspectID' => SecureRandom.uuid,
|
||||
'FirstName' => 'John',
|
||||
'LastName' => 'Doe',
|
||||
'EmailAddress' => search_key
|
||||
}]
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:get, full_url)
|
||||
.with(query: { key: search_key }, headers: headers, body: anything)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: lead_data.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns lead data array directly' do
|
||||
response = client.search_lead(search_key)
|
||||
expect(response).to eq(lead_data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#create_or_update_lead' do
|
||||
let(:path) { 'LeadManagement.svc/Lead.CreateOrUpdate' }
|
||||
let(:full_url) { URI.join(endpoint_url, path).to_s }
|
||||
let(:lead_data) do
|
||||
{
|
||||
'FirstName' => 'John',
|
||||
'LastName' => 'Doe',
|
||||
'EmailAddress' => 'john.doe@example.com'
|
||||
}
|
||||
end
|
||||
let(:formatted_lead_data) do
|
||||
lead_data.map do |key, value|
|
||||
{
|
||||
'Attribute' => key,
|
||||
'Value' => value
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lead data is missing' do
|
||||
it 'raises ArgumentError' do
|
||||
expect { client.create_or_update_lead(nil) }
|
||||
.to raise_error(ArgumentError, 'Lead data is required')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lead is successfully created' do
|
||||
let(:lead_id) { '8e0f69ae-e2ac-40fc-a0cf-827326181c8a' }
|
||||
let(:success_response) do
|
||||
{
|
||||
'Status' => 'Success',
|
||||
'Message' => {
|
||||
'Id' => lead_id
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: formatted_lead_data.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: success_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns lead ID directly' do
|
||||
response = client.create_or_update_lead(lead_data)
|
||||
expect(response).to eq(lead_id)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when request fails' do
|
||||
let(:error_response) do
|
||||
{
|
||||
'Status' => 'Error',
|
||||
'ExceptionMessage' => 'Error message'
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: formatted_lead_data.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: error_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError' do
|
||||
expect { client.create_or_update_lead(lead_data) }
|
||||
.to(raise_error { |error| expect(error.class.name).to eq('Crm::Leadsquared::Api::BaseClient::ApiError') })
|
||||
end
|
||||
end
|
||||
|
||||
# Add test for update_lead method
|
||||
describe '#update_lead' do
|
||||
let(:path) { 'LeadManagement.svc/Lead.Update' }
|
||||
let(:lead_id) { '8e0f69ae-e2ac-40fc-a0cf-827326181c8a' }
|
||||
let(:full_url) { URI.join(endpoint_url, "#{path}?leadId=#{lead_id}").to_s }
|
||||
|
||||
context 'with missing parameters' do
|
||||
it 'raises ArgumentError when lead_id is missing' do
|
||||
expect { client.update_lead(lead_data, nil) }
|
||||
.to raise_error(ArgumentError, 'Lead ID is required')
|
||||
end
|
||||
|
||||
it 'raises ArgumentError when lead_data is missing' do
|
||||
expect { client.update_lead(nil, lead_id) }
|
||||
.to raise_error(ArgumentError, 'Lead data is required')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when update is successful' do
|
||||
let(:success_response) do
|
||||
{
|
||||
'Status' => 'Success',
|
||||
'Message' => {
|
||||
'AffectedRows' => 1
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: formatted_lead_data.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: success_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns affected rows directly' do
|
||||
response = client.update_lead(lead_data, lead_id)
|
||||
expect(response).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when update fails' do
|
||||
let(:error_response) do
|
||||
{
|
||||
'Status' => 'Error',
|
||||
'ExceptionMessage' => 'Invalid lead ID'
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(:post, full_url)
|
||||
.with(
|
||||
body: formatted_lead_data.to_json,
|
||||
headers: headers
|
||||
)
|
||||
.to_return(
|
||||
status: 200,
|
||||
body: error_response.to_json,
|
||||
headers: { 'Content-Type' => 'application/json' }
|
||||
)
|
||||
end
|
||||
|
||||
it 'raises ApiError' do
|
||||
expect { client.update_lead(lead_data, lead_id) }
|
||||
.to(raise_error { |error| expect(error.class.name).to eq('Crm::Leadsquared::Api::BaseClient::ApiError') })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,98 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::LeadFinderService do
|
||||
let(:lead_client) { instance_double(Crm::Leadsquared::Api::LeadClient) }
|
||||
let(:service) { described_class.new(lead_client) }
|
||||
let(:contact) { create(:contact, email: 'test@example.com', phone_number: '+1234567890') }
|
||||
|
||||
describe '#find_or_create' do
|
||||
context 'when contact has stored lead ID' do
|
||||
before do
|
||||
contact.additional_attributes = { 'external' => { 'leadsquared_id' => '123' } }
|
||||
contact.save!
|
||||
end
|
||||
|
||||
it 'returns the stored lead ID' do
|
||||
result = service.find_or_create(contact)
|
||||
expect(result).to eq('123')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when contact has no stored lead ID' do
|
||||
context 'when lead is found by email' do
|
||||
before do
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.email)
|
||||
.and_return([{ 'ProspectID' => '456' }])
|
||||
end
|
||||
|
||||
it 'returns the found lead ID' do
|
||||
result = service.find_or_create(contact)
|
||||
expect(result).to eq('456')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lead is found by phone' do
|
||||
before do
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.email)
|
||||
.and_return([])
|
||||
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.phone_number)
|
||||
.and_return([{ 'ProspectID' => '789' }])
|
||||
end
|
||||
|
||||
it 'returns the found lead ID' do
|
||||
result = service.find_or_create(contact)
|
||||
expect(result).to eq('789')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lead is not found and needs to be created' do
|
||||
before do
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.email)
|
||||
.and_return([])
|
||||
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.phone_number)
|
||||
.and_return([])
|
||||
|
||||
allow(lead_client).to receive(:create_or_update_lead)
|
||||
.with(Crm::Leadsquared::Mappers::ContactMapper.map(contact))
|
||||
.and_return('999')
|
||||
end
|
||||
|
||||
it 'creates a new lead and returns its ID' do
|
||||
result = service.find_or_create(contact)
|
||||
expect(result).to eq('999')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when lead creation fails' do
|
||||
before do
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.email)
|
||||
.and_return([])
|
||||
|
||||
allow(lead_client).to receive(:search_lead)
|
||||
.with(contact.phone_number)
|
||||
.and_return([])
|
||||
|
||||
allow(Crm::Leadsquared::Mappers::ContactMapper).to receive(:map)
|
||||
.with(contact)
|
||||
.and_return({})
|
||||
|
||||
allow(lead_client).to receive(:create_or_update_lead)
|
||||
.with({})
|
||||
.and_raise(StandardError, 'Failed to create lead')
|
||||
end
|
||||
|
||||
it 'raises an error' do
|
||||
expect { service.find_or_create(contact) }.to raise_error(StandardError, 'Failed to create lead')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,48 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::Mappers::ContactMapper do
|
||||
let(:account) { create(:account) }
|
||||
let(:contact) { create(:contact, account: account, name: '', last_name: '', country_code: '') }
|
||||
let(:brand_name) { 'Test Brand' }
|
||||
|
||||
before do
|
||||
allow(GlobalConfig).to receive(:get).with('BRAND_NAME').and_return({ 'BRAND_NAME' => brand_name })
|
||||
end
|
||||
|
||||
describe '.map' do
|
||||
context 'with basic attributes' do
|
||||
it 'maps basic contact attributes correctly' do
|
||||
contact.update!(
|
||||
name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@example.com',
|
||||
# the phone number is intentionally wrong
|
||||
phone_number: '+1234567890'
|
||||
)
|
||||
|
||||
mapped_data = described_class.map(contact)
|
||||
|
||||
expect(mapped_data).to include(
|
||||
'FirstName' => 'John',
|
||||
'LastName' => 'Doe',
|
||||
'EmailAddress' => 'john@example.com',
|
||||
'Mobile' => '+1234567890',
|
||||
'Source' => 'Test Brand'
|
||||
)
|
||||
end
|
||||
|
||||
it 'represents the phone number correctly' do
|
||||
contact.update!(
|
||||
name: 'John',
|
||||
last_name: 'Doe',
|
||||
email: 'john@example.com',
|
||||
phone_number: '+917507684392'
|
||||
)
|
||||
|
||||
mapped_data = described_class.map(contact)
|
||||
|
||||
expect(mapped_data).to include('Mobile' => '+91-7507684392')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,265 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::Mappers::ConversationMapper do
|
||||
let(:account) { create(:account) }
|
||||
let(:inbox) { create(:inbox, account: account, name: 'Test Inbox', channel_type: 'Channel') }
|
||||
let(:conversation) { create(:conversation, account: account, inbox: inbox) }
|
||||
let(:user) { create(:user, name: 'John Doe') }
|
||||
let(:contact) { create(:contact, name: 'Jane Smith') }
|
||||
let(:hook) do
|
||||
create(:integrations_hook, :leadsquared, account: account, settings: {
|
||||
'access_key' => 'test_access_key',
|
||||
'secret_key' => 'test_secret_key',
|
||||
'endpoint_url' => 'https://api.leadsquared.com/v2',
|
||||
'timezone' => 'UTC'
|
||||
})
|
||||
end
|
||||
let(:hook_with_pst) do
|
||||
create(:integrations_hook, :leadsquared, account: account, settings: {
|
||||
'access_key' => 'test_access_key',
|
||||
'secret_key' => 'test_secret_key',
|
||||
'endpoint_url' => 'https://api.leadsquared.com/v2',
|
||||
'timezone' => 'America/Los_Angeles'
|
||||
})
|
||||
end
|
||||
let(:hook_without_timezone) do
|
||||
create(:integrations_hook, :leadsquared, account: account, settings: {
|
||||
'access_key' => 'test_access_key',
|
||||
'secret_key' => 'test_secret_key',
|
||||
'endpoint_url' => 'https://api.leadsquared.com/v2'
|
||||
})
|
||||
end
|
||||
|
||||
before do
|
||||
account.enable_features('crm_integration')
|
||||
allow(GlobalConfig).to receive(:get).with('BRAND_NAME').and_return({ 'BRAND_NAME' => 'TestBrand' })
|
||||
end
|
||||
|
||||
describe '.map_conversation_activity' do
|
||||
it 'generates conversation activity note with UTC timezone' do
|
||||
travel_to(Time.zone.parse('2024-01-01 10:00:00 UTC')) do
|
||||
result = described_class.map_conversation_activity(hook, conversation)
|
||||
|
||||
expect(result).to include('New conversation started on TestBrand')
|
||||
expect(result).to include('Channel: Test Inbox')
|
||||
expect(result).to include('Created: 2024-01-01 10:00:00')
|
||||
expect(result).to include("Conversation ID: #{conversation.display_id}")
|
||||
expect(result).to include('View in TestBrand: http://')
|
||||
end
|
||||
end
|
||||
|
||||
it 'formats time according to hook timezone setting' do
|
||||
travel_to(Time.zone.parse('2024-01-01 18:00:00 UTC')) do
|
||||
result = described_class.map_conversation_activity(hook_with_pst, conversation)
|
||||
|
||||
# PST is UTC-8, so 18:00 UTC becomes 10:00:00 PST
|
||||
expect(result).to include('Created: 2024-01-01 10:00:00')
|
||||
end
|
||||
end
|
||||
|
||||
it 'falls back to system timezone when hook has no timezone setting' do
|
||||
travel_to(Time.zone.parse('2024-01-01 10:00:00')) do
|
||||
result = described_class.map_conversation_activity(hook_without_timezone, conversation)
|
||||
|
||||
expect(result).to include('Created: 2024-01-01 10:00:00')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '.map_transcript_activity' do
|
||||
context 'when conversation has no messages' do
|
||||
it 'returns no messages message' do
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
expect(result).to eq('No messages in conversation')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when conversation has messages' do
|
||||
let(:message1) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: user,
|
||||
content: 'Hello',
|
||||
message_type: :outgoing,
|
||||
created_at: Time.zone.parse('2024-01-01 10:00:00'))
|
||||
end
|
||||
|
||||
let(:message2) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: contact,
|
||||
content: 'Hi there',
|
||||
message_type: :incoming,
|
||||
created_at: Time.zone.parse('2024-01-01 10:01:00'))
|
||||
end
|
||||
|
||||
let(:system_message) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: nil,
|
||||
content: 'System Message',
|
||||
message_type: :activity,
|
||||
created_at: Time.zone.parse('2024-01-01 10:02:00'))
|
||||
end
|
||||
|
||||
before do
|
||||
message1
|
||||
message2
|
||||
system_message
|
||||
end
|
||||
|
||||
def formatted_line_for(msg, hook_for_tz)
|
||||
tz = Time.find_zone(hook_for_tz.settings['timezone']) || Time.zone
|
||||
ts = msg.created_at.in_time_zone(tz).strftime('%Y-%m-%d %H:%M')
|
||||
sender = msg.sender&.name.presence || (msg.sender.present? ? "#{msg.sender_type} #{msg.sender_id}" : 'System')
|
||||
"[#{ts}] #{sender}: #{msg.content.presence || I18n.t('crm.no_content')}"
|
||||
end
|
||||
|
||||
it 'generates transcript with messages in reverse chronological order' do
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
|
||||
expect(result).to include('Conversation Transcript from TestBrand')
|
||||
expect(result).to include('Channel: Test Inbox')
|
||||
|
||||
# Check that messages appear in reverse order (newest first)
|
||||
newer = formatted_line_for(message2, hook)
|
||||
older = formatted_line_for(message1, hook)
|
||||
message_positions = {
|
||||
newer => result.index(newer),
|
||||
older => result.index(older)
|
||||
}
|
||||
|
||||
# Latest message (10:01) should come before older message (10:00)
|
||||
expect(message_positions[newer]).to be < message_positions[older]
|
||||
end
|
||||
|
||||
it 'formats message times according to hook timezone setting' do
|
||||
travel_to(Time.zone.parse('2024-01-01 18:00:00 UTC')) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: user,
|
||||
content: 'Test message',
|
||||
message_type: :outgoing,
|
||||
created_at: Time.zone.parse('2024-01-01 18:00:00 UTC'))
|
||||
|
||||
result = described_class.map_transcript_activity(hook_with_pst, conversation)
|
||||
|
||||
# PST is UTC-8, so 18:00 UTC becomes 10:00 PST
|
||||
expect(result).to include('[2024-01-01 10:00] John Doe: Test message')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when message has attachments' do
|
||||
let(:message_with_attachment) do
|
||||
create(:message, :with_attachment,
|
||||
conversation: conversation,
|
||||
sender: user,
|
||||
content: 'See attachment',
|
||||
message_type: :outgoing,
|
||||
created_at: Time.zone.parse('2024-01-01 10:03:00'))
|
||||
end
|
||||
|
||||
before { message_with_attachment }
|
||||
|
||||
it 'includes attachment information' do
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
|
||||
expect(result).to include('See attachment')
|
||||
expect(result).to include('[Attachment: image]')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when message has empty content' do
|
||||
let(:empty_message) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: user,
|
||||
content: '',
|
||||
message_type: :outgoing,
|
||||
created_at: Time.zone.parse('2024-01-01 10:04'))
|
||||
end
|
||||
|
||||
before { empty_message }
|
||||
|
||||
it 'shows no content placeholder' do
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
expect(result).to include('[No content]')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when sender has no name' do
|
||||
let(:unnamed_sender_message) do
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: create(:user, name: ''),
|
||||
content: 'Message',
|
||||
message_type: :outgoing,
|
||||
created_at: Time.zone.parse('2024-01-01 10:05'))
|
||||
end
|
||||
|
||||
before { unnamed_sender_message }
|
||||
|
||||
it 'uses sender type and id' do
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
expect(result).to include("User #{unnamed_sender_message.sender_id}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when messages exceed the ACTIVITY_NOTE_MAX_SIZE' do
|
||||
it 'truncates messages to stay within the character limit' do
|
||||
# Create a large number of messages with reasonably sized content
|
||||
long_message_content = 'A' * 200
|
||||
messages = []
|
||||
|
||||
# Create 15 messages (which should exceed the 1800 character limit)
|
||||
15.times do |i|
|
||||
messages << create(:message,
|
||||
conversation: conversation,
|
||||
sender: user,
|
||||
content: "#{long_message_content} #{i}",
|
||||
message_type: :outgoing,
|
||||
created_at: Time.zone.parse('2024-01-01 10:00:00') + i.hours)
|
||||
end
|
||||
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
|
||||
# Verify latest message is included (message 14)
|
||||
tz = Time.find_zone(hook.settings['timezone']) || Time.zone
|
||||
latest_label = "[#{messages.last.created_at.in_time_zone(tz).strftime('%Y-%m-%d %H:%M')}] John Doe: #{long_message_content} 14"
|
||||
expect(result).to include(latest_label)
|
||||
|
||||
# Calculate the expected character count of the formatted messages
|
||||
messages.map do |msg|
|
||||
"[#{msg.created_at.strftime('%Y-%m-%d %H:%M')}] John Doe: #{msg.content}"
|
||||
end
|
||||
|
||||
# Verify the result is within the character limit
|
||||
expect(result.length).to be <= described_class::ACTIVITY_NOTE_MAX_SIZE + 100
|
||||
|
||||
# Verify that not all messages are included (some were truncated)
|
||||
expect(messages.count).to be > result.scan('John Doe:').count
|
||||
end
|
||||
|
||||
it 'respects the ACTIVITY_NOTE_MAX_SIZE constant' do
|
||||
# Create a single message that would exceed the limit by itself
|
||||
giant_content = 'A' * 2000
|
||||
create(:message,
|
||||
conversation: conversation,
|
||||
sender: user,
|
||||
content: giant_content,
|
||||
message_type: :outgoing)
|
||||
|
||||
result = described_class.map_transcript_activity(hook, conversation)
|
||||
|
||||
# Extract just the formatted messages part
|
||||
id = conversation.display_id
|
||||
prefix = "Conversation Transcript from TestBrand\nChannel: Test Inbox\nConversation ID: #{id}\nView in TestBrand: "
|
||||
formatted_messages = result.sub(prefix, '').sub(%r{http://.*}, '')
|
||||
|
||||
# Check that it's under the limit (with some tolerance for the message format)
|
||||
expect(formatted_messages.length).to be <= described_class::ACTIVITY_NOTE_MAX_SIZE + 100
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,233 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::ProcessorService do
|
||||
let(:account) { create(:account) }
|
||||
let(:hook) do
|
||||
create(:integrations_hook, :leadsquared, account: account, settings: {
|
||||
'access_key' => 'test_access_key',
|
||||
'secret_key' => 'test_secret_key',
|
||||
'endpoint_url' => 'https://api.leadsquared.com/v2',
|
||||
'enable_transcript_activity' => true,
|
||||
'enable_conversation_activity' => true,
|
||||
'conversation_activity_code' => 1001,
|
||||
'transcript_activity_code' => 1002
|
||||
})
|
||||
end
|
||||
let(:contact) { create(:contact, account: account, email: 'test@example.com', phone_number: '+1234567890') }
|
||||
let(:contact_with_social_profile) do
|
||||
create(:contact, account: account, additional_attributes: { 'social_profiles' => { 'facebook' => 'chatwootapp' } })
|
||||
end
|
||||
let(:blank_contact) { create(:contact, account: account, email: '', phone_number: '') }
|
||||
let(:conversation) { create(:conversation, account: account, contact: contact) }
|
||||
let(:service) { described_class.new(hook) }
|
||||
let(:lead_client) { instance_double(Crm::Leadsquared::Api::LeadClient) }
|
||||
let(:activity_client) { instance_double(Crm::Leadsquared::Api::ActivityClient) }
|
||||
let(:lead_finder) { instance_double(Crm::Leadsquared::LeadFinderService) }
|
||||
|
||||
before do
|
||||
account.enable_features('crm_integration')
|
||||
allow(Crm::Leadsquared::Api::LeadClient).to receive(:new)
|
||||
.with('test_access_key', 'test_secret_key', 'https://api.leadsquared.com/v2')
|
||||
.and_return(lead_client)
|
||||
allow(Crm::Leadsquared::Api::ActivityClient).to receive(:new)
|
||||
.with('test_access_key', 'test_secret_key', 'https://api.leadsquared.com/v2')
|
||||
.and_return(activity_client)
|
||||
allow(Crm::Leadsquared::LeadFinderService).to receive(:new)
|
||||
.with(lead_client)
|
||||
.and_return(lead_finder)
|
||||
end
|
||||
|
||||
describe '.crm_name' do
|
||||
it 'returns leadsquared' do
|
||||
expect(described_class.crm_name).to eq('leadsquared')
|
||||
end
|
||||
end
|
||||
|
||||
describe '#handle_contact' do
|
||||
context 'when contact is valid' do
|
||||
before do
|
||||
allow(service).to receive(:identifiable_contact?).and_return(true)
|
||||
end
|
||||
|
||||
context 'when contact has no stored lead ID' do
|
||||
before do
|
||||
contact.update(additional_attributes: { 'external' => nil })
|
||||
contact.reload
|
||||
|
||||
allow(lead_client).to receive(:create_or_update_lead)
|
||||
.with(any_args)
|
||||
.and_return('new_lead_id')
|
||||
end
|
||||
|
||||
it 'creates a new lead and stores the ID' do
|
||||
service.handle_contact(contact)
|
||||
expect(lead_client).to have_received(:create_or_update_lead).with(any_args)
|
||||
expect(contact.reload.additional_attributes['external']['leadsquared_id']).to eq('new_lead_id')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when contact has existing lead ID' do
|
||||
before do
|
||||
contact.additional_attributes = { 'external' => { 'leadsquared_id' => 'existing_lead_id' } }
|
||||
contact.save!
|
||||
|
||||
allow(lead_client).to receive(:update_lead)
|
||||
.with(any_args)
|
||||
.and_return(nil) # The update method doesn't need to return anything
|
||||
end
|
||||
|
||||
it 'updates the lead using existing ID' do
|
||||
service.handle_contact(contact)
|
||||
expect(lead_client).to have_received(:update_lead).with(any_args)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when API call raises an error' do
|
||||
before do
|
||||
allow(lead_client).to receive(:create_or_update_lead)
|
||||
.with(any_args)
|
||||
.and_raise(Crm::Leadsquared::Api::BaseClient::ApiError.new('API Error'))
|
||||
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
it 'catches and logs the error' do
|
||||
service.handle_contact(contact)
|
||||
expect(Rails.logger).to have_received(:error).with(/LeadSquared API error/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when contact is invalid' do
|
||||
before do
|
||||
allow(service).to receive(:identifiable_contact?).and_return(false)
|
||||
allow(lead_client).to receive(:create_or_update_lead)
|
||||
end
|
||||
|
||||
it 'returns without making API calls' do
|
||||
service.handle_contact(blank_contact)
|
||||
expect(lead_client).not_to have_received(:create_or_update_lead)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#handle_conversation_created' do
|
||||
let(:activity_note) { 'New conversation started' }
|
||||
|
||||
before do
|
||||
allow(Crm::Leadsquared::Mappers::ConversationMapper).to receive(:map_conversation_activity)
|
||||
.with(hook, conversation)
|
||||
.and_return(activity_note)
|
||||
end
|
||||
|
||||
context 'when conversation activities are enabled' do
|
||||
before do
|
||||
service.instance_variable_set(:@allow_conversation, true)
|
||||
end
|
||||
|
||||
context 'when lead_id is found' do
|
||||
before do
|
||||
allow(lead_finder).to receive(:find_or_create)
|
||||
.with(contact)
|
||||
.and_return('test_lead_id')
|
||||
|
||||
allow(activity_client).to receive(:post_activity)
|
||||
.with('test_lead_id', 1001, activity_note)
|
||||
.and_return('test_activity_id')
|
||||
end
|
||||
|
||||
it 'creates the activity and stores metadata' do
|
||||
service.handle_conversation_created(conversation)
|
||||
expect(conversation.reload.additional_attributes['leadsquared']['created_activity_id']).to eq('test_activity_id')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when post_activity raises an error' do
|
||||
before do
|
||||
allow(lead_finder).to receive(:find_or_create)
|
||||
.with(contact)
|
||||
.and_return('test_lead_id')
|
||||
|
||||
allow(activity_client).to receive(:post_activity)
|
||||
.with('test_lead_id', 1001, activity_note)
|
||||
.and_raise(StandardError.new('Activity error'))
|
||||
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
it 'logs the error' do
|
||||
service.handle_conversation_created(conversation)
|
||||
expect(Rails.logger).to have_received(:error).with(/Error creating conversation activity/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when conversation activities are disabled' do
|
||||
before do
|
||||
service.instance_variable_set(:@allow_conversation, false)
|
||||
allow(activity_client).to receive(:post_activity)
|
||||
end
|
||||
|
||||
it 'does not create an activity' do
|
||||
service.handle_conversation_created(conversation)
|
||||
expect(activity_client).not_to have_received(:post_activity)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#handle_conversation_resolved' do
|
||||
let(:activity_note) { 'Conversation transcript' }
|
||||
|
||||
before do
|
||||
allow(Crm::Leadsquared::Mappers::ConversationMapper).to receive(:map_transcript_activity)
|
||||
.with(hook, conversation)
|
||||
.and_return(activity_note)
|
||||
end
|
||||
|
||||
context 'when transcript activities are enabled and conversation is resolved' do
|
||||
before do
|
||||
service.instance_variable_set(:@allow_transcript, true)
|
||||
conversation.update!(status: 'resolved')
|
||||
|
||||
allow(lead_finder).to receive(:find_or_create)
|
||||
.with(contact)
|
||||
.and_return('test_lead_id')
|
||||
|
||||
allow(activity_client).to receive(:post_activity)
|
||||
.with('test_lead_id', 1002, activity_note)
|
||||
.and_return('test_activity_id')
|
||||
end
|
||||
|
||||
it 'creates the transcript activity and stores metadata' do
|
||||
service.handle_conversation_resolved(conversation)
|
||||
expect(conversation.reload.additional_attributes['leadsquared']['transcript_activity_id']).to eq('test_activity_id')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when conversation is not resolved' do
|
||||
before do
|
||||
service.instance_variable_set(:@allow_transcript, true)
|
||||
conversation.update!(status: 'open')
|
||||
allow(activity_client).to receive(:post_activity)
|
||||
end
|
||||
|
||||
it 'does not create an activity' do
|
||||
service.handle_conversation_resolved(conversation)
|
||||
expect(activity_client).not_to have_received(:post_activity)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when transcript activities are disabled' do
|
||||
before do
|
||||
service.instance_variable_set(:@allow_transcript, false)
|
||||
conversation.update!(status: 'resolved')
|
||||
allow(activity_client).to receive(:post_activity)
|
||||
end
|
||||
|
||||
it 'does not create an activity' do
|
||||
service.handle_conversation_resolved(conversation)
|
||||
expect(activity_client).not_to have_received(:post_activity)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,125 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Crm::Leadsquared::SetupService do
|
||||
let(:account) { create(:account) }
|
||||
let(:hook) { create(:integrations_hook, :leadsquared, account: account) }
|
||||
let(:service) { described_class.new(hook) }
|
||||
let(:base_client) { instance_double(Crm::Leadsquared::Api::BaseClient) }
|
||||
let(:activity_client) { instance_double(Crm::Leadsquared::Api::ActivityClient) }
|
||||
let(:endpoint_response) do
|
||||
{
|
||||
'TimeZone' => 'Asia/Kolkata',
|
||||
'LSQCommonServiceURLs' => {
|
||||
'api' => 'api-in.leadsquared.com',
|
||||
'app' => 'app.leadsquared.com'
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
account.enable_features('crm_integration')
|
||||
allow(Crm::Leadsquared::Api::BaseClient).to receive(:new).and_return(base_client)
|
||||
allow(Crm::Leadsquared::Api::ActivityClient).to receive(:new).and_return(activity_client)
|
||||
allow(base_client).to receive(:get).with('Authentication.svc/UserByAccessKey.Get').and_return(endpoint_response)
|
||||
end
|
||||
|
||||
describe '#setup' do
|
||||
context 'when fetching activity types succeeds' do
|
||||
let(:started_type) do
|
||||
{ 'ActivityEventName' => 'Chatwoot Conversation Started', 'ActivityEvent' => 1001 }
|
||||
end
|
||||
|
||||
let(:transcript_type) do
|
||||
{ 'ActivityEventName' => 'Chatwoot Conversation Transcript', 'ActivityEvent' => 1002 }
|
||||
end
|
||||
|
||||
context 'when all required types exist' do
|
||||
before do
|
||||
allow(activity_client).to receive(:fetch_activity_types)
|
||||
.and_return([started_type, transcript_type])
|
||||
end
|
||||
|
||||
it 'uses existing activity types and updates hook settings' do
|
||||
service.setup
|
||||
|
||||
# Verify hook settings were merged with existing settings
|
||||
updated_settings = hook.reload.settings
|
||||
expect(updated_settings['endpoint_url']).to eq('https://api-in.leadsquared.com/v2/')
|
||||
expect(updated_settings['app_url']).to eq('https://app.leadsquared.com/')
|
||||
expect(updated_settings['timezone']).to eq('Asia/Kolkata')
|
||||
expect(updated_settings['conversation_activity_code']).to eq(1001)
|
||||
expect(updated_settings['transcript_activity_code']).to eq(1002)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when some activity types need to be created' do
|
||||
before do
|
||||
allow(activity_client).to receive(:fetch_activity_types)
|
||||
.and_return([started_type])
|
||||
|
||||
allow(activity_client).to receive(:create_activity_type)
|
||||
.with(
|
||||
name: 'Chatwoot Conversation Transcript',
|
||||
score: 0,
|
||||
direction: 0
|
||||
)
|
||||
.and_return(1002)
|
||||
end
|
||||
|
||||
it 'creates missing types and updates hook settings' do
|
||||
service.setup
|
||||
|
||||
# Verify hook settings were merged with existing settings
|
||||
updated_settings = hook.reload.settings
|
||||
expect(updated_settings['endpoint_url']).to eq('https://api-in.leadsquared.com/v2/')
|
||||
expect(updated_settings['app_url']).to eq('https://app.leadsquared.com/')
|
||||
expect(updated_settings['timezone']).to eq('Asia/Kolkata')
|
||||
expect(updated_settings['conversation_activity_code']).to eq(1001)
|
||||
expect(updated_settings['transcript_activity_code']).to eq(1002)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when activity type creation fails' do
|
||||
before do
|
||||
allow(activity_client).to receive(:fetch_activity_types)
|
||||
.and_return([started_type])
|
||||
|
||||
allow(activity_client).to receive(:create_activity_type)
|
||||
.with(anything)
|
||||
.and_raise(StandardError.new('Failed to create activity type'))
|
||||
|
||||
allow(Rails.logger).to receive(:error)
|
||||
end
|
||||
|
||||
it 'logs the error and returns nil' do
|
||||
expect(service.setup).to be_nil
|
||||
expect(Rails.logger).to have_received(:error).with(/Error during LeadSquared setup/)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#activity_types' do
|
||||
it 'defines conversation started activity type' do
|
||||
required_types = service.send(:activity_types)
|
||||
conversation_type = required_types.find { |t| t[:setting_key] == 'conversation_activity_code' }
|
||||
expect(conversation_type).to include(
|
||||
name: 'Chatwoot Conversation Started',
|
||||
score: 0,
|
||||
direction: 0,
|
||||
setting_key: 'conversation_activity_code'
|
||||
)
|
||||
end
|
||||
|
||||
it 'defines transcript activity type' do
|
||||
required_types = service.send(:activity_types)
|
||||
transcript_type = required_types.find { |t| t[:setting_key] == 'transcript_activity_code' }
|
||||
expect(transcript_type).to include(
|
||||
name: 'Chatwoot Conversation Transcript',
|
||||
score: 0,
|
||||
direction: 0,
|
||||
setting_key: 'transcript_activity_code'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user