diff --git a/apps/meteor/app/livechat/server/lib/Contacts.ts b/apps/meteor/app/livechat/server/lib/Contacts.ts index 1e01dfc9d58d..dd7fb0b99848 100644 --- a/apps/meteor/app/livechat/server/lib/Contacts.ts +++ b/apps/meteor/app/livechat/server/lib/Contacts.ts @@ -199,8 +199,8 @@ export async function createContactFromVisitor(visitor: ILivechatVisitor): Promi const contactData: InsertionModel = { 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, @@ -233,8 +233,8 @@ export async function createContact(params: CreateContactParams): Promise ({ address })), + phones: phones?.map((phoneNumber) => ({ phoneNumber })), contactManager, channels, customFields, @@ -262,8 +262,8 @@ export async function updateContact(params: UpdateContactParams): Promise ({ address })), + phones: phones?.map((phoneNumber) => ({ phoneNumber })), contactManager, channels, customFields, diff --git a/apps/meteor/server/models/raw/LivechatContacts.ts b/apps/meteor/server/models/raw/LivechatContacts.ts index 3daea28e326a..5e0f9d6703ac 100644 --- a/apps/meteor/server/models/raw/LivechatContacts.ts +++ b/apps/meteor/server/models/raw/LivechatContacts.ts @@ -19,14 +19,14 @@ export class LivechatContactsRaw extends BaseRaw 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, }, @@ -47,8 +47,8 @@ export class LivechatContactsRaw extends BaseRaw implements IL const match: Filter> = { $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' } }, ], }; diff --git a/apps/meteor/tests/end-to-end/api/livechat/contacts.ts b/apps/meteor/tests/end-to-end/api/livechat/contacts.ts index 556b232e258e..4c6222ffe74e 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/contacts.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/contacts.ts @@ -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 () => { @@ -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); }); @@ -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 () => { @@ -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 () => { @@ -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 () => { diff --git a/packages/core-typings/src/ILivechatContact.ts b/packages/core-typings/src/ILivechatContact.ts index 66f0eeeed825..466eeb23d039 100644 --- a/packages/core-typings/src/ILivechatContact.ts +++ b/packages/core-typings/src/ILivechatContact.ts @@ -1,3 +1,4 @@ +import type { IVisitorEmail, IVisitorPhone } from './ILivechatVisitor'; import type { IRocketChatRecord } from './IRocketChatRecord'; import type { IOmnichannelSource } from './IRoom'; @@ -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;