-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Contacts verification and merging (#33491)
* feat: contact verification and merging methods --------- Co-authored-by: Pierre Lehnen <55164754+pierre-lehnen-rc@users.noreply.github.com> Co-authored-by: Matheus Barbosa Silva <36537004+matheusbsilva137@users.noreply.github.com>
- Loading branch information
1 parent
8e83b07
commit bdaf64f
Showing
11 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@rocket.chat/meteor": minor | ||
"@rocket.chat/core-typings": minor | ||
"@rocket.chat/model-typings": minor | ||
--- | ||
|
||
Adds new EE capability to allow merging similar verified visitor contacts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import './closeBusinessHour'; | ||
import './getInstanceList'; | ||
import './isDepartmentCreationAvailable'; | ||
import './verifyContactChannel'; | ||
import './mergeContacts'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { ILivechatContact, ILivechatContactChannel } from '@rocket.chat/core-typings'; | ||
import { License } from '@rocket.chat/license'; | ||
import { LivechatContacts } from '@rocket.chat/models'; | ||
|
||
import { ContactMerger } from '../../../app/livechat/server/lib/ContactMerger'; | ||
import { mergeContacts } from '../../../app/livechat/server/lib/Contacts'; | ||
|
||
export const runMergeContacts = async (_next: any, contactId: string, visitorId: string): Promise<ILivechatContact | null> => { | ||
const originalContact = (await LivechatContacts.findOneById(contactId)) as ILivechatContact; | ||
if (!originalContact) { | ||
throw new Error('error-invalid-contact'); | ||
} | ||
|
||
const channel = originalContact.channels?.find((channel: ILivechatContactChannel) => channel.visitorId === visitorId); | ||
if (!channel) { | ||
throw new Error('error-invalid-channel'); | ||
} | ||
const similarContacts: ILivechatContact[] = await LivechatContacts.findSimilarVerifiedContacts(channel, contactId); | ||
|
||
if (!similarContacts.length) { | ||
return originalContact; | ||
} | ||
|
||
for await (const similarContact of similarContacts) { | ||
const fields = await ContactMerger.getAllFieldsFromContact(similarContact); | ||
await ContactMerger.mergeFieldsIntoContact(fields, originalContact); | ||
} | ||
|
||
await LivechatContacts.deleteMany({ _id: { $in: similarContacts.map((c) => c._id) } }); | ||
return LivechatContacts.findOneById(contactId); | ||
}; | ||
|
||
mergeContacts.patch(runMergeContacts, () => License.hasModule('contact-id-verification')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { ILivechatContact } from '@rocket.chat/core-typings'; | ||
import { License } from '@rocket.chat/license'; | ||
import { LivechatContacts, LivechatRooms } from '@rocket.chat/models'; | ||
|
||
import { verifyContactChannel, mergeContacts } from '../../../app/livechat/server/lib/Contacts'; | ||
|
||
export const runVerifyContactChannel = async ( | ||
_next: any, | ||
params: { | ||
contactId: string; | ||
field: string; | ||
value: string; | ||
visitorId: string; | ||
roomId: string; | ||
}, | ||
): Promise<ILivechatContact | null> => { | ||
const { contactId, field, value, visitorId, roomId } = params; | ||
|
||
await LivechatContacts.updateContactChannel(contactId, visitorId, { | ||
'unknown': false, | ||
'channels.$.verified': true, | ||
'channels.$.verifiedAt': new Date(), | ||
'channels.$.field': field, | ||
'channels.$.value': value, | ||
}); | ||
|
||
await LivechatRooms.update({ _id: roomId }, { $set: { verified: true } }); | ||
|
||
return mergeContacts(contactId, visitorId); | ||
}; | ||
|
||
verifyContactChannel.patch(runVerifyContactChannel, () => License.hasModule('contact-id-verification')); |
100 changes: 100 additions & 0 deletions
100
apps/meteor/ee/tests/unit/apps/livechat-enterprise/server/lib/mergeContacts.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { expect } from 'chai'; | ||
import proxyquire from 'proxyquire'; | ||
import sinon from 'sinon'; | ||
|
||
const modelsMock = { | ||
LivechatContacts: { | ||
findOneById: sinon.stub(), | ||
findSimilarVerifiedContacts: sinon.stub(), | ||
deleteMany: sinon.stub(), | ||
}, | ||
}; | ||
|
||
const contactMergerStub = { | ||
getAllFieldsFromContact: sinon.stub(), | ||
mergeFieldsIntoContact: sinon.stub(), | ||
}; | ||
|
||
const { runMergeContacts } = proxyquire.noCallThru().load('../../../../../../server/patches/mergeContacts', { | ||
'../../../app/livechat/server/lib/Contacts': { mergeContacts: { patch: sinon.stub() } }, | ||
'../../../app/livechat/server/lib/ContactMerger': { ContactMerger: contactMergerStub }, | ||
'@rocket.chat/models': modelsMock, | ||
}); | ||
|
||
describe('mergeContacts', () => { | ||
const targetChannel = { | ||
name: 'channelName', | ||
visitorId: 'visitorId', | ||
verified: true, | ||
verifiedAt: new Date(), | ||
field: 'field', | ||
value: 'value', | ||
}; | ||
|
||
beforeEach(() => { | ||
modelsMock.LivechatContacts.findOneById.reset(); | ||
modelsMock.LivechatContacts.findSimilarVerifiedContacts.reset(); | ||
modelsMock.LivechatContacts.deleteMany.reset(); | ||
contactMergerStub.getAllFieldsFromContact.reset(); | ||
contactMergerStub.mergeFieldsIntoContact.reset(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should throw an error if contact does not exist', async () => { | ||
modelsMock.LivechatContacts.findOneById.resolves(undefined); | ||
|
||
await expect(runMergeContacts(() => undefined, 'invalidId', 'visitorId')).to.be.rejectedWith('error-invalid-contact'); | ||
}); | ||
|
||
it('should throw an error if contact channel does not exist', async () => { | ||
modelsMock.LivechatContacts.findOneById.resolves({ | ||
_id: 'contactId', | ||
channels: [{ name: 'channelName', visitorId: 'visitorId' }], | ||
}); | ||
|
||
await expect(runMergeContacts(() => undefined, 'contactId', 'invalidVisitor')).to.be.rejectedWith('error-invalid-channel'); | ||
}); | ||
|
||
it('should do nothing if there are no similar verified contacts', async () => { | ||
modelsMock.LivechatContacts.findOneById.resolves({ _id: 'contactId', channels: [targetChannel] }); | ||
modelsMock.LivechatContacts.findSimilarVerifiedContacts.resolves([]); | ||
|
||
await runMergeContacts(() => undefined, 'contactId', 'visitorId'); | ||
|
||
expect(modelsMock.LivechatContacts.findOneById.calledOnceWith('contactId')).to.be.true; | ||
expect(modelsMock.LivechatContacts.findSimilarVerifiedContacts.calledOnceWith(targetChannel, 'contactId')).to.be.true; | ||
expect(modelsMock.LivechatContacts.deleteMany.notCalled).to.be.true; | ||
expect(contactMergerStub.getAllFieldsFromContact.notCalled).to.be.true; | ||
expect(contactMergerStub.mergeFieldsIntoContact.notCalled).to.be.true; | ||
}); | ||
|
||
it('should be able to merge similar contacts', async () => { | ||
const similarContact = { | ||
_id: 'differentId', | ||
emails: ['email2'], | ||
phones: ['phone2'], | ||
channels: [{ name: 'channelName2', visitorId: 'visitorId2', field: 'field', value: 'value' }], | ||
}; | ||
const originalContact = { | ||
_id: 'contactId', | ||
emails: ['email1'], | ||
phones: ['phone1'], | ||
channels: [targetChannel], | ||
}; | ||
|
||
modelsMock.LivechatContacts.findOneById.resolves(originalContact); | ||
modelsMock.LivechatContacts.findSimilarVerifiedContacts.resolves([similarContact]); | ||
|
||
await runMergeContacts(() => undefined, 'contactId', 'visitorId'); | ||
|
||
expect(modelsMock.LivechatContacts.findOneById.calledTwice).to.be.true; | ||
expect(modelsMock.LivechatContacts.findOneById.calledWith('contactId')).to.be.true; | ||
expect(modelsMock.LivechatContacts.findSimilarVerifiedContacts.calledOnceWith(targetChannel, 'contactId')).to.be.true; | ||
expect(contactMergerStub.getAllFieldsFromContact.calledOnceWith(similarContact)).to.be.true; | ||
expect(contactMergerStub.mergeFieldsIntoContact.getCall(0).args[1]).to.be.deep.equal(originalContact); | ||
expect(modelsMock.LivechatContacts.deleteMany.calledOnceWith({ _id: { $in: ['differentId'] } })).to.be.true; | ||
}); | ||
}); |
55 changes: 55 additions & 0 deletions
55
apps/meteor/ee/tests/unit/apps/livechat-enterprise/server/lib/verifyContactChannel.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { expect } from 'chai'; | ||
import proxyquire from 'proxyquire'; | ||
import sinon from 'sinon'; | ||
|
||
const modelsMock = { | ||
LivechatContacts: { | ||
updateContactChannel: sinon.stub(), | ||
}, | ||
LivechatRooms: { | ||
update: sinon.stub(), | ||
}, | ||
}; | ||
|
||
const mergeContactsStub = sinon.stub(); | ||
|
||
const { runVerifyContactChannel } = proxyquire.noCallThru().load('../../../../../../server/patches/verifyContactChannel', { | ||
'../../../app/livechat/server/lib/Contacts': { mergeContacts: mergeContactsStub, verifyContactChannel: { patch: sinon.stub() } }, | ||
'@rocket.chat/models': modelsMock, | ||
}); | ||
|
||
describe('verifyContactChannel', () => { | ||
beforeEach(() => { | ||
modelsMock.LivechatContacts.updateContactChannel.reset(); | ||
modelsMock.LivechatRooms.update.reset(); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
|
||
it('should be able to verify a contact channel', async () => { | ||
await runVerifyContactChannel(() => undefined, { | ||
contactId: 'contactId', | ||
field: 'field', | ||
value: 'value', | ||
visitorId: 'visitorId', | ||
roomId: 'roomId', | ||
}); | ||
|
||
expect( | ||
modelsMock.LivechatContacts.updateContactChannel.calledOnceWith( | ||
'contactId', | ||
'visitorId', | ||
sinon.match({ | ||
'unknown': false, | ||
'channels.$.verified': true, | ||
'channels.$.field': 'field', | ||
'channels.$.value': 'value', | ||
}), | ||
), | ||
).to.be.true; | ||
expect(modelsMock.LivechatRooms.update.calledOnceWith({ _id: 'roomId' }, { $set: { verified: true } })).to.be.true; | ||
expect(mergeContactsStub.calledOnceWith('contactId', 'visitorId')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters