@webitel/api-services 26.12.19 → 26.12.21

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/api-services",
3
- "version": "26.12.19",
3
+ "version": "26.12.21",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,129 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ const wireChatplan = {
4
+ id: 1,
5
+ name: 'Chat',
6
+ enabled: true,
7
+ schema: {
8
+ id: '2',
9
+ name: 'Bot',
10
+ },
11
+ };
12
+
13
+ const searchChatPlan = vi.fn(() =>
14
+ Promise.resolve({
15
+ data: {
16
+ items: [
17
+ {
18
+ id: 1,
19
+ },
20
+ ],
21
+ next: false,
22
+ },
23
+ }),
24
+ );
25
+ const createChatPlan = vi.fn(() =>
26
+ Promise.resolve({
27
+ data: wireChatplan,
28
+ }),
29
+ );
30
+ const updateChatPlan = vi.fn(() =>
31
+ Promise.resolve({
32
+ data: wireChatplan,
33
+ }),
34
+ );
35
+ const patchChatPlan = vi.fn(() =>
36
+ Promise.resolve({
37
+ data: wireChatplan,
38
+ }),
39
+ );
40
+
41
+ vi.mock('../../../../gen-wire', async (importOriginal) => ({
42
+ ...(await importOriginal<Record<string, unknown>>()),
43
+ getRoutingChatPlanService: () => ({
44
+ searchChatPlan,
45
+ createChatPlan,
46
+ updateChatPlan,
47
+ patchChatPlan,
48
+ }),
49
+ }));
50
+
51
+ const { ChatplansAPI } = await import('../chatplans');
52
+
53
+ const card = {
54
+ id: 1,
55
+ name: 'Chat',
56
+ description: '',
57
+ enabled: true,
58
+ createdAt: '5',
59
+ schema: {
60
+ id: '2',
61
+ name: 'Bot',
62
+ },
63
+ };
64
+
65
+ describe('ChatplansAPI', () => {
66
+ beforeEach(() => vi.clearAllMocks());
67
+
68
+ it('getList sends search as q', async () => {
69
+ await ChatplansAPI.getList({
70
+ search: 'cha',
71
+ });
72
+
73
+ expect(searchChatPlan).toHaveBeenCalledWith(
74
+ expect.objectContaining({
75
+ q: 'cha*',
76
+ }),
77
+ );
78
+ });
79
+
80
+ it('getList defaults enabled to false', async () => {
81
+ const { items } = await ChatplansAPI.getList({});
82
+ expect(items[0]).toMatchObject({
83
+ enabled: false,
84
+ });
85
+ });
86
+
87
+ it('add sends only body fields', async () => {
88
+ await ChatplansAPI.add({
89
+ itemInstance: card,
90
+ });
91
+
92
+ expect(createChatPlan).toHaveBeenCalledWith({
93
+ name: 'Chat',
94
+ description: '',
95
+ enabled: true,
96
+ schema: {
97
+ id: '2',
98
+ name: 'Bot',
99
+ },
100
+ });
101
+ });
102
+
103
+ it('update sends the body to the numeric id', async () => {
104
+ await ChatplansAPI.update({
105
+ itemId: '1',
106
+ itemInstance: card,
107
+ });
108
+
109
+ expect(updateChatPlan).toHaveBeenCalledWith(
110
+ 1,
111
+ expect.not.objectContaining({
112
+ created_at: expect.anything(),
113
+ }),
114
+ );
115
+ });
116
+
117
+ it('patch sends just the changed field', async () => {
118
+ await ChatplansAPI.patch({
119
+ id: '1',
120
+ changes: {
121
+ enabled: false,
122
+ },
123
+ });
124
+
125
+ expect(patchChatPlan).toHaveBeenCalledWith(1, {
126
+ enabled: false,
127
+ });
128
+ });
129
+ });
@@ -1,4 +1,10 @@
1
- import { getRoutingChatPlanService } from '../../../gen-wire';
1
+ import { getShallowFieldsToSendFromZodSchema } from '@webitel/api-services/gen/utils';
2
+
3
+ import {
4
+ getRoutingChatPlanService,
5
+ SearchChatPlanQueryParams,
6
+ UpdateChatPlanBody,
7
+ } from '../../../gen-wire';
2
8
  import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
