@truedat/core 4.53.1 → 4.53.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.53.2] 2022-10-07
4
+
5
+ ### Added
6
+
7
+ - [TD-5226] `useOperators` hook to load operators using GraphQL `functions`
8
+ query
9
+
3
10
  ## [4.52.4] 2022-09-30
4
11
 
5
12
  ### Fixed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@truedat/core",
3
- "version": "4.53.1",
3
+ "version": "4.53.2",
4
4
  "description": "Truedat Web Core",
5
5
  "sideEffects": false,
6
6
  "jsnext:main": "src/index.js",
@@ -35,7 +35,7 @@
35
35
  "@testing-library/jest-dom": "^5.16.4",
36
36
  "@testing-library/react": "^12.0.0",
37
37
  "@testing-library/user-event": "^13.2.1",
38
- "@truedat/test": "4.53.1",
38
+ "@truedat/test": "4.53.2",
39
39
  "babel-jest": "^28.1.0",
40
40
  "babel-plugin-dynamic-import-node": "^2.3.3",
41
41
  "babel-plugin-lodash": "^3.3.4",
@@ -112,5 +112,5 @@
112
112
  "react-dom": ">= 16.8.6 < 17",
113
113
  "semantic-ui-react": ">= 0.88.2 < 2.1"
114
114
  },
115
- "gitHead": "aec23f8f32a131dfa84620ec6e14c31916118d44"
115
+ "gitHead": "e29af66624aad6ecce065ced88c8cc2e320d67e2"
116
116
  }
@@ -32,3 +32,20 @@ export const TEMPLATES_QUERY = gql`
32
32
  }
33
33
  }
34
34
  `;
35
+
36
+ export const FUNCTIONS_QUERY = gql`
37
+ query Functions {
38
+ functions {
39
+ id
40
+ name
41
+ group
42
+ returnType
43
+ scope
44
+ args {
45
+ name
46
+ type
47
+ values
48
+ }
49
+ }
50
+ }
51
+ `;
@@ -3,3 +3,4 @@ export * from "./useActiveRoutes";
3
3
  export * from "./useAuthorized";
4
4
  export * from "./usePath";
5
5
  export * from "./useOnScreen";
