Skip to content

Commit

Permalink
[IMPROVE] Add agentId parameter to changeLivechatStatus method (#18571)
Browse files Browse the repository at this point in the history
* 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 <renato.augusto.becker@gmail.com>

* Fix review

Co-authored-by: Guilherme Gazzo <guilhermegazzo@gmail.com>
Co-authored-by: Renato Becker <renato.augusto.becker@gmail.com>
Co-authored-by: Guilherme Gazzo <guilherme@gazzo.xyz>
  • Loading branch information
4 people authored Aug 19, 2020
1 parent fdda142 commit dc549a5
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions app/livechat/server/methods/changeLivechatStatus.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
import { Meteor } from 'meteor/meteor';

import { Livechat } from '../lib/Livechat';
import { hasPermission } from '../../../authorization';
import Users from '../../../models/server/models/Users';

Meteor.methods({
'livechat:changeLivechatStatus'() {
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' });
}

const user = Meteor.user();
const agent = Users.findOneAgentById(agentId, {
fields: {
status: 1,
statusLivechat: 1,
},
});

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 (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);
},
});

0 comments on commit dc549a5

Please sign in to comment.