@webitel/api-services 26.12.21 → 26.12.23
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/cognitiveProfiles/cognitiveProfiles.ts +8 -32
- package/src/api/clients/cognitiveProfiles/defaults/cognitiveProfileDefaults.ts +39 -0
- package/src/api/clients/emailProfiles/emailProfiles.ts +6 -19
- package/src/api/clients/gateways/__tests__/gateways.spec.ts +138 -0
- package/src/api/clients/gateways/gateways.ts +60 -52
- package/src/api/clients/importTemplates/importTemplates.ts +36 -21
- package/src/api/clients/index.ts +2 -0
- package/src/api/clients/storagePolicies/storagePolicies.ts +24 -36
- package/src/api/clients/triggers/triggerJobs.ts +22 -12
- package/src/api/clients/triggers/triggers.ts +12 -37
- 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/scripts/convertDuration/__tests__/convertDurationWithMilliseconds.spec.ts +19 -0
- package/src/scripts/convertDuration/convertDurationWithMilliseconds.ts +18 -0
- package/src/scripts/index.ts +2 -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/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/emailProfile/__tests__/emailProfile.validations.spec.ts +105 -0
- package/src/validations/emailProfile/emailProfile.validations.ts +113 -0
- package/src/validations/gateway/__tests__/gateway.validations.spec.ts +168 -0
- package/src/validations/gateway/gateway.validations.ts +138 -0
- package/src/validations/importTemplate/__tests__/importTemplate.validations.spec.ts +77 -0
- package/src/validations/importTemplate/importTemplate.validations.ts +56 -0
- package/src/validations/index.ts +8 -0
- package/src/validations/storagePolicy/__tests__/storagePolicy.validations.spec.ts +52 -0
- package/src/validations/storagePolicy/storagePolicy.validations.ts +36 -0
- package/src/validations/trigger/__tests__/trigger.validations.spec.ts +92 -0
- package/src/validations/trigger/trigger.validations.ts +82 -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/importTemplates/importTemplates.d.ts +2 -2
- 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/scripts/convertDuration/convertDurationWithMilliseconds.d.ts +6 -0
- package/types/scripts/index.d.ts +2 -1
- 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/cognitiveProfile/cognitiveProfile.validations.d.ts +28 -0
- package/types/validations/emailProfile/emailProfile.validations.d.ts +36 -0
- package/types/validations/gateway/gateway.validations.d.ts +48 -0
- package/types/validations/importTemplate/importTemplate.validations.d.ts +62 -0
- package/types/validations/index.d.ts +8 -0
- package/types/validations/storagePolicy/storagePolicy.validations.d.ts +26 -0
- package/types/validations/trigger/trigger.validations.d.ts +38 -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
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type StorageCognitiveProfile,
|
|
3
|
+
StorageProviderType,
|
|
4
|
+
} from '@webitel/api-services/gen/models';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
|
|
7
|
+
import { i18nIssue } from '../_shared/i18nIssue';
|
|
8
|
+
import { isFilled } from '../_shared/isFilled';
|
|
9
|
+
|
|
10
|
+
/** [Claude] the generated model leaves out `properties`, the provider's own settings */
|
|
11
|
+
export type CognitiveProfileCard = StorageCognitiveProfile & {
|
|
12
|
+
properties?: Record<string, unknown>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* [Claude] Keys are optional here: which ones a provider requires is checked
|
|
17
|
+
* in `superRefine` below. A google key is the parsed json of its key file.
|
|
18
|
+
*/
|
|
19
|
+
const cognitiveProfilePropertiesSchema = z
|
|
20
|
+
.object({
|
|
21
|
+
key: z.unknown().optional(),
|
|
22
|
+
keyFilename: z.string().optional(),
|
|
23
|
+
region: z.string().optional(),
|
|
24
|
+
locale: z.string().optional(),
|
|
25
|
+
})
|
|
26
|
+
.passthrough();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* [Claude] Cognitive profile card schema.
|
|
30
|
+
*
|
|
31
|
+
* A saved key is not sent back, so an empty key on update means "keep the
|
|
32
|
+
* current one": microsoft and elevenlabs require it only when creating. A
|
|
33
|
+
* google key comes from a file, so it is required until a file is chosen.
|
|
34
|
+
*/
|
|
35
|
+
export const cognitiveProfileSchema = z
|
|
36
|
+
.object({
|
|
37
|
+
id: z.string().optional(),
|
|
38
|
+
name: z.string().min(1).default(''),
|
|
39
|
+
provider: z.string().min(1),
|
|
40
|
+
service: z.string().min(1),
|
|
41
|
+
default: z.boolean().default(false),
|
|
42
|
+
enabled: z.boolean().default(true),
|
|
43
|
+
description: z.string().optional(),
|
|
44
|
+
properties: cognitiveProfilePropertiesSchema.prefault({}),
|
|
45
|
+
})
|
|
46
|
+
.passthrough()
|
|
47
|
+
.superRefine((profile, ctx) => {
|
|
48
|
+
const { properties } = profile;
|
|
49
|
+
|
|
50
|
+
const requireProperty = (key: string) => {
|
|
51
|
+
if (isFilled(properties[key])) return;
|
|
52
|
+
ctx.addIssue({
|
|
53
|
+
code: 'custom',
|
|
54
|
+
path: [
|
|
55
|
+
'properties',
|
|
56
|
+
key,
|
|
57
|
+
],
|
|
58
|
+
...i18nIssue('required'),
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
switch (profile.provider) {
|
|
63
|
+
case StorageProviderType.Microsoft:
|
|
64
|
+
if (!profile.id) requireProperty('key');
|
|
65
|
+
requireProperty('region');
|
|
66
|
+
break;
|
|
67
|
+
case StorageProviderType.ElevenLabs:
|
|
68
|
+
if (!profile.id) requireProperty('key');
|
|
69
|
+
break;
|
|
70
|
+
case StorageProviderType.Google:
|
|
71
|
+
if (!properties.keyFilename) requireProperty('key');
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
@@ -2,23 +2,17 @@ import type { ApiDevice } from '@webitel/api-services/gen/models';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
4
|
import { i18nIssue } from '../_shared/i18nIssue';
|
|
5
|
+
import { isIp } from '../_shared/ip.validations';
|
|
5
6
|
import { flexibleLookupSchema } from '../_shared/lookup.validations';
|
|
6
7
|
import type { ZodShape } from '../types';
|
|
7
8
|
|
|
8
|
-
const ipv4Regex =
|
|
9
|
-
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
|
|
10
|
-
|
|
11
9
|
const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/;
|
|
12
10
|
|
|
13
11
|
const optionalIpSchema = z
|
|
14
12
|
.string()
|
|
15
13
|
.optional()
|
|
16
14
|
.default('')
|
|
17
|
-
.refine((
|
|
18
|
-
if (!value) return true;
|
|
19
|
-
if (value === '127.0.0.1' || value === '0.0.0.0') return false;
|
|
20
|
-
return ipv4Regex.test(value);
|
|
21
|
-
}, i18nIssue('ipValidator'));
|
|
15
|
+
.refine(isIp, i18nIssue('ipValidator'));
|
|
22
16
|
|
|
23
17
|
const optionalMacSchema = z
|
|
24
18
|
.string()
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { EngineEmailAuthType } from '@webitel/api-services/gen/models';
|
|
2
|
+
import { getDefaultsFromZodSchema } from '@webitel/api-services/gen/utils';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import { emailProfileSchema } from '../emailProfile.validations';
|
|
6
|
+
|
|
7
|
+
const issuePaths = (result: ReturnType<typeof emailProfileSchema.safeParse>) =>
|
|
8
|
+
result.error?.issues.map(({ path }) => path.join('.')) ?? [];
|
|
9
|
+
|
|
10
|
+
const filled = {
|
|
11
|
+
name: 'support',
|
|
12
|
+
mailbox: 'support@example.com',
|
|
13
|
+
imapHost: 'imap.example.com',
|
|
14
|
+
smtpHost: 'smtp.example.com',
|
|
15
|
+
login: 'support@example.com',
|
|
16
|
+
password: 'secret',
|
|
17
|
+
schema: {
|
|
18
|
+
id: '1',
|
|
19
|
+
name: 'flow',
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
describe('emailProfileSchema', () => {
|
|
24
|
+
it('seeds a new profile with plain auth and the usual ports', () => {
|
|
25
|
+
expect(getDefaultsFromZodSchema(emailProfileSchema, {})).toMatchObject({
|
|
26
|
+
enabled: true,
|
|
27
|
+
imapPort: 993,
|
|
28
|
+
smtpPort: 587,
|
|
29
|
+
fetchInterval: 60,
|
|
30
|
+
listen: false,
|
|
31
|
+
authType: EngineEmailAuthType.Plain,
|
|
32
|
+
params: {
|
|
33
|
+
oauth2: {
|
|
34
|
+
clientId: '',
|
|
35
|
+
clientSecret: '',
|
|
36
|
+
redirectUrl: '',
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('requires the connection settings, the flow and a password for a new profile', () => {
|
|
43
|
+
const paths = issuePaths(
|
|
44
|
+
emailProfileSchema.safeParse(
|
|
45
|
+
getDefaultsFromZodSchema(emailProfileSchema, {}),
|
|
46
|
+
),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(paths).toEqual(
|
|
50
|
+
expect.arrayContaining([
|
|
51
|
+
'name',
|
|
52
|
+
'mailbox',
|
|
53
|
+
'imapHost',
|
|
54
|
+
'smtpHost',
|
|
55
|
+
'login',
|
|
56
|
+
'schema.id',
|
|
57
|
+
'password',
|
|
58
|
+
]),
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('does not require the password of a saved profile', () => {
|
|
63
|
+
const result = emailProfileSchema.safeParse({
|
|
64
|
+
...getDefaultsFromZodSchema(emailProfileSchema, {}),
|
|
65
|
+
...filled,
|
|
66
|
+
id: '1',
|
|
67
|
+
password: '',
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(result.success).toBe(true);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('requires the client settings instead of a password for oauth2', () => {
|
|
74
|
+
const paths = issuePaths(
|
|
75
|
+
emailProfileSchema.safeParse({
|
|
76
|
+
...getDefaultsFromZodSchema(emailProfileSchema, {}),
|
|
77
|
+
...filled,
|
|
78
|
+
password: '',
|
|
79
|
+
authType: EngineEmailAuthType.OAuth2,
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(paths).toEqual([
|
|
84
|
+
'params.oauth2.clientId',
|
|
85
|
+
'params.oauth2.clientSecret',
|
|
86
|
+
'params.oauth2.redirectUrl',
|
|
87
|
+
]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('rejects a port out of range and an emptied one', () => {
|
|
91
|
+
const paths = issuePaths(
|
|
92
|
+
emailProfileSchema.safeParse({
|
|
93
|
+
...getDefaultsFromZodSchema(emailProfileSchema, {}),
|
|
94
|
+
...filled,
|
|
95
|
+
imapPort: 70000,
|
|
96
|
+
smtpPort: null,
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
expect(paths).toEqual([
|
|
101
|
+
'imapPort',
|
|
102
|
+
'smtpPort',
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EngineEmailAuthType,
|
|
3
|
+
type EngineEmailProfile,
|
|
4
|
+
} from '@webitel/api-services/gen/models';
|
|
5
|
+
import { get } from 'lodash-es';
|
|
6
|
+
import { z } from 'zod';
|
|
7
|
+
|
|
8
|
+
import { clearableNumberSchema } from '../_shared/clearableNumber.validations';
|
|
9
|
+
import { i18nIssue } from '../_shared/i18nIssue';
|
|
10
|
+
import { isFilled } from '../_shared/isFilled';
|
|
11
|
+
import { filledLookupSchema } from '../_shared/lookup.validations';
|
|
12
|
+
import type { ZodShape } from '../types';
|
|
13
|
+
|
|
14
|
+
/** [Claude] the highest tcp port, the limit of the imap and smtp port fields */
|
|
15
|
+
export const EMAIL_PROFILE_MAX_PORT = 65535;
|
|
16
|
+
|
|
17
|
+
const oauth2Fields = [
|
|
18
|
+
'clientId',
|
|
19
|
+
'clientSecret',
|
|
20
|
+
'redirectUrl',
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
const oauth2Schema = z
|
|
24
|
+
.object({
|
|
25
|
+
clientId: z.string().optional().default(''),
|
|
26
|
+
clientSecret: z.string().optional().default(''),
|
|
27
|
+
redirectUrl: z.string().optional().default(''),
|
|
28
|
+
})
|
|
29
|
+
.passthrough();
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* [Claude] Email profile card schema.
|
|
33
|
+
*
|
|
34
|
+
* Plain auth signs in with a password, oauth2 with the app's client settings.
|
|
35
|
+
* The password is required only when creating: an empty one on update means
|
|
36
|
+
* "keep the current one".
|
|
37
|
+
*/
|
|
38
|
+
export const emailProfileSchema = z
|
|
39
|
+
.object<ZodShape<EngineEmailProfile>>({
|
|
40
|
+
id: z.string().optional(),
|
|
41
|
+
name: z.string().min(1).default(''),
|
|
42
|
+
description: z.string().optional().default(''),
|
|
43
|
+
enabled: z.boolean().default(true),
|
|
44
|
+
mailbox: z.string().min(1).default(''),
|
|
45
|
+
imapHost: z.string().min(1).default(''),
|
|
46
|
+
imapPort: clearableNumberSchema.default(993),
|
|
47
|
+
smtpHost: z.string().min(1).default(''),
|
|
48
|
+
smtpPort: clearableNumberSchema.default(587),
|
|
49
|
+
// [Claude] `.prefault`, not `.default`: zod 4's `.default({})` would skip the required check
|
|
50
|
+
schema: filledLookupSchema.prefault({}),
|
|
51
|
+
fetchInterval: clearableNumberSchema.default(60),
|
|
52
|
+
listen: z.boolean().default(false),
|
|
53
|
+
logged: z.boolean().default(false),
|
|
54
|
+
authType: z.string().min(1).default(EngineEmailAuthType.Plain),
|
|
55
|
+
login: z.string().min(1).default(''),
|
|
56
|
+
password: z.string().optional().default(''),
|
|
57
|
+
params: z
|
|
58
|
+
.object({
|
|
59
|
+
oauth2: oauth2Schema.prefault({}),
|
|
60
|
+
})
|
|
61
|
+
.passthrough()
|
|
62
|
+
.prefault({}),
|
|
63
|
+
})
|
|
64
|
+
.passthrough()
|
|
65
|
+
.superRefine((profile, ctx) => {
|
|
66
|
+
const requireField = (path: string) => {
|
|
67
|
+
if (isFilled(get(profile, path))) return;
|
|
68
|
+
ctx.addIssue({
|
|
69
|
+
code: 'custom',
|
|
70
|
+
path: path.split('.'),
|
|
71
|
+
...i18nIssue('required'),
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
requireField('fetchInterval');
|
|
76
|
+
|
|
77
|
+
for (const path of [
|
|
78
|
+
'imapPort',
|
|
79
|
+
'smtpPort',
|
|
80
|
+
]) {
|
|
81
|
+
requireField(path);
|
|
82
|
+
const port = get(profile, path);
|
|
83
|
+
if (
|
|
84
|
+
typeof port === 'number' &&
|
|
85
|
+
(port < 0 || port > EMAIL_PROFILE_MAX_PORT)
|
|
86
|
+
) {
|
|
87
|
+
ctx.addIssue({
|
|
88
|
+
code: port < 0 ? 'too_small' : 'too_big',
|
|
89
|
+
origin: 'number',
|
|
90
|
+
...(port < 0
|
|
91
|
+
? {
|
|
92
|
+
minimum: 0,
|
|
93
|
+
}
|
|
94
|
+
: {
|
|
95
|
+
maximum: EMAIL_PROFILE_MAX_PORT,
|
|
96
|
+
}),
|
|
97
|
+
inclusive: true,
|
|
98
|
+
input: port,
|
|
99
|
+
path: [
|
|
100
|
+
path,
|
|
101
|
+
],
|
|
102
|
+
} as Parameters<typeof ctx.addIssue>[0]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (profile.authType === EngineEmailAuthType.OAuth2) {
|
|
107
|
+
for (const field of oauth2Fields) {
|
|
108
|
+
requireField(`params.oauth2.${field}`);
|
|
109
|
+
}
|
|
110
|
+
} else if (!profile.id) {
|
|
111
|
+
requireField('password');
|
|
112
|
+
}
|
|
113
|
+
});
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { gatewaySchema } from '../gateway.validations';
|
|
4
|
+
|
|
5
|
+
const issuePaths = (value: unknown) => {
|
|
6
|
+
const result = gatewaySchema.safeParse(value);
|
|
7
|
+
return result.success
|
|
8
|
+
? []
|
|
9
|
+
: result.error.issues.map(({ path }) => path.join('.'));
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const registerGateway = {
|
|
13
|
+
register: true,
|
|
14
|
+
name: 'reg',
|
|
15
|
+
account: 'sip:user@example.com',
|
|
16
|
+
password: 'secret',
|
|
17
|
+
proxy: 'sip.example.com',
|
|
18
|
+
expires: 600,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const trunkingGateway = {
|
|
22
|
+
register: false,
|
|
23
|
+
name: 'trunk',
|
|
24
|
+
proxy: '10.0.0.1',
|
|
25
|
+
host: 'sip:10.0.0.2:5060',
|
|
26
|
+
ipacl: [
|
|
27
|
+
{
|
|
28
|
+
ip: '10.0.0.3',
|
|
29
|
+
proto: 'udp',
|
|
30
|
+
port: 5060,
|
|
31
|
+
},
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe('gatewaySchema', () => {
|
|
36
|
+
it('fills defaults for a new gateway', () => {
|
|
37
|
+
expect(gatewaySchema.parse(registerGateway)).toMatchObject({
|
|
38
|
+
enable: true,
|
|
39
|
+
schema: {},
|
|
40
|
+
ipacl: [],
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('register', () => {
|
|
45
|
+
it('accepts a valid gateway', () => {
|
|
46
|
+
expect(issuePaths(registerGateway)).toEqual([]);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('requires account, proxy and password on create', () => {
|
|
50
|
+
expect(
|
|
51
|
+
issuePaths({
|
|
52
|
+
register: true,
|
|
53
|
+
}),
|
|
54
|
+
).toEqual([
|
|
55
|
+
'proxy',
|
|
56
|
+
'account',
|
|
57
|
+
'password',
|
|
58
|
+
]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('keeps the stored password on update', () => {
|
|
62
|
+
expect(
|
|
63
|
+
issuePaths({
|
|
64
|
+
...registerGateway,
|
|
65
|
+
id: '1',
|
|
66
|
+
password: '',
|
|
67
|
+
}),
|
|
68
|
+
).toEqual([]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it.each([
|
|
72
|
+
31,
|
|
73
|
+
3601,
|
|
74
|
+
null,
|
|
75
|
+
])('rejects expires %j', (expires) => {
|
|
76
|
+
expect(
|
|
77
|
+
issuePaths({
|
|
78
|
+
...registerGateway,
|
|
79
|
+
expires,
|
|
80
|
+
}),
|
|
81
|
+
).toEqual([
|
|
82
|
+
'expires',
|
|
83
|
+
]);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('rejects a password with special symbols', () => {
|
|
87
|
+
expect(
|
|
88
|
+
issuePaths({
|
|
89
|
+
...registerGateway,
|
|
90
|
+
password: 'pa$$ word',
|
|
91
|
+
}),
|
|
92
|
+
).toEqual([
|
|
93
|
+
'password',
|
|
94
|
+
]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('does not require trunking fields', () => {
|
|
98
|
+
expect(
|
|
99
|
+
issuePaths({
|
|
100
|
+
...registerGateway,
|
|
101
|
+
host: '',
|
|
102
|
+
}),
|
|
103
|
+
).toEqual([]);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('trunking', () => {
|
|
108
|
+
it('accepts a valid gateway', () => {
|
|
109
|
+
expect(issuePaths(trunkingGateway)).toEqual([]);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('requires host, proxy and every acl ip', () => {
|
|
113
|
+
expect(
|
|
114
|
+
issuePaths({
|
|
115
|
+
register: false,
|
|
116
|
+
ipacl: [
|
|
117
|
+
{
|
|
118
|
+
ip: '',
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
}),
|
|
122
|
+
).toEqual([
|
|
123
|
+
'ipacl.0.ip',
|
|
124
|
+
'proxy',
|
|
125
|
+
'host',
|
|
126
|
+
]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it.each([
|
|
130
|
+
'127.0.0.1',
|
|
131
|
+
'0.0.0.0',
|
|
132
|
+
'10.0.0',
|
|
133
|
+
])('rejects acl ip %j', (ip) => {
|
|
134
|
+
expect(
|
|
135
|
+
issuePaths({
|
|
136
|
+
...trunkingGateway,
|
|
137
|
+
ipacl: [
|
|
138
|
+
{
|
|
139
|
+
ip,
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
}),
|
|
143
|
+
).toEqual([
|
|
144
|
+
'ipacl.0.ip',
|
|
145
|
+
]);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('rejects a host that is neither ip nor fqdn', () => {
|
|
149
|
+
expect(
|
|
150
|
+
issuePaths({
|
|
151
|
+
...trunkingGateway,
|
|
152
|
+
host: 'not a host',
|
|
153
|
+
}),
|
|
154
|
+
).toEqual([
|
|
155
|
+
'host',
|
|
156
|
+
]);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('does not check registration fields', () => {
|
|
160
|
+
expect(
|
|
161
|
+
issuePaths({
|
|
162
|
+
...trunkingGateway,
|
|
163
|
+
expires: 0,
|
|
164
|
+
}),
|
|
165
|
+
).toEqual([]);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { GatewayIpAclProtocol } from '../../enums/GatewayIpAclProtocol/GatewayIpAclProtocol';
|
|
4
|
+
|
|
5
|
+
import { clearableNumberSchema } from '../_shared/clearableNumber.validations';
|
|
6
|
+
import { i18nIssue } from '../_shared/i18nIssue';
|
|
7
|
+
import { ipv4Pattern, isIp } from '../_shared/ip.validations';
|
|
8
|
+
import { isFilled } from '../_shared/isFilled';
|
|
9
|
+
import { lookupSchema } from '../_shared/lookup.validations';
|
|
10
|
+
|
|
11
|
+
const gatewayIpHostRegex = new RegExp(
|
|
12
|
+
`^(sips?:)?${ipv4Pattern}(:[0-9]{1,5})?$`,
|
|
13
|
+
);
|
|
14
|
+
const gatewayFqdnHostRegex =
|
|
15
|
+
/^(sips?:)?(?=.{1,254}$)((?=[a-z0-9-]{1,63}\.)(xn--+)?[a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,63}/i;
|
|
16
|
+
const sipAccountRegex =
|
|
17
|
+
/^(sips?:)?([\w\-.!~*()&=+$,;?/]+)(?:@([\w\-.]+)(?::(\d{3,5}))?)?$/i;
|
|
18
|
+
const sipPasswordRegex = /^[A-Za-z0-9\-_.~]+$/;
|
|
19
|
+
|
|
20
|
+
/** [Claude] format rule only: an empty value passes, required is checked per gateway type */
|
|
21
|
+
const isGatewayHost = (value: string) =>
|
|
22
|
+
!value || gatewayIpHostRegex.test(value) || gatewayFqdnHostRegex.test(value);
|
|
23
|
+
|
|
24
|
+
const gatewayHostSchema = z
|
|
25
|
+
.string()
|
|
26
|
+
.optional()
|
|
27
|
+
.default('')
|
|
28
|
+
.refine(isGatewayHost, i18nIssue('gatewayHostValidator'));
|
|
29
|
+
|
|
30
|
+
export const gatewayIpAclSchema = z.object({
|
|
31
|
+
ip: z
|
|
32
|
+
.string()
|
|
33
|
+
.refine(isFilled, i18nIssue('required'))
|
|
34
|
+
.refine(isIp, i18nIssue('ipValidator')),
|
|
35
|
+
proto: z.enum(GatewayIpAclProtocol).default(GatewayIpAclProtocol.Any),
|
|
36
|
+
port: clearableNumberSchema,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const GATEWAY_EXPIRES_MIN = 32;
|
|
40
|
+
export const GATEWAY_EXPIRES_MAX = 3600;
|
|
41
|
+
|
|
42
|
+
/** [Claude] checked only for a registration: trunks don't use expires and may come with 0 */
|
|
43
|
+
const expiresRangeSchema = z
|
|
44
|
+
.number()
|
|
45
|
+
.min(GATEWAY_EXPIRES_MIN)
|
|
46
|
+
.max(GATEWAY_EXPIRES_MAX);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* [Claude] One schema for both gateway types, `register` tells them apart:
|
|
50
|
+
* a SIP registration needs account, password (on create) and expires,
|
|
51
|
+
* a SIP trunk needs host and an ip for every ACL entry.
|
|
52
|
+
*/
|
|
53
|
+
export const gatewaySchema = z
|
|
54
|
+
.object({
|
|
55
|
+
id: z
|
|
56
|
+
.union([
|
|
57
|
+
z.string(),
|
|
58
|
+
z.number(),
|
|
59
|
+
])
|
|
60
|
+
.optional(),
|
|
61
|
+
name: z.string().default(''),
|
|
62
|
+
register: z.boolean().default(false),
|
|
63
|
+
enable: z.boolean().default(true),
|
|
64
|
+
proxy: gatewayHostSchema,
|
|
65
|
+
schema: lookupSchema.optional().default({}),
|
|
66
|
+
usage: z.string().default(''),
|
|
67
|
+
// [Claude] registration
|
|
68
|
+
account: z
|
|
69
|
+
.string()
|
|
70
|
+
.optional()
|
|
71
|
+
.default('')
|
|
72
|
+
.refine(
|
|
73
|
+
(value) => !value || sipAccountRegex.test(value),
|
|
74
|
+
i18nIssue('sipAccountValidator'),
|
|
75
|
+
),
|
|
76
|
+
username: z.string().default(''),
|
|
77
|
+
password: z
|
|
78
|
+
.string()
|
|
79
|
+
.optional()
|
|
80
|
+
.default('')
|
|
81
|
+
.refine(
|
|
82
|
+
(value) => !value || sipPasswordRegex.test(value),
|
|
83
|
+
i18nIssue('sipPasswordSymbolsValidator'),
|
|
84
|
+
),
|
|
85
|
+
expires: clearableNumberSchema.default(600),
|
|
86
|
+
// [Claude] trunking
|
|
87
|
+
host: gatewayHostSchema,
|
|
88
|
+
ipacl: z.array(gatewayIpAclSchema).optional().default([]),
|
|
89
|
+
})
|
|
90
|
+
.superRefine((data, ctx) => {
|
|
91
|
+
/** [Claude] password is required only on create: an empty one on update keeps the stored password */
|
|
92
|
+
const requiredFields = data.register
|
|
93
|
+
? ([
|
|
94
|
+
'proxy',
|
|
95
|
+
'account',
|
|
96
|
+
'expires',
|
|
97
|
+
...(data.id
|
|
98
|
+
? []
|
|
99
|
+
: ([
|
|
100
|
+
'password',
|
|
101
|
+
] as const)),
|
|
102
|
+
] as const)
|
|
103
|
+
: ([
|
|
104
|
+
'proxy',
|
|
105
|
+
'host',
|
|
106
|
+
] as const);
|
|
107
|
+
|
|
108
|
+
requiredFields
|
|
109
|
+
.filter((field) => !isFilled(data[field]))
|
|
110
|
+
.forEach((field) => {
|
|
111
|
+
ctx.addIssue({
|
|
112
|
+
code: 'custom',
|
|
113
|
+
path: [
|
|
114
|
+
field,
|
|
115
|
+
],
|
|
116
|
+
...i18nIssue('required'),
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (data.register && isFilled(data.expires)) {
|
|
121
|
+
expiresRangeSchema
|
|
122
|
+
.safeParse(data.expires)
|
|
123
|
+
.error?.issues.forEach((issue) => {
|
|
124
|
+
ctx.addIssue({
|
|
125
|
+
...issue,
|
|
126
|
+
path: [
|
|
127
|
+
'expires',
|
|
128
|
+
],
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
export type GatewayFormShape = z.input<typeof gatewaySchema>;
|
|
135
|
+
export type GatewayCard = GatewayFormShape & {
|
|
136
|
+
rState?: number;
|
|
137
|
+
};
|
|
138
|
+
export type GatewayIpAcl = z.input<typeof gatewayIpAclSchema>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { StorageImportSourceType } from '@webitel/api-services/gen/models';
|
|
2
|
+
import { getDefaultsFromZodSchema } from '@webitel/api-services/gen/utils';
|
|
3
|
+
import { describe, expect, it } from 'vitest';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type ImportTemplateCard,
|
|
7
|
+
importTemplateSchema,
|
|
8
|
+
} from '../importTemplate.validations';
|
|
9
|
+
|
|
10
|
+
const issuePaths = (
|
|
11
|
+
result: ReturnType<typeof importTemplateSchema.safeParse>,
|
|
12
|
+
) => result.error?.issues.map(({ path }) => path.join('.')) ?? [];
|
|
13
|
+
|
|
14
|
+
const defaults = () => getDefaultsFromZodSchema(importTemplateSchema, {});
|
|
15
|
+
|
|
16
|
+
describe('importTemplateSchema', () => {
|
|
17
|
+
it('seeds a new template as a utf-8, comma-separated dialer import', () => {
|
|
18
|
+
expect(defaults()).toMatchObject({
|
|
19
|
+
name: '',
|
|
20
|
+
sourceType: StorageImportSourceType.Dialer,
|
|
21
|
+
parameters: {
|
|
22
|
+
charset: {
|
|
23
|
+
value: 'utf-8',
|
|
24
|
+
},
|
|
25
|
+
separator: ',',
|
|
26
|
+
skipHeaders: true,
|
|
27
|
+
clearMembers: false,
|
|
28
|
+
mappings: {
|
|
29
|
+
name: '',
|
|
30
|
+
destination: [],
|
|
31
|
+
code: [],
|
|
32
|
+
variables: [],
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('requires name, source, separator and the name, destination and code columns', () => {
|
|
39
|
+
const template = defaults() as ImportTemplateCard;
|
|
40
|
+
template.parameters.separator = '';
|
|
41
|
+
|
|
42
|
+
expect(issuePaths(importTemplateSchema.safeParse(template))).toEqual(
|
|
43
|
+
expect.arrayContaining([
|
|
44
|
+
'name',
|
|
45
|
+
'source.id',
|
|
46
|
+
'parameters.separator',
|
|
47
|
+
'parameters.mappings.name',
|
|
48
|
+
'parameters.mappings.destination',
|
|
49
|
+
'parameters.mappings.code',
|
|
50
|
+
]),
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('accepts a filled template', () => {
|
|
55
|
+
const result = importTemplateSchema.safeParse({
|
|
56
|
+
...(defaults() as object),
|
|
57
|
+
name: 'import',
|
|
58
|
+
source: {
|
|
59
|
+
id: 1,
|
|
60
|
+
name: 'queue',
|
|
61
|
+
},
|
|
62
|
+
parameters: {
|
|
63
|
+
mappings: {
|
|
64
|
+
name: 'Name',
|
|
65
|
+
destination: [
|
|
66
|
+
'Phone',
|
|
67
|
+
],
|
|
68
|
+
code: [
|
|
69
|
+
'Type',
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(result.success).toBe(true);
|
|
76
|
+
});
|
|
77
|
+
});
|