Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: New livechat conversations are not assigned to contact manager #34210

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/popular-cameras-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rocket.chat/meteor": patch
"@rocket.chat/model-typings": patch
---

Fixes livechat conversations not being assigned to the contact manager even when the "Assign new conversations to the contact manager" setting is enabled
5 changes: 3 additions & 2 deletions apps/meteor/app/livechat/server/lib/LivechatTyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,12 @@ class LivechatClass {
throw new Meteor.Error('error-omnichannel-is-disabled');
}

if (await LivechatContacts.isChannelBlocked(this.makeVisitorAssociation(visitor._id, roomInfo.source))) {
const visitorAssociation = this.makeVisitorAssociation(visitor._id, roomInfo.source);
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
if (await LivechatContacts.isChannelBlocked(visitorAssociation)) {
throw new Error('error-contact-channel-blocked');
}

const defaultAgent = await callbacks.run('livechat.checkDefaultAgentOnNewRoom', agent, visitor);
const defaultAgent = await callbacks.run('livechat.checkDefaultAgentOnNewRoom', agent, visitorAssociation);
// if no department selected verify if there is at least one active and pick the first
if (!defaultAgent && !visitor.department) {
const department = await getRequiredDepartment();
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/methods/takeInquiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const takeInquiry = async (
});
}

const user = await Users.findOneOnlineAgentById(userId, settings.get<boolean>('Livechat_enabled_when_agent_idle'));
const user = await Users.findOneOnlineAgentById(userId, {}, settings.get<boolean>('Livechat_enabled_when_agent_idle'));
if (!user) {
throw new Meteor.Error('error-agent-status-service-offline', 'Agent status is offline or Omnichannel service is not active', {
method: 'livechat:takeInquiry',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { IUser, SelectedAgent } from '@rocket.chat/core-typings';
import { LivechatVisitors, LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models';
import { LivechatVisitors, LivechatContacts, LivechatInquiry, LivechatRooms, Users } from '@rocket.chat/models';

import { notifyOnLivechatInquiryChanged } from '../../../../../app/lib/server/lib/notifyListener';
import { RoutingManager } from '../../../../../app/livechat/server/lib/RoutingManager';
Expand All @@ -18,12 +18,15 @@ const normalizeDefaultAgent = (agent?: Pick<IUser, '_id' | 'username'> | null):
return { agentId, username };
};

const getDefaultAgent = async (username?: string): Promise<SelectedAgent | null> => {
if (!username) {
const getDefaultAgent = async ({ username, id }: { username?: string; id?: string }): Promise<SelectedAgent | null> => {
if (!username && !id) {
return null;
}

return normalizeDefaultAgent(await Users.findOneOnlineAgentByUserList(username, { projection: { _id: 1, username: 1 } }));
if (id) {
return normalizeDefaultAgent(await Users.findOneOnlineAgentById(id, { projection: { _id: 1, username: 1 } }));
KevLehman marked this conversation as resolved.
Show resolved Hide resolved
}
return normalizeDefaultAgent(await Users.findOneOnlineAgentByUserList(username || [], { projection: { _id: 1, username: 1 } }));
};

settings.watch<boolean>('Livechat_last_chatted_agent_routing', (value) => {
Expand Down Expand Up @@ -88,21 +91,28 @@ settings.watch<boolean>('Omnichannel_contact_manager_routing', (value) => {

callbacks.add(
'livechat.checkDefaultAgentOnNewRoom',
async (defaultAgent, defaultGuest) => {
if (defaultAgent || !defaultGuest) {
async (defaultAgent, visitor) => {
if (defaultAgent || !visitor) {
return defaultAgent;
}

const { _id: guestId } = defaultGuest;
const { visitorId: guestId } = visitor;
const guest = await LivechatVisitors.findOneEnabledById(guestId, {
projection: { lastAgent: 1, token: 1, contactManager: 1 },
});
if (!guest) {
return defaultAgent;
}

const contact = await LivechatContacts.findOneByVisitor(visitor, { projection: { contactManager: 1 } });

const { lastAgent, token, contactManager } = guest;
const guestManager = contactManager?.username && contactManagerPreferred && getDefaultAgent(contactManager?.username);
const contactManagerUsernameOrId = contact?.contactManager || contactManager?.username;
const guestManager =
contactManagerUsernameOrId &&
contactManagerPreferred &&
(await getDefaultAgent({ username: contactManager?.username, id: contact?.contactManager }));

if (guestManager) {
return guestManager;
}
Expand All @@ -111,7 +121,7 @@ callbacks.add(
return defaultAgent;
}

const guestAgent = lastAgent?.username && getDefaultAgent(lastAgent?.username);
const guestAgent = lastAgent?.username && getDefaultAgent({ username: lastAgent?.username });
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
if (guestAgent) {
return guestAgent;
}
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
IOmnichannelRoomInfo,
IOmnichannelInquiryExtraData,
IOmnichannelRoomExtraData,
ILivechatContactVisitorAssociation,
} from '@rocket.chat/core-typings';
import type { Updater } from '@rocket.chat/models';
import type { FilterOperators } from 'mongodb';
Expand Down Expand Up @@ -118,7 +119,7 @@ type ChainedCallbackSignatures = {
) => Promise<T>;

'livechat.beforeRouteChat': (inquiry: ILivechatInquiryRecord, agent?: { agentId: string; username: string }) => ILivechatInquiryRecord;
'livechat.checkDefaultAgentOnNewRoom': (agent: SelectedAgent, visitor?: ILivechatVisitor) => SelectedAgent | null;
'livechat.checkDefaultAgentOnNewRoom': (agent: SelectedAgent, visitor?: ILivechatContactVisitorAssociation) => SelectedAgent | null;
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved

'livechat.onLoadForwardDepartmentRestrictions': (params: { departmentId: string }) => Record<string, unknown>;

Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/server/models/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -1657,11 +1657,11 @@ export class UsersRaw extends BaseRaw {
return this.findOne(query);
}

findOneOnlineAgentById(_id, isLivechatEnabledWhenAgentIdle) {
findOneOnlineAgentById(_id, options, isLivechatEnabledWhenAgentIdle) {
matheusbsilva137 marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Create class Agent
const query = queryStatusAgentOnline({ _id }, isLivechatEnabledWhenAgentIdle);

return this.findOne(query);
return this.findOne(query, options);
}

findAgents() {
Expand Down
6 changes: 5 additions & 1 deletion packages/model-typings/src/models/IUsersModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ export interface IUsersModel extends IBaseModel<IUser> {
findOnlineAgents(agentId?: string): FindCursor<ILivechatAgent>;
countOnlineAgents(agentId: string): Promise<number>;
findOneBotAgent(): Promise<ILivechatAgent | null>;
findOneOnlineAgentById(agentId: string, isLivechatEnabledWhenAgentIdle?: boolean): Promise<ILivechatAgent | null>;
findOneOnlineAgentById(
agentId: string,
options?: FindOptions<IUser>,
isLivechatEnabledWhenAgentIdle?: boolean,
): Promise<ILivechatAgent | null>;
findAgents(): FindCursor<ILivechatAgent>;
countAgents(): Promise<number>;
getNextAgent(ignoreAgentId?: string, extraQuery?: Filter<IUser>): Promise<{ agentId: string; username: string } | null>;
Expand Down
Loading