@webitel/api-services 1.0.6 → 1.0.8

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 (64) hide show
  1. package/package.json +2 -1
  2. package/src/api/clients/calendars/__tests__/calendars.spec.ts +71 -0
  3. package/src/api/clients/calendars/calendars.ts +2 -2
  4. package/src/api/clients/caseComments/caseComments.ts +141 -0
  5. package/src/api/clients/caseFiles/caseFiles.ts +79 -0
  6. package/src/api/clients/caseLinks/caseLinks.ts +150 -0
  7. package/src/api/clients/caseRelatedCases/caseRelatedCases.ts +125 -0
  8. package/src/api/clients/caseServices/services.ts +38 -72
  9. package/src/api/clients/cases/_internals/ftsService.ts +67 -0
  10. package/src/api/clients/cases/_internals/stringifyCaseFilters.ts +156 -0
  11. package/src/api/clients/cases/cases.ts +360 -0
  12. package/src/api/clients/chatMessagesHistory/chatMessagesHistory.ts +64 -0
  13. package/src/api/clients/configurations/configurations.ts +56 -39
  14. package/src/api/clients/contactCases/contactCases.ts +109 -0
  15. package/src/api/clients/emails/emails.ts +11 -7
  16. package/src/api/clients/index.ts +9 -0
  17. package/src/api/clients/labels/labels.ts +24 -29
  18. package/src/api/clients/phones/phones.ts +11 -7
  19. package/src/api/clients/timeline/timeline.ts +124 -0
  20. package/src/api/clients/variables/variables.ts +10 -5
  21. package/src/api/clients/wtTypes/_shared/utils/assignFieldPositions.ts +28 -0
  22. package/src/api/clients/wtTypes/_shared/utils/sortDynamicFields.ts +7 -9
  23. package/src/api/clients/wtTypes/adjunctTypeRecords/adjunctTypeRecords.ts +19 -16
  24. package/src/api/clients/wtTypes/adjunctTypes/adjunctTypes.ts +4 -0
  25. package/src/api/clients/wtTypes/typeExtensions/typeExtensions.ts +21 -31
  26. package/src/api/clients//321/201ontacts/contacts.ts +103 -234
  27. package/src/api/clients//321/201ontacts/getContactAccessFromMode.ts +9 -0
  28. package/src/api/clients//321/201ontacts/index.ts +1 -1
  29. package/src/validations/adjunctType/adjunctType.validations.ts +18 -0
  30. package/src/validations/case/case.validations.ts +33 -0
  31. package/src/validations/caseClose/caseClose.validations.ts +30 -0
  32. package/src/validations/caseExportOptions/caseExportOptions.validations.ts +27 -0
  33. package/src/validations/contact/contact.validations.ts +12 -7
  34. package/src/validations/index.ts +5 -0
  35. package/src/validations/typeExtension/typeExtension.validations.ts +8 -0
  36. package/types/.tsbuildinfo +1 -1
  37. package/types/api/clients/caseComments/caseComments.d.ts +20 -0
  38. package/types/api/clients/caseFiles/caseFiles.d.ts +10 -0
  39. package/types/api/clients/caseLinks/caseLinks.d.ts +22 -0
  40. package/types/api/clients/caseRelatedCases/caseRelatedCases.d.ts +14 -0
  41. package/types/api/clients/caseServices/services.d.ts +3 -12
  42. package/types/api/clients/cases/_internals/ftsService.d.ts +7 -0
  43. package/types/api/clients/cases/_internals/stringifyCaseFilters.d.ts +2 -0
  44. package/types/api/clients/cases/cases.d.ts +35 -0
  45. package/types/api/clients/chatMessagesHistory/chatMessagesHistory.d.ts +15 -0
  46. package/types/api/clients/contactCases/contactCases.d.ts +9 -0
  47. package/types/api/clients/index.d.ts +9 -0
  48. package/types/api/clients/labels/labels.d.ts +2 -2
  49. package/types/api/clients/timeline/timeline.d.ts +16 -0
  50. package/types/api/clients/wtTypes/_shared/utils/assignFieldPositions.d.ts +2 -0
  51. package/types/api/clients/wtTypes/_shared/utils/sortDynamicFields.d.ts +2 -2
  52. package/types/api/clients/wtTypes/adjunctTypeRecords/adjunctTypeRecords.d.ts +11 -11
  53. package/types/api/clients/wtTypes/typeExtensions/typeExtensions.d.ts +1 -1
  54. package/types/api/clients//321/201ontacts/getContactAccessFromMode.d.ts +5 -0
  55. package/types/api/clients//321/201ontacts/index.d.ts +1 -1
  56. package/types/validations/adjunctType/adjunctType.validations.d.ts +11 -0
  57. package/types/validations/case/case.validations.d.ts +31 -0
  58. package/types/validations/caseClose/caseClose.validations.d.ts +8 -0
  59. package/types/validations/caseExportOptions/caseExportOptions.validations.d.ts +9 -0
  60. package/types/validations/contact/contact.validations.d.ts +15 -15
  61. package/types/validations/index.d.ts +5 -0
  62. package/types/validations/typeExtension/typeExtension.validations.d.ts +20 -0
  63. package/src/api/clients//321/201ontacts/types/ContactEntity.types.ts +0 -25
  64. package/types/api/clients//321/201ontacts/types/ContactEntity.types.d.ts +0 -14
