Skip to content

Commit

Permalink
stub mergeContact function
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusbsilva137 committed Oct 16, 2024
1 parent 007dd55 commit 7d85c79
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
46 changes: 20 additions & 26 deletions apps/meteor/ee/server/patches/mergeContacts.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
import type { ILivechatContact, ILivechatContactChannel } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { LivechatContacts } from '@rocket.chat/models';
import type { ILivechatContact, ILivechatContactChannel } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { LivechatContacts } from '@rocket.chat/models';

import { hasLicense } from '../../../app/license/client';
import { hasLicense } from '../../../app/license/client';
import { ContactMerger } from '../../../app/livechat/server/lib/ContactMerger';
import { mergeContacts } from '../../../app/livechat/server/lib/Contacts';

export const runMergeContacts = async (
_next: any,
contactId: string,
channel: ILivechatContactChannel,
): Promise<ILivechatContact | null> => {
if (!(await hasLicense('contact-id-verification'))) {
return null;
}

const originalContact = (await LivechatContacts.findOneById(contactId)) as ILivechatContact;
await License.onLicense('contact-id-verification', () => {
mergeContacts.patch(async (_next, contactId: string, channel: ILivechatContactChannel): Promise<ILivechatContact | null> => {
if (!(await hasLicense('contact-id-verification'))) {
return null;
}

const similarContacts: ILivechatContact[] = await LivechatContacts.findSimilarVerifiedContacts(channel, contactId);
const originalContact = (await LivechatContacts.findOneById(contactId)) as ILivechatContact;

if (!similarContacts.length) {
return originalContact;
}
const similarContacts: ILivechatContact[] = await LivechatContacts.findSimilarVerifiedContacts(channel, contactId);

for await (const similarContact of similarContacts) {
await ContactMerger.mergeContact(originalContact, similarContact);
}
if (!similarContacts.length) {
return originalContact;
}

await LivechatContacts.deleteMany({ _id: { $in: similarContacts.map((c) => c._id) } });
return LivechatContacts.findOneById(contactId);
};
for await (const similarContact of similarContacts) {
await ContactMerger.mergeContact(originalContact, similarContact);
}

await License.onLicense('contact-id-verification', () => {
mergeContacts.patch(runMergeContacts);
await LivechatContacts.deleteMany({ _id: { $in: similarContacts.map((c) => c._id) } });
return LivechatContacts.findOneById(contactId);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { expect } from 'chai';
import proxyquire from 'proxyquire';
import sinon from 'sinon';

import { runMergeContacts } from '../../../../../../server/patches/mergeContacts';

const modelsMock = {
LivechatContacts: {
findOneById: sinon.stub(),
Expand Down Expand Up @@ -37,7 +35,6 @@ describe('verifyContactChannel', () => {
modelsMock.LivechatContacts.deleteMany.reset();
modelsMock.LivechatVisitors.updateMany.reset();
modelsMock.LivechatContacts.updateContact.reset();
mergeContactsStub.callsFake((contactId, channel) => runMergeContacts(() => undefined, contactId, channel));

sinon.useFakeTimers(new Date().getTime());
});
Expand Down

0 comments on commit 7d85c79

Please sign in to comment.