Skip to content

Fix legacy null values in LivechatVisitors (visitorEmails and phone)#40250

Open
JarvisX-Z wants to merge 3 commits into
RocketChat:developfrom
JarvisX-Z:patch-6
Open

Fix legacy null values in LivechatVisitors (visitorEmails and phone)#40250
JarvisX-Z wants to merge 3 commits into
RocketChat:developfrom
JarvisX-Z:patch-6

Conversation

@JarvisX-Z
Copy link
Copy Markdown

@JarvisX-Z JarvisX-Z commented Apr 22, 2026

Proposed changes

This PR addresses legacy data inconsistencies in the LivechatVisitors collection.

It resolves an issue where some documents contain null values for visitorEmails and phone fields, which breaks Lead Capture functionality due to incompatible usage with the MongoDB $addToSet operator.

A database migration is introduced to normalize these fields to empty arrays, ensuring backward compatibility and preventing runtime failures.

This implements the recommended solution from issue #40168.

Issue(s)

Closes #40168

Further comments

This migration is safe and idempotent. It only targets invalid or missing fields and does not affect properly structured data.

It ensures backward compatibility for legacy records while maintaining schema consistency for future operations.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed null values in LiveChat visitor email and phone fields, now using empty arrays instead for improved data consistency and system reliability.

This migration updates the 'livechat_visitor' collection to set 'visitorEmails' and 'phone' to empty arrays if they are null or do not exist.
@JarvisX-Z JarvisX-Z requested a review from a team as a code owner April 22, 2026 06:53
@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented Apr 22, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project
  • This PR has an invalid title

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 22, 2026

⚠️ No Changeset found

Latest commit: 033c625

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 22, 2026

Walkthrough

Added a new database migration (version 336) for the livechat_visitor collection that converts null or missing visitorEmails and phone fields to empty arrays, addressing legacy data inconsistencies that break downstream field operations.

Changes

Cohort / File(s) Summary
Database Migration
apps/meteor/server/startup/migrations/v336.ts
New migration script fixing null or missing visitorEmails and phone fields in the livechat_visitor collection by converting them to empty arrays.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

type: bug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing legacy null values in LivechatVisitors for visitorEmails and phone fields.
Linked Issues check ✅ Passed The PR implements the migration solution proposed in issue #40168, which addresses the legacy null values in visitorEmails and phone fields that break Lead Capture functionality.
Out of Scope Changes check ✅ Passed The change is narrowly scoped to the migration file (v336.ts) and directly addresses the linked issue #40168 requirements without introducing unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@JarvisX-Z
Copy link
Copy Markdown
Author

This PR implements the migration-based solution suggested in #40168.

The migration has been tested to ensure:

  • null and missing fields are normalized to []
  • $addToSet operations work correctly afterward

This change is a database migration fix and does not require a version bump.

