From 92ec5827b30b2b71751394ec963222e9b1870ec6 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Mon, 18 Nov 2024 19:06:14 -0300 Subject: [PATCH 1/4] apply changes requested --- apps/meteor/server/models/raw/LivechatContacts.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/apps/meteor/server/models/raw/LivechatContacts.ts b/apps/meteor/server/models/raw/LivechatContacts.ts index 09ed2cc0dd20..90c5ff69214a 100644 --- a/apps/meteor/server/models/raw/LivechatContacts.ts +++ b/apps/meteor/server/models/raw/LivechatContacts.ts @@ -58,12 +58,6 @@ export class LivechatContactsRaw extends BaseRaw implements IL }, unique: false, }, - { - key: { - channels: 1, - }, - unique: false, - }, ]; } From 153f6e1cd93c35a01078ac6d7db9ebbff332d237 Mon Sep 17 00:00:00 2001 From: matheusbsilva137 Date: Mon, 18 Nov 2024 19:39:36 -0300 Subject: [PATCH 2/4] make channels field required in contacts --- apps/meteor/server/models/raw/LivechatContacts.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/meteor/server/models/raw/LivechatContacts.ts b/apps/meteor/server/models/raw/LivechatContacts.ts index 90c5ff69214a..09ed2cc0dd20 100644 --- a/apps/meteor/server/models/raw/LivechatContacts.ts +++ b/apps/meteor/server/models/raw/LivechatContacts.ts @@ -58,6 +58,12 @@ export class LivechatContactsRaw extends BaseRaw implements IL }, unique: false, }, + { + key: { + channels: 1, + }, + unique: false, + }, ]; } From 73ccd1a5d3b30548ff1cd759c3ee7f323c9a0678 Mon Sep 17 00:00:00 2001 From: Pierre Date: Tue, 19 Nov 2024 10:21:44 -0300 Subject: [PATCH 3/4] chore: improve indexes of the livechat contacts collection --- .../server/lib/contacts/ContactMerger.ts | 4 ++++ .../server/models/raw/LivechatContacts.ts | 21 +++++++++++++++---- packages/core-typings/src/ILivechatContact.ts | 3 +++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/apps/meteor/app/livechat/server/lib/contacts/ContactMerger.ts b/apps/meteor/app/livechat/server/lib/contacts/ContactMerger.ts index 33b61e32fb89..a4bd703f1473 100644 --- a/apps/meteor/app/livechat/server/lib/contacts/ContactMerger.ts +++ b/apps/meteor/app/livechat/server/lib/contacts/ContactMerger.ts @@ -295,6 +295,10 @@ export class ContactMerger { ...(allConflicts.length ? { conflictingFields: { $each: allConflicts } } : {}), }; + if (newChannels.length) { + dataToSet.preRegistration = false; + } + const updateData: UpdateFilter = { ...(Object.keys(dataToSet).length ? { $set: dataToSet } : {}), ...(Object.keys(dataToAdd).length ? { $addToSet: dataToAdd } : {}), diff --git a/apps/meteor/server/models/raw/LivechatContacts.ts b/apps/meteor/server/models/raw/LivechatContacts.ts index 09ed2cc0dd20..b8f44f442605 100644 --- a/apps/meteor/server/models/raw/LivechatContacts.ts +++ b/apps/meteor/server/models/raw/LivechatContacts.ts @@ -56,12 +56,24 @@ export class LivechatContactsRaw extends BaseRaw implements IL 'channels.visitor.source.type': 1, 'channels.visitor.source.id': 1, }, + name: 'visitorAssociation', unique: false, }, { key: { - channels: 1, + 'channels.field': 1, + 'channels.value': 1, + 'channels.verified': 1, }, + partialFilterExpression: { 'channels.verified': true }, + name: 'verificationKey', + unique: false, + }, + { + key: { + preRegistration: 1, + }, + name: 'preRegistration', unique: false, }, ]; @@ -73,6 +85,7 @@ export class LivechatContactsRaw extends BaseRaw implements IL const result = await this.insertOne({ createdAt: new Date(), ...data, + preRegistration: !data.channels.length, }); return result.insertedId; @@ -81,7 +94,7 @@ export class LivechatContactsRaw extends BaseRaw implements IL async updateContact(contactId: string, data: Partial, options?: FindOneAndUpdateOptions): Promise { const updatedValue = await this.findOneAndUpdate( { _id: contactId }, - { $set: { ...data, unknown: false } }, + { $set: { ...data, unknown: false, ...(data.channels && { preRegistration: !data.channels.length }) } }, { returnDocument: 'after', ...options }, ); return updatedValue.value as ILivechatContact; @@ -132,7 +145,7 @@ export class LivechatContactsRaw extends BaseRaw implements IL ], }, { - channels: [], + preRegistration: true, }, ], }; @@ -164,7 +177,7 @@ export class LivechatContactsRaw extends BaseRaw implements IL } async addChannel(contactId: string, channel: ILivechatContactChannel): Promise { - await this.updateOne({ _id: contactId }, { $push: { channels: channel } }); + await this.updateOne({ _id: contactId }, { $push: { channels: channel }, $set: { preRegistration: false } }); } async updateLastChatById( diff --git a/packages/core-typings/src/ILivechatContact.ts b/packages/core-typings/src/ILivechatContact.ts index c6591ad1d9e5..7f9301d81d7b 100644 --- a/packages/core-typings/src/ILivechatContact.ts +++ b/packages/core-typings/src/ILivechatContact.ts @@ -45,4 +45,7 @@ export interface ILivechatContact extends IRocketChatRecord { ts: Date; }; importIds?: string[]; + // When preRegistration is true, the contact was added by an admin and it doesn't have any visitor association yet + // This contact may then be linked to new visitors that use the same email address or phone number + preRegistration?: boolean; } From 6471ee7b07adabcc8624985db47aa24e31b52d18 Mon Sep 17 00:00:00 2001 From: Pierre Lehnen <55164754+pierre-lehnen-rc@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:41:02 -0300 Subject: [PATCH 4/4] Update apps/meteor/server/models/raw/LivechatContacts.ts Co-authored-by: Diego Sampaio --- apps/meteor/server/models/raw/LivechatContacts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/meteor/server/models/raw/LivechatContacts.ts b/apps/meteor/server/models/raw/LivechatContacts.ts index b8f44f442605..4e80f23956e6 100644 --- a/apps/meteor/server/models/raw/LivechatContacts.ts +++ b/apps/meteor/server/models/raw/LivechatContacts.ts @@ -73,7 +73,7 @@ export class LivechatContactsRaw extends BaseRaw implements IL key: { preRegistration: 1, }, - name: 'preRegistration', + sparse: true, unique: false, }, ];