Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve indexes of the livechat contacts collection #33991

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ export class ContactMerger {
...(allConflicts.length ? { conflictingFields: { $each: allConflicts } } : {}),
};

if (newChannels.length) {
dataToSet.preRegistration = false;
}

const updateData: UpdateFilter<ILivechatContact> = {
...(Object.keys(dataToSet).length ? { $set: dataToSet } : {}),
...(Object.keys(dataToAdd).length ? { $addToSet: dataToAdd } : {}),
Expand Down
21 changes: 17 additions & 4 deletions apps/meteor/server/models/raw/LivechatContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,24 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> 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,
},
sparse: true,
unique: false,
},
];
Expand All @@ -73,6 +85,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
const result = await this.insertOne({
createdAt: new Date(),
...data,
preRegistration: !data.channels.length,
});

return result.insertedId;
Expand All @@ -81,7 +94,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
async updateContact(contactId: string, data: Partial<ILivechatContact>, options?: FindOneAndUpdateOptions): Promise<ILivechatContact> {
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;
Expand Down Expand Up @@ -132,7 +145,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
],
},
{
channels: [],
preRegistration: true,
},
],
};
Expand Down Expand Up @@ -164,7 +177,7 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
}

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

async updateLastChatById(
Expand Down
3 changes: 3 additions & 0 deletions packages/core-typings/src/ILivechatContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading