@webitel/api-services 26.12.20 → 26.12.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/api/clients/_shared/appUrls.ts +11 -0
- package/src/api/clients/chatGateways/__tests__/chatGatewayChannels.spec.ts +84 -0
- package/src/api/clients/chatGateways/__tests__/chatGateways.spec.ts +115 -0
- package/src/api/clients/chatGateways/chatGatewayChannels.ts +162 -0
- package/src/api/clients/chatGateways/chatGateways.ts +66 -25
- package/src/api/clients/chatGateways/defaults/chatGatewayProviderDefaults.ts +91 -0
- package/src/api/clients/chatGateways/defaults/defaultChatGateway.ts +3 -1
- package/src/api/clients/chatGateways/defaults/webChatGateway.ts +8 -5
- package/src/api/clients/chatplans/__tests__/chatplans.spec.ts +129 -0
- package/src/api/clients/chatplans/chatplans.ts +35 -45
- package/src/api/clients/cognitiveProfiles/cognitiveProfiles.ts +8 -32
- package/src/api/clients/cognitiveProfiles/defaults/cognitiveProfileDefaults.ts +39 -0
- package/src/api/clients/gateways/__tests__/gateways.spec.ts +138 -0
- package/src/api/clients/gateways/gateways.ts +60 -52
- package/src/api/clients/index.ts +2 -0
- package/src/enums/ChatGatewayChannelSetup/ChatGatewayChannelSetup.ts +10 -0
- package/src/enums/GatewayIpAclProtocol/GatewayIpAclProtocol.ts +8 -0
- package/src/enums/GatewayRegisterState/GatewayRegisterState.ts +17 -0
- package/src/enums/StorageType/StorageType.ts +10 -0
- package/src/enums/WebchatAlternativeChannel/WebchatAlternativeChannel.ts +15 -0
- package/src/enums/index.ts +10 -0
- package/src/locale/en/en.ts +8 -0
- package/src/locale/es/es.ts +8 -0
- package/src/locale/kz/kz.ts +8 -0
- package/src/locale/pl/pl.ts +8 -0
- package/src/locale/ro/ro.ts +8 -0
- package/src/locale/ru/ru.ts +8 -0
- package/src/locale/uk/uk.ts +8 -0
- package/src/locale/uz/uz.ts +8 -0
- package/src/locale/vi/vi.ts +8 -0
- package/src/validations/_shared/ip.validations.ts +10 -0
- package/src/validations/backendProfile/__tests__/backendProfile.validations.spec.ts +104 -0
- package/src/validations/backendProfile/backendProfile.validations.ts +79 -0
- package/src/validations/chatGateway/__tests__/chatGateway.validations.spec.ts +189 -0
- package/src/validations/chatGateway/chatGateway.validations.ts +452 -0
- package/src/validations/chatplan/__tests__/chatplan.validations.spec.ts +43 -0
- package/src/validations/chatplan/chatplan.validations.ts +17 -0
- package/src/validations/cognitiveProfile/__tests__/cognitiveProfile.validations.spec.ts +83 -0
- package/src/validations/cognitiveProfile/cognitiveProfile.validations.ts +74 -0
- package/src/validations/device/device.validations.ts +2 -8
- package/src/validations/gateway/__tests__/gateway.validations.spec.ts +168 -0
- package/src/validations/gateway/gateway.validations.ts +138 -0
- package/src/validations/index.ts +5 -0
- package/types/.tsbuildinfo +1 -1
- package/types/api/clients/_shared/appUrls.d.ts +11 -0
- package/types/api/clients/chatGateways/chatGatewayChannels.d.ts +62 -0
- package/types/api/clients/chatGateways/chatGateways.d.ts +4 -4
- package/types/api/clients/chatGateways/defaults/chatGatewayProviderDefaults.d.ts +21 -0
- package/types/api/clients/chatGateways/defaults/defaultChatGateway.d.ts +3 -1
- package/types/api/clients/chatGateways/defaults/webChatGateway.d.ts +5 -4
- package/types/api/clients/cognitiveProfiles/cognitiveProfiles.d.ts +1 -1
- package/types/api/clients/cognitiveProfiles/defaults/cognitiveProfileDefaults.d.ts +13 -0
- package/types/api/clients/index.d.ts +2 -0
- package/types/enums/ChatGatewayChannelSetup/ChatGatewayChannelSetup.d.ts +8 -0
- package/types/enums/GatewayIpAclProtocol/GatewayIpAclProtocol.d.ts +6 -0
- package/types/enums/GatewayRegisterState/GatewayRegisterState.d.ts +15 -0
- package/types/enums/StorageType/StorageType.d.ts +9 -0
- package/types/enums/WebchatAlternativeChannel/WebchatAlternativeChannel.d.ts +13 -0
- package/types/enums/index.d.ts +6 -1
- package/types/locale/en/en.d.ts +7 -0
- package/types/locale/es/es.d.ts +7 -0
- package/types/locale/index.d.ts +63 -0
- package/types/locale/kz/kz.d.ts +7 -0
- package/types/locale/pl/pl.d.ts +7 -0
- package/types/locale/ro/ro.d.ts +7 -0
- package/types/locale/ru/ru.d.ts +7 -0
- package/types/locale/uk/uk.d.ts +7 -0
- package/types/locale/uz/uz.d.ts +7 -0
- package/types/locale/vi/vi.d.ts +7 -0
- package/types/validations/_shared/ip.validations.d.ts +4 -0
- package/types/validations/backendProfile/backendProfile.validations.d.ts +24 -0
- package/types/validations/chatGateway/chatGateway.validations.d.ts +165 -0
- package/types/validations/chatplan/chatplan.validations.d.ts +14 -0
- package/types/validations/cognitiveProfile/cognitiveProfile.validations.d.ts +28 -0
- package/types/validations/gateway/gateway.validations.d.ts +48 -0
- package/types/validations/index.d.ts +5 -0
- package/src/api/clients/chatGateways/enums/WebchatAlternativeChannel.enum.ts +0 -12
- package/src/api/clients/gateways/defaults/defaultGateway.ts +0 -9
- package/src/api/clients/gateways/defaults/registerGateway.ts +0 -12
- package/src/api/clients/gateways/defaults/trunkingGateway.ts +0 -16
- package/types/api/clients/chatGateways/enums/WebchatAlternativeChannel.enum.d.ts +0 -12
- package/types/api/clients/gateways/defaults/defaultGateway.d.ts +0 -8
- package/types/api/clients/gateways/defaults/registerGateway.d.ts +0 -13
- package/types/api/clients/gateways/defaults/trunkingGateway.d.ts +0 -16
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [Claude] Where the app's services live, and its access token. The app passes
|
|
3
|
+
* them: the urls and the token are its concern, not this package's. Used for
|
|
4
|
+
* `src`/`href` targets that can't send the auth header, and for services
|
|
5
|
+
* outside the api base url, like the chat service.
|
|
6
|
+
*/
|
|
7
|
+
export type AppUrls = {
|
|
8
|
+
apiUrl: string;
|
|
9
|
+
chatUrl: string;
|
|
10
|
+
accessToken: string;
|
|
11
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { ChatGatewayChannelSetup } from '../../../../enums';
|
|
4
|
+
|
|
5
|
+
const instance = vi.hoisted(() => ({
|
|
6
|
+
get: vi.fn(),
|
|
7
|
+
post: vi.fn(),
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
vi.mock('../../../defaults', async (importOriginal) => ({
|
|
11
|
+
...(await importOriginal<typeof import('../../../defaults')>()),
|
|
12
|
+
getDefaultInstance: () => instance,
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
const { ChatGatewayChannelsAPI } = await import('../chatGatewayChannels');
|
|
16
|
+
|
|
17
|
+
const params = {
|
|
18
|
+
uri: '/bot',
|
|
19
|
+
appUrls: {
|
|
20
|
+
chatUrl: 'https://example.com/chat',
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
describe('ChatGatewayChannelsAPI', () => {
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
vi.clearAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('requests the chat service at the app chat url', async () => {
|
|
30
|
+
instance.get.mockResolvedValue({
|
|
31
|
+
data: [
|
|
32
|
+
{
|
|
33
|
+
id: '1',
|
|
34
|
+
subscribed_fields: [
|
|
35
|
+
'messages',
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const pages = await ChatGatewayChannelsAPI.getFacebookPages(params);
|
|
42
|
+
|
|
43
|
+
expect(instance.get).toHaveBeenCalledWith(
|
|
44
|
+
'https://example.com/chat/bot?pages=',
|
|
45
|
+
);
|
|
46
|
+
expect(pages).toEqual([
|
|
47
|
+
{
|
|
48
|
+
id: '1',
|
|
49
|
+
subscribedFields: [
|
|
50
|
+
'messages',
|
|
51
|
+
],
|
|
52
|
+
accounts: [],
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('builds subscription and removal requests', async () => {
|
|
58
|
+
instance.get.mockResolvedValue({});
|
|
59
|
+
|
|
60
|
+
await ChatGatewayChannelsAPI.updateWhatsappSubscription({
|
|
61
|
+
...params,
|
|
62
|
+
id: '7',
|
|
63
|
+
value: false,
|
|
64
|
+
});
|
|
65
|
+
await ChatGatewayChannelsAPI.deleteWhatsappAccount({
|
|
66
|
+
...params,
|
|
67
|
+
id: '7',
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(instance.get.mock.calls.map(([url]) => url)).toEqual([
|
|
71
|
+
'https://example.com/chat/bot?whatsapp=unsubscribe&id=7',
|
|
72
|
+
'https://example.com/chat/bot?whatsapp=remove&id=7&ack=true',
|
|
73
|
+
]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('builds the setup page url', () => {
|
|
77
|
+
expect(
|
|
78
|
+
ChatGatewayChannelsAPI.getSetupUrl({
|
|
79
|
+
...params,
|
|
80
|
+
setup: ChatGatewayChannelSetup.TelegramApp,
|
|
81
|
+
}),
|
|
82
|
+
).toBe('https://example.com/chat/bot?auth');
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { ChatGatewayProvider } from '../../../../enums';
|
|
4
|
+
|
|
5
|
+
const instance = vi.hoisted(() => ({
|
|
6
|
+
get: vi.fn(),
|
|
7
|
+
post: vi.fn(),
|
|
8
|
+
put: vi.fn(),
|
|
9
|
+
patch: vi.fn(),
|
|
10
|
+
delete: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock('../../../defaults', async (importOriginal) => ({
|
|
14
|
+
...(await importOriginal<typeof import('../../../defaults')>()),
|
|
15
|
+
getDefaultInstance: () => instance,
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
const { ChatGatewaysAPI } = await import('../chatGateways');
|
|
19
|
+
|
|
20
|
+
describe('ChatGatewaysAPI', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.clearAllMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('get seeds the provider defaults and clears an empty flow', async () => {
|
|
26
|
+
instance.get.mockResolvedValue({
|
|
27
|
+
data: {
|
|
28
|
+
id: '1',
|
|
29
|
+
name: 'bot',
|
|
30
|
+
provider: ChatGatewayProvider.TELEGRAM_BOT,
|
|
31
|
+
flow: {},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const item = await ChatGatewaysAPI.get({
|
|
36
|
+
itemId: '1',
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
expect(item.flow).toBeUndefined();
|
|
40
|
+
expect(item.metadata).toEqual({
|
|
41
|
+
token: '',
|
|
42
|
+
});
|
|
43
|
+
expect(item.updates.filePolicyFail).toBe('');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('get parses webchat metadata over the webchat defaults', async () => {
|
|
47
|
+
instance.get.mockResolvedValue({
|
|
48
|
+
data: {
|
|
49
|
+
id: '2',
|
|
50
|
+
provider: ChatGatewayProvider.WEBCHAT,
|
|
51
|
+
metadata: {
|
|
52
|
+
allow_origin: 'a.com,b.com',
|
|
53
|
+
read_timeout: '60s',
|
|
54
|
+
_btn_code_dirty: 'true',
|
|
55
|
+
view: JSON.stringify({
|
|
56
|
+
lang: 'uk',
|
|
57
|
+
}),
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const item = await ChatGatewaysAPI.get({
|
|
63
|
+
itemId: '2',
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(item.metadata).toMatchObject({
|
|
67
|
+
allowOrigin: [
|
|
68
|
+
'a.com',
|
|
69
|
+
'b.com',
|
|
70
|
+
],
|
|
71
|
+
readTimeout: 60,
|
|
72
|
+
_btnCodeDirty: true,
|
|
73
|
+
view: {
|
|
74
|
+
lang: 'uk',
|
|
75
|
+
position: '',
|
|
76
|
+
},
|
|
77
|
+
chat: {
|
|
78
|
+
enabled: true,
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('add returns the saved item in the card shape', async () => {
|
|
84
|
+
instance.post.mockResolvedValue({
|
|
85
|
+
data: {
|
|
86
|
+
id: '3',
|
|
87
|
+
provider: ChatGatewayProvider.MESSENGER,
|
|
88
|
+
metadata: {
|
|
89
|
+
instagram_comments: 'true',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const item = await ChatGatewaysAPI.add({
|
|
95
|
+
itemInstance: {
|
|
96
|
+
name: 'meta',
|
|
97
|
+
provider: ChatGatewayProvider.MESSENGER,
|
|
98
|
+
metadata: {
|
|
99
|
+
clientId: 'id',
|
|
100
|
+
clientSecret: 'secret',
|
|
101
|
+
version: '',
|
|
102
|
+
instagramComments: true,
|
|
103
|
+
instagramMentions: false,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const [, body] = instance.post.mock.calls[0];
|
|
109
|
+
expect(body.metadata).not.toHaveProperty('version');
|
|
110
|
+
expect(item.metadata).toMatchObject({
|
|
111
|
+
instagramComments: true,
|
|
112
|
+
instagramMentions: false,
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { ChatGatewayChannelSetup } from '../../../enums';
|
|
2
|
+
import { getDefaultInstance } from '../../defaults';
|
|
3
|
+
import {
|
|
4
|
+
applyTransform,
|
|
5
|
+
mergeEach,
|
|
6
|
+
notify,
|
|
7
|
+
snakeToCamel,
|
|
8
|
+
} from '../../transformers';
|
|
9
|
+
import type { AppUrls } from '../_shared/appUrls';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* [Claude] The channels a chat gateway connects through the chat service,
|
|
13
|
+
* addressed by the gateway's uri: telegram app sign-in, meta pages and accounts.
|
|
14
|
+
* The chat service lives outside the api base url, so the app gives its url;
|
|
15
|
+
* the default instance still adds the auth header to each request.
|
|
16
|
+
*/
|
|
17
|
+
const instance = getDefaultInstance();
|
|
18
|
+
|
|
19
|
+
export type ChatGatewayChannelParams = {
|
|
20
|
+
uri: string;
|
|
21
|
+
appUrls: Pick<AppUrls, 'chatUrl'>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type SubscriptionParams = ChatGatewayChannelParams & {
|
|
25
|
+
id: string;
|
|
26
|
+
value: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type ChatGatewayAccount = {
|
|
30
|
+
name?: string;
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type FacebookPage = {
|
|
35
|
+
id: string;
|
|
36
|
+
name?: string;
|
|
37
|
+
accounts: ChatGatewayAccount[];
|
|
38
|
+
subscribedFields?: string[];
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type InstagramPage = {
|
|
42
|
+
id: string;
|
|
43
|
+
name?: string;
|
|
44
|
+
accounts: ChatGatewayAccount[];
|
|
45
|
+
instagram?: {
|
|
46
|
+
username?: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type WhatsappPhoneNumber = {
|
|
51
|
+
verifiedName?: string;
|
|
52
|
+
displayPhoneNumber?: string;
|
|
53
|
+
nameStatus?: string;
|
|
54
|
+
status?: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type WhatsappAccount = {
|
|
58
|
+
id: string;
|
|
59
|
+
phoneNumbers: WhatsappPhoneNumber[];
|
|
60
|
+
subscribedFields?: string[];
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export type TelegramAppAuthState = {
|
|
64
|
+
authorized?: boolean;
|
|
65
|
+
account?: {
|
|
66
|
+
FirstName?: string;
|
|
67
|
+
LastName?: string;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const channelUrl = ({ uri, appUrls }: ChatGatewayChannelParams, query = '') =>
|
|
72
|
+
`${appUrls.chatUrl}${uri}${query ? `?${query}` : ''}`;
|
|
73
|
+
|
|
74
|
+
const getChannelList = async <Item>(
|
|
75
|
+
params: ChatGatewayChannelParams,
|
|
76
|
+
query: string,
|
|
77
|
+
defaultItem: object,
|
|
78
|
+
): Promise<Item[]> => {
|
|
79
|
+
try {
|
|
80
|
+
const response = await instance.get(channelUrl(params, query));
|
|
81
|
+
return applyTransform(response.data, [
|
|
82
|
+
snakeToCamel(),
|
|
83
|
+
mergeEach(defaultItem),
|
|
84
|
+
]);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
throw applyTransform(err, [
|
|
87
|
+
notify,
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const getSetupUrl = (
|
|
93
|
+
params: ChatGatewayChannelParams & {
|
|
94
|
+
setup: ChatGatewayChannelSetup;
|
|
95
|
+
},
|
|
96
|
+
) => channelUrl(params, params.setup);
|
|
97
|
+
|
|
98
|
+
const getTelegramAppAuth = async (
|
|
99
|
+
params: ChatGatewayChannelParams,
|
|
100
|
+
): Promise<TelegramAppAuthState> => {
|
|
101
|
+
const response = await instance.get(channelUrl(params));
|
|
102
|
+
return response.data;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const logoutTelegramApp = (params: ChatGatewayChannelParams) =>
|
|
106
|
+
instance.post(channelUrl(params, 'logout='), {});
|
|
107
|
+
|
|
108
|
+
const getFacebookPages = (params: ChatGatewayChannelParams) =>
|
|
109
|
+
getChannelList<FacebookPage>(params, 'pages=', {
|
|
110
|
+
accounts: [],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const updateFacebookPageSubscription = ({
|
|
114
|
+
id,
|
|
115
|
+
value,
|
|
116
|
+
...params
|
|
117
|
+
}: SubscriptionParams) =>
|
|
118
|
+
instance.get(
|
|
119
|
+
channelUrl(params, `pages=${value ? 'subscribe' : 'unsubscribe'}&id=${id}`),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const getInstagramPages = (params: ChatGatewayChannelParams) =>
|
|
123
|
+
getChannelList<InstagramPage>(params, 'instagram=', {
|
|
124
|
+
accounts: [],
|
|
125
|
+
instagram: {},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const getWhatsappAccounts = (params: ChatGatewayChannelParams) =>
|
|
129
|
+
getChannelList<WhatsappAccount>(params, 'whatsapp=', {
|
|
130
|
+
phoneNumbers: [],
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
const updateWhatsappSubscription = ({
|
|
134
|
+
id,
|
|
135
|
+
value,
|
|
136
|
+
...params
|
|
137
|
+
}: SubscriptionParams) =>
|
|
138
|
+
instance.get(
|
|
139
|
+
channelUrl(
|
|
140
|
+
params,
|
|
141
|
+
`whatsapp=${value ? 'subscribe' : 'unsubscribe'}&id=${id}`,
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
const deleteWhatsappAccount = ({
|
|
146
|
+
id,
|
|
147
|
+
...params
|
|
148
|
+
}: ChatGatewayChannelParams & {
|
|
149
|
+
id: string;
|
|
150
|
+
}) => instance.get(channelUrl(params, `whatsapp=remove&id=${id}&ack=true`));
|
|
151
|
+
|
|
152
|
+
export const ChatGatewayChannelsAPI = {
|
|
153
|
+
getSetupUrl,
|
|
154
|
+
getTelegramAppAuth,
|
|
155
|
+
logoutTelegramApp,
|
|
156
|
+
getFacebookPages,
|
|
157
|
+
updateFacebookPageSubscription,
|
|
158
|
+
getInstagramPages,
|
|
159
|
+
getWhatsappAccounts,
|
|
160
|
+
updateWhatsappSubscription,
|
|
161
|
+
deleteWhatsappAccount,
|
|
162
|
+
};
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
sanitize,
|
|
17
17
|
snakeToCamel,
|
|
18
18
|
starToSearch,
|
|
19
|
+
translateError,
|
|
19
20
|
} from '../../transformers';
|
|
20
21
|
import type {
|
|
21
22
|
AddItemParams,
|
|
@@ -25,7 +26,7 @@ import type {
|
|
|
25
26
|
PatchItemParams,
|
|
26
27
|
UpdateItemParams,
|
|
27
28
|
} from '../_shared/types';
|
|
28
|
-
import {
|
|
29
|
+
import { getChatGatewayDefaults } from './defaults/chatGatewayProviderDefaults';
|
|
29
30
|
|
|
30
31
|
const instance = getDefaultInstance();
|
|
31
32
|
|
|
@@ -87,6 +88,10 @@ const messengerRequestConverter = (data: ApiParams) => {
|
|
|
87
88
|
const copy = deepCopy(data);
|
|
88
89
|
copy.metadata.instagramComments = data.metadata.instagramComments.toString();
|
|
89
90
|
copy.metadata.instagramMentions = data.metadata.instagramMentions.toString();
|
|
91
|
+
// [Claude] an empty version is not sent, so the backend falls back to its default one
|
|
92
|
+
if (!copy.metadata.version) {
|
|
93
|
+
delete copy.metadata.version;
|
|
94
|
+
}
|
|
90
95
|
return copy;
|
|
91
96
|
};
|
|
92
97
|
|
|
@@ -139,7 +144,7 @@ const webChatResponseConverter = (data: ApiParams) => {
|
|
|
139
144
|
}
|
|
140
145
|
copy.metadata._btnCodeDirty = data.metadata._btnCodeDirty === 'true';
|
|
141
146
|
|
|
142
|
-
return
|
|
147
|
+
return copy;
|
|
143
148
|
};
|
|
144
149
|
|
|
145
150
|
const messengerResponseConverter = (item: ApiParams) => {
|
|
@@ -171,6 +176,56 @@ const preRequestHandler = (item: ApiParams) => {
|
|
|
171
176
|
}
|
|
172
177
|
};
|
|
173
178
|
|
|
179
|
+
const providerResponseConverter = (item: ApiParams) => {
|
|
180
|
+
switch (item.provider) {
|
|
181
|
+
case ChatGatewayProvider.WEBCHAT:
|
|
182
|
+
return webChatResponseConverter(item);
|
|
183
|
+
case ChatGatewayProvider.MESSENGER:
|
|
184
|
+
return messengerResponseConverter(item);
|
|
185
|
+
case ChatGatewayProvider.VIBER:
|
|
186
|
+
return viberResponseConverter(item);
|
|
187
|
+
default:
|
|
188
|
+
return item;
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const isEmptyLookup = (value: unknown) =>
|
|
193
|
+
!!value && typeof value === 'object' && !Object.keys(value).length;
|
|
194
|
+
|
|
195
|
+
/** [Claude] a stored `{}` lookup reads as unset, the way the defaults seed it */
|
|
196
|
+
const clearEmptyLookups = (item: ApiParams) => {
|
|
197
|
+
if (isEmptyLookup(item.flow)) item.flow = undefined;
|
|
198
|
+
const { appointment, call } = item.metadata;
|
|
199
|
+
if (appointment) {
|
|
200
|
+
if (isEmptyLookup(appointment.queue)) appointment.queue = undefined;
|
|
201
|
+
if (isEmptyLookup(appointment.communicationType)) {
|
|
202
|
+
appointment.communicationType = undefined;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (call && isEmptyLookup(call.flow)) call.flow = undefined;
|
|
206
|
+
return item;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* [Claude] The item as the card edits it: provider-specific fields parsed,
|
|
211
|
+
* then laid over the provider's defaults so every form field has a key.
|
|
212
|
+
* Used for the add/update responses too, as the card takes those as its new state.
|
|
213
|
+
*/
|
|
214
|
+
const toChatGatewayItem = (data: ApiParams) => {
|
|
215
|
+
const item = applyTransform(data, [
|
|
216
|
+
snakeToCamel(),
|
|
217
|
+
]);
|
|
218
|
+
const converted = providerResponseConverter({
|
|
219
|
+
...item,
|
|
220
|
+
metadata: item.metadata ?? {},
|
|
221
|
+
});
|
|
222
|
+
return clearEmptyLookups(
|
|
223
|
+
deepmerge(getChatGatewayDefaults(converted.provider), converted, {
|
|
224
|
+
arrayMerge: (_target, source) => source,
|
|
225
|
+
}),
|
|
226
|
+
);
|
|
227
|
+
};
|
|
228
|
+
|
|
174
229
|
const getChatGatewayList = async (params: ApiParams) => {
|
|
175
230
|
const fieldsToSend = [
|
|
176
231
|
'page',
|
|
@@ -195,6 +250,7 @@ const getChatGatewayList = async (params: ApiParams) => {
|
|
|
195
250
|
close: '',
|
|
196
251
|
join: '',
|
|
197
252
|
left: '',
|
|
253
|
+
filePolicyFail: '',
|
|
198
254
|
},
|
|
199
255
|
};
|
|
200
256
|
|
|
@@ -229,27 +285,11 @@ const getChatGatewayList = async (params: ApiParams) => {
|
|
|
229
285
|
};
|
|
230
286
|
|
|
231
287
|
const getChatGateway = async ({ itemId: id }: GetItemParams) => {
|
|
232
|
-
const itemResponseHandler = (response: ApiParams) => {
|
|
233
|
-
switch (response.provider) {
|
|
234
|
-
case ChatGatewayProvider.WEBCHAT:
|
|
235
|
-
return webChatResponseConverter(response);
|
|
236
|
-
case ChatGatewayProvider.MESSENGER:
|
|
237
|
-
return messengerResponseConverter(response);
|
|
238
|
-
case ChatGatewayProvider.VIBER:
|
|
239
|
-
return viberResponseConverter(response);
|
|
240
|
-
default:
|
|
241
|
-
return response;
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
|
|
245
288
|
const url = `${baseUrl}/${id}`;
|
|
246
289
|
|
|
247
290
|
try {
|
|
248
291
|
const response = await instance.get(url);
|
|
249
|
-
return
|
|
250
|
-
snakeToCamel(),
|
|
251
|
-
itemResponseHandler,
|
|
252
|
-
]);
|
|
292
|
+
return toChatGatewayItem(response.data);
|
|
253
293
|
} catch (err) {
|
|
254
294
|
throw applyTransform(err, [
|
|
255
295
|
notify,
|
|
@@ -265,11 +305,10 @@ const addChatGateway = async ({ itemInstance }: AddItemParams) => {
|
|
|
265
305
|
]);
|
|
266
306
|
try {
|
|
267
307
|
const response = await instance.post(baseUrl, item);
|
|
268
|
-
return
|
|
269
|
-
snakeToCamel(),
|
|
270
|
-
]);
|
|
308
|
+
return toChatGatewayItem(response.data);
|
|
271
309
|
} catch (err) {
|
|
272
310
|
throw applyTransform(err, [
|
|
311
|
+
translateError,
|
|
273
312
|
notify,
|
|
274
313
|
]);
|
|
275
314
|
}
|
|
@@ -288,11 +327,10 @@ const updateChatGateway = async ({
|
|
|
288
327
|
const url = `${baseUrl}/${id}`;
|
|
289
328
|
try {
|
|
290
329
|
const response = await instance.put(url, item);
|
|
291
|
-
return
|
|
292
|
-
snakeToCamel(),
|
|
293
|
-
]);
|
|
330
|
+
return toChatGatewayItem(response.data);
|
|
294
331
|
} catch (err) {
|
|
295
332
|
throw applyTransform(err, [
|
|
333
|
+
translateError,
|
|
296
334
|
notify,
|
|
297
335
|
]);
|
|
298
336
|
}
|
|
@@ -311,6 +349,7 @@ const patchChatGateway = async ({ changes, id }: PatchItemParams) => {
|
|
|
311
349
|
]);
|
|
312
350
|
} catch (err) {
|
|
313
351
|
throw applyTransform(err, [
|
|
352
|
+
translateError,
|
|
314
353
|
notify,
|
|
315
354
|
]);
|
|
316
355
|
}
|
|
@@ -349,3 +388,5 @@ export const ChatGatewaysAPI = {
|
|
|
349
388
|
delete: deleteChatGateway,
|
|
350
389
|
getLookup,
|
|
351
390
|
};
|
|
391
|
+
|
|
392
|
+
export { getChatGatewayDefaults } from './defaults/chatGatewayProviderDefaults';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ChatGatewayProvider } from '../../../../enums';
|
|
2
|
+
import { defaultChatGateway } from './defaultChatGateway';
|
|
3
|
+
import { webChatGateway } from './webChatGateway';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* [Claude] Every metadata key a provider's form can touch is seeded here, on
|
|
7
|
+
* create and on read: regle builds its `$fields` from the state keys, so a
|
|
8
|
+
* key missing from the draft has no validation status in the form.
|
|
9
|
+
*/
|
|
10
|
+
const telegramBotChatGateway = () => ({
|
|
11
|
+
...defaultChatGateway(),
|
|
12
|
+
provider: ChatGatewayProvider.TELEGRAM_BOT,
|
|
13
|
+
metadata: {
|
|
14
|
+
token: '',
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const telegramAppChatGateway = () => ({
|
|
19
|
+
...defaultChatGateway(),
|
|
20
|
+
provider: ChatGatewayProvider.TELEGRAM_APP,
|
|
21
|
+
metadata: {
|
|
22
|
+
apiId: '',
|
|
23
|
+
apiHash: '',
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const infobipChatGateway = () => ({
|
|
28
|
+
...defaultChatGateway(),
|
|
29
|
+
provider: ChatGatewayProvider.INFOBIP,
|
|
30
|
+
metadata: {
|
|
31
|
+
apiKey: '',
|
|
32
|
+
url: '',
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const messengerChatGateway = () => ({
|
|
37
|
+
...defaultChatGateway(),
|
|
38
|
+
provider: ChatGatewayProvider.MESSENGER,
|
|
39
|
+
metadata: {
|
|
40
|
+
clientId: '',
|
|
41
|
+
clientSecret: '',
|
|
42
|
+
version: '',
|
|
43
|
+
instagramComments: false,
|
|
44
|
+
instagramMentions: false,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
export const VIBER_BTN_BACK_COLOR = '#1d2733';
|
|
49
|
+
export const VIBER_BTN_FONT_COLOR = '#ffffff';
|
|
50
|
+
|
|
51
|
+
const viberChatGateway = () => ({
|
|
52
|
+
...defaultChatGateway(),
|
|
53
|
+
provider: ChatGatewayProvider.VIBER,
|
|
54
|
+
metadata: {
|
|
55
|
+
token: '',
|
|
56
|
+
botName: '',
|
|
57
|
+
btnBackColor: VIBER_BTN_BACK_COLOR,
|
|
58
|
+
btnFontColor: VIBER_BTN_FONT_COLOR,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const customChatGateway = () => ({
|
|
63
|
+
...defaultChatGateway(),
|
|
64
|
+
provider: ChatGatewayProvider.CUSTOM,
|
|
65
|
+
metadata: {
|
|
66
|
+
secret: '',
|
|
67
|
+
webhook: '',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const chatGatewayProviderDefaults: Record<
|
|
72
|
+
string,
|
|
73
|
+
() => ReturnType<typeof defaultChatGateway>
|
|
74
|
+
> = {
|
|
75
|
+
[ChatGatewayProvider.TELEGRAM_BOT]: telegramBotChatGateway,
|
|
76
|
+
[ChatGatewayProvider.TELEGRAM_APP]: telegramAppChatGateway,
|
|
77
|
+
[ChatGatewayProvider.INFOBIP]: infobipChatGateway,
|
|
78
|
+
[ChatGatewayProvider.MESSENGER]: messengerChatGateway,
|
|
79
|
+
[ChatGatewayProvider.VIBER]: viberChatGateway,
|
|
80
|
+
[ChatGatewayProvider.WEBCHAT]: webChatGateway,
|
|
81
|
+
[ChatGatewayProvider.CUSTOM]: customChatGateway,
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* [Claude] The field set a chat gateway of `provider` is created with.
|
|
86
|
+
* An unknown provider gets the provider-agnostic base.
|
|
87
|
+
*/
|
|
88
|
+
export const getChatGatewayDefaults = (provider?: string) => {
|
|
89
|
+
const defaults = provider ? chatGatewayProviderDefaults[provider] : undefined;
|
|
90
|
+
return defaults ? defaults() : defaultChatGateway();
|
|
91
|
+
};
|
|
@@ -7,7 +7,8 @@ import { generateUri } from '../scripts/generateUri';
|
|
|
7
7
|
export const defaultChatGateway = () => ({
|
|
8
8
|
name: '',
|
|
9
9
|
uri: generateUri(),
|
|
10
|
-
|
|
10
|
+
/** [Claude] undefined, not {}: regle gives an empty object no field status, so its required error would not show */
|
|
11
|
+
flow: undefined,
|
|
11
12
|
enabled: false,
|
|
12
13
|
provider: '',
|
|
13
14
|
metadata: {},
|
|
@@ -16,5 +17,6 @@ export const defaultChatGateway = () => ({
|
|
|
16
17
|
close: '',
|
|
17
18
|
join: '',
|
|
18
19
|
left: '',
|
|
20
|
+
filePolicyFail: '',
|
|
19
21
|
},
|
|
20
22
|
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ChatGatewayProvider,
|
|
3
|
+
WebchatAlternativeChannel,
|
|
4
|
+
} from '@webitel/api-services/enums';
|
|
2
5
|
import { defaultChatGateway } from '../defaults/defaultChatGateway';
|
|
3
|
-
import { WebchatAlternativeChannel } from '../enums/WebchatAlternativeChannel.enum';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* Creates a default webchat gateway configuration
|
|
@@ -44,8 +46,9 @@ export const webChatGateway = (_btnCodeDirty = false) => ({
|
|
|
44
46
|
appointment: {
|
|
45
47
|
enabled: false,
|
|
46
48
|
url: '',
|
|
47
|
-
|
|
48
|
-
|
|
49
|
+
// [Claude] lookups start undefined so their required error has a field to show on
|
|
50
|
+
queue: undefined,
|
|
51
|
+
communicationType: undefined,
|
|
49
52
|
duration: '30m',
|
|
50
53
|
days: 3,
|
|
51
54
|
availableAgents: 1,
|
|
@@ -58,7 +61,7 @@ export const webChatGateway = (_btnCodeDirty = false) => ({
|
|
|
58
61
|
call: {
|
|
59
62
|
enabled: false,
|
|
60
63
|
url: '',
|
|
61
|
-
flow:
|
|
64
|
+
flow: undefined,
|
|
62
65
|
id: '',
|
|
63
66
|
},
|
|
64
67
|
alternativeChannels: Object.values(WebchatAlternativeChannel).reduce(
|