Skip to content

Commit

Permalink
remove room find from take inquiry
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed May 8, 2024
1 parent 01b7931 commit 691fa18
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
24 changes: 17 additions & 7 deletions apps/meteor/app/livechat/server/lib/Helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,14 @@ export const forwardRoomToAgent = async (room: IOmnichannelRoom, transferData: T
// There are some Enterprise features that may interrupt the forwarding process
// Due to that we need to check whether the agent has been changed or not
logger.debug(`Forwarding inquiry ${inquiry._id} to agent ${agent.agentId}`);
const roomTaken = await RoutingManager.takeInquiry(inquiry, agent, {
...(clientAction && { clientAction }),
});
const roomTaken = await RoutingManager.takeInquiry(
inquiry,
agent,
{
...(clientAction && { clientAction }),
},
room,
);
if (!roomTaken) {
logger.debug(`Cannot forward inquiry ${inquiry._id}`);
return false;
Expand Down Expand Up @@ -562,10 +567,15 @@ export const forwardRoomToDepartment = async (room: IOmnichannelRoom, guest: ILi
// Fake the department to forward the inquiry - Case the forward process does not success
// the inquiry will stay in the same original department
inquiry.department = departmentId;
const roomTaken = await RoutingManager.delegateInquiry(inquiry, agent, {
forwardingToDepartment: { oldDepartmentId },
...(clientAction && { clientAction }),
});
const roomTaken = await RoutingManager.delegateInquiry(
inquiry,
agent,
{
forwardingToDepartment: { oldDepartmentId },
...(clientAction && { clientAction }),
},
room,
);
if (!roomTaken) {
logger.debug(`Cannot forward room ${room._id}. Unable to delegate inquiry`);
return false;
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/app/livechat/server/lib/QueueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const queueInquiry = async (inquiry: ILivechatInquiryRecord, defaultAgent

if (dbInquiry.status === 'ready') {
logger.debug(`Inquiry with id ${inquiry._id} is ready. Delegating to agent ${inquiryAgent?.username}`);
return RoutingManager.delegateInquiry(dbInquiry, inquiryAgent);
return RoutingManager.delegateInquiry(dbInquiry, inquiryAgent, undefined, room);
}
};

Expand Down
33 changes: 17 additions & 16 deletions apps/meteor/app/livechat/server/lib/RoutingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ type Routing = {
inquiry: InquiryWithAgentInfo,
agent?: SelectedAgent | null,
options?: { clientAction?: boolean; forwardingToDepartment?: { oldDepartmentId?: string; transferData?: any } },
room?: IOmnichannelRoom,
): Promise<(IOmnichannelRoom & { chatQueued?: boolean }) | null | void>;
assignAgent(inquiry: InquiryWithAgentInfo, agent: SelectedAgent): Promise<InquiryWithAgentInfo>;
unassignAgent(inquiry: ILivechatInquiryRecord, departmentId?: string, shouldQueue?: boolean): Promise<boolean>;
takeInquiry(
inquiry: Omit<
ILivechatInquiryRecord,
'estimatedInactivityCloseTimeAt' | 'message' | 't' | 'source' | 'estimatedWaitingTimeQueue' | 'priorityWeight' | '_updatedAt'
>,
agent: SelectedAgent | null,
options?: { clientAction?: boolean; forwardingToDepartment?: { oldDepartmentId?: string; transferData?: any } },
options: { clientAction?: boolean; forwardingToDepartment?: { oldDepartmentId?: string; transferData?: any } },
room: IOmnichannelRoom,
): Promise<IOmnichannelRoom | null | void>;
transferRoom(room: IOmnichannelRoom, guest: ILivechatVisitor, transferData: TransferData): Promise<boolean>;
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>;
};

export const RoutingManager: Routing = {
Expand Down Expand Up @@ -99,7 +102,7 @@ export const RoutingManager: Routing = {
return this.getMethod().getNextAgent(department, ignoreAgentId);
},

async delegateInquiry(inquiry, agent, options = {}) {
async delegateInquiry(inquiry, agent, options = {}, room) {
const { department, rid } = inquiry;
logger.debug(`Attempting to delegate inquiry ${inquiry._id}`);
if (!agent || (agent.username && !(await Users.findOneOnlineAgentByUserList(agent.username)) && !(await allowAgentSkipQueue(agent)))) {
Expand All @@ -115,11 +118,15 @@ export const RoutingManager: Routing = {
return LivechatRooms.findOneById(rid);
}

if (!room) {
throw new Meteor.Error('error-invalid-room');
}

logger.debug(`Inquiry ${inquiry._id} will be taken by agent ${agent.agentId}`);
return this.takeInquiry(inquiry, agent, options);
return this.takeInquiry(inquiry, agent, options, room);
},

async assignAgent(inquiry, agent) {
async assignAgent(inquiry: InquiryWithAgentInfo, room: IOmnichannelRoom, agent: SelectedAgent): Promise<InquiryWithAgentInfo> {
check(
agent,
Match.ObjectIncluding({
Expand All @@ -140,19 +147,14 @@ export const RoutingManager: Routing = {
await Rooms.incUsersCountById(rid, 1);

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

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

if (!room) {
logger.debug(`Cannot assign agent to inquiry ${inquiry._id}: Room not found`);
throw new Meteor.Error('error-room-not-found', 'Room not found');
}

await dispatchAgentDelegated(rid, agent.agentId);
logger.debug(`Agent ${agent.agentId} assigned to inquriy ${inquiry._id}. Instances notified`);

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

void Apps.self?.getBridges()?.getListenerBridge().livechatEvent(AppEvents.IPostLivechatAgentAssigned, { room, user });
return inquiry;
Expand Down Expand Up @@ -195,7 +197,7 @@ export const RoutingManager: Routing = {
return true;
},

async takeInquiry(inquiry, agent, options = { clientAction: false }) {
async takeInquiry(inquiry, agent, options = { clientAction: false }, room) {
check(
agent,
Match.ObjectIncluding({
Expand All @@ -216,7 +218,6 @@ export const RoutingManager: Routing = {
logger.debug(`Attempting to take Inquiry ${inquiry._id} [Agent ${agent.agentId}] `);

const { _id, rid } = inquiry;
const room = await LivechatRooms.findOneById(rid);
if (!room?.open) {
logger.debug(`Cannot take Inquiry ${inquiry._id}: Room is closed`);
return room;
Expand Down Expand Up @@ -250,13 +251,13 @@ export const RoutingManager: Routing = {
}

await LivechatInquiry.takeInquiry(_id);
const inq = await this.assignAgent(inquiry as InquiryWithAgentInfo, agent);

logger.info(`Inquiry ${inquiry._id} taken by agent ${agent.agentId}`);

callbacks.runAsync(
'livechat.afterTakeInquiry',
{
inquiry: inq,
inquiry: await this.assignAgent(inquiry as InquiryWithAgentInfo, room, agent),
room,
},
agent,
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 @@ -60,7 +60,7 @@ export const takeInquiry = async (
};

try {
await RoutingManager.takeInquiry(inquiry, agent, options);
await RoutingManager.takeInquiry(inquiry, agent, options ?? {}, room);
} catch (e: any) {
throw new Meteor.Error(e.message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ settings.watch('Livechat_auto_transfer_chat_timeout', (value) => {
return;
}

if (!autoTransferTimeout || autoTransferTimeout <= 0) {
return inquiry;
}

// const room = await LivechatRooms.findOneById(rid, { projection: { _id: 1, autoTransferredAt: 1, autoTransferOngoing: 1 } });
// if (!room) {
// return inquiry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Meteor.methods<ServerMethods>({
const {
servedBy: { _id: agentId, username },
} = room;
await RoutingManager.takeInquiry(inquiry, { agentId, username }, options);
await RoutingManager.takeInquiry(inquiry, { agentId, username }, options, room);

const onHoldChatResumedBy = options.clientAction ? await Meteor.userAsync() : await Users.findOneById('rocket.cat');
if (!onHoldChatResumedBy) {
Expand Down
6 changes: 1 addition & 5 deletions apps/meteor/server/models/raw/LivechatRooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2296,15 +2296,11 @@ export class LivechatRoomsRaw extends BaseRaw<IOmnichannelRoom> implements ILive
servedBy: {
_id: newAgent.agentId,
username: newAgent.username,
ts: new Date(),
ts: newAgent.ts ?? new Date(),
},
},
};

if (newAgent.ts) {
update.$set.servedBy.ts = newAgent.ts;
}

return this.updateOne(query, update);
}

Expand Down
7 changes: 2 additions & 5 deletions apps/meteor/server/services/omnichannel/queue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import { type InquiryWithAgentInfo, type IOmnichannelQueue } from '@rocket.chat/core-typings';
import { License } from '@rocket.chat/license';
import { LivechatInquiry, LivechatRooms } from '@rocket.chat/models';
Expand Down Expand Up @@ -185,9 +184,7 @@ export class OmnichannelQueue implements IOmnichannelQueue {
queueLogger.debug(`Processing inquiry ${inquiry._id} from queue ${queue}`);
const { defaultAgent } = inquiry;

const roomFromDb = await LivechatRooms.findOneById<Pick<IOmnichannelRoom, '_id' | 'servedBy' | 'closedAt'>>(inquiry.rid, {
projection: { servedBy: 1, closedAt: 1 },
});
const roomFromDb = await LivechatRooms.findOneById(inquiry.rid);

// This is a precaution to avoid taking inquiries tied to rooms that no longer exist.
// This should never happen.
Expand All @@ -205,7 +202,7 @@ export class OmnichannelQueue implements IOmnichannelQueue {
return this.reconciliation('closed', { roomId: inquiry.rid, inquiryId: inquiry._id });
}

const room = await RoutingManager.delegateInquiry(inquiry, defaultAgent);
const room = await RoutingManager.delegateInquiry(inquiry, defaultAgent, undefined, roomFromDb);

if (room?.servedBy) {
const {
Expand Down

0 comments on commit 691fa18

Please sign in to comment.