diff --git a/app/livechat/server/lib/RoutingManager.js b/app/livechat/server/lib/RoutingManager.js index 6e996f617b7e0..b4615778560fb 100644 --- a/app/livechat/server/lib/RoutingManager.js +++ b/app/livechat/server/lib/RoutingManager.js @@ -99,8 +99,7 @@ export const RoutingManager = { throw new Meteor.Error('error-creating-subscription', 'Error creating subscription'); } - LivechatRooms.changeAgentByRoomId(rid, agent); - Rooms.incUsersCountById(rid); + Rooms.incUsersCountAndChangeAgentByRoomId(rid, agent); const user = Users.findOneById(agent.agentId); const room = LivechatRooms.findOneById(rid); diff --git a/app/models/server/models/LivechatRooms.js b/app/models/server/models/LivechatRooms.js index f6ceb96c9ef94..a326615e15a51 100644 --- a/app/models/server/models/LivechatRooms.js +++ b/app/models/server/models/LivechatRooms.js @@ -699,28 +699,6 @@ export class LivechatRooms extends Base { return this.find(query); } - changeAgentByRoomId(roomId, newAgent) { - const query = { - _id: roomId, - t: 'l', - }; - const update = { - $set: { - servedBy: { - _id: newAgent.agentId, - username: newAgent.username, - ts: new Date(), - }, - }, - }; - - if (newAgent.ts) { - update.$set.servedBy.ts = newAgent.ts; - } - - this.update(query, update); - } - changeDepartmentIdByRoomId(roomId, departmentId) { const query = { _id: roomId, diff --git a/app/models/server/models/Rooms.js b/app/models/server/models/Rooms.js index 64cc36b9ba07f..be06a5f8dee5a 100644 --- a/app/models/server/models/Rooms.js +++ b/app/models/server/models/Rooms.js @@ -999,6 +999,28 @@ export class Rooms extends Base { return this.update(query, update); } + // Note: regarding omnichannel property - "servedBy" - over here + // For some reason, the update statement with only "servedBy" is not working properly if the update is performed from "LivechatRooms" collection. + // hence, I had to move it over here + incUsersCountAndChangeAgentByRoomId(_id, newAgent, inc = 1) { + const query = { _id }; + + const update = { + $inc: { + usersCount: inc, + }, + $set: { + servedBy: { + _id: newAgent.agentId, + username: newAgent.username, + ts: newAgent.ts || new Date(), + }, + }, + }; + + return this.update(query, update); + } + incUsersCountByIds(ids, inc = 1) { const query = { _id: {