@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.
Files changed (85) hide show
  1. package/package.json +1 -1
  2. package/src/api/clients/_shared/appUrls.ts +11 -0
  3. package/src/api/clients/chatGateways/__tests__/chatGatewayChannels.spec.ts +84 -0
  4. package/src/api/clients/chatGateways/__tests__/chatGateways.spec.ts +115 -0
  5. package/src/api/clients/chatGateways/chatGatewayChannels.ts +162 -0
  6. package/src/api/clients/chatGateways/chatGateways.ts +66 -25
  7. package/src/api/clients/chatGateways/defaults/chatGatewayProviderDefaults.ts +91 -0
  8. package/src/api/clients/chatGateways/defaults/defaultChatGateway.ts +3 -1
  9. package/src/api/clients/chatGateways/defaults/webChatGateway.ts +8 -5
  10. package/src/api/clients/chatplans/__tests__/chatplans.spec.ts +129 -0
  11. package/src/api/clients/chatplans/chatplans.ts +35 -45
  12. package/src/api/clients/cognitiveProfiles/cognitiveProfiles.ts +8 -32
  13. package/src/api/clients/cognitiveProfiles/defaults/cognitiveProfileDefaults.ts +39 -0
  14. package/src/api/clients/gateways/__tests__/gateways.spec.ts +138 -0
  15. package/src/api/clients/gateways/gateways.ts +60 -52
  16. package/src/api/clients/index.ts +2 -0
  17. package/src/enums/ChatGatewayChannelSetup/ChatGatewayChannelSetup.ts +10 -0
  18. package/src/enums/GatewayIpAclProtocol/GatewayIpAclProtocol.ts +8 -0
  19. package/src/enums/GatewayRegisterState/GatewayRegisterState.ts +17 -0
  20. package/src/enums/StorageType/StorageType.ts +10 -0
  21. package/src/enums/WebchatAlternativeChannel/WebchatAlternativeChannel.ts +15 -0
  22. package/src/enums/index.ts +10 -0
  23. package/src/locale/en/en.ts +8 -0
  24. package/src/locale/es/es.ts +8 -0
  25. package/src/locale/kz/kz.ts +8 -0
  26. package/src/locale/pl/pl.ts +8 -0
  27. package/src/locale/ro/ro.ts +8 -0
  28. package/src/locale/ru/ru.ts +8 -0
  29. package/src/locale/uk/uk.ts +8 -0
  30. package/src/locale/uz/uz.ts +8 -0
  31. package/src/locale/vi/vi.ts +8 -0
  32. package/src/validations/_shared/ip.validations.ts +10 -0
  33. package/src/validations/backendProfile/__tests__/backendProfile.validations.spec.ts +104 -0
  34. package/src/validations/backendProfile/backendProfile.validations.ts +79 -0
  35. package/src/validations/chatGateway/__tests__/chatGateway.validations.spec.ts +189 -0
  36. package/src/validations/chatGateway/chatGateway.validations.ts +452 -0
  37. package/src/validations/chatplan/__tests__/chatplan.validations.spec.ts +43 -0
  38. package/src/validations/chatplan/chatplan.validations.ts +17 -0
  39. package/src/validations/cognitiveProfile/__tests__/cognitiveProfile.validations.spec.ts +83 -0
  40. package/src/validations/cognitiveProfile/cognitiveProfile.validations.ts +74 -0
  41. package/src/validations/device/device.validations.ts +2 -8
  42. package/src/validations/gateway/__tests__/gateway.validations.spec.ts +168 -0
  43. package/src/validations/gateway/gateway.validations.ts +138 -0
  44. package/src/validations/index.ts +5 -0
  45. package/types/.tsbuildinfo +1 -1
  46. package/types/api/clients/_shared/appUrls.d.ts +11 -0
  47. package/types/api/clients/chatGateways/chatGatewayChannels.d.ts +62 -0
  48. package/types/api/clients/chatGateways/chatGateways.d.ts +4 -4
  49. package/types/api/clients/chatGateways/defaults/chatGatewayProviderDefaults.d.ts +21 -0
  50. package/types/api/clients/chatGateways/defaults/defaultChatGateway.d.ts +3 -1
  51. package/types/api/clients/chatGateways/defaults/webChatGateway.d.ts +5 -4
  52. package/types/api/clients/cognitiveProfiles/cognitiveProfiles.d.ts +1 -1
  53. package/types/api/clients/cognitiveProfiles/defaults/cognitiveProfileDefaults.d.ts +13 -0
  54. package/types/api/clients/index.d.ts +2 -0
  55. package/types/enums/ChatGatewayChannelSetup/ChatGatewayChannelSetup.d.ts +8 -0
  56. package/types/enums/GatewayIpAclProtocol/GatewayIpAclProtocol.d.ts +6 -0
  57. package/types/enums/GatewayRegisterState/GatewayRegisterState.d.ts +15 -0
  58. package/types/enums/StorageType/StorageType.d.ts +9 -0
  59. package/types/enums/WebchatAlternativeChannel/WebchatAlternativeChannel.d.ts +13 -0
  60. package/types/enums/index.d.ts +6 -1
  61. package/types/locale/en/en.d.ts +7 -0
  62. package/types/locale/es/es.d.ts +7 -0
  63. package/types/locale/index.d.ts +63 -0
  64. package/types/locale/kz/kz.d.ts +7 -0
  65. package/types/locale/pl/pl.d.ts +7 -0
  66. package/types/locale/ro/ro.d.ts +7 -0
  67. package/types/locale/ru/ru.d.ts +7 -0
  68. package/types/locale/uk/uk.d.ts +7 -0
  69. package/types/locale/uz/uz.d.ts +7 -0
  70. package/types/locale/vi/vi.d.ts +7 -0
  71. package/types/validations/_shared/ip.validations.d.ts +4 -0
  72. package/types/validations/backendProfile/backendProfile.validations.d.ts +24 -0
  73. package/types/validations/chatGateway/chatGateway.validations.d.ts +165 -0
  74. package/types/validations/chatplan/chatplan.validations.d.ts +14 -0
  75. package/types/validations/cognitiveProfile/cognitiveProfile.validations.d.ts +28 -0
  76. package/types/validations/gateway/gateway.validations.d.ts +48 -0
  77. package/types/validations/index.d.ts +5 -0
  78. package/src/api/clients/chatGateways/enums/WebchatAlternativeChannel.enum.ts +0 -12
  79. package/src/api/clients/gateways/defaults/defaultGateway.ts +0 -9
  80. package/src/api/clients/gateways/defaults/registerGateway.ts +0 -12
  81. package/src/api/clients/gateways/defaults/trunkingGateway.ts +0 -16
  82. package/types/api/clients/chatGateways/enums/WebchatAlternativeChannel.enum.d.ts +0 -12
  83. package/types/api/clients/gateways/defaults/defaultGateway.d.ts +0 -8
  84. package/types/api/clients/gateways/defaults/registerGateway.d.ts +0 -13
  85. package/types/api/clients/gateways/defaults/trunkingGateway.d.ts +0 -16
