|
1 | 1 | import { Meteor } from 'meteor/meteor'; |
2 | 2 |
|
3 | 3 | import { Livechat } from '../lib/Livechat'; |
| 4 | +import { hasPermission } from '../../../authorization'; |
| 5 | +import Users from '../../../models/server/models/Users'; |
4 | 6 |
|
5 | 7 | 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) { |
8 | 12 | throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'livechat:changeLivechatStatus' }); |
9 | 13 | } |
10 | 14 |
|
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 | + } |
12 | 38 |
|
13 | | - const newStatus = user.statusLivechat === 'available' ? 'not-available' : 'available'; |
14 | | - if (!Livechat.allowAgentChangeServiceStatus(newStatus, user._id)) { |
| 39 | + if (!Livechat.allowAgentChangeServiceStatus(newStatus, agentId)) { |
15 | 40 | throw new Meteor.Error('error-business-hours-are-closed', 'Not allowed', { method: 'livechat:changeLivechatStatus' }); |
16 | 41 | } |
17 | 42 |
|
18 | | - return Livechat.setUserStatusLivechat(user._id, newStatus); |
| 43 | + return Livechat.setUserStatusLivechat(agentId, newStatus); |
19 | 44 | }, |
20 | 45 | }); |
0 commit comments