@@ -0,0 +1,360 @@
1
+ import { getShallowFieldsToSendFromZodSchema } from '@webitel/api-services/gen/utils';
2
+ import { snakeToKebab } from '@webitel/api-services/utils';
3
+ import {
4
+ CreateCaseBody,
5
+ getCases,
6
+ UpdateCase2Body,
7
+ UpdateCaseBody,
8
+ } from '../../../gen-wire';
9
+ import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
10
+ import {
11
+ applyTransform,
12
+ camelToSnake,
13
+ merge,
14
+ notify,
15
+ sanitizeToWire,
16
+ snakeToCamel,
17
+ } from '../../transformers';
18
+ import { generatePermissionsApi } from '../_shared/generatePermissionsApi';
19
+ import type {
20
+ AddItemParams,
21
+ ApiId,
22
+ ApiParams,
23
+ DeleteItemParams,
24
+ GetItemParams,
25
+ } from '../_shared/types';
26
+ import { FTSServiceAPI } from './_internals/ftsService';
27
+ import { stringifyCaseFilters } from './_internals/stringifyCaseFilters';
28
+
29
+ const casesService = getCases();
30
+
31
+ const baseUrl = '/cases';
32
+
33
+ function transformSourceType(data: ApiParams | ApiParams[]) {
34
+ if (Array.isArray(data)) {
35
+ return data.map((item) => {
36
+ if (item.source?.type) {
37
+ item.source.type = snakeToKebab(item.source.type.toLowerCase());
38
+ }
39
+ return item;
40
+ });
41
+ }
42
+
43
+ if (data.source?.type) {
44
+ data.source.type = snakeToKebab(data.source.type.toLowerCase());
45
+ }
46
+ return data;
47
+ }
48
+
49
+ const transformCustomFields = (data: ApiParams) => {
50
+ if (!data.custom) {
51
+ data.custom = {};
52
+ }
53
+
54
+ return data;
55
+ };
56
+
57
+ const checkCustomFields = (data: ApiParams) => {
58
+ if (!Object.keys(data.custom).length) {
59
+ delete data.custom;
60
+ }
61
+
62
+ return data;
63
+ };
64
+
65
+ const getCasesList = async (params: ApiParams) => {
66
+ let ftsIds: string[] | undefined;
67
+ const { fts } = params;
68
+ if (fts) {
69
+ try {
70
+ const { items } = await FTSServiceAPI.getList({
71
+ page: params.page,
72
+ size: params.size,
73
+ fts: params.fts,
74
+ sort: params.sort,
75
+ objectName: [
76
+ 'cases',
77
+ 'case_comments',
78
+ ],
79
+ });
80
+ ftsIds = items.map(({ id }: ApiParams) => id);
81
+ } catch {
82
+ // skip error, load cases without fts
83
+ }
84
+ }
85
+
86
+ const { page, size, q, ids, sort, fields, options, ...filters } =
87
+ applyTransform(
88
+ {
89
+ ...params,
90
+ ids: params.ids || ftsIds,
91
+ },
92
+ [
93
+ merge(getDefaultGetParams()),
94
+ (params) => ({
95
+ ...params,
96
+ q: params.search,
97
+ }),
98
+ ],
99
+ );
100
+
101
+ try {
102
+ const response = await casesService.searchCases(
103
+ {
104
+ page,
105
+ size,
106
+ q,
107
+ ids,
108
+ sort,
109
+ fields: [
110
+ 'custom',
111
+ 'priority',
112
+ 'description',
113
+ 'comments',
114
+ ...fields,
115
+ ],
116
+ filters: stringifyCaseFilters(filters),
117
+ },
118
+ options,
119
+ );
120
+
121
+ const { items, next } = applyTransform(response.data, [
122
+ merge(getDefaultGetListResponse()),
123
+ ]);
124
+ return {
125
+ items: applyTransform(items, [
126
+ snakeToCamel(),
127
+ transformSourceType,
128
+ ]),
129
+ next,
130
+ };
131
+ } catch (err) {
132
+ throw applyTransform(err, [
133
+ notify,
134
+ ]);
135
+ }
136
+ };
137
+
138
+ const caseFieldsToSend = [
139
+ 'etag',
140
+ 'id',
141
+ 'name',
142
+ 'subject',
143
+ 'description',
144
+ 'contact_info',
145
+ 'created_at',
146
+ 'planned_reaction_at',
147
+ 'planned_resolve_at',
148
+ 'status_lookup',
149
+ 'close_reason_lookup',
150
+ 'author',
151
+ 'assignee',
152
+ 'reporter',
153
+ 'impacted',
154
+ 'group',
155
+ 'priority',
156
+ 'source',
157
+ 'status',
158
+ 'close_reason',
159
+ 'close_result',
160
+ 'rating',
161
+ 'rating_comment',
162
+ 'reacted_at',
163
+ 'resolved_at',
164
+ 'sla_condition',
165
+ 'difference_in_reaction',
166
+ 'difference_in_resolve',
167
+ 'sla',
168
+ 'service',
169
+ 'comments',
170
+ 'related_cases',
171
+ 'links',
172
+ 'status_condition',
173
+ 'created_by',
174
+ 'custom',
175
+ ];
176
+
177
+ const getCase = async ({ itemId: id }: GetItemParams) => {
178
+ try {
179
+ const response = await casesService.locateCase(String(id), {
180
+ fields: caseFieldsToSend,
181
+ });
182
+ return applyTransform(response.data, [
183
+ snakeToCamel([
184
+ 'custom',
185
+ ]),
186
+ transformCustomFields,
187
+ transformSourceType,
188
+ ]);
189
+ } catch (err) {
190
+ throw applyTransform(err, [
191
+ notify,
192
+ ]);
193
+ }
194
+ };
195
+
196
+ const deleteCase = async ({ id }: DeleteItemParams) => {
197
+ try {
198
+ const response = await casesService.deleteCase(String(id));
199
+ return applyTransform(response.data, []);
200
+ } catch (err) {
201
+ throw applyTransform(err, [
202
+ notify,
203
+ ]);
204
+ }
205
+ };
206
+
207
+ const updateFieldsToSend = getShallowFieldsToSendFromZodSchema(UpdateCaseBody);
208
+
209
+ const updateCase = async ({ itemInstance }: AddItemParams) => {
210
+ const { etag } = itemInstance;
211
+
212
+ const item = applyTransform(itemInstance, [
213
+ sanitizeToWire(updateFieldsToSend),
214
+ camelToSnake([
215
+ 'custom',
216
+ ]),
217
+ checkCustomFields,
218
+ ]);
219
+
220
+ try {
221
+ const response = await casesService.updateCase(etag, item, {
222
+ fields: caseFieldsToSend,
223
+ });
224
+ return applyTransform(response.data.case, [
225
+ snakeToCamel([
226
+ 'custom',
227
+ ]),
228
+ transformCustomFields,
229
+ transformSourceType,
230
+ ]);
231
+ } catch (err) {
232
+ throw applyTransform(err, [
233
+ notify,
234
+ ]);
235
+ }
236
+ };
237
+
238
+ const addFieldsToSend = getShallowFieldsToSendFromZodSchema(CreateCaseBody);
239
+
240
+ const addCase = async ({ itemInstance }: AddItemParams) => {
241
+ const item = applyTransform(itemInstance, [
242
+ sanitizeToWire(addFieldsToSend),
243
+ camelToSnake([
244
+ 'custom',
245
+ ]),
246
+ ]);
247
+ try {
248
+ const response = await casesService.createCase(item);
249
+ return applyTransform(response.data, [
250
+ snakeToCamel(),
251
+ ]);
252
+ } catch (err) {
253
+ throw applyTransform(err, [
254
+ notify,
255
+ ]);
256
+ }
257
+ };
258
+
259
+ const patchCase = async ({
260
+ changes,
261
+ etag,
262
+ }: {
263
+ changes: ApiParams;
264
+ etag: ApiId;
265
+ }) => {
266
+ const body = applyTransform(changes, [
267
+ sanitizeToWire(getShallowFieldsToSendFromZodSchema(UpdateCase2Body)),
268
+ camelToSnake(),
269
+ ]);
270
+ try {
271
+ const response = await casesService.updateCase2(String(etag), body, {
272
+ fields: caseFieldsToSend,
273
+ });
274
+ return applyTransform(response.data.case, [
275
+ snakeToCamel([
276
+ 'custom',
277
+ ]),
278
+ transformCustomFields,
279
+ transformSourceType,
280
+ ]);
281
+ } catch (err) {
282
+ throw applyTransform(err, [
283
+ notify,
284
+ ]);
285
+ }
286
+ };
287
+
288
+ const exportCase = async (params: ApiParams) => {
289
+ const { q, sort, fields, options, format, separator, ids, ...filters } =
290
+ applyTransform(
291
+ {
292
+ ...params,
293
+ },
294
+ [
295
+ merge(getDefaultGetParams()),
296
+ (params) => ({
297
+ ...params,
298
+ q: params.search,
299
+ }),
300
+ ],
301
+ );
302
+
303
+ delete filters.page;
304
+ delete filters.size;
305
+
306
+ const exportParams: Record<string, unknown> = {
307
+ q,
308
+ sort,
309
+ fields,
310
+ format,
311
+ separator,
312
+ filters: stringifyCaseFilters(filters),
313
+ };
314
+
315
+ if (ids?.length) {
316
+ exportParams.ids = ids;
317
+ }
318
+
319
+ try {
320
+ const response = await casesService.exportCases(exportParams, {
321
+ ...options,
322
+ responseType: 'blob',
323
+ });
324
+
325
+ return {
326
+ response,
327
+ };
328
+ } catch (err) {
329
+ throw applyTransform(err, [
330
+ notify,
331
+ ]);
332
+ }
333
+ };
334
+
335
+ const getCasesLookup = (params: Parameters<typeof getCasesList>[0]) =>
336
+ getCasesList({
337
+ ...params,
338
+ fields: params.fields || [
339
+ 'id',
340
+ 'name',
341
+ 'subject',
342
+ 'priority',
343
+ ],
344
+ });
345
+
346
+ export const CasesAPI = {
347
+ getList: getCasesList,
348
+ getLookup: getCasesLookup,
349
+ get: getCase,
350
+ delete: deleteCase,
351
+ update: updateCase,
352
+ add: addCase,
353
+ patch: patchCase,
354
+ exportData: exportCase,
355
+
356
+ ...generatePermissionsApi(baseUrl),
357
+ };
358
+
359
+ export { FTSServiceAPI } from './_internals/ftsService';
360
+ export { stringifyCaseFilters } from './_internals/stringifyCaseFilters';
@@ -0,0 +1,64 @@
1
+ import { getCasesChatCatalog, getContactsChatCatalog } from '../../../gen-wire';
2
+ import { applyTransform, notify, snakeToCamel } from '../../transformers';
3
+ import type { ApiId, ApiParams } from '../_shared/types';
4
+
5
+ const mergeChatMessagesData = ({
6
+ messages,
7
+ peers,
8
+ }: {
9
+ messages: ApiParams[];
10
+ peers: ApiParams[];
11
+ }) => {
12
+ if (!messages) return [];
13
+ return messages.map(({ from, ...message }) => {
14
+ return {
15
+ ...message,
16
+ peer: from && peers ? peers[from.id - 1] : undefined,
17
+ };
18
+ });
19
+ };
20
+
21
+ const buildGetChatHistory =
22
+ (
23
+ getChatHistory: (
24
+ parentId: string,
25
+ taskId: string,
26
+ ) => Promise<{
27
+ data: ApiParams;
28
+ }>,
29
+ ) =>
30
+ async ({ parentId, taskId }: { parentId: ApiId; taskId: ApiId }) => {
31
+ try {
32
+ const response = await getChatHistory(String(parentId), String(taskId));
33
+ const { messages, peers } = applyTransform(response.data, [
34
+ snakeToCamel(),
35
+ ]);
36
+ return {
37
+ items: applyTransform(
38
+ {
39
+ messages,
40
+ peers,
41
+ },
42
+ [
43
+ mergeChatMessagesData,
44
+ ],
45
+ ),
46
+ };
47
+ } catch (err) {
48
+ throw applyTransform(err, [
49
+ notify,
50
+ ]);
51
+ }
52
+ };
53
+
54
+ export const ChatMessagesHistoryAPI = {
55
+ getContactChatHistory: buildGetChatHistory((contactId, chatId) =>
56
+ getContactsChatCatalog().contactsChatCatalogGetContactChatHistory(
57
+ contactId,
58
+ chatId,
59
+ ),
60
+ ),
61
+ getCaseChatHistory: buildGetChatHistory((caseId, chatId) =>
62
+ getCasesChatCatalog().casesChatCatalogGetCaseChatHistory(caseId, chatId),
63
+ ),
64
+ };
@@ -1,16 +1,17 @@
1
- import { SystemSettingServiceApiFactory } from 'webitel-sdk';
1
+ import { getShallowFieldsToSendFromZodSchema } from '@webitel/api-services/gen/utils';
2
2
  import {
3
- getDefaultGetListResponse,
4
- getDefaultGetParams,
5
- getDefaultInstance,
6
- getDefaultOpenAPIConfig,
7
- } from '../../defaults';
3
+ CreateSystemSettingBody,
4
+ getSystemSettingService,
5
+ SearchAvailableSystemSettingQueryParams,
6
+ SearchSystemSettingQueryParams,
7
+ } from '../../../gen-wire';
8
+ import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
8
9
  import {
9
10
  applyTransform,
10
11
  camelToSnake,
11
12
  merge,
12
13
  notify,
13
- sanitize,
14
+ sanitizeToWire,
14
15
  snakeToCamel,
15
16
  starToSearch,
16
17
  } from '../../transformers';
