iachat/app/javascript/dashboard/i18n/locale/en
Vinay Keerthi 005a22fd69
feat: Add sorting by contacts count to companies list (#13012)
## Description

Adds the ability to sort companies by the number of contacts they have
(contacts_count) in ascending or descending order. This is part of the
Chatwoot 5.0 release requirements for the companies feature.

The implementation uses a scope-based approach consistent with other
sorting implementations in the codebase (e.g., contacts sorting by
last_activity_at).

## Type of change

- [x] New feature (non-breaking change which adds functionality)

## Available Sorting Options

After this change, the Companies API supports the following sorting
options:

| Sort Field | Type | Ascending | Descending |
|------------|------|-----------|------------|
| `name` | string | `?sort=name` | `?sort=-name` |
| `domain` | string | `?sort=domain` | `?sort=-domain` |
| `created_at` | datetime | `?sort=created_at` | `?sort=-created_at` |
| `contacts_count` | integer (scope) | `?sort=contacts_count` |
`?sort=-contacts_count` |

**Note:** Prefix with `-` for descending order. Companies with NULL
contacts_count will appear last (NULLS LAST).

## CURL Examples

**Sort by contacts count (ascending):**
```bash
curl -X GET 'https://app.chatwoot.com/api/v1/accounts/{account_id}/companies?sort=contacts_count' \
  -H 'api_access_token: YOUR_API_TOKEN'
```

**Sort by contacts count (descending):**
```bash
curl -X GET 'https://app.chatwoot.com/api/v1/accounts/{account_id}/companies?sort=-contacts_count' \
  -H 'api_access_token: YOUR_API_TOKEN'
```

**Sort by name (ascending):**
```bash
curl -X GET 'https://app.chatwoot.com/api/v1/accounts/{account_id}/companies?sort=name' \
  -H 'api_access_token: YOUR_API_TOKEN'
```

**Sort by created_at (descending):**
```bash
curl -X GET 'https://app.chatwoot.com/api/v1/accounts/{account_id}/companies?sort=-created_at' \
  -H 'api_access_token: YOUR_API_TOKEN'
```

**With pagination:**
```bash
curl -X GET 'https://app.chatwoot.com/api/v1/accounts/{account_id}/companies?sort=-contacts_count&page=2' \
  -H 'api_access_token: YOUR_API_TOKEN'
```

## How Has This Been Tested?

- Added RSpec tests for both ascending and descending sort
- All 24 existing specs pass
- Manually tested the sorting functionality with test data

**Test configuration:**
- Ruby 3.4.4
- Rails 7.1.5.2
- PostgreSQL (test database)

**To reproduce:**
1. Run `bundle exec rspec
spec/enterprise/controllers/api/v1/accounts/companies_controller_spec.rb`
2. All tests should pass (24 examples, 0 failures)

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [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

## Technical Details

**Backend changes:**
- Controller: Added `sort_on :contacts_count` with scope-based sorting
- Model: Added `order_on_contacts_count` scope using
`Arel::Nodes::SqlLiteral` and `sanitize_sql_for_order` with `NULLS LAST`
for consistent NULL handling
- Specs: Added 2 new tests for ascending/descending sort validation

**Files changed:**
- `enterprise/app/controllers/api/v1/accounts/companies_controller.rb`
- `enterprise/app/models/company.rb`
-
`spec/enterprise/controllers/api/v1/accounts/companies_controller_spec.rb`

**Note:** This PR only includes the backend implementation. Frontend
changes (sort menu UI + i18n) will follow in a separate commit.

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: iamsivin <iamsivin@gmail.com>
Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Pranav <pranav@chatwoot.com>
2026-01-09 15:20:08 -08:00
..
advancedFilters.json feat(v4): Add new conversation filters component (#10502) 2024-11-27 20:05:54 -08:00
agentBots.json feat: Ability to reset api_access_token (#11565) 2025-05-29 14:42:13 +05:30
agentMgmt.json fix: Translate "None" option in agent assignment dropdown (#11060) 2025-03-11 14:49:27 -07:00
attributesMgmt.json feat: Custom attribute page redesign (#13087) 2025-12-17 14:30:49 +05:30
auditLogs.json feat: Add conversation delete feature (#11677) 2025-06-05 15:53:17 -05:00
automation.json feat: Add support for labels in automations (#11658) 2025-09-18 14:17:54 +05:30
bulkActions.json feat: Bulk actions for contacts (#12763) 2025-10-30 15:28:28 +05:30
campaign.json feat: WhatsApp campaigns (#11910) 2025-07-16 09:04:02 +05:30
cannedMgmt.json feat: Vite + vue 3 💚 (#10047) 2024-10-02 00:36:30 -07:00
chatlist.json feat: TikTok channel (#12741) 2025-12-17 07:54:50 -08:00
companies.json feat: Add sorting by contacts count to companies list (#13012) 2026-01-09 15:20:08 -08:00
components.json chore: Improve pagination with compact number formatting and pluralization (#12962) 2025-11-27 10:32:34 +05:30
contact.json feat: TikTok channel (#12741) 2025-12-17 07:54:50 -08:00
contactFilters.json feat: Ability to filter contact based on labels (#12343) 2025-09-02 12:35:18 +05:30
contentTemplates.json feat: Add call-to-action template support for Twilio (#13179) 2026-01-06 10:38:36 +04:00
conversation.json feat: Voice Channel (#11602) 2025-12-19 12:41:33 -08:00
csatMgmt.json feat: more CSAT filters (#7038) 2023-05-18 22:50:46 +05:30
customRole.json feat: Vite + vue 3 💚 (#10047) 2024-10-02 00:36:30 -07:00
datePicker.json feat: Custom date picker (#9247) 2024-04-29 14:43:57 +05:30
emoji.json feat: Adds the ability to set an emoji for help center category (#8111) 2023-10-20 13:52:30 +05:30
general.json feat: SAML feedback changes [CW-5666] (#12511) 2025-09-24 16:07:07 +05:30
generalSettings.json fix: Translate Priority and Messages types in Automations and Macros (#11741) 2025-07-08 12:40:40 +05:30
helpCenter.json feat: Allow detaching help center widget (#12459) 2025-09-17 22:27:50 +05:30
inbox.json feat: add reauth flow for wa embedded signup (#11940) 2025-08-08 01:48:45 +05:30
inboxMgmt.json feat: Add support for sending CSAT surveys via templates (Whatsapp Cloud) (#12787) 2026-01-06 11:46:00 +04:00
index.js feat(ce): Add Year in review feature (#13078) 2025-12-15 17:24:45 -08:00
integrationApps.json chore: Update settings to match the new design (#11084) 2025-03-14 14:28:14 +05:30
integrations.json chore: Hide assistant switcher on paywall screen (#12875) 2025-11-17 09:58:59 +05:30
labelsMgmt.json feat: Vite + vue 3 💚 (#10047) 2024-10-02 00:36:30 -07:00
login.json feat: Form validation message for password input (#11705) 2025-09-29 11:12:04 +05:30
macros.json fix: Translate Priority and Messages types in Automations and Macros (#11741) 2025-07-08 12:40:40 +05:30
mfa.json feat: Add the frontend support for MFA (#12372) 2025-09-18 21:16:06 +05:30
report.json feat: allow selecting month range in overview reports (#12701) 2025-11-12 18:34:00 +05:30
resetPassword.json feat: Support dark mode in login pages (#7420) 2023-07-01 07:49:52 +05:30
search.json feat: Advanced Search Backend (#12917) 2026-01-07 15:30:49 +05:30
setNewPassword.json feat: update product copy [CWM-45] (#6372) 2023-04-25 22:02:15 -07:00
settings.json feat(ce): Add Year in review feature (#13078) 2025-12-15 17:24:45 -08:00
signup.json feat: Form validation message for password input (#11705) 2025-09-29 11:12:04 +05:30
sla.json feat: Update the design for integration page (#9825) 2024-07-24 16:35:40 -07:00
teamsSettings.json chore: update interpolation syntax for i18n files (#10198) 2024-10-07 11:12:40 -07:00
webhooks.json Feature: As a admin, I should be able to add webhooks to account (#572) 2020-02-29 17:43:49 +05:30
whatsappTemplates.json feat: Add media_name support for WhatsApp templates document files (#12462) 2025-09-18 15:25:31 +05:30
yearInReview.json feat(ce): Add Year in review feature (#13078) 2025-12-15 17:24:45 -08:00