Skip to content
Snippets Groups Projects
Commit 93d807cd authored by Diego Sampaio's avatar Diego Sampaio
Browse files

get next agent on queue

parent 88053735
No related branches found
No related tags found
No related merge requests found
this.getNextAgent = function(department) {
var agentFilter = {};
// find agents from that department
if (department) {
var agents = RocketChat.models.LivechatDepartment.getNextAgent(department);
if (!agents) {
return;
}
// sort = {
// count: 1,
// order: 1,
// 'user.name': 1
// }
// update = {
// $inc: {
// count: 1
// }
// }
// queueUser = findAndModify query, sort, update
} else {
return RocketChat.models.Users.getNextAgent();
}
};
Meteor.methods({
registerGuest: function(token, name, email) {
console.log('registerGuest ->'.green, token);
var pass, qt, user, userData, userExists, userId, inc = 0;
check(token, String);
user = Meteor.users.findOne({
"profile.token": token
}, {
......@@ -10,9 +11,11 @@ Meteor.methods({
_id: 1
}
});
if (user != null) {
throw new Meteor.Error('token-already-exists', 'Token already exists');
}
while (true) {
qt = Meteor.users.find({
'profile.guest': true
......
Meteor.methods({
sendMessageLivechat: function(message) {
var guest, operator, room;
console.log('sendMessageLivechat ->', arguments);
var guest, agent, room;
check(message.rid, String);
check(message.token, String);
guest = Meteor.users.findOne(Meteor.userId(), {
fields: {
username: 1
}
});
room = RocketChat.models.Rooms.findOneById(message.rid);
if (room == null) {
operator = Meteor.users.findOne({
operator: true,
status: 'online'
});
if (!operator) {
throw new Meteor.Error('no-operators', 'Sorry, no online operators');
agent = getNextAgent();
if (!agent) {
throw new Meteor.Error('no-agent-online', 'Sorry, no online agents');
}
RocketChat.models.Rooms.insert({
_id: message.rid,
name: guest.username,
msgs: 1,
lm: new Date(),
usernames: [operator.username, guest.username],
usernames: [agent.username, guest.username],
t: 'l',
ts: new Date(),
v: {
......@@ -38,8 +37,8 @@ Meteor.methods({
unread: 1,
answered: false,
u: {
_id: operator._id,
username: operator.username
_id: agent._id,
username: agent.username
},
t: 'l'
});
......
......@@ -13,6 +13,65 @@ RocketChat.models.Users.setOperator = function(_id, operator) {
return this.update(_id, update);
};
/**
* Gets all online agents
* @return
*/
RocketChat.models.Users.findOnlineAgents = function() {
var query = {
status: 'online',
roles: {}
};
query.roles[Roles.GLOBAL_GROUP] = 'livechat-agent';
return this.find(query);
};
/**
* Find online users from a list
* @param {array} userList - array of usernames
* @return
*/
RocketChat.models.Users.findOnlineUserFromList = function(userList) {
var query = {
status: 'online',
username: {
$in: [].concat(userList)
}
};
return this.find(query);
};
/**
* Get next user agent in order
* @return {object} User from db
*/
RocketChat.models.Users.getNextAgent = function() {
var query = {
status: 'online'
};
query['roles.' + Roles.GLOBAL_GROUP] = 'livechat-agent';
var collectionObj = this.model.rawCollection();
var findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
var sort = {
livechatCount: 1,
username: 1
};
var update = {
$inc: {
livechatCount: 1
}
};
return findAndModify(query, sort, update);
};
/**
* Gets visitor by token
* @param {string} token - Visitor token
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment