@webitel/api-services 26.8.44 → 26.8.46

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.44",
3
+ "version": "26.8.46",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,7 +8,7 @@ import {
8
8
  snakeToCamel,
9
9
  starToSearch,
10
10
  } from '../../transformers';
11
- import type { ApiParams } from '../_shared/types';
11
+ import type { ApiParams, GetItemParams } from '../_shared/types';
12
12
 
13
13
  /**
14
14
  * The logger endpoints have no natural ordering, so both lists default to
@@ -146,6 +146,19 @@ const getLogsByConfigList = async (params: ApiParams) => {
146
146
  }
147
147
  };
148
148
 
149
+ const getLog = async ({ itemId: id }: GetItemParams) => {
150
+ try {
151
+ const response = await getLoggerService().loggerServiceGetLog(Number(id));
152
+ return applyTransform(response.data, [
153
+ snakeToCamel(),
154
+ ]);
155
+ } catch (err) {
156
+ throw applyTransform(err, [
157
+ notify,
158
+ ]);
159
+ }
160
+ };
161
+
149
162
  /** Audit log of everything one user did. */
150
163
  export const UserLogsAPI = {
151
164
  getList: getLogsByUserList,
@@ -154,4 +167,5 @@ export const UserLogsAPI = {
154
167
  /** Audit log of everything done to one logger config's object. */
155
168
  export const ConfigLogsAPI = {
156
169
  getList: getLogsByConfigList,
170
+ get: getLog,
157
171
  };
@@ -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
  };