Skip to content

Commit

Permalink
chore: improve indexes of the livechat contacts collection
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-lehnen-rc committed Nov 19, 2024
1 parent e0ac2c9 commit 4da013a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 4 additions & 0 deletions apps/meteor/app/livechat/server/lib/contacts/ContactMerger.ts
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,
},
name: 'preRegistration',
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;
}

0 comments on commit 4da013a

Please sign in to comment.