@webitel/api-services 26.12.18 → 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.
@@ -1,5 +1,12 @@
1
1
  import { EngineRoutingSchemaType } from '@webitel/api-services/gen/models';
2
- import { getRoutingSchemaService } from '../../../gen-wire';
2
+ import { getShallowFieldsToSendFromZodSchema } from '@webitel/api-services/gen/utils';
3
+
4
+ import {
5
+ getRoutingSchemaService,
6
+ SearchRoutingSchemaQueryParams,
7
+ SearchRoutingSchemaTagsQueryParams,
8
+ UpdateRoutingSchemaBody,
9
+ } from '../../../gen-wire';
3
10
  import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
4
11
  import {
5
12
  applyTransform,
@@ -8,6 +15,7 @@ import {
8
15
  mergeEach,
9
16
  notify,
10
17
  sanitize,
18
+ sanitizeToWire,
11
19
  snakeToCamel,
12
20
  starToSearch,
13
21
  } from '../../transformers';
@@ -19,64 +27,71 @@ import type {
19
27
  UpdateItemParams,
20
28
  } from '../_shared/types';
21
29
 
22
- /*
23
- CONVERT "SCHEMA" FIELD TO JSON TO PREVENT ITS CHANGE
24
- BY CAMEL-SNAKE TRANSFORMERS
25
- */
30
+ /** [Claude] `schema` and `payload` are free-form JSON: case converters keep them as they are */
26
31
  const doNotConvertKeys = [
27
32
  'schema',
33
+ 'payload',
28
34
  ];
29
35
 
36
+ /** [Claude] the generated body leaves `schema` and `payload` out: the spec types them as free-form */
30
37
  const fieldsToSend = [
31
- 'name',
32
- 'schema',
33
- 'type',
34
- 'payload',
35
- 'editor',
36
- 'tags',
38
+ ...getShallowFieldsToSendFromZodSchema(UpdateRoutingSchemaBody),
39
+ ...doNotConvertKeys,
37
40
  ];
38
41
 
39
- const getFlowList = async (params: ApiParams) => {
40
- const defaultObject = {
41
- type: EngineRoutingSchemaType.Default,
42
- editor: false,
43
- };
42
+ /** [Claude] a type filter always keeps typeless ("default") flows in the list */
43
+ const withDefaultType = (type: unknown) => {
44
+ if (!type) return type;
45
+ const types = [
46
+ type,
47
+ ].flat();
48
+ return types.includes(EngineRoutingSchemaType.Default)
49
+ ? types
50
+ : [
51
+ ...types,
52
+ EngineRoutingSchemaType.Default,
53
+ ];
54
+ };
44
55
 
45
- const paramsCopy = {
46
- ...params,
47
- };
56
+ /** [Claude] the code editor edits `schema` as text: get, add and update all hand it back stringified */
57
+ const stringifySchema = (item: ApiParams) => ({
58
+ ...item,
59
+ schema: JSON.stringify(item.schema, null, 4),
60
+ });
48
61
 
49
- if (paramsCopy.type) {
50
- const _type = Array.isArray(paramsCopy.type)
51
- ? paramsCopy.type
52
- : [
53
- paramsCopy.type,
54
- ];
55
- if (!paramsCopy.type.includes(EngineRoutingSchemaType.Default)) {
56
- _type.push(EngineRoutingSchemaType.Default);
57
- }
58
- paramsCopy.type = _type;
59
- }
62
+ const parseSchema = (item: ApiParams) => ({
63
+ ...item,
64
+ schema:
65
+ typeof item.schema === 'string' ? JSON.parse(item.schema) : item.schema,
66
+ });
67
+
68
+ const toRequestItem = (itemInstance: ApiParams) =>
69
+ applyTransform(itemInstance, [
70
+ parseSchema,
71
+ sanitize(fieldsToSend),
72
+ camelToSnake(doNotConvertKeys),
73
+ ]);
74
+
75
+ const fromResponseItem = (item: ApiParams) =>
76
+ applyTransform(item, [
77
+ snakeToCamel(doNotConvertKeys),
78
+ stringifySchema,
79
+ ]);
60
80
 
61
- const requestParams = applyTransform(paramsCopy, [
81
+ const getFlowList = async (params: ApiParams) => {
82
+ const requestParams = applyTransform(params, [
62
83
  merge(getDefaultGetParams()),
63
84
  starToSearch('search'),
64
- camelToSnake(doNotConvertKeys),
65
- (params) => ({
85
+ (params: ApiParams) => ({
66
86
  ...params,
67
87
  q: params.search,
88
+ // [Claude] the table filter is named `flowType` (cf. `queueType`); plain `type` still works
89
+ type: withDefaultType(params.flowType ?? params.type),
68
90
  }),
69
- sanitize([
70
- 'page',
71
- 'size',
72
- 'q',
73
- 'sort',
74
- 'fields',
75
- 'id',
76
- 'name',
77
- 'type',
78
- 'tags',
79
- ]),
91
+ sanitizeToWire(
92
+ getShallowFieldsToSendFromZodSchema(SearchRoutingSchemaQueryParams),
93
+ ),
94
+ camelToSnake(doNotConvertKeys),
80
95
  ]);
81
96
 
82
97
  try {
@@ -88,7 +103,10 @@ const getFlowList = async (params: ApiParams) => {
88
103
  ]);
89
104
  return {
90
105
  items: applyTransform(items, [
91
- mergeEach(defaultObject),
106
+ mergeEach({
107
+ type: EngineRoutingSchemaType.Default,
108
+ editor: false,
109
+ }),
92
110
  ]),
93
111
  next,
94
112
  };
@@ -98,30 +116,20 @@ const getFlowList = async (params: ApiParams) => {
98
116
  ]);
99
117
  }
100
118
  };
101
- const getFlow = async ({ itemId: id }: GetItemParams) => {
102
- const defaultObject = {
103
- tags: [],
104
- editor: false,
105
- type: EngineRoutingSchemaType.Default,
106
- };
107
-
108
- const itemResponseHandler = (item: ApiParams) => ({
109
- ...item,
110
- schema: JSON.stringify(item.schema, null, 4),
111
- });
112
119
 
120
+ const getFlow = async ({ itemId: id }: GetItemParams) => {
113
121
  try {
114
122
  const response = await getRoutingSchemaService().readRoutingSchema(
115
123
  String(id),
116
124
  );
117
125
  return applyTransform(response.data, [
118
- ({ payload, schema, ...rest }) => ({
119
- payload,
120
- schema,
121
- ...snakeToCamel(doNotConvertKeys)(rest),
126
+ snakeToCamel(doNotConvertKeys),
127
+ merge({
128
+ tags: [],
129
+ editor: false,
130
+ type: EngineRoutingSchemaType.Default,
122
131
  }),
123
- merge(defaultObject),
124
- itemResponseHandler,
132
+ stringifySchema,
125
133
  ]);
126
134
  } catch (err) {
127
135
  throw applyTransform(err, [
@@ -130,59 +138,26 @@ const getFlow = async ({ itemId: id }: GetItemParams) => {
130
138
  }
131
139
  };
132
140
 
133
- const preRequestHandler = (item: ApiParams) => ({
134
- ...item,
135
- schema:
136
- typeof item.schema === 'string' ? JSON.parse(item.schema) : item.schema,
137
- });
138
-
139
141
  const addFlow = async ({ itemInstance }: AddItemParams) => {
140
- const item = applyTransform(itemInstance, [
141
- preRequestHandler,
142
- sanitize(fieldsToSend),
143
- ({ payload, schema, ...rest }) => ({
144
- payload,
145
- schema,
146
- ...camelToSnake(doNotConvertKeys)(rest),
147
- }),
148
- ]);
149
142
  try {
150
- const response = await getRoutingSchemaService().createRoutingSchema(item);
151
- return applyTransform(response.data, [
152
- ({ payload, schema, ...rest }) => ({
153
- payload,
154
- schema,
155
- ...snakeToCamel(doNotConvertKeys)(rest),
156
- }),
157
- ]);
143
+ const response = await getRoutingSchemaService().createRoutingSchema(
144
+ toRequestItem(itemInstance),
145
+ );
146
+ return fromResponseItem(response.data);
158
147
  } catch (err) {
159
148
  throw applyTransform(err, [
160
149
  notify,
161
150
  ]);
162
151
  }
163
152
  };
153
+
164
154
  const updateFlow = async ({ itemInstance, itemId: id }: UpdateItemParams) => {
165
- const item = applyTransform(itemInstance, [
166
- preRequestHandler,
167
- sanitize(fieldsToSend),
168
- ({ payload, schema, ...rest }) => ({
169
- payload,
170
- schema,
171
- ...camelToSnake(doNotConvertKeys)(rest),
172
- }),
173
- ]);
174
155
  try {
175
156
  const response = await getRoutingSchemaService().updateRoutingSchema(
176
157
  String(id),
177
- item,
158
+ toRequestItem(itemInstance),
178
159
  );
179
- return applyTransform(response.data, [
180
- ({ payload, schema, ...rest }) => ({
181
- payload,
182
- schema,
183
- ...snakeToCamel(doNotConvertKeys)(rest),
184
- }),
185
- ]);
160
+ return fromResponseItem(response.data);
186
161
  } catch (err) {
187
162
  throw applyTransform(err, [
188
163
  notify,
@@ -217,8 +192,7 @@ const getFlowTags = async (params: ApiParams) => {
217
192
  const requestParams = applyTransform(params, [
218
193
  merge(getDefaultGetParams()),
219
194
  starToSearch(),
220
- camelToSnake(doNotConvertKeys),
221
- (params) => ({
195
+ (params: ApiParams) => ({
222
196
  ...params,
223
197
  q: params.search,
224
198
  // fixme: `ids` was passed into the `type` positional slot of the
@@ -226,14 +200,10 @@ const getFlowTags = async (params: ApiParams) => {
226
200
  // `id` filter, so this is very likely a latent bug.
227
201
  type: params.ids,
228
202
  }),
229
- sanitize([
230
- 'page',
231
- 'size',
232
- 'q',
233
- 'sort',
234
- 'fields',
235
- 'type',
236
- ]),
203
+ sanitizeToWire(
204
+ getShallowFieldsToSendFromZodSchema(SearchRoutingSchemaTagsQueryParams),
205
+ ),
206
+ camelToSnake(doNotConvertKeys),
237
207
  ]);
238
208
  try {
239
209
  const response =
@@ -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;
@@ -0,0 +1,46 @@
1
+ import { getDefaultsFromZodSchema } from '@webitel/api-services/gen/utils';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { flowSchema } from '../flow.validations';
5
+
6
+ const issuePaths = (value: Record<string, unknown>) =>
7
+ flowSchema
8
+ .safeParse(value)
9
+ .error?.issues.map((issue) => issue.path.join('.')) ?? [];
10
+
11
+ describe('flowSchema', () => {
12
+ it('seeds a new code flow with an empty schema', () => {
13
+ expect(getDefaultsFromZodSchema(flowSchema, {})).toMatchObject({
14
+ schema: '[]',
15
+ tags: [],
16
+ editor: false,
17
+ });
18
+ });
19
+
20
+ it('requires a name and a schema', () => {
21
+ expect(
22
+ issuePaths({
23
+ name: '',
24
+ schema: '',
25
+ }),
26
+ ).toEqual([
27
+ 'name',
28
+ 'schema',
29
+ ]);
30
+ });
31
+
32
+ it('accepts a filled flow with tags', () => {
33
+ expect(
34
+ issuePaths({
35
+ name: 'IVR',
36
+ schema: '[]',
37
+ type: 'voice',
38
+ tags: [
39
+ {
40
+ name: 'main',
41
+ },
42
+ ],
43
+ }),
44
+ ).toEqual([]);
45
+ });
46
+ });
@@ -0,0 +1,30 @@
1
+ import {
2
+ type EngineRoutingSchema,
3
+ EngineRoutingSchemaType,
4
+ } from '@webitel/api-services/gen/models';
5
+ import { z } from 'zod';
6
+
7
+ /**
8
+ * [Claude] A code-editor flow: `schema` travels as JSON text (the API stringifies
9
+ * it on read and parses it back on write), so the form only needs it non-empty.
10
+ */
11
+ export const flowSchema = z.object({
12
+ name: z.string().min(1).default(''),
13
+ schema: z.string().min(1).default('[]'),
14
+ type: z.enum(EngineRoutingSchemaType).optional(),
15
+ tags: z
16
+ .array(
17
+ z.object({
18
+ name: z.string().optional(),
19
+ }),
20
+ )
21
+ .optional()
22
+ .default([]),
23
+ editor: z.boolean().optional().default(false),
24
+ });
25
+
26
+ export type FlowFormShape = z.input<typeof flowSchema>;
27
+
28
+ /** [Claude] the flow as a card holds it: the API entity with the form's fields (`schema` as text) */
29
+ export type FlowCard = Omit<EngineRoutingSchema, keyof FlowFormShape> &
30
+ FlowFormShape;
@@ -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,8 @@ 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';
39
+ export * from './flow/flow.validations';
37
40
  export * from './license/licenseImport.validations';
38
41
  export * from './OAuth/OAuth.validations';
39
42
  export * from './onlineSkill/onlineSkill.validations';