Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/lazy-cheetahs-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes app event `IPostLivechatAgentAssigned` receiving a room object previous to the assignment of agent, causing `room.servedBy` to be undefined on apps
20 changes: 11 additions & 9 deletions apps/meteor/app/livechat/server/lib/RoutingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
SelectedAgent,
InquiryWithAgentInfo,
TransferData,
IUser,
} from '@rocket.chat/core-typings';
import { LivechatInquiryStatus } from '@rocket.chat/core-typings';
import { Logger } from '@rocket.chat/logger';
Expand Down Expand Up @@ -60,7 +61,7 @@ type Routing = {
delegateAgent(agent: SelectedAgent | undefined, inquiry: ILivechatInquiryRecord): Promise<SelectedAgent | null | undefined>;
removeAllRoomSubscriptions(room: Pick<IOmnichannelRoom, '_id'>, ignoreUser?: { _id: string }): Promise<void>;

assignAgent(inquiry: InquiryWithAgentInfo, room: IOmnichannelRoom, agent: SelectedAgent): Promise<InquiryWithAgentInfo>;
assignAgent(inquiry: InquiryWithAgentInfo, agent: SelectedAgent): Promise<{ inquiry: InquiryWithAgentInfo; user: IUser }>;
};

export const RoutingManager: Routing = {
Expand Down Expand Up @@ -116,7 +117,7 @@ export const RoutingManager: Routing = {
return this.takeInquiry(inquiry, agent, options, room);
},

async assignAgent(inquiry: InquiryWithAgentInfo, room: IOmnichannelRoom, agent: SelectedAgent): Promise<InquiryWithAgentInfo> {
async assignAgent(inquiry: InquiryWithAgentInfo, agent: SelectedAgent): Promise<{ inquiry: InquiryWithAgentInfo; user: IUser }> {
check(
agent,
Match.ObjectIncluding({
Expand All @@ -137,17 +138,17 @@ export const RoutingManager: Routing = {
await Rooms.incUsersCountById(rid, 1);

const user = await Users.findOneById(agent.agentId);

if (user) {
await Promise.all([Message.saveSystemMessage('command', rid, 'connected', user), Message.saveSystemMessage('uj', rid, '', user)]);
if (!user) {
throw new Error('error-user-not-found');
}

await Promise.all([Message.saveSystemMessage('command', rid, 'connected', user), Message.saveSystemMessage('uj', rid, '', user)]);

await dispatchAgentDelegated(rid, agent.agentId);

logger.debug(`Agent ${agent.agentId} assigned to inquiry ${inquiry._id}. Instances notified`);

void Apps.self?.getBridges()?.getListenerBridge().livechatEvent(AppEvents.IPostLivechatAgentAssigned, { room, user });
return inquiry;
return { inquiry, user };
},

async unassignAgent(inquiry, departmentId, shouldQueue = false) {
Expand Down Expand Up @@ -254,18 +255,19 @@ export const RoutingManager: Routing = {
logger.info(`Inquiry ${inquiry._id} taken by agent ${agent.agentId}`);

// assignAgent changes the room data to add the agent serving the conversation. afterTakeInquiry expects room object to be updated
const inq = await this.assignAgent(inquiry as InquiryWithAgentInfo, room, agent);
const { inquiry: returnedInquiry, user } = await this.assignAgent(inquiry as InquiryWithAgentInfo, agent);
const roomAfterUpdate = await LivechatRooms.findOneById(rid);

if (!roomAfterUpdate) {
// This should never happen
throw new Error('error-room-not-found');
}

void Apps.self?.getBridges()?.getListenerBridge().livechatEvent(AppEvents.IPostLivechatAgentAssigned, { room: roomAfterUpdate, user });
callbacks.runAsync(
'livechat.afterTakeInquiry',
{
inquiry: inq,
inquiry: returnedInquiry,
room: roomAfterUpdate,
},
agent,
Expand Down
Loading