@maintainers please add 'stat: QA assured' label and assign milestone if appropriate.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@apps/meteor/server/startup/migrations/v336.ts`:
- Around line 1-27: The migration currently uses
this.db.collection('livechat_visitor') which fails because IMigration does not
expose db; instead, import and use the LivechatVisitors model (e.g., call
LivechatVisitors.updateMany) inside the up() function; replace the col =
this.db.collection('livechat_visitor') usage and run the same two updateMany
operations against LivechatVisitors with the same filter and $set payloads so
the visitorEmails and phone fields are fixed via the model API.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 127c691f-23be-4895-b864-aec4f5211358

📥 Commits

Reviewing files that changed from the base of the PR and between 6a49770 and 0228b41.

📒 Files selected for processing (1)
  • apps/meteor/server/startup/migrations/v336.ts
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/server/startup/migrations/v336.ts
🧠 Learnings (10)
📓 Common learnings
Learnt from: nazabucciarelli
Repo: RocketChat/Rocket.Chat PR: 39905
File: packages/models/src/models/LivechatVisitors.ts:391-406
Timestamp: 2026-03-31T23:29:12.037Z
Learning: In `packages/models/src/models/LivechatVisitors.ts`, `saveGuestEmailPhoneById` uses a two-step approach: (1) a `{ visitorEmails: null }` / `{ phone: null }` guard to lazily initialize legacy null fields to empty arrays (concurrency-safe: subsequent concurrent calls simply skip the no-match), then (2) `$addToSet` with `$each` to append without reordering. This is intentional and correct. Do not flag concurrency or ordering concerns: `visitorEmails[0]` is the primary email set during visitor registration and is never displaced by `$addToSet`; the `leadCapture` hook is a data-scraping bucket where entry order is irrelevant to business logic.
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39647
File: apps/meteor/app/api/server/v1/users.ts:891-899
Timestamp: 2026-03-15T14:31:23.493Z
Learning: In RocketChat/Rocket.Chat, `IUser.inactiveReason` in `packages/core-typings/src/IUser.ts` is typed as `'deactivated' | 'pending_approval' | 'idle_too_long'` (optional, no `null`), but the database stores `null` for newly created users. The Typia-generated `$ref: '#/components/schemas/IUser'` schema therefore correctly rejects `null` for `inactiveReason`. This causes the test "should create a new user with default roles" to fail when response validation is active (TEST_MODE). The fix is to add `| null` to `inactiveReason` in core-typings and rebuild Typia schemas in a separate PR. Do not flag this test failure as a bug introduced by the users.create OpenAPI migration (PR `#39647`). Do not suggest inlining a custom schema to work around it, as migration rules require using `$ref` when a Typia schema exists.
Learnt from: smirk-dev
Repo: RocketChat/Rocket.Chat PR: 39625
File: apps/meteor/app/api/server/v1/push.ts:85-97
Timestamp: 2026-03-14T14:58:58.834Z
Learning: In RocketChat/Rocket.Chat, the `push.token` POST/DELETE endpoints in `apps/meteor/app/api/server/v1/push.ts` were already migrated to the chained router API pattern on `develop` prior to PR `#39625`. `cleanTokenResult` (which strips `authToken` and returns `PushTokenResult`) and `isPushTokenPOSTProps`/`isPushTokenDELETEProps` validators already exist on `develop`. PR `#39625` only migrates `push.get` and `push.info` to the chained pattern. Do not flag `cleanTokenResult` or `PushTokenResult` as newly introduced behavior-breaking changes when reviewing this PR.
Learnt from: rodrigok
Repo: RocketChat/Rocket.Chat PR: 38623
File: apps/meteor/app/lib/server/functions/cleanRoomHistory.ts:146-149
Timestamp: 2026-04-18T12:32:50.305Z
Learning: In `apps/meteor/app/lib/server/functions/cleanRoomHistory.ts` (PR `#38623`), the read-receipt cleanup (both `ReadReceipts.removeByMessageIds` and `ReadReceiptsArchive.removeByMessageIds`) is intentionally only performed in the limited prune path (`limit && selectedMessageIds`). The unlimited/delete-all path (`limit === 0`) deliberately skips cleaning up orphaned read receipts in both hot and cold storage — this is by design. Do not flag this as a bug or missing cleanup in future reviews.
Learnt from: ricardogarim
Repo: RocketChat/Rocket.Chat PR: 39535
File: apps/meteor/app/apps/server/bridges/livechat.ts:249-249
Timestamp: 2026-03-11T16:46:55.955Z
Learning: In `apps/meteor/app/apps/server/bridges/livechat.ts`, `createVisitor()` intentionally does not propagate `externalIds` to `registerData`. This is by design: the method is deprecated (JSDoc: `deprecated Use createAndReturnVisitor instead. Note: This method does not support externalIds.`) to push callers toward `createAndReturnVisitor()`, which does support `externalIds`. Do not flag the missing `externalIds` field in `createVisitor()` as a bug.
Learnt from: ahmed-n-abdeltwab
Repo: RocketChat/Rocket.Chat PR: 38974
File: apps/meteor/app/api/server/v1/im.ts:220-221
Timestamp: 2026-02-24T19:09:09.561Z
Learning: In RocketChat/Rocket.Chat OpenAPI migration PRs for apps/meteor/app/api/server/v1 endpoints, maintainers prefer to avoid any logic changes; style-only cleanups (like removing inline comments) may be deferred to follow-ups to keep scope tight.
📚 Learning: 2026-03-31T23:29:12.037Z
Learnt from: nazabucciarelli
Repo: RocketChat/Rocket.Chat PR: 39905
File: packages/models/src/models/LivechatVisitors.ts:391-406
Timestamp: 2026-03-31T23:29:12.037Z
Learning: In `packages/models/src/models/LivechatVisitors.ts`, `saveGuestEmailPhoneById` uses a two-step approach: (1) a `{ visitorEmails: null }` / `{ phone: null }` guard to lazily initialize legacy null fields to empty arrays (concurrency-safe: subsequent concurrent calls simply skip the no-match), then (2) `$addToSet` with `$each` to append without reordering. This is intentional and correct. Do not flag concurrency or ordering concerns: `visitorEmails[0]` is the primary email set during visitor registration and is never displaced by `$addToSet`; the `leadCapture` hook is a data-scraping bucket where entry order is irrelevant to business logic.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-03-11T16:46:55.955Z
Learnt from: ricardogarim
Repo: RocketChat/Rocket.Chat PR: 39535
File: apps/meteor/app/apps/server/bridges/livechat.ts:249-249
Timestamp: 2026-03-11T16:46:55.955Z
Learning: In `apps/meteor/app/apps/server/bridges/livechat.ts`, `createVisitor()` intentionally does not propagate `externalIds` to `registerData`. This is by design: the method is deprecated (JSDoc: `deprecated Use createAndReturnVisitor instead. Note: This method does not support externalIds.`) to push callers toward `createAndReturnVisitor()`, which does support `externalIds`. Do not flag the missing `externalIds` field in `createVisitor()` as a bug.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-03-16T11:57:17.987Z
Learnt from: ricardogarim
Repo: RocketChat/Rocket.Chat PR: 39657
File: apps/meteor/tests/end-to-end/apps/app-resolve-visitor.ts:43-45
Timestamp: 2026-03-16T11:57:17.987Z
Learning: In `apps/meteor/tests/end-to-end/apps/app-resolve-visitor.ts`, `externalIds` lookups are namespaced per `app.id`. Because `cleanupApps()` is called before each test run and a fresh app is installed (giving a new unique `app.id`), fixed `userId` strings like `'nonexistent-id'` in null-path tests are safe and cannot collide with stale visitor records from previous runs. Do not flag these as needing unique/random values.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-03-20T13:51:23.302Z
Learnt from: ggazzo
Repo: RocketChat/Rocket.Chat PR: 39553
File: apps/meteor/app/integrations/server/methods/incoming/updateIncomingIntegration.ts:179-181
Timestamp: 2026-03-20T13:51:23.302Z
Learning: In `apps/meteor/app/integrations/server/methods/incoming/updateIncomingIntegration.ts`, the truthiness guards `...(integration.avatar && { avatar })`, `...(integration.emoji && { emoji })`, `...(integration.alias && { alias })`, and `...(integration.script && { script })` in the `$set` payload of `updateIncomingIntegration` are intentional. Empty-string values for these fields should NOT overwrite the stored value — only truthy values are persisted. Do not flag these as bugs preventing explicit clears.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-03-15T14:31:23.493Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39647
File: apps/meteor/app/api/server/v1/users.ts:891-899
Timestamp: 2026-03-15T14:31:23.493Z
Learning: In RocketChat/Rocket.Chat, `IUser.inactiveReason` in `packages/core-typings/src/IUser.ts` is typed as `'deactivated' | 'pending_approval' | 'idle_too_long'` (optional, no `null`), but the database stores `null` for newly created users. The Typia-generated `$ref: '#/components/schemas/IUser'` schema therefore correctly rejects `null` for `inactiveReason`. This causes the test "should create a new user with default roles" to fail when response validation is active (TEST_MODE). The fix is to add `| null` to `inactiveReason` in core-typings and rebuild Typia schemas in a separate PR. Do not flag this test failure as a bug introduced by the users.create OpenAPI migration (PR `#39647`). Do not suggest inlining a custom schema to work around it, as migration rules require using `$ref` when a Typia schema exists.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-01-17T01:51:47.764Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 38219
File: packages/core-typings/src/cloud/Announcement.ts:5-6
Timestamp: 2026-01-17T01:51:47.764Z
Learning: In packages/core-typings/src/cloud/Announcement.ts, the AnnouncementSchema.createdBy field intentionally overrides IBannerSchema.createdBy (object with _id and optional username) with a string enum ['cloud', 'system'] to match existing runtime behavior. This is documented as technical debt with a FIXME comment at apps/meteor/app/cloud/server/functions/syncWorkspace/handleCommsSync.ts:53 and should not be flagged as an error until the runtime behavior is corrected.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-04-18T12:32:50.305Z
Learnt from: rodrigok
Repo: RocketChat/Rocket.Chat PR: 38623
File: apps/meteor/app/lib/server/functions/cleanRoomHistory.ts:146-149
Timestamp: 2026-04-18T12:32:50.305Z
Learning: In `apps/meteor/app/lib/server/functions/cleanRoomHistory.ts` (PR `#38623`), the read-receipt cleanup (both `ReadReceipts.removeByMessageIds` and `ReadReceiptsArchive.removeByMessageIds`) is intentionally only performed in the limited prune path (`limit && selectedMessageIds`). The unlimited/delete-all path (`limit === 0`) deliberately skips cleaning up orphaned read receipts in both hot and cold storage — this is by design. Do not flag this as a bug or missing cleanup in future reviews.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/server/startup/migrations/v336.ts

