diff --git a/app/livechat/server/lib/Helper.js b/app/livechat/server/lib/Helper.js index 4ec60f142e38..bf6f3b26dae0 100644 --- a/app/livechat/server/lib/Helper.js +++ b/app/livechat/server/lib/Helper.js @@ -379,6 +379,10 @@ export const updateDepartmentAgents = (departmentId, agents) => { } upsert.forEach((agent) => { + if (!Users.findOneById(agent.agentId, { fields: { _id: 1 } })) { + return; + } + LivechatDepartmentAgents.saveAgent({ agentId: agent.agentId, departmentId, @@ -400,4 +404,6 @@ export const updateDepartmentAgents = (departmentId, agents) => { const numAgents = LivechatDepartmentAgents.find({ departmentId }).count(); LivechatDepartment.updateNumAgentsById(departmentId, numAgents); } + + return true; }; diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 88278a48746b..0fe3480aa77d 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -813,22 +813,22 @@ export const Livechat = { saveDepartmentAgents(_id, departmentAgents) { check(_id, String); check(departmentAgents, { - upsert: [ + upsert: Match.Maybe([ Match.ObjectIncluding({ agentId: String, username: String, count: Match.Maybe(Match.Integer), order: Match.Maybe(Match.Integer), }), - ], - remove: [ + ]), + remove: Match.Maybe([ Match.ObjectIncluding({ agentId: String, username: Match.Maybe(String), count: Match.Maybe(Match.Integer), order: Match.Maybe(Match.Integer), }), - ], + ]), }); const department = LivechatDepartment.findOneById(_id); @@ -836,8 +836,7 @@ export const Livechat = { throw new Meteor.Error('error-department-not-found', 'Department not found', { method: 'livechat:saveDepartmentAgents' }); } - const departmentDB = LivechatDepartment.createOrUpdateDepartment(_id, department); - return departmentDB && updateDepartmentAgents(departmentDB._id, departmentAgents); + return updateDepartmentAgents(_id, departmentAgents); }, saveDepartment(_id, departmentData, departmentAgents) { @@ -880,7 +879,7 @@ export const Livechat = { } const departmentDB = LivechatDepartment.createOrUpdateDepartment(_id, departmentData); - if (departmentDB) { + if (departmentDB && departmentAgents) { updateDepartmentAgents(departmentDB._id, departmentAgents); }