From 9b46e5e245926b5827926894884cae50c78ddc94 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 14 Aug 2020 18:11:45 -0300 Subject: [PATCH 1/6] Add agentId parameter to changeLivechatStatus method --- app/livechat/server/methods/changeLivechatStatus.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/livechat/server/methods/changeLivechatStatus.js b/app/livechat/server/methods/changeLivechatStatus.js index 8717d16eaf01..23e07a34f2cf 100644 --- a/app/livechat/server/methods/changeLivechatStatus.js +++ b/app/livechat/server/methods/changeLivechatStatus.js @@ -1,14 +1,23 @@ import { Meteor } from 'meteor/meteor'; import { Livechat } from '../lib/Livechat'; +import { hasPermission, hasRole } from '../../../authorization'; +import { Users } from '../../../models'; Meteor.methods({ - 'livechat:changeLivechatStatus'() { + 'livechat:changeLivechatStatus'(agentId) { if (!Meteor.userId()) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:changeLivechatStatus' }); } - const user = Meteor.user(); + let user = Meteor.user(); + + if (agentId && hasPermission(Meteor.userId(), 'manage-livechat-agents')) { + user = Users.findOneById(agentId); + if (!user || !hasRole(agentId, 'livechat-agent')) { + throw new Meteor.Error('error-user-is-not-agent', 'User is not a livechat agent', { method: 'livechat:saveAgentInfo' }); + } + } const newStatus = user.statusLivechat === 'available' ? 'not-available' : 'available'; if (!Livechat.allowAgentChangeServiceStatus(newStatus, user._id)) { From 4042004593360255a6b8b4126d7e85c9af1d3798 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 17 Aug 2020 11:10:50 -0300 Subject: [PATCH 2/6] Fix reviews --- .../server/methods/changeLivechatStatus.js | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/app/livechat/server/methods/changeLivechatStatus.js b/app/livechat/server/methods/changeLivechatStatus.js index 23e07a34f2cf..af750922d49a 100644 --- a/app/livechat/server/methods/changeLivechatStatus.js +++ b/app/livechat/server/methods/changeLivechatStatus.js @@ -1,29 +1,35 @@ import { Meteor } from 'meteor/meteor'; import { Livechat } from '../lib/Livechat'; -import { hasPermission, hasRole } from '../../../authorization'; -import { Users } from '../../../models'; +import { hasPermission } from '../../../authorization'; +import { Users } from '../../../models/server/models/Users'; Meteor.methods({ - 'livechat:changeLivechatStatus'(agentId) { - if (!Meteor.userId()) { + 'livechat:changeLivechatStatus'({ status, agentId = Meteor.userId() }) { + const uid = Meteor.userId(); + + if (!uid || !agentId) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:changeLivechatStatus' }); } - let user = Meteor.user(); + const agent = Users.findOneAgentById({ _id: agentId }, { fields: { statusLivechat: 1 } }); + const newStatus = status || agent.statusLivechat === 'available' ? 'not-available' : 'available'; + + if (!agent) { + throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' }); + } - if (agentId && hasPermission(Meteor.userId(), 'manage-livechat-agents')) { - user = Users.findOneById(agentId); - if (!user || !hasRole(agentId, 'livechat-agent')) { - throw new Meteor.Error('error-user-is-not-agent', 'User is not a livechat agent', { method: 'livechat:saveAgentInfo' }); + if (agentId !== uid) { + if (!hasPermission(uid, 'manage-livechat-agents')) { + throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' }); } + return Livechat.setUserStatusLivechat(agentId, newStatus); } - const newStatus = user.statusLivechat === 'available' ? 'not-available' : 'available'; - if (!Livechat.allowAgentChangeServiceStatus(newStatus, user._id)) { + if (!Livechat.allowAgentChangeServiceStatus(newStatus, agentId)) { throw new Meteor.Error('error-business-hours-are-closed', 'Not allowed', { method: 'livechat:changeLivechatStatus' }); } - return Livechat.setUserStatusLivechat(user._id, newStatus); + return Livechat.setUserStatusLivechat(agentId, newStatus); }, }); From 277a22ebb3b5894eb5fa1313870609b3b7496401 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 17 Aug 2020 13:16:29 -0300 Subject: [PATCH 3/6] fix problems --- app/livechat/server/methods/changeLivechatStatus.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/livechat/server/methods/changeLivechatStatus.js b/app/livechat/server/methods/changeLivechatStatus.js index af750922d49a..a0bb10ebe1a6 100644 --- a/app/livechat/server/methods/changeLivechatStatus.js +++ b/app/livechat/server/methods/changeLivechatStatus.js @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor'; import { Livechat } from '../lib/Livechat'; import { hasPermission } from '../../../authorization'; -import { Users } from '../../../models/server/models/Users'; +import Users from '../../../models/server/models/Users'; Meteor.methods({ 'livechat:changeLivechatStatus'({ status, agentId = Meteor.userId() }) { @@ -12,8 +12,14 @@ Meteor.methods({ throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:changeLivechatStatus' }); } - const agent = Users.findOneAgentById({ _id: agentId }, { fields: { statusLivechat: 1 } }); - const newStatus = status || agent.statusLivechat === 'available' ? 'not-available' : 'available'; + const agent = Users.findOneAgentById(agentId, { + fields: { + status: 1, + statusLivechat: 1, + }, + }); + + const newStatus = status || (agent.statusLivechat === 'available' ? 'not-available' : 'available'); if (!agent) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' }); From a3e0dddb35d3b6e3ffac7df1a814133495ccd148 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 17 Aug 2020 14:34:06 -0300 Subject: [PATCH 4/6] return if the same as before --- app/livechat/server/methods/changeLivechatStatus.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/livechat/server/methods/changeLivechatStatus.js b/app/livechat/server/methods/changeLivechatStatus.js index a0bb10ebe1a6..893bded7fdad 100644 --- a/app/livechat/server/methods/changeLivechatStatus.js +++ b/app/livechat/server/methods/changeLivechatStatus.js @@ -21,6 +21,10 @@ Meteor.methods({ const newStatus = status || (agent.statusLivechat === 'available' ? 'not-available' : 'available'); + if (newStatus === agent.statusLivechat) { + return; + } + if (!agent) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' }); } From ae7a8a8e386f6b9cf658bec2aed90b5cd22662c1 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 19 Aug 2020 02:24:58 -0300 Subject: [PATCH 5/6] Update app/livechat/server/methods/changeLivechatStatus.js Co-authored-by: Renato Becker --- app/livechat/server/methods/changeLivechatStatus.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/livechat/server/methods/changeLivechatStatus.js b/app/livechat/server/methods/changeLivechatStatus.js index 893bded7fdad..e206e81075cf 100644 --- a/app/livechat/server/methods/changeLivechatStatus.js +++ b/app/livechat/server/methods/changeLivechatStatus.js @@ -5,7 +5,7 @@ import { hasPermission } from '../../../authorization'; import Users from '../../../models/server/models/Users'; Meteor.methods({ - 'livechat:changeLivechatStatus'({ status, agentId = Meteor.userId() }) { + 'livechat:changeLivechatStatus'({ status, agentId = Meteor.userId() } = {}) { const uid = Meteor.userId(); if (!uid || !agentId) { From d57aaae94de51f0a820690489651aef19c92df43 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Wed, 19 Aug 2020 02:42:02 -0300 Subject: [PATCH 6/6] Fix review --- app/livechat/server/methods/changeLivechatStatus.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/livechat/server/methods/changeLivechatStatus.js b/app/livechat/server/methods/changeLivechatStatus.js index 893bded7fdad..978c7fb277d7 100644 --- a/app/livechat/server/methods/changeLivechatStatus.js +++ b/app/livechat/server/methods/changeLivechatStatus.js @@ -19,16 +19,16 @@ Meteor.methods({ }, }); + if (!agent) { + throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' }); + } + const newStatus = status || (agent.statusLivechat === 'available' ? 'not-available' : 'available'); if (newStatus === agent.statusLivechat) { return; } - if (!agent) { - throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' }); - } - if (agentId !== uid) { if (!hasPermission(uid, 'manage-livechat-agents')) { throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' });