iachat/spec/models
Muhsin Keloth 7554156abe
chore: Account switching issue in newly added accounts (#12403)
The system determines a user’s active account by checking the
`active_at` field in the `account_users` table and selecting the most
recently active account:

```ruby
def active_account_user
  account_users.order(active_at: :desc)&.first
end
```

This works fine when all accounts have a valid active_at timestamp.

**Problem**

When a user is added to a new account, the `active_at` value is NULL
(because the account has never been explicitly activated). Ordering by
active_at DESC produces inconsistent results across databases, since
handling of NULL values differs (sometimes treated as high, sometimes
low).

As a result:

- Mobile apps (critical impact): `/profile` returns the wrong account.
The UI keeps showing the old account even after switching, and
restarting does not fix it.
- Web app (accidentally works): Appears correct because the active
account is inferred from the browser URL, but the backend API is still
wrong.

**Root Cause**

- The ordering logic did not account for NULL `active_at`.
- New accounts without active_at sometimes get incorrectly prioritized
as the “active” account.

**Solution**

Explicitly ensure that accounts with NULL active_at are sorted after
accounts with real timestamps by using NULLS LAST:

```ruby
def active_account_user
  account_users.order(Arel.sql('active_at DESC NULLS LAST, id DESC'))&.first
end
```

- Accounts with actual `active_at` values will always be prioritized.
- New accounts (with NULL active_at) will be placed at the bottom until
the user explicitly activates them.
- Adding id DESC as a secondary ordering ensures consistent tie-breaking
when multiple accounts have the same `active_at`.
2025-09-10 14:12:22 +05:30
..
channel feat: Remove subscription on WhatsApp inbox delete (#11977) 2025-07-24 14:04:19 +04:00
concerns feat: Agent language settings (#11222) 2025-09-09 14:27:36 +05:30
enterprise/audit feat: Add conversation delete feature (#11677) 2025-06-05 15:53:17 -05:00
integrations feat: integrate LeadSquared CRM (#11284) 2025-04-29 09:14:00 +05:30
.keep
account_spec.rb feat: Add support for minutes in auto resolve feature (#11269) 2025-05-07 00:36:15 -07:00
account_user_spec.rb
agent_bot_inbox_spec.rb
agent_bot_spec.rb feat: Implement UI for Agent Bots in settings and remove CSML support (#11276) 2025-04-16 18:02:49 +05:30
article_spec.rb chore: Update LLM formatter classes to include additional details (#11491) 2025-05-15 17:47:37 -07:00
assignment_policy_spec.rb feat: Add assignment policies controllers with jbuilder views (#12199) 2025-08-18 19:15:21 -07:00
attachment_spec.rb fix: cw-5411 handle unrepresentable image attachments (#12178) 2025-08-12 19:26:58 -07:00
automation_rule_spec.rb
campaign_spec.rb chore: Add validations to campaign model 2025-03-19 17:29:08 -07:00
category_spec.rb
contact_inbox_spec.rb
contact_spec.rb feat: Introduce the crm_v2 feature flag for CRM changes (#12014) 2025-07-23 17:07:02 +04:00
conversation_participants_spec.rb
conversation_spec.rb fix: Reply time calculation for re-opened conversations (#11787) 2025-06-27 10:48:07 +05:30
csat_survey_response_spec.rb
data_import_spec.rb
folder_spec.rb
inbox_member_spec.rb
inbox_spec.rb feat: remove colon and semicolons when sanitizing inbox name (#11889) 2025-07-08 09:41:40 +05:30
installation_config_spec.rb
label_spec.rb
macro_spec.rb
mention_spec.rb
message_spec.rb feat(ee): Setup advanced, performant message search (#12193) 2025-08-28 10:10:28 +05:30
note_spec.rb feat(ee): Add Captain features (#10665) 2025-01-14 16:15:47 -08:00
notification_setting_spec.rb
notification_spec.rb fix: Handle emoji and special characters in mention notifications (#11857) 2025-07-03 11:32:13 +05:30
platform_app_permissible_spec.rb
platform_app_spec.rb
portal_spec.rb chore: Clean up report & knowledge base policies (#11234) 2025-04-03 16:00:32 -07:00
related_category_spec.rb
reporting_event_spec.rb
team_member_spec.rb
team_spec.rb feat: invalidate cache after inbox members or team members update (#10869) 2025-02-20 21:28:38 -08:00
user_spec.rb chore: Account switching issue in newly added accounts (#12403) 2025-09-10 14:12:22 +05:30
webhook_spec.rb
working_hour_spec.rb