@webitel/api-services 26.8.45 → 26.8.47

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.8.45",
3
+ "version": "26.8.47",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,101 @@
1
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ const get = vi.fn();
4
+
5
+ vi.mock('../../../../defaults', async (importOriginal) => ({
6
+ ...(await importOriginal<Record<string, unknown>>()),
7
+ getDefaultInstance: () => ({
8
+ get,
9
+ }),
10
+ }));
11
+
12
+ const { SysTypesAPI } = await import('../sysTypes');
13
+
14
+ describe('SysTypesAPI.getLookup', () => {
15
+ beforeEach(() => get.mockReset());
16
+
17
+ it('queries the path with search, paging, preselected ids and schema filters', async () => {
18
+ get.mockResolvedValue({
19
+ data: {
20
+ items: [],
21
+ },
22
+ });
23
+
24
+ await SysTypesAPI.getLookup({
25
+ path: '/dictionary/cities',
26
+ display: 'name',
27
+ primary: 'id',
28
+ filters: [
29
+ 'country=ua',
30
+ ],
31
+ search: 'kyi',
32
+ id: [
33
+ 7,
34
+ ],
35
+ page: 2,
36
+ });
37
+
38
+ const url = get.mock.calls[0][0] as string;
39
+ expect(url.startsWith('/dictionary/cities?')).toBe(true);
40
+ expect(url).toContain('q=kyi%2A');
41
+ expect(url).toContain('page=2');
42
+ expect(url).toContain('ids=7');
43
+ expect(url).toContain('fields=id');
44
+ expect(url.endsWith('&country=ua')).toBe(true);
45
+ });
46
+
47
+ it('maps records to options by primary and a display template', async () => {
48
+ get.mockResolvedValue({
49
+ data: {
50
+ data: [
51
+ {
52
+ code: 7,
53
+ name: 'Kyiv',
54
+ region: {
55
+ name: 'Kyiv oblast',
56
+ },
57
+ },
58
+ ],
59
+ next: true,
60
+ },
61
+ });
62
+
63
+ const response = await SysTypesAPI.getLookup({
64
+ path: '/dictionary/cities',
65
+ display: '{name} ({region.name}, {missing})',
66
+ primary: 'code',
67
+ });
68
+
69
+ expect(response.next).toBe(true);
70
+ expect(response.items[0]).toMatchObject({
71
+ id: 7,
72
+ name: 'Kyiv (Kyiv oblast, {missing})',
73
+ });
74
+ });
75
+
76
+ it('reads a plain dot-path display from `items` responses', async () => {
77
+ get.mockResolvedValue({
78
+ data: {
79
+ items: [
80
+ {
81
+ id: 'a',
82
+ profile: {
83
+ title: 'Alpha',
84
+ },
85
+ },
86
+ ],
87
+ },
88
+ });
89
+
90
+ const response = await SysTypesAPI.getLookup({
91
+ path: '/objects',
92
+ display: 'profile.title',
93
+ primary: 'id',
94
+ });
95
+
96
+ expect(response.items[0]).toMatchObject({
97
+ id: 'a',
98
+ name: 'Alpha',
99
+ });
100
+ });
101
+ });
@@ -4,6 +4,7 @@ import {
4
4
  getDefaultInstance,
5
5
  } from '../../../defaults';
