Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVE][Omnichannel] Allow set other agent status via method livechat:changeLivechatStatus #18571

Merged
merged 8 commits into from
Aug 19, 2020
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,
},
});

const newStatus = status || (agent.statusLivechat === 'available' ? 'not-available' : 'available');

if (newStatus === agent.statusLivechat) {
return;
}

if (!agent) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, move this check above this line. Otherwise, agent.statusLivechat may raise an error when the agent is not found.

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