Files
upstream-rocketchat/apps/meteor/ee/server/patches/verifyContactChannel.ts
upstream-mirror 30810eecd7
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
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

107 lines
3.9 KiB
TypeScript

import { LivechatInquiryStatus } from '@rocket.chat/core-typings';
import type { ILivechatContact, IOmnichannelRoom } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { LivechatContacts, LivechatInquiry, LivechatRooms } from '@rocket.chat/models';
import { client, shouldRetryTransaction } from '../../../server/database/utils';
import { QueueManager } from '../../../server/lib/omnichannel/QueueManager';
import { mergeContacts } from '../../../server/lib/omnichannel/contacts/mergeContacts';
import { verifyContactChannel } from '../../../server/lib/omnichannel/contacts/verifyContactChannel';
import { contactLogger as logger } from '../lib/omnichannel/logger';
type VerifyContactChannelParams = {
contactId: string;
field: string;
value: string;
visitorId: string;
roomId: string;
};
async function _verifyContactChannel(
params: VerifyContactChannelParams,
room: Pick<IOmnichannelRoom, '_id' | 'source'>,
attempts = 2,
): Promise<ILivechatContact | null> {
const { contactId, field, value, visitorId, roomId } = params;
const session = client.startSession();
try {
session.startTransaction();
logger.debug({ msg: 'Start verifying contact channel', contactId, visitorId, roomId });
const updater = LivechatContacts.getUpdater();
LivechatContacts.setVerifiedUpdateQuery(true, updater);
LivechatContacts.setFieldAndValueUpdateQuery(field, value.toLowerCase(), updater);
await LivechatContacts.updateFromUpdaterByAssociation({ visitorId, source: room.source }, updater, { session });
await LivechatRooms.update({ _id: roomId }, { $set: { verified: true } }, { session });
logger.debug({ msg: 'Merging contacts', contactId, visitorId, roomId });
const mergeContactsResult = await mergeContacts(contactId, { visitorId, source: room.source }, session);
await session.commitTransaction();
return mergeContactsResult;
} catch (e) {
await session.abortTransaction();
if (shouldRetryTransaction(e) && attempts > 0) {
logger.debug({ msg: 'Retrying to verify contact channel', contactId, visitorId, roomId });
return _verifyContactChannel(params, room, attempts - 1);
}
logger.error({ msg: 'Error verifying contact channel', contactId, visitorId, roomId, error: e });
throw new Error('error-verifying-contact-channel');
} finally {
await session.endSession();
}
}
export const runVerifyContactChannel = async (
_next: any,
params: {
contactId: string;
field: string;
value: string;
visitorId: string;
roomId: string;
},
): Promise<ILivechatContact | null> => {
const { roomId, contactId, visitorId } = params;
const room = await LivechatRooms.findOneById(roomId);
if (!room) {
throw new Error('error-invalid-room');
}
const result = await _verifyContactChannel(params, room);
logger.debug({ msg: 'Finding inquiry', roomId });
// Note: we are not using the session here since allowing the transactional flow to be used inside the
// saveQueueInquiry function would require a lot of changes across the codebase, so if we fail here we
// will not be able to rollback the transaction. That is not a big deal since the contact will be properly
// merged and the inquiry will be saved in the queue (will need to be taken manually by an agent though).
const inquiry = await LivechatInquiry.findOneByRoomId(roomId);
if (!inquiry) {
// Note: if this happens, something is really wrong with the queue, so we should throw an error to avoid
// carrying on a weird state.
throw new Error('error-invalid-inquiry');
}
if (inquiry.status === LivechatInquiryStatus.VERIFYING) {
logger.debug({ msg: 'Verifying inquiry', roomId });
await QueueManager.verifyInquiry(inquiry, room);
}
logger.debug({
msg: 'Contact channel has been verified and merged successfully',
contactId,
visitorId,
roomId,
});
return result;
};
verifyContactChannel.patch(runVerifyContactChannel, () => License.hasModule('contact-id-verification'));