Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions apps/meteor/app/livechat/server/api/v1/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { ILivechatAgent } from '@rocket.chat/core-typings';
import { ILivechatAgentStatus } from '@rocket.chat/core-typings';
import { Users } from '@rocket.chat/models';
import {
GETAgentInfoSuccessResponse,
GETAgentNextSuccessResponse,
isGETAgentNextToken,
isPOSTLivechatAgentSaveInfoParams,
isPOSTLivechatAgentStatusProps,
Expand All @@ -21,8 +23,15 @@ import { saveAgentInfo } from '../../lib/omni-users';
import { setUserStatusLivechat, allowAgentChangeServiceStatus } from '../../lib/utils';
import { findRoom, findGuest, findAgent, findOpenRoom } from '../lib/livechat';

API.v1.addRoute('livechat/agent.info/:rid/:token', {
async get() {
const livechatAgentInfoEndpoint = API.v1.get(
'livechat/agent.info/:rid/:token',
{
response: {
200: GETAgentInfoSuccessResponse,
400: validateBadRequestErrorResponse,
},
},
async function action() {
const visitor = await findGuest(this.urlParams.token);
if (!visitor) {
throw new Error('invalid-token');
Expand All @@ -40,39 +49,43 @@ API.v1.addRoute('livechat/agent.info/:rid/:token', {

return API.v1.success({ agent });
},
});
);

API.v1.addRoute(
const livechatAgentNextEndpoint = API.v1.get(
'livechat/agent.next/:token',
{ validateParams: isGETAgentNextToken },
{
async get() {
const { token } = this.urlParams;
const room = await findOpenRoom(token, undefined, this.userId);
if (room) {
return API.v1.success();
}
query: isGETAgentNextToken,
response: {
200: GETAgentNextSuccessResponse,
400: validateBadRequestErrorResponse,
},
},
async function action() {
const { token } = this.urlParams;
const room = await findOpenRoom(token, undefined, this.userId);
if (room) {
return API.v1.success();
}

let { department } = this.queryParams;
if (!department) {
const requireDepartment = await getRequiredDepartment();
if (requireDepartment) {
department = requireDepartment._id;
}
let { department } = this.queryParams;
if (!department) {
const requireDepartment = await getRequiredDepartment();
if (requireDepartment) {
department = requireDepartment._id;
}
}

const agentData = await RoutingManager.getNextAgent(department);
if (!agentData) {
throw new Error('agent-not-found');
}
const agentData = await RoutingManager.getNextAgent(department);
if (!agentData) {
throw new Error('agent-not-found');
}

const agent = await findAgent(agentData.agentId);
if (!agent) {
throw new Error('invalid-agent');
}
const agent = await findAgent(agentData.agentId);
if (!agent) {
throw new Error('invalid-agent');
}

return API.v1.success({ agent });
},
return API.v1.success({ agent });
},
);

Expand Down Expand Up @@ -162,9 +175,11 @@ const livechatAgentsEndpoints = API.v1.post(
},
);

type LivechatAgentInfoEndpoints = ExtractRoutesFromAPI<typeof livechatAgentInfoEndpoint>;
type LivechatAgentNextEndpoints = ExtractRoutesFromAPI<typeof livechatAgentNextEndpoint>;
type LivechatAgentsEndpoints = ExtractRoutesFromAPI<typeof livechatAgentsEndpoints>;

declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends LivechatAgentsEndpoints {}
// eslint-disable-next-line @typescript-eslint/naming-convention
interface Endpoints extends LivechatAgentInfoEndpoints, LivechatAgentNextEndpoints, LivechatAgentsEndpoints {}
}
38 changes: 38 additions & 0 deletions packages/rest-typings/src/v1/omnichannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,44 @@ const GETAgentNextTokenSchema = {

export const isGETAgentNextToken = ajvQuery.compile<GETAgentNextToken>(GETAgentNextTokenSchema);

const GETAgentInfoSuccessResponseSchema = {
type: 'object',
properties: {
agent: {
type: 'object',
},
success: {
type: 'boolean',
enum: [true],
},
},
required: ['agent', 'success'],
additionalProperties: false,
};

export const GETAgentInfoSuccessResponse = ajv.compile<{ agent: ILivechatAgent | { hiddenInfo: true }; success: boolean }>(
GETAgentInfoSuccessResponseSchema,
);

const GETAgentNextSuccessResponseSchema = {
type: 'object',
properties: {
agent: {
type: 'object',
},
success: {
type: 'boolean',
enum: [true],
},
},
required: ['success'],
additionalProperties: false,
};

export const GETAgentNextSuccessResponse = ajv.compile<
{ agent?: ILivechatAgent | { hiddenInfo: true }; success: boolean } | { success: boolean }
>(GETAgentNextSuccessResponseSchema);

type GETLivechatConfigParams = {
token?: string;
department?: string;
Expand Down