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 2s
110 lines
4.2 KiB
TypeScript
110 lines
4.2 KiB
TypeScript
import type { ILivechatContact, ILivechatContactChannel } from '@rocket.chat/core-typings';
|
|
import { LivechatContacts, LivechatInquiry, LivechatRooms, Settings, Subscriptions } from '@rocket.chat/models';
|
|
|
|
import { getAllowedCustomFields } from './getAllowedCustomFields';
|
|
import { patchContact } from './patchContact';
|
|
import { validateContactManager } from './validateContactManager';
|
|
import { validateCustomFields } from './validateCustomFields';
|
|
import {
|
|
notifyOnSubscriptionChangedByVisitorIds,
|
|
notifyOnRoomChangedByContactId,
|
|
notifyOnLivechatInquiryChangedByVisitorIds,
|
|
notifyOnSettingChanged,
|
|
} from '../../notifyListener';
|
|
|
|
export type UpdateContactParams = {
|
|
contactId: string;
|
|
name?: string;
|
|
emails?: string[];
|
|
phones?: string[];
|
|
customFields?: Record<string, unknown>;
|
|
contactManager?: string;
|
|
channels?: ILivechatContactChannel[];
|
|
wipeConflicts?: boolean;
|
|
};
|
|
|
|
export async function updateContact(params: UpdateContactParams): Promise<ILivechatContact> {
|
|
const { contactId, name, emails, phones, customFields: receivedCustomFields, contactManager, channels, wipeConflicts } = params;
|
|
|
|
const contact = await LivechatContacts.findOneEnabledById<Pick<ILivechatContact, '_id' | 'name' | 'customFields' | 'conflictingFields'>>(
|
|
contactId,
|
|
{
|
|
projection: { _id: 1, name: 1, customFields: 1, conflictingFields: 1 },
|
|
},
|
|
);
|
|
|
|
if (!contact) {
|
|
throw new Error('error-contact-not-found');
|
|
}
|
|
|
|
if (contactManager) {
|
|
await validateContactManager(contactManager);
|
|
}
|
|
|
|
if (wipeConflicts && contact.conflictingFields?.length) {
|
|
const value = await Settings.incrementValueById('Resolved_Conflicts_Count', contact.conflictingFields.length, {
|
|
returnDocument: 'after',
|
|
});
|
|
if (value) {
|
|
void notifyOnSettingChanged(value);
|
|
}
|
|
}
|
|
|
|
const workspaceAllowedCustomFields = await getAllowedCustomFields();
|
|
const workspaceAllowedCustomFieldsIds = workspaceAllowedCustomFields.map((customField) => customField._id);
|
|
const currentCustomFieldsIds = Object.keys(contact.customFields || {});
|
|
const notRegisteredCustomFields = currentCustomFieldsIds
|
|
.filter((customFieldId) => !workspaceAllowedCustomFieldsIds.includes(customFieldId))
|
|
.map((customFieldId) => ({ _id: customFieldId }));
|
|
|
|
const customFieldsToUpdate =
|
|
receivedCustomFields &&
|
|
validateCustomFields(workspaceAllowedCustomFields, receivedCustomFields, {
|
|
ignoreAdditionalFields: !!notRegisteredCustomFields.length,
|
|
});
|
|
|
|
if (receivedCustomFields && customFieldsToUpdate && notRegisteredCustomFields.length) {
|
|
const allowedCustomFields = [...workspaceAllowedCustomFields, ...notRegisteredCustomFields];
|
|
validateCustomFields(allowedCustomFields, receivedCustomFields);
|
|
|
|
notRegisteredCustomFields.forEach((notRegisteredCustomField) => {
|
|
customFieldsToUpdate[notRegisteredCustomField._id] = contact.customFields?.[notRegisteredCustomField._id] as string;
|
|
});
|
|
}
|
|
|
|
const set = {
|
|
...(name !== undefined && { name }),
|
|
...(emails && { emails: emails?.map((address) => ({ address })) }),
|
|
...(phones && { phones: phones?.map((phoneNumber) => ({ phoneNumber })) }),
|
|
...(contactManager && { contactManager }),
|
|
...(channels && { channels }),
|
|
...(customFieldsToUpdate && { customFields: customFieldsToUpdate }),
|
|
...(wipeConflicts && { conflictingFields: [] }),
|
|
};
|
|
const unset: Partial<Record<keyof ILivechatContact, '' | 1>> =
|
|
'contactManager' in params && !contactManager ? { contactManager: '' } : {};
|
|
|
|
const updatedContact = await patchContact(contactId, { set, unset });
|
|
|
|
if (!updatedContact) {
|
|
throw new Error('error-contact-not-found');
|
|
}
|
|
|
|
// If the contact name changed, update the name of its existing rooms and subscriptions
|
|
if (name !== undefined && name !== contact.name) {
|
|
await LivechatRooms.updateContactDataByContactId(contactId, { name });
|
|
void notifyOnRoomChangedByContactId(contactId);
|
|
|
|
const visitorIds = updatedContact.channels?.map((channel) => channel.visitor.visitorId);
|
|
if (visitorIds?.length) {
|
|
await Subscriptions.updateNameAndFnameByVisitorIds(visitorIds, name);
|
|
void notifyOnSubscriptionChangedByVisitorIds(visitorIds);
|
|
|
|
await LivechatInquiry.updateNameByVisitorIds(visitorIds, name);
|
|
void notifyOnLivechatInquiryChangedByVisitorIds(visitorIds, 'updated', { name });
|
|
}
|
|
}
|
|
|
|
return updatedContact;
|
|
}
|