Skip to content

Commit

Permalink
chore: store contact emails and phones on the same format as user's a…
Browse files Browse the repository at this point in the history
…nd visitor's (#33484)

Co-authored-by: Rafael Tapia <[email protected]>
  • Loading branch information
pierre-lehnen-rc and tapiarafael authored Oct 15, 2024
1 parent cb49d7a commit 956bf61
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
12 changes: 6 additions & 6 deletions apps/meteor/app/livechat/server/lib/Contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ export async function createContactFromVisitor(visitor: ILivechatVisitor): Promi

const contactData: InsertionModel<ILivechatContact> = {
name: visitor.name || visitor.username,
emails: visitor.visitorEmails?.map(({ address }) => address),
phones: visitor.phone?.map(({ phoneNumber }) => phoneNumber),
emails: visitor.visitorEmails,
phones: visitor.phone || undefined,
unknown: true,
channels: [],
customFields: visitor.livechatData,
Expand Down Expand Up @@ -233,8 +233,8 @@ export async function createContact(params: CreateContactParams): Promise<string

const { insertedId } = await LivechatContacts.insertOne({
name,
emails,
phones,
emails: emails?.map((address) => ({ address })),
phones: phones?.map((phoneNumber) => ({ phoneNumber })),
contactManager,
channels,
customFields,
Expand Down Expand Up @@ -262,8 +262,8 @@ export async function updateContact(params: UpdateContactParams): Promise<ILivec

const updatedContact = await LivechatContacts.updateContact(contactId, {
name,
emails,
phones,
emails: emails?.map((address) => ({ address })),
phones: phones?.map((phoneNumber) => ({ phoneNumber })),
contactManager,
channels,
customFields,
Expand Down
8 changes: 4 additions & 4 deletions apps/meteor/server/models/raw/LivechatContacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
collation: { locale: 'en', strength: 2, caseLevel: false },
},
{
key: { emails: 1 },
key: { 'emails.address': 1 },
unique: false,
name: 'emails_insensitive',
partialFilterExpression: { emails: { $exists: true } },
collation: { locale: 'en', strength: 2, caseLevel: false },
},
{
key: { phones: 1 },
key: { 'phones.phoneNumber': 1 },
partialFilterExpression: { phones: { $exists: true } },
unique: false,
},
Expand All @@ -47,8 +47,8 @@ export class LivechatContactsRaw extends BaseRaw<ILivechatContact> implements IL
const match: Filter<ILivechatContact & RootFilterOperators<ILivechatContact>> = {
$or: [
{ name: { $regex: searchRegex, $options: 'i' } },
{ emails: { $regex: searchRegex, $options: 'i' } },
{ phones: { $regex: searchRegex, $options: 'i' } },
{ 'emails.address': { $regex: searchRegex, $options: 'i' } },
{ 'phones.phoneNumber': { $regex: searchRegex, $options: 'i' } },
],
};

Expand Down
26 changes: 19 additions & 7 deletions apps/meteor/tests/end-to-end/api/livechat/contacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,16 @@ describe('LIVECHAT - contacts', () => {
expect(res.body).to.have.property('success', true);
expect(res.body.contact._id).to.be.equal(contactId);
expect(res.body.contact.name).to.be.equal(name);
expect(res.body.contact.emails).to.be.deep.equal(emails);
expect(res.body.contact.phones).to.be.deep.equal(phones);
expect(res.body.contact.emails).to.be.deep.equal([
{
address: emails[0],
},
]);
expect(res.body.contact.phones).to.be.deep.equal([
{
phoneNumber: phones[0],
},
]);
});

it('should set the unknown field to false when updating a contact', async () => {
Expand Down Expand Up @@ -607,8 +615,12 @@ describe('LIVECHAT - contacts', () => {
expect(res.body.contact).to.have.property('createdAt');
expect(res.body.contact._id).to.be.equal(contactId);
expect(res.body.contact.name).to.be.equal(contact.name);
expect(res.body.contact.emails).to.be.deep.equal(contact.emails);
expect(res.body.contact.phones).to.be.deep.equal(contact.phones);
expect(res.body.contact.emails).to.be.deep.equal([
{
address: contact.emails[0],
},
]);
expect(res.body.contact.phones).to.be.deep.equal([{ phoneNumber: contact.phones[0] }]);
expect(res.body.contact.contactManager).to.be.equal(contact.contactManager);
});

Expand Down Expand Up @@ -715,7 +727,7 @@ describe('LIVECHAT - contacts', () => {
expect(res.body.total).to.be.equal(1);
expect(res.body.contacts[0]._id).to.be.equal(contactId);
expect(res.body.contacts[0].name).to.be.equal(contact.name);
expect(res.body.contacts[0].emails[0]).to.be.equal(contact.emails[0]);
expect(res.body.contacts[0].emails[0].address).to.be.equal(contact.emails[0]);
});

it('should return only contacts that match the searchText using phone number', async () => {
Expand All @@ -727,7 +739,7 @@ describe('LIVECHAT - contacts', () => {
expect(res.body.total).to.be.equal(1);
expect(res.body.contacts[0]._id).to.be.equal(contactId);
expect(res.body.contacts[0].name).to.be.equal(contact.name);
expect(res.body.contacts[0].emails[0]).to.be.equal(contact.emails[0]);
expect(res.body.contacts[0].phones[0].phoneNumber).to.be.equal(contact.phones[0]);
});

it('should return only contacts that match the searchText using name', async () => {
Expand All @@ -739,7 +751,7 @@ describe('LIVECHAT - contacts', () => {
expect(res.body.total).to.be.equal(1);
expect(res.body.contacts[0]._id).to.be.equal(contactId);
expect(res.body.contacts[0].name).to.be.equal(contact.name);
expect(res.body.contacts[0].emails[0]).to.be.equal(contact.emails[0]);
expect(res.body.contacts[0].emails[0].address).to.be.equal(contact.emails[0]);
});

it('should return an empty list if no contacts exist', async () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/core-typings/src/ILivechatContact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IVisitorEmail, IVisitorPhone } from './ILivechatVisitor';
import type { IRocketChatRecord } from './IRocketChatRecord';
import type { IOmnichannelSource } from './IRoom';

Expand All @@ -20,8 +21,8 @@ export interface ILivechatContactConflictingField {

export interface ILivechatContact extends IRocketChatRecord {
name: string;
phones?: string[];
emails?: string[];
phones?: IVisitorPhone[];
emails?: IVisitorEmail[];
contactManager?: string;
unknown?: boolean;
hasConflict?: boolean;
Expand Down

0 comments on commit 956bf61

Please sign in to comment.