Skip to content

Commit

Permalink
[FIX] Error when updating omnichannel department without agents param…
Browse files Browse the repository at this point in the history
…eter (#18428)

* Fix error when updating department through REST API.

* Improves validation of endpoint parameters
  • Loading branch information
renatobecker authored and sampaiodiego committed Jul 31, 2020
1 parent 2354cfd commit 68d98a5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
6 changes: 6 additions & 0 deletions app/livechat/server/lib/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -400,4 +404,6 @@ export const updateDepartmentAgents = (departmentId, agents) => {
const numAgents = LivechatDepartmentAgents.find({ departmentId }).count();
LivechatDepartment.updateNumAgentsById(departmentId, numAgents);
}

return true;
};
13 changes: 6 additions & 7 deletions app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,31 +813,30 @@ 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);
if (!department) {
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) {
Expand Down Expand Up @@ -880,7 +879,7 @@ export const Livechat = {
}

const departmentDB = LivechatDepartment.createOrUpdateDepartment(_id, departmentData);
if (departmentDB) {
if (departmentDB && departmentAgents) {
updateDepartmentAgents(departmentDB._id, departmentAgents);
}

Expand Down

0 comments on commit 68d98a5

Please sign in to comment.