6
+ export * from "./useOperators";
@@ -0,0 +1,10 @@
1
+ import { useQuery } from "@apollo/client";
2
+ import { FUNCTIONS_QUERY } from "../api/queries";
3
+ import { transform } from "../services/operators";
4
+
5
+ export const useOperators = () => {
6
+ const { loading, error, data } = useQuery(FUNCTIONS_QUERY);
7
+ return { loading, error, data: data ? transform(data) : [] };
8
+ };
9
+
10
+ export default useOperators;
@@ -0,0 +1,215 @@
1
+ import { toItem, transform } from "../operators";
2
+
3
+ describe("services: operators toItem should transform to existing operator/modifier format", () => {
4
+ const id = "123";
5
+
6
+ it("simple operator", () => {
7
+ expect(
8
+ toItem({
9
+ id,
10
+ name: "name",
11
+ returnType: "boolean",
12
+ args: [{ type: "xyz" }],
13
+ })
14
+ ).toEqual({ id, category: "xyz", name: "name", returnType: "boolean" });
15
+ });
16
+
17
+ it("includes scope and group", () => {
18
+ expect(
19
+ toItem({
20
+ id,
21
+ name: "name",
22
+ group: "group",
23
+ scope: "scope",
24
+ returnType: "boolean",
25
+ args: [{ type: "any" }],
26
+ })
27
+ ).toEqual({
28
+ id,
29
+ category: "any",
30
+ name: "name",
31
+ group: "group",
32
+ scope: "scope",
33
+ returnType: "boolean",
34
+ });
35
+ });
36
+
37
+ it("maps as a modifier with params if returnType is not boolean", () => {
38
+ expect(
39
+ toItem({
40
+ id,
41
+ name: "name",
42
+ returnType: "string",
43
+ args: [
44
+ { type: "cat" },
45
+ { name: "start", type: "number" },
46
+ { name: "end", type: "number" },
47
+ ],
48
+ })
49
+ ).toEqual({
50
+ id,
51
+ category: "cat",
52
+ name: "name",
53
+ type: "string",
54
+ params: [
55
+ { name: "start", type: "number" },
56
+ { name: "end", type: "number" },
57
+ ],
58
+ returnType: "string",
59
+ });
60
+ });
61
+
62
+ it("maps arity and values", () => {
63
+ expect(
64
+ toItem({
65
+ id,
66
+ name: "variation_on_count",
67
+ scope: "validation",
68
+ returnType: "boolean",
69
+ args: [
70
+ { type: "date" },
71
+ {
72
+ type: "string",
73
+ values: ["D-0", "D-1", "M-0", "M-1", "M-2", "Y-0", "Y-1"],
74
+ },
75
+ {
76
+ type: "string",
77
+ values: ["D-0", "D-1", "M-0", "M-1", "M-2", "Y-0", "Y-1"],
78
+ },
79
+ ],
80
+ })
81
+ ).toEqual({
82
+ id,
83
+ category: "date",
84
+ name: "variation_on_count",
85
+ value_type: "string",
86
+ fixed_values: ["D-0", "D-1", "M-0", "M-1", "M-2", "Y-0", "Y-1"],
87
+ scope: "validation",
88
+ arity: 2,
89
+ returnType: "boolean",
90
+ });
91
+ });
92
+
93
+ it("maps value_type_filter if type is 'ref'", () => {
94
+ expect(
95
+ toItem({
96
+ id,
97
+ name: "references",
98
+ scope: "validation_filter",
99
+ group: "references",
100
+ returnType: "boolean",
101
+ args: [{ type: "any" }, { type: "ref" }],
102
+ })
103
+ ).toEqual({
104
+ id,
105
+ category: "any",
106
+ group: "references",
107
+ name: "references",
108
+ value_type: "field",
109
+ value_type_filter: "any",
110
+ scope: "validation",
111
+ population: true,
112
+ returnType: "boolean",
113
+ });
114
+ });
115
+ });
116
+
117
+ describe("services: operators transform", () => {
118
+ test("transforms items and groups by category", () => {
119
+ const data = {
120
+ functions: [
121
+ {
122
+ id: "1",
123
+ name: "name",
124
+ returnType: "boolean",
125
+ args: [{ type: "xyz" }],
126
+ },
127
+ {
128
+ id: "2",
129
+ name: "name",
130
+ group: "group",
131
+ scope: "scope",
132
+ returnType: "boolean",
133
+ args: [{ type: "any" }],
134
+ },
135
+ {
136
+ id: "3",
137
+ name: "name",
138
+ returnType: "string",
139
+ args: [
140
+ { type: "cat" },
141
+ { name: "start", type: "number" },
142
+ { name: "end", type: "number" },
143
+ ],
144
+ },
145
+ {
146
+ id: "4",
147
+ name: "variation_on_count",
148
+ scope: "validation",
149
+ returnType: "boolean",
150
+ args: [
151
+ { type: "date" },
152
+ {
153
+ type: "string",
154
+ values: ["D-0", "D-1", "M-0", "M-1", "M-2", "Y-0", "Y-1"],
155
+ },
156
+ {
157
+ type: "string",
158
+ values: ["D-0", "D-1", "M-0", "M-1", "M-2", "Y-0", "Y-1"],
159
+ },
160
+ ],
161
+ },
162
+ {
163
+ id: "5",
164
+ name: "references",
165
+ scope: "validation_filter",
166
+ group: "references",
167
+ returnType: "boolean",
168
+ args: [{ type: "any" }, { type: "ref" }],
169
+ },
170
+ ],
171
+ };
172
+ expect(transform(data)).toEqual({
173
+ xyz: { operators: [{ id: "1", name: "name" }] },
174
+ any: {
175
+ operators: [
176
+ { id: "2", name: "name", group: "group", scope: "scope" },
177
+ {
178
+ id: "5",
179
+ name: "references",
180
+ group: "references",
181
+ scope: "validation",
182
+ value_type: "field",
183
+ value_type_filter: "any",
184
+ population: true,
185
+ },
186
+ ],
187
+ },
188
+ cat: {
189
+ modifiers: [
190
+ {
191
+ id: "3",
192
+ name: "name",
193
+ type: "string",
194
+ params: [
195
+ { name: "start", type: "number" },
196
+ { name: "end", type: "number" },
197
+ ],
198
+ },
199
+ ],
200
+ },
201
+ date: {
202
+ operators: [
203
+ {
204
+ arity: 2,
205
+ id: "4",
206
+ name: "variation_on_count",
207
+ scope: "validation",
208
+ value_type: "string",
209
+ fixed_values: ["D-0", "D-1", "M-0", "M-1", "M-2", "Y-0", "Y-1"],
210
+ },
211
+ ],
212
+ },
213
+ });
214
+ });
215
+ });
@@ -0,0 +1,54 @@
1
+ import _ from "lodash/fp";
2
+
3
+ export const toItem = ({
4
+ name,
5
+ group,
6
+ returnType,
7
+ scope,
8
+ id,
9
+ args: [{ type: category }, ...args],
10
+ }) => {
11
+ const arity = _.size(args);
12
+ const arg1 = _.head(args);
13
+ const type = arg1?.type;
14
+ const values = arg1?.values;
15
+ const params =
16
+ _.isEmpty(args) || returnType === "boolean"
17
+ ? null
18
+ : _.map(_.pick(["name", "type"]))(args);
19
+ const value_type =
20
+ returnType === "boolean" ? (type === "ref" ? "field" : type) : null;
21
+ return _.pickBy(_.identity)({
22
+ arity: returnType === "boolean" && arity > 1 ? arity : null,
23
+ category,
24
+ name,
25
+ group: group,
26
+ scope: scope === "validation_filter" ? "validation" : scope || null,
27
+ id,
28
+ type: returnType === "boolean" ? null : returnType,
29
+ value_type,
30
+ value_type_filter: type === "ref" ? "any" : null,
31
+ population: scope === "validation_filter",
32
+ fixed_values: values,
33
+ returnType,
34
+ params,
35
+ });
36
+ };
37
+
38
+ // Transfrom functions from GraphQL API to old model (operators + modifiers)
39
+ export const transform = _.flow(
40
+ _.propOr([], "functions"),
41
+ _.map(toItem),
42
+ _.flow(
43
+ _.groupBy("category"),
44
+ _.mapValues(_.map(_.omit(["category"]))),
45
+ _.mapValues(
46
+ _.flow(
47
+ _.groupBy(({ returnType }) =>
48
+ returnType === "boolean" ? "operators" : "modifiers"
49
+ ),
50
+ _.mapValues(_.map(_.omit(["returnType"])))
51
+ )
52
+ )
53
+ )
54
+ );