-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathconfig.ts
More file actions
124 lines (109 loc) · 3.88 KB
/
config.ts
File metadata and controls
124 lines (109 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import type { ILivechatAgent, ILivechatVisitor, IOmnichannelRoom } from '@rocket.chat/core-typings';
import { schemas } from '@rocket.chat/core-typings';
import { ajv, GETLivechatConfigRouting, validateUnauthorizedErrorResponse } from '@rocket.chat/rest-typings';
import mem from 'mem';
import { API } from '../../../../api/server';
import type { ExtractRoutesFromAPI } from '../../../../api/server/ApiClass';
import { settings as serverSettings } from '../../../../settings/server/index';
import { RoutingManager } from '../../lib/RoutingManager';
import { online } from '../../lib/service-status';
import { settings, findOpenRoom, getExtraConfigInfo, findAgent, findGuestWithoutActivity } from '../lib/livechat';
const schemaComponents = schemas.components?.schemas;
(['IOmnichannelRoom', 'ILivechatAgent', 'ILivechatVisitor'] as const).forEach((key) => {
const schema = schemaComponents?.[key];
if (schema && !ajv.getSchema(`#/components/schemas/${key}`)) {
ajv.addSchema(schema, `#/components/schemas/${key}`);
}
});
type GETLivechatConfigParams = {
token?: string;
department?: string;
businessUnit?: string;
};
const GETLivechatConfigParamsSchema = {
type: 'object',
properties: {
token: {
type: 'string',
nullable: true,
},
department: {
type: 'string',
nullable: true,
},
businessUnit: {
type: 'string',
nullable: true,
},
},
additionalProperties: false,
};
const isGETLivechatConfigParams = ajv.compile<GETLivechatConfigParams>(GETLivechatConfigParamsSchema);
const cachedSettings = mem(settings, { maxAge: process.env.TEST_MODE === 'true' ? 1 : 1000, cacheKey: JSON.stringify });
const livechatConfigEndpoints = API.v1
.get(
'livechat/config',
{
query: isGETLivechatConfigParams,
response: {
200: ajv.compile<{
config: Record<string, unknown> & { room?: IOmnichannelRoom; agent?: ILivechatAgent; guest?: ILivechatVisitor };
success: boolean;
}>({
type: 'object',
properties: {
config: {
type: 'object',
properties: {
room: { $ref: '#/components/schemas/IOmnichannelRoom' },
agent: { $ref: '#/components/schemas/ILivechatAgent' },
guest: { $ref: '#/components/schemas/ILivechatVisitor' },
},
additionalProperties: true,
},
success: { type: 'boolean', enum: [true] },
},
required: ['config', 'success'],
additionalProperties: false,
}),
},
},
async function action() {
const enabled = serverSettings.get<boolean>('Livechat_enabled');
if (!enabled) {
return API.v1.success({ config: { enabled: false } });
}
const { token, department, businessUnit } = this.queryParams;
const [config, status, guest] = await Promise.all([
cachedSettings({ businessUnit }),
online(department),
token ? findGuestWithoutActivity(token) : null,
]);
const room = guest ? await findOpenRoom(guest.token, undefined, this.userId) : undefined;
const agentPromise = room?.servedBy ? findAgent(room.servedBy._id) : null;
const extraInfoPromise = getExtraConfigInfo({ room });
const [agent, extraInfo] = await Promise.all([agentPromise, extraInfoPromise]);
return API.v1.success({
config: { ...config, online: status, ...extraInfo, ...(guest && { guest }), ...(room && { room }), ...(agent && { agent }) },
});
},
)
.get(
'livechat/config/routing',
{
authRequired: true,
response: {
200: GETLivechatConfigRouting,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const config = RoutingManager.getConfig();
return API.v1.success({ config });
},
);
type LivechatConfigEndpoints = ExtractRoutesFromAPI<typeof livechatConfigEndpoints>;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-object-type, @typescript-eslint/no-empty-interface
interface Endpoints extends LivechatConfigEndpoints {}
}