3
9
  import {
4
10
  applyTransform,
@@ -6,7 +12,7 @@ import {
6
12
  merge,
7
13
  mergeEach,
8
14
  notify,
9
- sanitize,
15
+ sanitizeToWire,
10
16
  snakeToCamel,
11
17
  starToSearch,
12
18
  } from '../../transformers';
@@ -19,44 +25,38 @@ import type {
19
25
  UpdateItemParams,
20
26
  } from '../_shared/types';
21
27
 
22
- const fieldsToSend = [
23
- 'name',
24
- 'schema',
25
- 'description',
26
- 'enabled',
27
- ];
28
+ const toRequestItem = (item: ApiParams) =>
29
+ applyTransform(item, [
30
+ sanitizeToWire(getShallowFieldsToSendFromZodSchema(UpdateChatPlanBody)),
31
+ camelToSnake(),
32
+ ]);
28
33
 
29
34
  const getChatplansList = async (params: ApiParams) => {
30
- const defaultObject = {
31
- enabled: false,
32
- };
33
-
34
- const { page, size, search, sort, fields, id, name, enabled } =
35
- applyTransform(params, [
36
- merge(getDefaultGetParams()),
37
- starToSearch('search'),
38
- camelToSnake(),
39
- ]);
35
+ const requestParams = applyTransform(params, [
36
+ merge(getDefaultGetParams()),
37
+ starToSearch('search'),
38
+ (params: ApiParams) => ({
39
+ ...params,
40
+ q: params.search,
41
+ }),
42
+ sanitizeToWire(
43
+ getShallowFieldsToSendFromZodSchema(SearchChatPlanQueryParams),
44
+ ),
45
+ camelToSnake(),
46
+ ]);
40
47
 
41
48
  try {
42
- const response = await getRoutingChatPlanService().searchChatPlan({
43
- page,
44
- size,
45
- // the generated param is `q`; `search` is what the datalist store sends
46
- q: search,
47
- sort,
48
- fields,
49
- id,
50
- name,
51
- enabled,
52
- });
49
+ const response =
50
+ await getRoutingChatPlanService().searchChatPlan(requestParams);
53
51
  const { items, next } = applyTransform(response.data, [
54
52
  snakeToCamel(),
55
53
  merge(getDefaultGetListResponse()),
56
54
  ]);
57
55
  return {
58
56
  items: applyTransform(items, [
59
- mergeEach(defaultObject),
57
+ mergeEach({
58
+ enabled: false,
59
+ }),
60
60
  ]),
61
61
  next,
62
62
  };
@@ -81,12 +81,10 @@ const getChatplan = async ({ itemId: id }: GetItemParams) => {
81
81
  };
82
82
 
83
83
  const addChatplan = async ({ itemInstance }: AddItemParams) => {
84
- const item = applyTransform(itemInstance, [
85
- sanitize(fieldsToSend),
86
- camelToSnake(),
87
- ]);
88
84
  try {
89
- const response = await getRoutingChatPlanService().createChatPlan(item);
85
+ const response = await getRoutingChatPlanService().createChatPlan(
86
+ toRequestItem(itemInstance),
87
+ );
90
88
  return applyTransform(response.data, [
91
89
  snakeToCamel(),
92
90
  ]);
@@ -98,14 +96,10 @@ const addChatplan = async ({ itemInstance }: AddItemParams) => {
98
96
  };
99
97
 
100
98
  const patchChatplan = async ({ changes, id }: PatchItemParams) => {
101
- const body = applyTransform(changes, [
102
- sanitize(fieldsToSend),
103
- camelToSnake(),
104
- ]);
105
99
  try {
106
100
  const response = await getRoutingChatPlanService().patchChatPlan(
107
101
  Number(id),
108
- body,
102
+ toRequestItem(changes),
109
103
  );
110
104
  return applyTransform(response.data, [
111
105
  snakeToCamel(),
@@ -121,14 +115,10 @@ const updateChatplan = async ({
121
115
  itemInstance,
122
116
  itemId: id,
123
117
  }: UpdateItemParams) => {
124
- const item = applyTransform(itemInstance, [
125
- sanitize(fieldsToSend),
126
- camelToSnake(),
127
- ]);
128
118
  try {
129
119
  const response = await getRoutingChatPlanService().updateChatPlan(
130
120
  Number(id),
131
- item,
121
+ toRequestItem(itemInstance),
132
122
  );
133
123
  return applyTransform(response.data, [
134
124
  snakeToCamel(),
@@ -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
- type SearchRoutingOutboundCallParams,
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
- sanitize,
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
- const fieldsToSend = [
27
- 'name',
28
- 'schema',
29
- 'pattern',
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 getDialplansList = async (params: ApiParams) => {
36
- const defaultObject = {
37
- disabled: false,
38
- };
34
+ const toRequestItem = (item: ApiParams) =>
35
+ applyTransform(item, [
36
+ sanitizeToWire(fieldsToSend),
37
+ camelToSnake(),
38
+ ]);
39
39
 
40
- const { page, size, search, sort, fields, id, allowTransfer } =
41
- applyTransform(params, [
42
- merge(getDefaultGetParams()),
43
- starToSearch('search'),
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
- page,
50
- size,
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(defaultObject),
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(item);
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
- item,
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
- body,
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,43 @@
1
+ import { getDefaultsFromZodSchema } from '@webitel/api-services/gen/utils';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { chatplanSchema } from '../chatplan.validations';
5
+
6
+ const issuePaths = (value: Record<string, unknown>) =>
7
+ chatplanSchema
8
+ .safeParse(value)
9
+ .error?.issues.map((issue) => issue.path.join('.')) ?? [];
10
+
11
+ describe('chatplanSchema', () => {
12
+ it('seeds a new chatplan enabled, with empty fields', () => {
13
+ expect(getDefaultsFromZodSchema(chatplanSchema, {})).toMatchObject({
14
+ name: '',
15
+ description: '',
16
+ enabled: true,
17
+ });
18
+ });
19
+
20
+ it('accepts a filled chatplan', () => {
21
+ expect(
22
+ issuePaths({
23
+ name: 'Chat',
24
+ schema: {
25
+ id: 1,
26
+ name: 'Bot',
27
+ },
28
+ }),
29
+ ).toEqual([]);
30
+ });
31
+
32
+ it('requires a name and a flow', () => {
33
+ expect(
34
+ issuePaths({
35
+ name: '',
36
+ schema: {},
37
+ }),
38
+ ).toEqual([
39
+ 'name',
40
+ 'schema.id',
41
+ ]);
42
+ });
43
+ });
@@ -0,0 +1,17 @@
1
+ import type { EngineChatPlan } from '@webitel/api-services/gen/models';
2
+ import { z } from 'zod';
3
+
4
+ import { filledLookupSchema } from '../_shared/lookup.validations';
5
+
6
+ export const chatplanSchema = z.object({
7
+ name: z.string().min(1).default(''),
8
+ schema: filledLookupSchema.default({}),
9
+ description: z.string().optional().default(''),
10
+ enabled: z.boolean().optional().default(true),
11
+ });
12
+
13
+ export type ChatplanFormShape = z.input<typeof chatplanSchema>;
14
+
15
+ /** [Claude] the chatplan as a card holds it: the API entity with the form's fields */
16
+ export type ChatplanCard = Omit<EngineChatPlan, keyof ChatplanFormShape> &
17
+ ChatplanFormShape;
@@ -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
+ });