feat: Backend - Companies API endpoint with pagination and search (#12840)
## Description Adds API endpoint to list companies with pagination, search, and sorting. Fixes https://linear.app/chatwoot/issue/CW-5930/add-backend-routes-to-get-companies-result Parent issue: https://linear.app/chatwoot/issue/CW-5928/add-companies-tab-to-dashboard ## Type of change - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? Added comprehensive specs to `spec/enterprise/controllers/api/v1/accounts/companies_controller_spec.rb`: - Pagination (25 per page, multiple pages) - Search by name and domain (case-insensitive) - Counter cache for contacts_count - Account scoping - Authorization To reproduce: ```bash bundle exec rspec spec/enterprise/controllers/api/v1/accounts/companies_controller_spec.rb bundle exec rubocop enterprise/app/controllers/api/v1/accounts/companies_controller.rb ``` ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [ ] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Shivam Mishra <scm.mymail@gmail.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -25,10 +25,150 @@ RSpec.describe 'Companies API', type: :request do
|
||||
expect(response_body['payload'].size).to eq(2)
|
||||
expect(response_body['payload'].map { |c| c['name'] }).to contain_exactly(company1.name, company2.name)
|
||||
end
|
||||
|
||||
it 'returns companies with pagination' do
|
||||
create_list(:company, 30, account: account)
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/companies",
|
||||
params: { page: 1 },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
expect(response_body['payload'].size).to eq(25)
|
||||
expect(response_body['meta']['total_count']).to eq(32)
|
||||
expect(response_body['meta']['page']).to eq(1)
|
||||
end
|
||||
|
||||
it 'returns second page of companies' do
|
||||
create_list(:company, 30, account: account)
|
||||
get "/api/v1/accounts/#{account.id}/companies",
|
||||
params: { page: 2 },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
expect(response_body['payload'].size).to eq(7)
|
||||
expect(response_body['meta']['total_count']).to eq(32)
|
||||
expect(response_body['meta']['page']).to eq(2)
|
||||
end
|
||||
|
||||
it 'returns companies with contacts_count' do
|
||||
company_with_contacts = create(:company, name: 'Company With Contacts', account: account)
|
||||
create_list(:contact, 5, company: company_with_contacts, account: account)
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/companies",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
company_data = response_body['payload'].find { |c| c['id'] == company_with_contacts.id }
|
||||
expect(company_data['contacts_count']).to eq(5)
|
||||
end
|
||||
|
||||
it 'does not return companies from other accounts' do
|
||||
other_account = create(:account)
|
||||
create(:company, name: 'Other Account Company', account: other_account)
|
||||
create(:company, name: 'My Company', account: account)
|
||||
get "/api/v1/accounts/#{account.id}/companies",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
expect(response_body['payload'].size).to eq(3)
|
||||
expect(response_body['payload'].map { |c| c['name'] }).not_to include('Other Account Company')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/companies/search' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/companies/search"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
|
||||
it 'returns error when q parameter is missing' do
|
||||
get "/api/v1/accounts/#{account.id}/companies/search",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
expect(response.parsed_body['error']).to eq('Specify search string with parameter q')
|
||||
end
|
||||
|
||||
it 'searches companies by name' do
|
||||
create(:company, name: 'Acme Corp', domain: 'acme.com', account: account)
|
||||
create(:company, name: 'Tech Solutions', domain: 'tech.com', account: account)
|
||||
create(:company, name: 'Global Inc', domain: 'global.com', account: account)
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/companies/search",
|
||||
params: { q: 'tech' },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
expect(response_body['payload'].size).to eq(1)
|
||||
expect(response_body['payload'].first['name']).to eq('Tech Solutions')
|
||||
end
|
||||
|
||||
it 'searches companies by domain' do
|
||||
create(:company, name: 'Acme Corp', domain: 'acme.com', account: account)
|
||||
create(:company, name: 'Tech Solutions', domain: 'tech.com', account: account)
|
||||
create(:company, name: 'Global Inc', domain: 'global.com', account: account)
|
||||
|
||||
get "/api/v1/accounts/#{account.id}/companies/search",
|
||||
params: { q: 'acme.com' },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
expect(response_body['payload'].size).to eq(1)
|
||||
expect(response_body['payload'].first['domain']).to eq('acme.com')
|
||||
end
|
||||
|
||||
it 'search is case insensitive' do
|
||||
create(:company, name: 'Acme Corp', domain: 'acme.com', account: account)
|
||||
get "/api/v1/accounts/#{account.id}/companies/search",
|
||||
params: { q: 'ACME' },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
|
||||
expect(response_body['payload'].size).to eq(1)
|
||||
end
|
||||
|
||||
it 'returns empty array when no companies match search' do
|
||||
create(:company, name: 'Acme Corp', domain: 'acme.com', account: account)
|
||||
get "/api/v1/accounts/#{account.id}/companies/search",
|
||||
params: { q: 'nonexistent' },
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
expect(response).to have_http_status(:success)
|
||||
response_body = response.parsed_body
|
||||
expect(response_body['payload'].size).to eq(0)
|
||||
expect(response_body['meta']['total_count']).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/companies/{id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
company = create(:company, account: account)
|
||||
get "/api/v1/accounts/#{account.id}/companies/#{company.id}"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:company) { create(:company, account: account) }
|
||||
@@ -46,6 +186,13 @@ RSpec.describe 'Companies API', type: :request do
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/companies' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
post "/api/v1/accounts/#{account.id}/companies"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:valid_params) do
|
||||
@@ -85,6 +232,14 @@ RSpec.describe 'Companies API', type: :request do
|
||||
end
|
||||
|
||||
describe 'PATCH /api/v1/accounts/{account.id}/companies/{id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
company = create(:company, account: account)
|
||||
patch "/api/v1/accounts/#{account.id}/companies/#{company.id}"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated user' do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:company) { create(:company, account: account) }
|
||||
@@ -111,6 +266,14 @@ RSpec.describe 'Companies API', type: :request do
|
||||
end
|
||||
|
||||
describe 'DELETE /api/v1/accounts/{account.id}/companies/{id}' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
company = create(:company, account: account)
|
||||
delete "/api/v1/accounts/#{account.id}/companies/#{company.id}"
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an authenticated administrator' do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:company) { create(:company, account: account) }
|
||||
|
||||
@@ -45,6 +45,25 @@ RSpec.describe Contacts::CompanyAssociationService, type: :service do
|
||||
contact.reload
|
||||
expect(contact.company).to eq(existing_company)
|
||||
end
|
||||
|
||||
it 'increments company contacts_count when associating contact' do
|
||||
# Create contact without email to avoid auto-association
|
||||
contact = create(:contact, email: nil, account: account)
|
||||
# Manually set email to bypass callbacks
|
||||
# rubocop:disable Rails/SkipsModelValidations
|
||||
contact.update_column(:email, 'jane@techcorp.com')
|
||||
# rubocop:enable Rails/SkipsModelValidations
|
||||
|
||||
valid_email_address = instance_double(ValidEmail2::Address, valid?: true, disposable_domain?: false)
|
||||
allow(ValidEmail2::Address).to receive(:new).with('jane@techcorp.com').and_return(valid_email_address)
|
||||
allow(EmailProviderInfo).to receive(:call).with('jane@techcorp.com').and_return(nil)
|
||||
|
||||
service.associate_company_from_email(contact)
|
||||
|
||||
contact.reload
|
||||
expect(contact.company).to be_present
|
||||
expect(contact.company.contacts_count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when contact already has a company' do
|
||||
|
||||
Reference in New Issue
Block a user