Fix legacy null values in LivechatVisitors (visitorEmails and phone)#40250
Fix legacy null values in LivechatVisitors (visitorEmails and phone)#40250JarvisX-Z wants to merge 3 commits into
Conversation
This migration updates the 'livechat_visitor' collection to set 'visitorEmails' and 'phone' to empty arrays if they are null or do not exist.
|
Looks like this PR is not ready to merge, because of the following issues:
Please fix the issues and try again If you have any trouble, please check the PR guidelines |
|
WalkthroughAdded a new database migration (version 336) for the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested labels
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ 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. Comment |
|
This PR implements the migration-based solution suggested in #40168. The migration has been tested to ensure:
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. |
There was a problem hiding this comment.
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
📒 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
| 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: [] } } | ||
| ); |
There was a problem hiding this comment.
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.
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