Skip to content

Commit dc549a5

Browse files
MartinSchoelerggazzorenatobecker
authored
[IMPROVE] Add agentId parameter to changeLivechatStatus method (#18571)
* Add agentId parameter to changeLivechatStatus method * Fix reviews * fix problems * return if the same as before * Update app/livechat/server/methods/changeLivechatStatus.js Co-authored-by: Renato Becker <[email protected]> * Fix review Co-authored-by: Guilherme Gazzo <[email protected]> Co-authored-by: Renato Becker <[email protected]> Co-authored-by: Guilherme Gazzo <[email protected]>
1 parent fdda142 commit dc549a5

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed
Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
import { Meteor } from 'meteor/meteor';
22

33
import { Livechat } from '../lib/Livechat';
4+
import { hasPermission } from '../../../authorization';
5+
import Users from '../../../models/server/models/Users';
46

57
Meteor.methods({
6-
'livechat:changeLivechatStatus'() {
7-
if (!Meteor.userId()) {
8+
'livechat:changeLivechatStatus'({ status, agentId = Meteor.userId() } = {}) {
9+
const uid = Meteor.userId();
10+
11+
if (!uid || !agentId) {
812
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:changeLivechatStatus' });
913
}
1014

11-
const user = Meteor.user();
15+
const agent = Users.findOneAgentById(agentId, {
16+
fields: {
17+
status: 1,
18+
statusLivechat: 1,
19+
},
20+
});
21+
22+
if (!agent) {
23+
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' });
24+
}
25+
26+
const newStatus = status || (agent.statusLivechat === 'available' ? 'not-available' : 'available');
27+
28+
if (newStatus === agent.statusLivechat) {
29+
return;
30+
}
31+
32+
if (agentId !== uid) {
33+
if (!hasPermission(uid, 'manage-livechat-agents')) {
34+
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:saveAgentInfo' });
35+
}
36+
return Livechat.setUserStatusLivechat(agentId, newStatus);
37+
}
1238

13-
const newStatus = user.statusLivechat === 'available' ? 'not-available' : 'available';
14-
if (!Livechat.allowAgentChangeServiceStatus(newStatus, user._id)) {
39+
if (!Livechat.allowAgentChangeServiceStatus(newStatus, agentId)) {
1540
throw new Meteor.Error('error-business-hours-are-closed', 'Not allowed', { method: 'livechat:changeLivechatStatus' });
1641
}
1742

18-
return Livechat.setUserStatusLivechat(user._id, newStatus);
43+
return Livechat.setUserStatusLivechat(agentId, newStatus);
1944
},
2045
});

0 commit comments

Comments
 (0)