@webitel/api-services 26.12.19 → 26.12.20
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/dialplans/__tests__/dialplans.spec.ts +139 -0
- package/src/api/clients/dialplans/dialplans.ts +38 -45
- package/src/validations/_shared/regExp.validations.ts +17 -0
- package/src/validations/dialplan/__tests__/dialplan.validations.spec.ts +58 -0
- package/src/validations/dialplan/dialplan.validations.ts +22 -0
- package/src/validations/index.ts +2 -0
- package/types/.tsbuildinfo +1 -1
- package/types/validations/_shared/regExp.validations.d.ts +3 -0
- package/types/validations/dialplan/dialplan.validations.d.ts +15 -0
- package/types/validations/index.d.ts +2 -0
package/package.json
CHANGED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
const wireDialplan = {
|
|
4
|
+
id: '1',
|
|
5
|
+
name: 'Outbound',
|
|
6
|
+
pattern: '^1$',
|
|
7
|
+
allow_transfer: true,
|
|
8
|
+
domain_id: '10',
|
|
9
|
+
schema: {
|
|
10
|
+
id: '2',
|
|
11
|
+
name: 'IVR',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const searchRoutingOutboundCall = vi.fn(() =>
|
|
16
|
+
Promise.resolve({
|
|
17
|
+
data: {
|
|
18
|
+
items: [
|
|
19
|
+
{
|
|
20
|
+
id: '1',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
next: false,
|
|
24
|
+
},
|
|
25
|
+
}),
|
|
26
|
+
);
|
|
27
|
+
const createRoutingOutboundCall = vi.fn(() =>
|
|
28
|
+
Promise.resolve({
|
|
29
|
+
data: wireDialplan,
|
|
30
|
+
}),
|
|
31
|
+
);
|
|
32
|
+
const updateRoutingOutboundCall = vi.fn(() =>
|
|
33
|
+
Promise.resolve({
|
|
34
|
+
data: wireDialplan,
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
const patchRoutingOutboundCall = vi.fn(() =>
|
|
38
|
+
Promise.resolve({
|
|
39
|
+
data: wireDialplan,
|
|
40
|
+
}),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
vi.mock('../../../../gen-wire', async (importOriginal) => ({
|
|
44
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
45
|
+
getRoutingOutboundCallService: () => ({
|
|
46
|
+
searchRoutingOutboundCall,
|
|
47
|
+
createRoutingOutboundCall,
|
|
48
|
+
updateRoutingOutboundCall,
|
|
49
|
+
patchRoutingOutboundCall,
|
|
50
|
+
}),
|
|
51
|
+
}));
|
|
52
|
+
|
|
53
|
+
const { DialplansAPI } = await import('../dialplans');
|
|
54
|
+
|
|
55
|
+
const card = {
|
|
56
|
+
id: '1',
|
|
57
|
+
name: 'Outbound',
|
|
58
|
+
pattern: '^1$',
|
|
59
|
+
allowTransfer: true,
|
|
60
|
+
description: '',
|
|
61
|
+
domainId: '10',
|
|
62
|
+
createdAt: '5',
|
|
63
|
+
schema: {
|
|
64
|
+
id: '2',
|
|
65
|
+
name: 'IVR',
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
describe('DialplansAPI', () => {
|
|
70
|
+
beforeEach(() => vi.clearAllMocks());
|
|
71
|
+
|
|
72
|
+
it('getList sends search as q and allowTransfer as allow_transfer', async () => {
|
|
73
|
+
await DialplansAPI.getList({
|
|
74
|
+
search: 'out',
|
|
75
|
+
allowTransfer: true,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(searchRoutingOutboundCall).toHaveBeenCalledWith(
|
|
79
|
+
expect.objectContaining({
|
|
80
|
+
q: 'out*',
|
|
81
|
+
allow_transfer: true,
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('getList defaults disabled to false', async () => {
|
|
87
|
+
const { items } = await DialplansAPI.getList({});
|
|
88
|
+
expect(items[0]).toMatchObject({
|
|
89
|
+
disabled: false,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('add sends only body fields, never domain_id', async () => {
|
|
94
|
+
await DialplansAPI.add({
|
|
95
|
+
itemInstance: card,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
expect(createRoutingOutboundCall).toHaveBeenCalledWith({
|
|
99
|
+
name: 'Outbound',
|
|
100
|
+
pattern: '^1$',
|
|
101
|
+
allow_transfer: true,
|
|
102
|
+
description: '',
|
|
103
|
+
schema: {
|
|
104
|
+
id: '2',
|
|
105
|
+
name: 'IVR',
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('update sends the same body to the item id', async () => {
|
|
111
|
+
const response = await DialplansAPI.update({
|
|
112
|
+
itemId: '1',
|
|
113
|
+
itemInstance: card,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(updateRoutingOutboundCall).toHaveBeenCalledWith(
|
|
117
|
+
'1',
|
|
118
|
+
expect.not.objectContaining({
|
|
119
|
+
domain_id: expect.anything(),
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
expect(response).toMatchObject({
|
|
123
|
+
allowTransfer: true,
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('patch sends just the changed field', async () => {
|
|
128
|
+
await DialplansAPI.patch({
|
|
129
|
+
id: '1',
|
|
130
|
+
changes: {
|
|
131
|
+
disabled: true,
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
expect(patchRoutingOutboundCall).toHaveBeenCalledWith('1', {
|
|
136
|
+
disabled: true,
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { getShallowFieldsToSendFromZodSchema } from '@webitel/api-services/gen/utils';
|
|
2
|
+
|
|
1
3
|
import {
|
|
2
4
|
getRoutingOutboundCallService,
|
|
3
|
-
|
|
5
|
+
SearchRoutingOutboundCallQueryParams,
|
|
6
|
+
UpdateRoutingOutboundCallBody,
|
|
4
7
|
} from '../../../gen-wire';
|
|
5
8
|
import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
|
|
6
9
|
import {
|
|
@@ -9,7 +12,7 @@ import {
|
|
|
9
12
|
merge,
|
|
10
13
|
mergeEach,
|
|
11
14
|
notify,
|
|
12
|
-
|
|
15
|
+
sanitizeToWire,
|
|
13
16
|
snakeToCamel,
|
|
14
17
|
starToSearch,
|
|
15
18
|
} from '../../transformers';
|
|
@@ -23,45 +26,45 @@ import type {
|
|
|
23
26
|
UpdateItemParams,
|
|
24
27
|
} from '../_shared/types';
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
'description',
|
|
31
|
-
'disabled',
|
|
32
|
-
'allowTransfer',
|
|
33
|
-
];
|
|
29
|
+
/** [Claude] the body fields minus `domain_id`: the domain comes from the session, never from the form */
|
|
30
|
+
const fieldsToSend = getShallowFieldsToSendFromZodSchema(
|
|
31
|
+
UpdateRoutingOutboundCallBody,
|
|
32
|
+
).filter((field) => field !== 'domain_id');
|
|
34
33
|
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const toRequestItem = (item: ApiParams) =>
|
|
35
|
+
applyTransform(item, [
|
|
36
|
+
sanitizeToWire(fieldsToSend),
|
|
37
|
+
camelToSnake(),
|
|
38
|
+
]);
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
40
|
+
const getDialplansList = async (params: ApiParams) => {
|
|
41
|
+
const requestParams = applyTransform(params, [
|
|
42
|
+
merge(getDefaultGetParams()),
|
|
43
|
+
starToSearch('search'),
|
|
44
|
+
(params: ApiParams) => ({
|
|
45
|
+
...params,
|
|
46
|
+
q: params.search,
|
|
47
|
+
}),
|
|
48
|
+
sanitizeToWire(
|
|
49
|
+
getShallowFieldsToSendFromZodSchema(SearchRoutingOutboundCallQueryParams),
|
|
50
|
+
),
|
|
51
|
+
camelToSnake(),
|
|
52
|
+
]);
|
|
45
53
|
|
|
46
54
|
try {
|
|
47
55
|
const response =
|
|
48
|
-
await getRoutingOutboundCallService().searchRoutingOutboundCall(
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// the generated param is `q`; `search` is what the datalist store sends
|
|
52
|
-
q: search,
|
|
53
|
-
sort,
|
|
54
|
-
fields,
|
|
55
|
-
id,
|
|
56
|
-
allow_transfer: allowTransfer,
|
|
57
|
-
} as SearchRoutingOutboundCallParams);
|
|
56
|
+
await getRoutingOutboundCallService().searchRoutingOutboundCall(
|
|
57
|
+
requestParams,
|
|
58
|
+
);
|
|
58
59
|
const { items, next } = applyTransform(response.data, [
|
|
59
60
|
snakeToCamel(),
|
|
60
61
|
merge(getDefaultGetListResponse()),
|
|
61
62
|
]);
|
|
62
63
|
return {
|
|
63
64
|
items: applyTransform(items, [
|
|
64
|
-
mergeEach(
|
|
65
|
+
mergeEach({
|
|
66
|
+
disabled: false,
|
|
67
|
+
}),
|
|
65
68
|
]),
|
|
66
69
|
next,
|
|
67
70
|
};
|
|
@@ -87,13 +90,11 @@ const getDialplan = async ({ itemId: id }: GetItemParams) => {
|
|
|
87
90
|
};
|
|
88
91
|
|
|
89
92
|
const addDialplan = async ({ itemInstance }: AddItemParams) => {
|
|
90
|
-
const item = applyTransform(itemInstance, [
|
|
91
|
-
sanitize(fieldsToSend),
|
|
92
|
-
camelToSnake(),
|
|
93
|
-
]);
|
|
94
93
|
try {
|
|
95
94
|
const response =
|
|
96
|
-
await getRoutingOutboundCallService().createRoutingOutboundCall(
|
|
95
|
+
await getRoutingOutboundCallService().createRoutingOutboundCall(
|
|
96
|
+
toRequestItem(itemInstance),
|
|
97
|
+
);
|
|
97
98
|
return applyTransform(response.data, [
|
|
98
99
|
snakeToCamel(),
|
|
99
100
|
]);
|
|
@@ -108,15 +109,11 @@ const updateDialplan = async ({
|
|
|
108
109
|
itemInstance,
|
|
109
110
|
itemId: id,
|
|
110
111
|
}: UpdateItemParams) => {
|
|
111
|
-
const item = applyTransform(itemInstance, [
|
|
112
|
-
sanitize(fieldsToSend),
|
|
113
|
-
camelToSnake(),
|
|
114
|
-
]);
|
|
115
112
|
try {
|
|
116
113
|
const response =
|
|
117
114
|
await getRoutingOutboundCallService().updateRoutingOutboundCall(
|
|
118
115
|
String(id),
|
|
119
|
-
|
|
116
|
+
toRequestItem(itemInstance),
|
|
120
117
|
);
|
|
121
118
|
return applyTransform(response.data, [
|
|
122
119
|
snakeToCamel(),
|
|
@@ -129,15 +126,11 @@ const updateDialplan = async ({
|
|
|
129
126
|
};
|
|
130
127
|
|
|
131
128
|
const patchDialplan = async ({ changes, id }: PatchItemParams) => {
|
|
132
|
-
const body = applyTransform(changes, [
|
|
133
|
-
sanitize(fieldsToSend),
|
|
134
|
-
camelToSnake(),
|
|
135
|
-
]);
|
|
136
129
|
try {
|
|
137
130
|
const response =
|
|
138
131
|
await getRoutingOutboundCallService().patchRoutingOutboundCall(
|
|
139
132
|
String(id),
|
|
140
|
-
|
|
133
|
+
toRequestItem(changes),
|
|
141
134
|
);
|
|
142
135
|
return applyTransform(response.data, [
|
|
143
136
|
snakeToCamel(),
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
import { i18nIssue } from './i18nIssue';
|
|
4
|
+
|
|
5
|
+
const isRegExp = (value: string) => {
|
|
6
|
+
try {
|
|
7
|
+
new RegExp(value);
|
|
8
|
+
return true;
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** [Claude] a string the browser accepts as a RegExp source, e.g. a dialplan pattern */
|
|
15
|
+
export const regExpSchema = z
|
|
16
|
+
.string()
|
|
17
|
+
.refine(isRegExp, i18nIssue('regExpValidator'));
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { getDefaultsFromZodSchema } from '@webitel/api-services/gen/utils';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { dialplanSchema } from '../dialplan.validations';
|
|
5
|
+
|
|
6
|
+
const filled = {
|
|
7
|
+
name: 'Outbound',
|
|
8
|
+
schema: {
|
|
9
|
+
id: 1,
|
|
10
|
+
name: 'IVR',
|
|
11
|
+
},
|
|
12
|
+
pattern: '^\\+?380\\d{9}$',
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const issuePaths = (value: Record<string, unknown>) =>
|
|
16
|
+
dialplanSchema
|
|
17
|
+
.safeParse(value)
|
|
18
|
+
.error?.issues.map((issue) => issue.path.join('.')) ?? [];
|
|
19
|
+
|
|
20
|
+
describe('dialplanSchema', () => {
|
|
21
|
+
it('seeds a new dialplan with empty fields', () => {
|
|
22
|
+
expect(getDefaultsFromZodSchema(dialplanSchema, {})).toMatchObject({
|
|
23
|
+
name: '',
|
|
24
|
+
pattern: '',
|
|
25
|
+
description: '',
|
|
26
|
+
allowTransfer: false,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('accepts a filled dialplan', () => {
|
|
31
|
+
expect(issuePaths(filled)).toEqual([]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('requires a name, a flow and a pattern', () => {
|
|
35
|
+
expect(
|
|
36
|
+
issuePaths({
|
|
37
|
+
name: '',
|
|
38
|
+
schema: {},
|
|
39
|
+
pattern: '',
|
|
40
|
+
}),
|
|
41
|
+
).toEqual([
|
|
42
|
+
'name',
|
|
43
|
+
'schema.id',
|
|
44
|
+
'pattern',
|
|
45
|
+
]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('rejects a pattern that is not a valid regular expression', () => {
|
|
49
|
+
expect(
|
|
50
|
+
issuePaths({
|
|
51
|
+
...filled,
|
|
52
|
+
pattern: '([0-9]',
|
|
53
|
+
}),
|
|
54
|
+
).toEqual([
|
|
55
|
+
'pattern',
|
|
56
|
+
]);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { EngineRoutingOutboundCall } from '@webitel/api-services/gen/models';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
import { filledLookupSchema } from '../_shared/lookup.validations';
|
|
5
|
+
import { regExpSchema } from '../_shared/regExp.validations';
|
|
6
|
+
|
|
7
|
+
export const dialplanSchema = z.object({
|
|
8
|
+
name: z.string().min(1).default(''),
|
|
9
|
+
schema: filledLookupSchema.default({}),
|
|
10
|
+
pattern: regExpSchema.min(1).default(''),
|
|
11
|
+
description: z.string().optional().default(''),
|
|
12
|
+
allowTransfer: z.boolean().optional().default(false),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export type DialplanFormShape = z.input<typeof dialplanSchema>;
|
|
16
|
+
|
|
17
|
+
/** [Claude] the dialplan as a card holds it: the API entity with the form's fields */
|
|
18
|
+
export type DialplanCard = Omit<
|
|
19
|
+
EngineRoutingOutboundCall,
|
|
20
|
+
keyof DialplanFormShape
|
|
21
|
+
> &
|
|
22
|
+
DialplanFormShape;
|
package/src/validations/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './_shared/duration.validations';
|
|
|
3
3
|
export * from './_shared/i18nIssue';
|
|
4
4
|
export * from './_shared/lookup.validations';
|
|
5
5
|
export * from './_shared/phoneNumber.validations';
|
|
6
|
+
export * from './_shared/regExp.validations';
|
|
6
7
|
export * from './_shared/timeRange.validations';
|
|
7
8
|
export * from './_shared/variablePair.validations';
|
|
8
9
|
export * from './adjunctType/adjunctType.validations';
|
|
@@ -34,6 +35,7 @@ export * from './contactGroup/contactGroup.validations';
|
|
|
34
35
|
export * from './contactGroupCondition/contactGroupCondition.validations';
|
|
35
36
|
export * from './contactPhone/contactPhone.validations';
|
|
36
37
|
export * from './device/device.validations';
|
|
38
|
+
export * from './dialplan/dialplan.validations';
|
|
37
39
|
export * from './flow/flow.validations';
|
|
38
40
|
export * from './license/licenseImport.validations';
|
|
39
41
|
export * from './OAuth/OAuth.validations';
|