@@ -22,30 +23,30 @@ import type {
22
23
  UpdateItemParams,
23
24
  } from '../_shared/types';
24
25
 
25
- const instance = getDefaultInstance();
26
- const configuration = getDefaultOpenAPIConfig();
27
-
28
- const configurationService = SystemSettingServiceApiFactory(
29
- configuration,
30
- '',
31
- instance,
32
- );
33
-
34
26
  const getList = async (params: ApiParams) => {
35
- const { page, size, search, sort, fields, name } = applyTransform(params, [
27
+ const listFieldsToSend = getShallowFieldsToSendFromZodSchema(
28
+ SearchSystemSettingQueryParams,
29
+ );
30
+
31
+ const { page, size, fields, sort, name, q } = applyTransform(params, [
36
32
  merge(getDefaultGetParams()),
37
- starToSearch('search'),
33
+ merge({
34
+ q: params.search,
35
+ }),
36
+ sanitizeToWire(listFieldsToSend),
37
+ starToSearch('q'),
38
+ camelToSnake(),
38
39
  ]);
39
40
 
40
41
  try {
41
- const response = await configurationService.searchSystemSetting(
42
+ const response = await getSystemSettingService().searchSystemSetting({
42
43
  page,
43
44
  size,
44
- search,
45
- sort,
46
45
  fields,
46
+ sort,
47
47
  name,
48
- );
48
+ q,
49
+ });
49
50
  const { items, next } = applyTransform(response.data, [
50
51
  snakeToCamel(),
51
52
  merge(getDefaultGetListResponse()),
@@ -63,7 +64,9 @@ const getList = async (params: ApiParams) => {
63
64
 
64
65
  const get = async ({ itemId: id }: GetItemParams) => {
65
66
  try {
66
- const response = await configurationService.readSystemSetting(Number(id));
67
+ const response = await getSystemSettingService().readSystemSetting(
68
+ Number(id),
69
+ );
67
70
  return applyTransform(response.data, [
68
71
  snakeToCamel(),
69
72
  ]);
@@ -74,19 +77,21 @@ const get = async ({ itemId: id }: GetItemParams) => {
74
77
  }
75
78
  };
76
79
 
80
+ // `value` is a free-form `google.protobuf.Value` on the backend and has no
81
+ // fixed shape, so it's not part of the generated body schema — kept on the
82
+ // allowlist by hand alongside it (mirrors `custom` on contacts).
77
83
  const fieldsToSend = [
78
- 'id',
79
- 'name',
84
+ ...getShallowFieldsToSendFromZodSchema(CreateSystemSettingBody),
80
85
  'value',
81
86
  ];
82
87
 
83
88
  const add = async ({ itemInstance }: AddItemParams) => {
84
89
  const item = applyTransform(itemInstance, [
85
- sanitize(fieldsToSend),
90
+ sanitizeToWire(fieldsToSend),
86
91
  camelToSnake(),
87
92
  ]);
88
93
  try {
89
- const response = await configurationService.createSystemSetting(item);
94
+ const response = await getSystemSettingService().createSystemSetting(item);
90
95
  return applyTransform(response.data, [
91
96
  snakeToCamel(),
92
97
  ]);
@@ -99,11 +104,11 @@ const add = async ({ itemInstance }: AddItemParams) => {
99
104
 
100
105
  const update = async ({ itemInstance, itemId: id }: UpdateItemParams) => {
101
106
  const item = applyTransform(itemInstance, [
102
- sanitize(fieldsToSend),
107
+ sanitizeToWire(fieldsToSend),
103
108
  camelToSnake(),
104
109
  ]);
105
110
  try {
106
- const response = await configurationService.updateSystemSetting(
111
+ const response = await getSystemSettingService().updateSystemSetting(
107
112
  Number(id),
108
113
  item,
109
114
  );
@@ -127,7 +132,9 @@ const getLookup = (params: Parameters<typeof getList>[0]) =>
127
132
 
128
133
  const deleteItem = async ({ id }: DeleteItemParams) => {
129
134
  try {
130
- const response = await configurationService.deleteSystemSetting(Number(id));
135
+ const response = await getSystemSettingService().deleteSystemSetting(
136
+ Number(id),
137
+ );
131
138
  return applyTransform(response.data, []);
132
139
  } catch (err) {
133
140
  throw applyTransform(err, [
@@ -137,19 +144,29 @@ const deleteItem = async ({ id }: DeleteItemParams) => {
137
144
  };
138
145
 
139
146
  const getObjectsList = async (params: ApiParams) => {
140
- const { page, size, search, sort, fields } = applyTransform(params, [
147
+ const listFieldsToSend = getShallowFieldsToSendFromZodSchema(
148
+ SearchAvailableSystemSettingQueryParams,
149
+ );
150
+
151
+ const { page, size, fields, sort, q } = applyTransform(params, [
141
152
  merge(getDefaultGetParams()),
142
- starToSearch('search'),
153
+ merge({
154
+ q: params.search,
155
+ }),
156
+ sanitizeToWire(listFieldsToSend),
157
+ starToSearch('q'),
158
+ camelToSnake(),
143
159
  ]);
144
160
 
145
161
  try {
146
- const response = await configurationService.searchAvailableSystemSetting(
147
- page,
148
- size,
149
- search,
150
- sort,
151
- fields,
152
- );
162
+ const response =
163
+ await getSystemSettingService().searchAvailableSystemSetting({
164
+ page,
165
+ size,
166
+ fields,
167
+ sort,
168
+ q,
169
+ });
153
170
  const { items, next } = applyTransform(response.data, [
154
171
  snakeToCamel(),
155
172
  merge(getDefaultGetListResponse()),
@@ -0,0 +1,109 @@
1
+ import { snakeToKebab } from '@webitel/api-services/utils';
2
+ import { getCases } from '../../../gen-wire';
3
+ import { getDefaultGetListResponse, getDefaultGetParams } from '../../defaults';
4
+ import {
5
+ applyTransform,
6
+ merge,
7
+ notify,
8
+ snakeToCamel,
9
+ } from '../../transformers';
10
+ import type { ApiId, ApiParams } from '../_shared/types';
11
+ import { FTSServiceAPI } from '../cases/_internals/ftsService';
12
+ import { stringifyCaseFilters } from '../cases/_internals/stringifyCaseFilters';
13
+
14
+ function transformSourceType(data: ApiParams) {
15
+ if (Array.isArray(data)) {
16
+ return data.map((item) => {
17
+ if (item.source?.type) {
18
+ item.source.type = snakeToKebab(item.source.type.toLowerCase());
19
+ }
20
+ return item;
21
+ });
22
+ }
23
+
24
+ if (data.source?.type) {
25
+ data.source.type = snakeToKebab(data.source.type.toLowerCase());
26
+ }
27
+ return data;
28
+ }
29
+
30
+ const getContactCasesList = async ({
31
+ parentId,
32
+ ...params
33
+ }: {
34
+ parentId: ApiId;
35
+ } & ApiParams) => {
36
+ let ftsIds: Array<string | number> | undefined;
37
+ const { fts } = params;
38
+ if (fts) {
39
+ try {
40
+ const { items } = await FTSServiceAPI.getList({
41
+ page: params.page,
42
+ size: params.size,
43
+ fts: params.fts,
44
+ sort: params.sort,
45
+ objectName: [
46
+ 'cases',
47
+ 'case_comments',
48
+ ],
49
+ });
50
+ ftsIds = items.map(({ id }: ApiParams) => id);
51
+ } catch {
52
+ // skip error, load cases without fts
53
+ }
54
+ }
55
+
56
+ /* everything left over after pulling out the technical query params is a
57
+ * filter, keyed by the (camelCase) filter names stringifyCaseFilters expects */
58
+ const { page, size, q, ids, sort, fields, ...filters } = applyTransform(
59
+ {
60
+ ...params,
61
+ ids: params.ids || ftsIds,
62
+ },
63
+ [
64
+ merge(getDefaultGetParams()),
65
+ (params) => ({
66
+ ...params,
67
+ q: params.search,
68
+ }),
69
+ ],
70
+ );
71
+
72
+ try {
73
+ const response = await getCases().searchCases2(String(parentId), {
74
+ page,
75
+ size,
76
+ q,
77
+ ids,
78
+ sort,
79
+ fields: [
80
+ ...(fields || []),
81
+ 'etag',
82
+ 'id',
83
+ 'custom',
84
+ ],
85
+ filters: stringifyCaseFilters(filters),
86
+ });
87
+
88
+ const { items, next } = applyTransform(response.data, [
89
+ merge(getDefaultGetListResponse()),
90
+ ]);
91
+ return {
92
+ items: applyTransform(items, [
93
+ snakeToCamel([
94
+ 'custom',
95
+ ]),
96
+ transformSourceType,
97
+ ]),
98
+ next,
99
+ };
100
+ } catch (err) {
101
+ throw applyTransform(err, [
102
+ notify,
103
+ ]);
104
+ }
105
+ };
106
+
107
+ export const ContactCasesAPI = {
108
+ getList: getContactCasesList,
109
+ };