Skip to content

Commit f52d33d

Browse files
pierre-lehnen-rcmatheusbsilva137sampaiodiego
authored
chore: improve indexes of the livechat contacts collection (#33991)
Co-authored-by: matheusbsilva137 <[email protected]> Co-authored-by: Diego Sampaio <[email protected]>
1 parent 8e5c430 commit f52d33d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

apps/meteor/app/livechat/server/lib/contacts/ContactMerger.ts

+4
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ export class ContactMerger {
295295
...(allConflicts.length ? { conflictingFields: { $each: allConflicts } } : {}),
296296
};
297297

298+
if (newChannels.length) {
299+
dataToSet.preRegistration = false;
300+
}
301+
298302
const updateData: UpdateFilter<ILivechatContact> = {
299303
...(Object.keys(dataToSet).length ? { $set: dataToSet } : {}),
300304
...(Object.keys(dataToAdd).length ? { $addToSet: dataToAdd } : {}),

apps/meteor/server/models/raw/LivechatContacts.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,24 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
5656
'channels.visitor.source.type': 1,
5757
'channels.visitor.source.id': 1,
5858
},
59+
name: 'visitorAssociation',
5960
unique: false,
6061
},
6162
{
6263
key: {
63-
channels: 1,
64+
'channels.field': 1,
65+
'channels.value': 1,
66+
'channels.verified': 1,
6467
},
68+
partialFilterExpression: { 'channels.verified': true },
69+
name: 'verificationKey',
70+
unique: false,
71+
},
72+
{
73+
key: {
74+
preRegistration: 1,
75+
},
76+
sparse: true,
6577
unique: false,
6678
},
6779
];
@@ -73,6 +85,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
7385
const result = await this.insertOne({
7486
createdAt: new Date(),
7587
...data,
88+
preRegistration: !data.channels.length,
7689
});
7790

7891
return result.insertedId;
@@ -81,7 +94,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
8194
async updateContact(contactId: string, data: Partial<ILivechatContact>, options?: FindOneAndUpdateOptions): Promise<ILivechatContact> {
8295
const updatedValue = await this.findOneAndUpdate(
8396
{ _id: contactId },
84-
{ $set: { ...data, unknown: false } },
97+
{ $set: { ...data, unknown: false, ...(data.channels && { preRegistration: !data.channels.length }) } },
8598
{ returnDocument: 'after', ...options },
8699
);
87100
return updatedValue.value as ILivechatContact;
@@ -132,7 +145,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
132145
],
133146
},
134147
{
135-
channels: [],
148+
preRegistration: true,
136149
},
137150
],
138151
};
@@ -164,7 +177,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
164177
}
165178

166179
async addChannel(contactId: string, channel: ILivechatContactChannel): Promise<void> {
167-
await this.updateOne({ _id: contactId }, { $push: { channels: channel } });
180+
await this.updateOne({ _id: contactId }, { $push: { channels: channel }, $set: { preRegistration: false } });
168181
}
169182

170183
async updateLastChatById(

packages/core-typings/src/ILivechatContact.ts

+3
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ export interface ILivechatContact extends IRocketChatRecord {
4545
ts: Date;
4646
};
4747
importIds?: string[];
48+
// When preRegistration is true, the contact was added by an admin and it doesn't have any visitor association yet
49+
// This contact may then be linked to new visitors that use the same email address or phone number
50+
preRegistration?: boolean;
4851
}

0 commit comments

Comments
 (0)