Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import type { ILivechatVisitor, IOmnichannelSource, ILivechatContact, IOmnichannelRoom } from '@rocket.chat/core-typings';
|
|
import { LivechatContacts, LivechatRooms } from '@rocket.chat/models';
|
|
|
|
import { livechatContactsLogger as logger } from '../logger';
|
|
import { ContactMerger } from './ContactMerger';
|
|
import { createContactFromVisitor } from './createContactFromVisitor';
|
|
|
|
/**
|
|
This function assumes you already ensured that the visitor is not yet linked to any contact
|
|
**/
|
|
export async function migrateVisitorToContactId({
|
|
visitor,
|
|
source,
|
|
requireRoom = true,
|
|
}: {
|
|
visitor: ILivechatVisitor;
|
|
source: IOmnichannelSource;
|
|
requireRoom?: boolean;
|
|
}): Promise<ILivechatContact['_id'] | null> {
|
|
if (requireRoom) {
|
|
// Do not migrate the visitor with this source if they have no rooms matching it
|
|
const anyRoom = await LivechatRooms.findNewestByContactVisitorAssociation<Pick<IOmnichannelRoom, '_id'>>(
|
|
{ visitorId: visitor._id, source },
|
|
{
|
|
projection: { _id: 1 },
|
|
},
|
|
);
|
|
|
|
if (!anyRoom) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Search for any contact that is not yet associated with any visitor and that have the same email or phone number as this visitor.
|
|
const existingContact = await LivechatContacts.findContactMatchingVisitor(visitor);
|
|
if (!existingContact) {
|
|
logger.debug({ msg: 'Creating a new contact for existing visitor', visitorId: visitor._id });
|
|
return createContactFromVisitor(visitor, source);
|
|
}
|
|
|
|
// There is already an existing contact with no linked visitors and matching this visitor's phone or email, so let's use it
|
|
logger.debug({ msg: 'Adding channel to existing contact', contactId: existingContact._id });
|
|
await ContactMerger.mergeVisitorIntoContact(visitor, existingContact, source);
|
|
|
|
// Update all existing rooms matching the visitor id and source to set the contactId to them
|
|
await LivechatRooms.setContactByVisitorAssociation(
|
|
{
|
|
visitorId: visitor._id,
|
|
source,
|
|
},
|
|
existingContact,
|
|
);
|
|
|
|
return existingContact._id;
|
|
}
|