@webitel/api-services 26.12.20 → 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.20",
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,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;
@@ -28,6 +28,7 @@ export * from './caseServiceCatalog/caseServiceCatalog.validations';
28
28
  export * from './caseSource/caseSource.validations';
29
29
  export * from './caseStatus/caseStatus.validations';
30
30
  export * from './caseStatusCondition/caseStatusCondition.validations';
31
+ export * from './chatplan/chatplan.validations';
31
32
  export * from './communicationType/communicationType.validations';
32
33
  export * from './contact/contact.validations';
33
34
  export * from './contactEmail/contactEmail.validations';