From 4e92002c118a3d95ad806a0dffb4e861acd2ec41 Mon Sep 17 00:00:00 2001 From: Renato Becker Date: Thu, 11 Apr 2019 19:01:24 -0300 Subject: [PATCH] [FIX] Receiving agent for new livechats from REST API (#14103) --- app/livechat/server/api/lib/livechat.js | 4 ++-- app/livechat/server/api/v1/room.js | 13 +++++++++++-- app/livechat/server/api/v1/videoCall.js | 3 ++- app/livechat/server/lib/QueueMethods.js | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/livechat/server/api/lib/livechat.js b/app/livechat/server/api/lib/livechat.js index c29208239d2..418fafca401 100644 --- a/app/livechat/server/api/lib/livechat.js +++ b/app/livechat/server/api/lib/livechat.js @@ -64,7 +64,7 @@ export function findOpenRoom(token, departmentId) { return room; } -export function getRoom(guest, rid, roomInfo) { +export function getRoom({ guest, rid, roomInfo, agent }) { const token = guest && guest.token; const message = { @@ -75,7 +75,7 @@ export function getRoom(guest, rid, roomInfo) { ts: new Date(), }; - return Livechat.getRoom(guest, message, roomInfo); + return Livechat.getRoom(guest, message, roomInfo, agent); } export function findAgent(agentId) { diff --git a/app/livechat/server/api/v1/room.js b/app/livechat/server/api/v1/room.js index b700bd89a37..707d619d715 100644 --- a/app/livechat/server/api/v1/room.js +++ b/app/livechat/server/api/v1/room.js @@ -5,7 +5,7 @@ import { TAPi18n } from 'meteor/tap:i18n'; import { settings as rcSettings } from '../../../../settings'; import { Messages, Rooms } from '../../../../models'; import { API } from '../../../../api'; -import { findGuest, findRoom, getRoom, settings } from '../lib/livechat'; +import { findGuest, findRoom, getRoom, settings, findAgent } from '../lib/livechat'; import { Livechat } from '../../lib/Livechat'; API.v1.addRoute('livechat/room', { @@ -14,6 +14,7 @@ API.v1.addRoute('livechat/room', { check(this.queryParams, { token: String, rid: Match.Maybe(String), + agentId: Match.Maybe(String), }); const { token } = this.queryParams; @@ -22,8 +23,16 @@ API.v1.addRoute('livechat/room', { throw new Meteor.Error('invalid-token'); } + let agent; + const { agentId } = this.queryParams; + const agentObj = agentId && findAgent(agentId); + if (agentObj) { + const { username } = agentObj; + agent = Object.assign({}, { agentId, username }); + } + const rid = this.queryParams.rid || Random.id(); - const room = getRoom(guest, rid); + const room = getRoom({ guest, rid, agent }); return API.v1.success(room); } catch (e) { diff --git a/app/livechat/server/api/v1/videoCall.js b/app/livechat/server/api/v1/videoCall.js index a200667a47c..7845b4ffe26 100644 --- a/app/livechat/server/api/v1/videoCall.js +++ b/app/livechat/server/api/v1/videoCall.js @@ -25,7 +25,8 @@ API.v1.addRoute('livechat/video.call/:token', { } const rid = this.queryParams.rid || Random.id(); - const { room } = getRoom(guest, rid, { jitsiTimeout: new Date(Date.now() + 3600 * 1000) }); + const roomInfo = { jitsiTimeout: new Date(Date.now() + 3600 * 1000) }; + const { room } = getRoom({ guest, rid, roomInfo }); const config = settings(); if (!config.theme || !config.theme.actionLinks) { throw new Meteor.Error('invalid-livechat-config'); diff --git a/app/livechat/server/lib/QueueMethods.js b/app/livechat/server/lib/QueueMethods.js index 6dd4642913c..aed907b4f6f 100644 --- a/app/livechat/server/lib/QueueMethods.js +++ b/app/livechat/server/lib/QueueMethods.js @@ -14,7 +14,7 @@ export const QueueMethods = { * of open chats is paired with the incoming livechat */ 'Least_Amount'(guest, message, roomInfo, agent) { - if (!agent) { + if (!agent || (agent.username && !Users.findOneOnlineAgentByUsername(agent.username))) { agent = Livechat.getNextAgent(guest.department); if (!agent) { throw new Meteor.Error('no-agent-online', 'Sorry, no online agents');