@@ -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(),
@@ -1,3 +1,5 @@
1
+ import { getShallowFieldsToSendFromZodSchema } from '@webitel/api-services/gen/utils';
2
+ import { cognitiveProfileSchema } from '@webitel/api-services/validations';
1
3
  import { getCognitiveProfileService } from '../../../gen-wire';
2
4
  import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
3
5
  import {
@@ -22,36 +24,10 @@ import type {
22
24
 
23
25
  const baseUrl = '/storage/cognitive_profiles';
24
26
 
25
- /**
26
- * `service` and `properties.region` are returned as the raw backend values.
27
- * Mapping them onto the localized service lookup and the Microsoft region
28
- * option object is the caller's job — those lookups are app-side.
29
- */
30
- const fieldsToSend = [
31
- 'name',
32
- 'default',
33
- 'enabled',
34
- 'provider',
35
- 'service',
36
- 'description',
37
- 'properties',
38
- ];
39
-
40
- /** The form binds `service` and `properties.region` as option objects. */
41
- const preRequestHandler = (item: ApiParams) => {
42
- const result = {
43
- ...item,
44
- service: item.service?.value ?? item.service,
45
- properties: {
46
- ...item.properties,
47
- },
48
- };
49
- if (result.properties.region) {
50
- result.properties.region =
51
- item.properties.region?.id ?? item.properties.region;
52
- }
53
- return result;
54
- };
27
+ /** [Claude] the card schema's fields; `id` goes in the url, not the body */
28
+ const fieldsToSend = getShallowFieldsToSendFromZodSchema(
29
+ cognitiveProfileSchema,
30
+ ).filter((field) => field !== 'id');
55
31
 
56
32
  const getCognitiveProfilesList = async (params: ApiParams) => {
57
33
  const defaultObject = {
@@ -116,7 +92,6 @@ const getCognitiveProfile = async ({ itemId: id }: GetItemParams) => {
116
92
 
117
93
  const addCognitiveProfile = async ({ itemInstance }: AddItemParams) => {
118
94
  const item = applyTransform(itemInstance, [
119
- preRequestHandler,
120
95
  sanitize(fieldsToSend),
121
96
  camelToSnake(),
122
97
  ]);
@@ -158,7 +133,6 @@ const updateCognitiveProfile = async ({
158
133
  itemId: id,
159
134
  }: UpdateItemParams) => {
160
135
  const item = applyTransform(itemInstance, [
161
- preRequestHandler,
162
136
  sanitize(fieldsToSend),
163
137
  camelToSnake(),
164
138
  ]);
@@ -212,3 +186,5 @@ export const CognitiveProfilesAPI = {
212
186
 
213
187
  ...generatePermissionsApi(baseUrl),
214
188
  };
189
+
190
+ export { getCognitiveProfileDefaults } from './defaults/cognitiveProfileDefaults';
@@ -0,0 +1,39 @@
1
+ import {
2
+ StorageProviderType,
3
+ StorageServiceType,
4
+ } from '@webitel/api-services/gen/models';
5
+
6
+ const DEFAULT_LOCALE = 'en-US';
7
+
8
+ /** [Claude] the settings each provider starts with, in `properties` */
9
+ const providerProperties: Partial<
10
+ Record<StorageProviderType, Record<string, string>>
11
+ > = {
12
+ [StorageProviderType.Microsoft]: {
13
+ key: '',
14
+ region: '',
15
+ locale: DEFAULT_LOCALE,
16
+ },
17
+ [StorageProviderType.Google]: {
18
+ key: '',
19
+ locale: DEFAULT_LOCALE,
20
+ },
21
+ [StorageProviderType.ElevenLabs]: {
22
+ key: '',
23
+ },
24
+ };
25
+
26
+ /** [Claude] a new cognitive profile of the provider chosen in the create popup */
27
+ export const getCognitiveProfileDefaults = (
28
+ provider?: StorageProviderType,
29
+ ) => ({
30
+ name: '',
31
+ default: false,
32
+ enabled: true,
33
+ provider,
34
+ service: StorageServiceType.Tts,
35
+ description: '',
36
+ properties: {
37
+ ...(provider && providerProperties[provider]),
38
+ },
39
+ });
@@ -0,0 +1,138 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ const instance = vi.hoisted(() => ({
4
+ get: vi.fn(),
5
+ post: vi.fn(),
6
+ put: vi.fn(),
7
+ patch: vi.fn(),
8
+ delete: vi.fn(),
9
+ }));
10
+
11
+ vi.mock('../../../defaults', async (importOriginal) => ({
12
+ ...(await importOriginal<typeof import('../../../defaults')>()),
13
+ getDefaultInstance: () => instance,
14
+ }));
15
+
16
+ import { GatewaysAPI } from '../gateways';
17
+
18
+ const common = {
19
+ name: 'gw',
20
+ proxy: 'sip.example.com',
21
+ schema: {
22
+ id: '1',
23
+ name: 'flow',
24
+ },
25
+ usage: '',
26
+ enable: true,
27
+ };
28
+
29
+ describe('GatewaysAPI', () => {
30
+ beforeEach(() => {
31
+ vi.clearAllMocks();
32
+ for (const method of Object.values(instance)) {
33
+ method.mockResolvedValue({
34
+ data: {},
35
+ });
36
+ }
37
+ });
38
+
39
+ it('sends only registration fields for a register gateway', async () => {
40
+ await GatewaysAPI.add({
41
+ itemInstance: {
42
+ ...common,
43
+ register: true,
44
+ account: 'user',
45
+ username: 'auth',
46
+ password: 'secret',
47
+ expires: 600,
48
+ host: 'leftover',
49
+ ipacl: [],
50
+ rState: 3,
51
+ },
52
+ });
53
+
54
+ expect(instance.post).toHaveBeenCalledWith('/sip/gateways', {
55
+ ...common,
56
+ register: true,
57
+ account: 'user',
58
+ username: 'auth',
59
+ password: 'secret',
60
+ expires: 600,
61
+ });
62
+ });
63
+
64
+ it('sends only trunking fields for a trunking gateway', async () => {
65
+ await GatewaysAPI.update({
66
+ itemId: '5',
67
+ itemInstance: {
68
+ ...common,
69
+ register: false,
70
+ host: '10.0.0.2',
71
+ ipacl: [
72
+ {
73
+ ip: '10.0.0.3',
74
+ proto: 'any',
75
+ port: null,
76
+ },
77
+ ],
78
+ account: 'leftover',
79
+ expires: 600,
80
+ },
81
+ });
82
+
83
+ expect(instance.put).toHaveBeenCalledWith('/sip/gateways/5', {
84
+ ...common,
85
+ register: false,
86
+ host: '10.0.0.2',
87
+ ipacl: [
88
+ {
89
+ ip: '10.0.0.3',
90
+ proto: 'any',
91
+ port: null,
92
+ },
93
+ ],
94
+ });
95
+ });
96
+
97
+ it('patches the changed field only', async () => {
98
+ await GatewaysAPI.patch({
99
+ id: '5',
100
+ changes: {
101
+ enable: false,
102
+ },
103
+ });
104
+
105
+ expect(instance.patch).toHaveBeenCalledWith('/sip/gateways/5', {
106
+ enable: false,
107
+ });
108
+ });
109
+
110
+ it('fills trunking defaults on get', async () => {
111
+ instance.get.mockResolvedValue({
112
+ data: {
113
+ id: '5',
114
+ register: false,
115
+ ipacl: [
116
+ {
117
+ ip: '10.0.0.3',
118
+ },
119
+ ],
120
+ },
121
+ });
122
+
123
+ expect(
124
+ await GatewaysAPI.get({
125
+ itemId: '5',
126
+ }),
127
+ ).toMatchObject({
128
+ host: '',
129
+ ipacl: [
130
+ {
131
+ ip: '10.0.0.3',
132
+ proto: 'any',
133
+ port: null,
134
+ },
135
+ ],
136
+ });
137
+ });
138
+ });
@@ -1,3 +1,4 @@
1
+ import { GatewayIpAclProtocol } from '../../../enums/GatewayIpAclProtocol/GatewayIpAclProtocol';
1
2
  import {
2
3
  getDefaultGetListResponse,
3
4
  getDefaultGetParams,
@@ -13,6 +14,7 @@ import {
13
14
  sanitize,
14
15
  snakeToCamel,
15
16
  starToSearch,
17
+ translateError,
16
18
  } from '../../transformers';
17
19
  import type {
18
20
  AddItemParams,
@@ -22,8 +24,6 @@ import type {
22
24
  PatchItemParams,
23
25
  UpdateItemParams,
24
26
  } from '../_shared/types';
25
- import registerGateway from './defaults/registerGateway';
26
- import trunkingGateway from './defaults/trunkingGateway';
27
27
 
28
28
  const instance = getDefaultInstance();
29
29
 
@@ -78,45 +78,41 @@ const getGatewayList = async (params: ApiParams) => {
78
78
  };
79
79
 
80
80
  const getGateway = async ({ itemId: id }: GetItemParams) => {
81
- const coerceTrunkingResponse = (response: ApiParams) => {
82
- const defaultIPacl = {
83
- ip: '',
84
- proto: 'any',
85
- port: null,
86
- };
87
-
88
- const result = {
89
- ...trunkingGateway(),
90
- ...response,
91
- };
92
- result.ipacl = result.ipacl.map((acl) => ({
93
- ...defaultIPacl,
94
- ...acl,
95
- }));
96
- return result;
97
- };
98
-
99
- const coerceRegisterResponse = (response: ApiParams) => {
100
- const result = {
101
- ...registerGateway(),
102
- ...response,
103
- };
104
- return result;
81
+ /** [Claude] one set for both types: on save each type sends only its own fields */
82
+ const defaultObject = {
83
+ name: '',
84
+ usage: '',
85
+ proxy: '',
86
+ schema: {},
87
+ enable: true,
88
+ account: '',
89
+ username: '',
90
+ password: '',
91
+ expires: 600,
92
+ host: '',
93
+ ipacl: [],
105
94
  };
106
95
 
107
- const itemResponseHandler = (response: ApiParams) => {
108
- if (response.register) return coerceRegisterResponse(response);
109
- return coerceTrunkingResponse(response);
96
+ const defaultIpAcl = {
97
+ ip: '',
98
+ proto: GatewayIpAclProtocol.Any,
99
+ port: null,
110
100
  };
111
101
 
112
102
  const url = `${baseUrl}/${id}`;
113
103
 
114
104
  try {
115
105
  const response = await instance.get(url);
116
- return applyTransform(response.data, [
106
+ const item = applyTransform(response.data, [
117
107
  snakeToCamel(),
118
- itemResponseHandler,
108
+ merge(defaultObject),
119
109
  ]);
110
+ return {
111
+ ...item,
112
+ ipacl: applyTransform(item.ipacl, [
113
+ mergeEach(defaultIpAcl),
114
+ ]),
115
+ };
120
116
  } catch (err) {
121
117
  throw applyTransform(err, [
122
118
  notify,
@@ -124,37 +120,46 @@ const getGateway = async ({ itemId: id }: GetItemParams) => {
124
120
  }
125
121
  };
126
122
 
127
- const fieldsToSend = [
123
+ const commonFieldsToSend = [
128
124
  'name',
129
125
  'proxy',
130
- 'id',
131
- 'host',
132
- 'ipacl',
126
+ 'register',
127
+ 'schema',
128
+ 'usage',
129
+ 'enable',
130
+ ];
131
+
132
+ const registerFieldsToSend = [
133
+ ...commonFieldsToSend,
133
134
  'account',
134
135
  'username',
135
136
  'expires',
136
- 'account',
137
137
  'registrar',
138
- 'name',
139
- 'register',
140
138
  'password',
141
- 'schema',
142
- 'usage',
143
- 'enable',
144
139
  ];
145
140
 
146
- const addGateway = async ({ itemInstance }: AddItemParams) => {
147
- const item = applyTransform(itemInstance, [
148
- sanitize(fieldsToSend),
141
+ const trunkingFieldsToSend = [
142
+ ...commonFieldsToSend,
143
+ 'host',
144
+ 'ipacl',
145
+ ];
146
+
147
+ /** [Claude] each gateway type sends only its own fields */
148
+ const toRequestItem = (item: ApiParams) =>
149
+ applyTransform(item, [
150
+ sanitize(item.register ? registerFieldsToSend : trunkingFieldsToSend),
149
151
  camelToSnake(),
150
152
  ]);
153
+
154
+ const addGateway = async ({ itemInstance }: AddItemParams) => {
151
155
  try {
152
- const response = await instance.post(baseUrl, item);
156
+ const response = await instance.post(baseUrl, toRequestItem(itemInstance));
153
157
  return applyTransform(response.data, [
154
158
  snakeToCamel(),
155
159
  ]);
156
160
  } catch (err) {
157
161
  throw applyTransform(err, [
162
+ translateError,
158
163
  notify,
159
164
  ]);
160
165
  }
@@ -163,27 +168,29 @@ const updateGateway = async ({
163
168
  itemInstance,
164
169
  itemId: id,
165
170
  }: UpdateItemParams) => {
166
- const item = applyTransform(itemInstance, [
167
- sanitize(fieldsToSend),
168
- camelToSnake(),
169
- ]);
170
-
171
171
  const url = `${baseUrl}/${id}`;
172
172
  try {
173
- const response = await instance.put(url, item);
173
+ const response = await instance.put(url, toRequestItem(itemInstance));
174
174
  return applyTransform(response.data, [
175
175
  snakeToCamel(),
176
176
  ]);
177
177
  } catch (err) {
178
178
  throw applyTransform(err, [
179
+ translateError,
179
180
  notify,
180
181
  ]);
181
182
  }
182
183
  };
183
184
 
185
+ /** [Claude] a patch carries only the changed fields, so the type is unknown here */
184
186
  const patchGateway = async ({ changes, id }: PatchItemParams) => {
185
187
  const body = applyTransform(changes, [
186
- sanitize(fieldsToSend),
188
+ sanitize([
189
+ ...new Set([
190
+ ...registerFieldsToSend,
191
+ ...trunkingFieldsToSend,
192
+ ]),
193
+ ]),
187
194
  camelToSnake(),
188
195
  ]);
189
196
  const url = `${baseUrl}/${id}`;
@@ -194,6 +201,7 @@ const patchGateway = async ({ changes, id }: PatchItemParams) => {
194
201
  ]);
195
202
  } catch (err) {
196
203
  throw applyTransform(err, [
204
+ translateError,
197
205
  notify,
198
206
  ]);
199
207
  }