6
6
  import {
7
+ addQueryParamsToUrl,
7
8
  applyTransform,
8
9
  camelToSnake,
9
10
  generateUrl,
@@ -16,15 +17,60 @@ import type { ApiParams } from '../../_shared/types';
16
17
 
17
18
  const instance = getDefaultInstance();
18
19
 
20
+ type SysTypeRecord = Record<string, unknown>;
21
+
22
+ // `{path.to.field}` placeholders in a display template
23
+ const displayPlaceholder = /\{([^}]+)\}/g;
24
+
25
+ const readPath = (record: SysTypeRecord, path: string): unknown =>
26
+ path
27
+ .split('.')
28
+ .reduce<unknown>(
29
+ (value, key) => (value as SysTypeRecord | undefined)?.[key],
30
+ record,
31
+ );
32
+
33
+ /**
34
+ * A display is either a plain (dot-)path to one field, or a template such as
35
+ * `{name} ({code})`; an unresolved placeholder is left as written.
36
+ */
37
+ const displayName = (display: string, record: SysTypeRecord): unknown => {
38
+ if (!display.match(displayPlaceholder)) return readPath(record, display);
39
+ return display.replace(displayPlaceholder, (_, key: string) => {
40
+ const value = readPath(record, key);
41
+ return value === undefined || value === null ? `{${key}}` : String(value);
42
+ });
43
+ };
44
+
45
+ /** Maps records to select options: `id` from `primary`, `name` from `display`. */
46
+ const toSelectOptions =
47
+ ({ display, primary }: { display?: string; primary?: string }) =>
48
+ (records: SysTypeRecord[]) =>
49
+ records.map((record) => ({
50
+ ...record,
51
+ ...(primary && {
52
+ id: record[primary],
53
+ }),
54
+ ...(display && {
55
+ name: displayName(display, record),
56
+ }),
57
+ }));
58
+
59
+ /**
60
+ * Records of a system type at `path`, for lookups. `filters` are pre-built
61
+ * `key=value` query conditions (as form schemas store them) appended as-is.
62
+ */
19
63
  const getSysTypeRecordsList = async ({
20
64
  path,
21
65
  display,
22
66
  primary,
67
+ filters = [],
23
68
  ...params
24
69
  }: {
25
70
  path: string;
26
71
  display: string;
27
72
  primary: string;
73
+ filters?: string[];
28
74
  } & ApiParams) => {
29
75
  const fieldsToSend = [
30
76
  'page',
@@ -50,6 +96,7 @@ const getSysTypeRecordsList = async ({
50
96
  sanitize(fieldsToSend),
51
97
  camelToSnake(),
52
98
  generateUrl(path),
99
+ addQueryParamsToUrl(filters),
53
100
  ]);
54
101
  try {
55
102
  const response = await instance.get(url);
@@ -60,8 +107,11 @@ const getSysTypeRecordsList = async ({
60
107
  return {
61
108
  // Some endpoints return data, some return items so we need to check for both of them
62
109
  items:
63
- applyTransform(data || items, [
64
- // transformItemsForSelect({ display, primary }),
110
+ applyTransform(data || items || [], [
111
+ toSelectOptions({
112
+ display,
113
+ primary,
114
+ }),
65
115
  ]) ?? [],
66
116
  next,
67
117
  };
@@ -0,0 +1,67 @@
1
+ import { getDefaultsFromZodSchema } from '@webitel/api-services/gen/utils';
2
+ import { describe, expect, it } from 'vitest';
3
+
4
+ import { agentSkillSchema } from '../agentSkill.validations';
5
+
6
+ const filledSkill = {
7
+ id: '1',
8
+ name: 'English',
9
+ };
10
+
11
+ describe('agentSkillSchema', () => {
12
+ it('seeds a new skill with the defaults the form expects', () => {
13
+ expect(getDefaultsFromZodSchema(agentSkillSchema, {})).toMatchObject({
14
+ capacity: 10,
15
+ enabled: true,
16
+ });
17
+ });
18
+
19
+ it('keeps values already on the draft', () => {
20
+ expect(
21
+ getDefaultsFromZodSchema(agentSkillSchema, {
22
+ skill: filledSkill,
23
+ capacity: 42,
24
+ enabled: false,
25
+ }),
26
+ ).toMatchObject({
27
+ skill: filledSkill,
28
+ capacity: 42,
29
+ enabled: false,
30
+ });
31
+ });
32
+
33
+ it('requires a skill', () => {
34
+ const result = agentSkillSchema.safeParse({
35
+ capacity: 10,
36
+ });
37
+
38
+ expect(result.success).toBe(false);
39
+ expect(result.error?.issues.map((issue) => issue.path.join('.'))).toContain(
40
+ 'skill',
41
+ );
42
+ });
43
+
44
+ it('rejects capacity outside 0-100', () => {
45
+ const tooHigh = agentSkillSchema.safeParse({
46
+ skill: filledSkill,
47
+ capacity: 101,
48
+ });
49
+ const tooLow = agentSkillSchema.safeParse({
50
+ skill: filledSkill,
51
+ capacity: -1,
52
+ });
53
+
54
+ expect(tooHigh.success).toBe(false);
55
+ expect(tooLow.success).toBe(false);
56
+ });
57
+
58
+ it('accepts a valid skill', () => {
59
+ expect(
60
+ agentSkillSchema.safeParse({
61
+ skill: filledSkill,
62
+ capacity: 50,
63
+ enabled: true,
64
+ }).success,
65
+ ).toBe(true);
66
+ });
67
+ });
@@ -0,0 +1,9 @@
1
+ import { z } from 'zod';
2
+
3
+ import { filledLookupSchema } from '../_shared/lookup.validations';
4
+
5
+ export const agentSkillSchema = z.object({
6
+ skill: filledLookupSchema,
7
+ capacity: z.number().min(0).max(100).default(10),
8
+ enabled: z.boolean().default(true),
9
+ });
@@ -5,6 +5,7 @@ export * from './_shared/lookup.validations';
5
5
  export * from './_shared/phoneNumber.validations';
6
6
  export * from './_shared/variablePair.validations';
7
7
  export * from './adjunctType/adjunctType.validations';
8
+ export * from './agentSkill/agentSkill.validations';
8
9
  export * from './auditForm/auditForm.validations';
9
10
  export * from './bucket/bucket.validations';
10
11
  export * from './calendar/calendar.validations';