Comment on lines +1 to +27
import { addMigration } from '../../lib/migrations';

addMigration({
version: 336,
name: 'Fix null visitorEmails and phone in livechat_visitor',
async up() {
const col = this.db.collection('livechat_visitor');

await col.updateMany(
{
$or: [
{ visitorEmails: null },
{ visitorEmails: { $exists: false } }
]
},
{ $set: { visitorEmails: [] } }
);

await col.updateMany(
{
$or: [
{ phone: null },
{ phone: { $exists: false } }
]
},
{ $set: { phone: [] } }
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Use the model API instead of this.db.

IMigration does not expose db, so Line 7 will fail before either updateMany runs. Follow the existing migration pattern and call the LivechatVisitors model directly.

🐛 Proposed fix
+import { LivechatVisitors } from '@rocket.chat/models';
+
 import { addMigration } from '../../lib/migrations';
 
 addMigration({
 	version: 336,
 	name: 'Fix null visitorEmails and phone in livechat_visitor',
 	async up() {
-		const col = this.db.collection('livechat_visitor');
-
-		await col.updateMany(
+		await LivechatVisitors.updateMany(
 			{
 				$or: [
 					{ visitorEmails: null },
 					{ visitorEmails: { $exists: false } }
 				]
 			},
 			{ $set: { visitorEmails: [] } }
 		);
 
-		await col.updateMany(
+		await LivechatVisitors.updateMany(
 			{
 				$or: [
 					{ phone: null },
 					{ phone: { $exists: false } }
 				]
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/meteor/server/startup/migrations/v336.ts` around lines 1 - 27, The
migration currently uses this.db.collection('livechat_visitor') which fails
because IMigration does not expose db; instead, import and use the
LivechatVisitors model (e.g., call LivechatVisitors.updateMany) inside the up()
function; replace the col = this.db.collection('livechat_visitor') usage and run
the same two updateMany operations against LivechatVisitors with the same filter
and $set payloads so the visitorEmails and phone fields are fixed via the model
API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lead Capture features: Either create a migration script fixing records or deprecate it

1 participant