@media-quest/builder 0.0.2 → 0.0.3
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/dist/public-api.d.mts +24 -6
- package/dist/public-api.d.ts +24 -6
- package/dist/public-api.js +58 -32
- package/dist/public-api.mjs +39 -32
- package/package.json +3 -4
- package/src/public-api.ts +3 -0
- package/src/rulebuilder/Builder-rule.spec.ts +182 -182
- package/src/rulebuilder/Builder-rule.ts +34 -65
- package/src/rulebuilder/RuleBuilder-test-utils.ts +228 -228
- package/src/rulebuilder/SingleSelectItem.ts +118 -118
- package/src/rulebuilder/{Builder-condition-group.ts → condition/Builder-condition-group.ts} +15 -33
- package/src/rulebuilder/condition/Builder-condition.spec.ts +169 -0
- package/src/rulebuilder/condition/Builder-condition.ts +186 -0
- package/src/rulebuilder/index.ts +11 -11
- package/src/rulebuilder/Builder-condition.spec.ts +0 -169
- package/src/rulebuilder/Builder-condition.ts +0 -186
- /package/src/rulebuilder/{Builder-condition-group.spec.ts → condition/Builder-condition-group.spec.ts} +0 -0
- /package/src/rulebuilder/{Builder-operator.spec.ts → condition/Builder-operator.spec.ts} +0 -0
- /package/src/rulebuilder/{Builder-operator.ts → condition/Builder-operator.ts} +0 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { BuilderObject } from "../../BuilderObject";
|
|
2
|
+
import { BuilderOperator } from "./Builder-operator";
|
|
3
|
+
import type { BuilderVariable, BuilderVariableOption } from "../RuleVariable";
|
|
4
|
+
import { OperatorSelectItem, RuleOptionSelectItem, RuleVariableSelectItem } from "../SingleSelectItem";
|
|
5
|
+
export interface BuilderConditionDto {
|
|
6
|
+
readonly kind: "condition";
|
|
7
|
+
readonly operator: BuilderOperator | "";
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly variableId: string;
|
|
10
|
+
readonly value: number | string | boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class BuilderCondition extends BuilderObject<"builder-condition", BuilderConditionDto> {
|
|
14
|
+
readonly objectType: "builder-condition" = "builder-condition";
|
|
15
|
+
public static readonly NUMBER_OPERATORS: ReadonlyArray<OperatorSelectItem> = [
|
|
16
|
+
OperatorSelectItem.EQ,
|
|
17
|
+
OperatorSelectItem.NOT_EQ,
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
private initialDto: BuilderConditionDto;
|
|
21
|
+
name = "";
|
|
22
|
+
|
|
23
|
+
public static create = (variableList: ReadonlyArray<BuilderVariable>) => {
|
|
24
|
+
const condition = new BuilderCondition(
|
|
25
|
+
{
|
|
26
|
+
kind: "condition",
|
|
27
|
+
name: "",
|
|
28
|
+
operator: "",
|
|
29
|
+
variableId: "",
|
|
30
|
+
value: "",
|
|
31
|
+
},
|
|
32
|
+
variableList,
|
|
33
|
+
);
|
|
34
|
+
return condition;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
public static fromDto = (dto: BuilderConditionDto, variables: ReadonlyArray<BuilderVariable>) => {
|
|
38
|
+
const _dto: BuilderConditionDto = {
|
|
39
|
+
kind: "condition",
|
|
40
|
+
name: dto.name ?? "",
|
|
41
|
+
value: dto.value ?? "",
|
|
42
|
+
operator: dto.operator ?? "",
|
|
43
|
+
variableId: dto.variableId ?? "",
|
|
44
|
+
};
|
|
45
|
+
const instance = new BuilderCondition(_dto, variables);
|
|
46
|
+
return instance;
|
|
47
|
+
};
|
|
48
|
+
private _variable: BuilderVariable | false = false;
|
|
49
|
+
private _operator: BuilderOperator | "" = "";
|
|
50
|
+
private _value: BuilderVariableOption | false = false;
|
|
51
|
+
private _variableList: ReadonlyArray<BuilderVariable> = [];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Can only set variables that exist in variableList.
|
|
55
|
+
* @param variable
|
|
56
|
+
*/
|
|
57
|
+
set variable(variable: BuilderVariable | false) {
|
|
58
|
+
if (variable === this._variable) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this._variable = variable;
|
|
62
|
+
this._operator = "";
|
|
63
|
+
this._value = false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get variable() {
|
|
67
|
+
return this._variable;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
set value(variableValue: BuilderVariableOption | false) {
|
|
71
|
+
this._value = variableValue;
|
|
72
|
+
}
|
|
73
|
+
get value() {
|
|
74
|
+
return this._value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
validate(): { isValid: true } | { isValid: false; message: string } {
|
|
78
|
+
if (this._variableList.length === 0) {
|
|
79
|
+
return {
|
|
80
|
+
isValid: false,
|
|
81
|
+
message: "Has no variableList to check dto against.",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (!this._variable) {
|
|
85
|
+
return {
|
|
86
|
+
isValid: false,
|
|
87
|
+
message: "Variable has not been initialized from variableList.",
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!this._operator) {
|
|
92
|
+
return { isValid: false, message: "Operator has not been initialized" };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!this._value) {
|
|
96
|
+
return {
|
|
97
|
+
isValid: false,
|
|
98
|
+
message: "Value (BuilderVariableOption) is not initialized",
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { isValid: true };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private findVariableInUniverse(variableId: string): BuilderVariable | false {
|
|
106
|
+
const v = this._variableList.find((v) => v.varId === variableId);
|
|
107
|
+
return v ?? false;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
set operator(operator: BuilderOperator | "") {
|
|
111
|
+
if (BuilderOperator.is(operator)) {
|
|
112
|
+
this._operator = operator;
|
|
113
|
+
} else {
|
|
114
|
+
this._operator = "";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get operator() {
|
|
119
|
+
return this._operator;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private constructor(dto: BuilderConditionDto, variables: ReadonlyArray<BuilderVariable>) {
|
|
123
|
+
super(dto);
|
|
124
|
+
this.initialDto = dto;
|
|
125
|
+
this.name = dto.name;
|
|
126
|
+
this._setVariableList(variables);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
get variableSelectItemsInUniverse(): ReadonlyArray<RuleVariableSelectItem> {
|
|
130
|
+
return this._variableList.map(RuleVariableSelectItem.create);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get operatorsSelectItems(): ReadonlyArray<OperatorSelectItem> {
|
|
134
|
+
return this._variable ? BuilderCondition.NUMBER_OPERATORS : [];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
get selectValueItems(): ReadonlyArray<RuleOptionSelectItem> {
|
|
138
|
+
if (!this._variable) {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
const opt = this._variable.options.map(RuleOptionSelectItem.create);
|
|
142
|
+
return opt;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
clone(): BuilderConditionDto {
|
|
146
|
+
return this.toJson();
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private _setVariableList(variables: ReadonlyArray<BuilderVariable>): boolean {
|
|
150
|
+
this._variableList = variables;
|
|
151
|
+
const v = this._variableList.find((v) => v.varId === this.originalDto.variableId);
|
|
152
|
+
if (!v) {
|
|
153
|
+
this._variable = false;
|
|
154
|
+
this._operator = "";
|
|
155
|
+
this._value = false;
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
this._variable = v;
|
|
159
|
+
const op = this.originalDto.operator;
|
|
160
|
+
|
|
161
|
+
if (!BuilderOperator.is(op)) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
this._operator = op;
|
|
165
|
+
const maybeOption = v.options.find((op) => op.value === this.originalDto.value);
|
|
166
|
+
if (!maybeOption) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
this._value = maybeOption;
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
toJson(): BuilderConditionDto {
|
|
174
|
+
const name = this.name;
|
|
175
|
+
const variableId = this._variable ? this._variable.varId : "";
|
|
176
|
+
const operator = this._operator ? this._operator : "";
|
|
177
|
+
const value = this._value ? this._value.value : "";
|
|
178
|
+
return {
|
|
179
|
+
kind: "condition",
|
|
180
|
+
name,
|
|
181
|
+
operator,
|
|
182
|
+
variableId,
|
|
183
|
+
value,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
package/src/rulebuilder/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export { BuilderCondition, type BuilderConditionDto } from "./Builder-condition";
|
|
1
|
+
export { BuilderCondition, type BuilderConditionDto } from "./condition/Builder-condition";
|
|
2
2
|
export {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
} from "./Builder-condition-group";
|
|
7
|
-
export { BuilderOperator } from "./Builder-operator";
|
|
3
|
+
BuilderConditionGroup,
|
|
4
|
+
type BuilderConditionGroupDto,
|
|
5
|
+
type ConditionGroupType,
|
|
6
|
+
} from "./condition/Builder-condition-group";
|
|
7
|
+
export { BuilderOperator } from "./condition/Builder-operator";
|
|
8
8
|
export { BuilderRule, type BuilderRuleDto } from "./Builder-rule";
|
|
9
9
|
export { JumpToActionManager } from "./jump-to-action-manager";
|
|
10
10
|
export { MultiSelectItem, ExcludeByPageIdSelectItem, ExcludeByTagSelectItem } from "./multi-select-item";
|
|
@@ -13,10 +13,10 @@ export { type ExcludeByPageAction, type ExcludeByTagAction, type JumpToPageActio
|
|
|
13
13
|
export { RuleInput } from "./RuleInput";
|
|
14
14
|
export { CustomVariable, BuilderVariableOption, type BuilderVariable, QuestionVariable } from "./RuleVariable";
|
|
15
15
|
export {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
SingleSelectItem,
|
|
17
|
+
OperatorSelectItem,
|
|
18
|
+
RuleOptionSelectItem,
|
|
19
|
+
RuleVariableSelectItem,
|
|
20
|
+
JumpToPageSelectItem,
|
|
21
21
|
} from "./SingleSelectItem";
|
|
22
22
|
export { TagActionManager } from "./tag-action-manager";
|
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import { BuilderCondition, type BuilderConditionDto } from "./Builder-condition";
|
|
2
|
-
import { RuleBuilderTestUtils } from "./RuleBuilder-test-utils";
|
|
3
|
-
import type { BuilderVariable, BuilderVariableOption } from "./RuleVariable";
|
|
4
|
-
import { QuestionVariable } from "./RuleVariable";
|
|
5
|
-
|
|
6
|
-
let condition = BuilderCondition.create([]);
|
|
7
|
-
|
|
8
|
-
beforeEach(() => {
|
|
9
|
-
condition = BuilderCondition.create([]);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
describe("Builder Operator", () => {
|
|
13
|
-
test("Can create", () => {
|
|
14
|
-
expect(condition).toBeInstanceOf(BuilderCondition);
|
|
15
|
-
});
|
|
16
|
-
test("Can resolve dto from Variables in universe.", () => {
|
|
17
|
-
const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
18
|
-
const a = vs[0];
|
|
19
|
-
const dto: BuilderConditionDto = {
|
|
20
|
-
kind: "condition",
|
|
21
|
-
name: "a",
|
|
22
|
-
variableId: a.varId,
|
|
23
|
-
operator: "equal",
|
|
24
|
-
value: 1,
|
|
25
|
-
};
|
|
26
|
-
const c = BuilderCondition.fromDto(dto, vs);
|
|
27
|
-
// c.setVariableList(vs);
|
|
28
|
-
expect(c.operatorsSelectItems.length).toBe(2);
|
|
29
|
-
expect(c.operator === "equal").toBe(true);
|
|
30
|
-
expect(c.value).toBeTruthy();
|
|
31
|
-
});
|
|
32
|
-
test("Can not resolve value if invalid value", () => {
|
|
33
|
-
const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
34
|
-
const a = vs[0];
|
|
35
|
-
const dto: BuilderConditionDto = {
|
|
36
|
-
kind: "condition",
|
|
37
|
-
name: "a",
|
|
38
|
-
variableId: a.varId,
|
|
39
|
-
operator: "equal",
|
|
40
|
-
value: 8,
|
|
41
|
-
};
|
|
42
|
-
const c = BuilderCondition.fromDto(dto, vs);
|
|
43
|
-
expect(c.operatorsSelectItems.length).toBe(2);
|
|
44
|
-
expect(c.value).toBe(false);
|
|
45
|
-
// expect(match).toBe(false);
|
|
46
|
-
});
|
|
47
|
-
test("Will nullify dto if not matchedFrom is called", () => {
|
|
48
|
-
const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
49
|
-
const a = vs[0];
|
|
50
|
-
const dto: BuilderConditionDto = {
|
|
51
|
-
kind: "condition",
|
|
52
|
-
name: "a",
|
|
53
|
-
variableId: a.varId,
|
|
54
|
-
operator: "equal",
|
|
55
|
-
value: 0,
|
|
56
|
-
};
|
|
57
|
-
const c = BuilderCondition.fromDto(dto, []);
|
|
58
|
-
expect(c.operatorsSelectItems.length).toBe(0);
|
|
59
|
-
expect(c.toJson().name).toBe(dto.name);
|
|
60
|
-
expect(c.toJson().value).toBe("");
|
|
61
|
-
expect(c.toJson().operator).toBe("");
|
|
62
|
-
expect(c.toJson().variableId).toBe("");
|
|
63
|
-
// expect(match).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
test("Will not nullify if created with valid universe", () => {
|
|
66
|
-
const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
67
|
-
const a = vs[0];
|
|
68
|
-
const dto: BuilderConditionDto = {
|
|
69
|
-
kind: "condition",
|
|
70
|
-
name: "a",
|
|
71
|
-
variableId: a.varId,
|
|
72
|
-
operator: "equal",
|
|
73
|
-
value: 0,
|
|
74
|
-
};
|
|
75
|
-
const c = BuilderCondition.fromDto(dto, vs);
|
|
76
|
-
expect(c.variable).toBeInstanceOf(QuestionVariable);
|
|
77
|
-
expect(c.operator === "equal").toBe(true);
|
|
78
|
-
const value = c.value as BuilderVariableOption;
|
|
79
|
-
expect(value.value).toBe(0);
|
|
80
|
-
expect(value.label).toBe("Nei");
|
|
81
|
-
expect(c.variable).toBe(a);
|
|
82
|
-
});
|
|
83
|
-
test("Condition is valid, when in sync with universe.", () => {
|
|
84
|
-
const vs = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
85
|
-
const a = vs[0];
|
|
86
|
-
const dto: BuilderConditionDto = {
|
|
87
|
-
kind: "condition",
|
|
88
|
-
|
|
89
|
-
name: "name of a",
|
|
90
|
-
variableId: a.varId,
|
|
91
|
-
operator: "equal",
|
|
92
|
-
value: 0,
|
|
93
|
-
};
|
|
94
|
-
const c = BuilderCondition.fromDto(dto, vs);
|
|
95
|
-
expect(c.variable).toBeInstanceOf(QuestionVariable);
|
|
96
|
-
expect(c.operator === "equal").toBe(true);
|
|
97
|
-
const value = c.value as BuilderVariableOption;
|
|
98
|
-
expect(value.value).toBe(0);
|
|
99
|
-
expect(value.label).toBe("Nei");
|
|
100
|
-
expect(c.validate().isValid).toBe(true);
|
|
101
|
-
});
|
|
102
|
-
test("Condition is invalid, when not matched against universe", () => {
|
|
103
|
-
const dto: BuilderConditionDto = {
|
|
104
|
-
kind: "condition",
|
|
105
|
-
name: "name of a",
|
|
106
|
-
variableId: "a",
|
|
107
|
-
operator: "equal",
|
|
108
|
-
value: 0,
|
|
109
|
-
};
|
|
110
|
-
const c = BuilderCondition.fromDto(dto, []);
|
|
111
|
-
expect(c.validate().isValid).toBe(false);
|
|
112
|
-
});
|
|
113
|
-
test("Condition is invalid, when variable dont exist in universe.", () => {
|
|
114
|
-
const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
115
|
-
const dto: BuilderConditionDto = {
|
|
116
|
-
kind: "condition",
|
|
117
|
-
name: "invalid variable name in dto",
|
|
118
|
-
variableId: "kkk",
|
|
119
|
-
operator: "equal",
|
|
120
|
-
value: 9,
|
|
121
|
-
};
|
|
122
|
-
const c = BuilderCondition.fromDto(dto, universe);
|
|
123
|
-
expect(c.variable).toBe(false);
|
|
124
|
-
expect(c.validate().isValid).toBe(false);
|
|
125
|
-
});
|
|
126
|
-
test("Condition is invalid if not all set, when variable dont exist in universe.", () => {
|
|
127
|
-
const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
128
|
-
const dto: BuilderConditionDto = {
|
|
129
|
-
kind: "condition",
|
|
130
|
-
name: "invalid variable name in dto",
|
|
131
|
-
variableId: "kkk",
|
|
132
|
-
operator: "equal",
|
|
133
|
-
value: 9,
|
|
134
|
-
};
|
|
135
|
-
const c = BuilderCondition.fromDto(dto, universe);
|
|
136
|
-
expect(c.variable).toBe(false);
|
|
137
|
-
expect(c.validate().isValid).toBe(false);
|
|
138
|
-
});
|
|
139
|
-
test("Condition is invalid if operator is not set", () => {
|
|
140
|
-
const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
141
|
-
const dto: BuilderConditionDto = {
|
|
142
|
-
kind: "condition",
|
|
143
|
-
name: "invalid variable name in dto",
|
|
144
|
-
variableId: "a",
|
|
145
|
-
operator: "",
|
|
146
|
-
value: 1,
|
|
147
|
-
};
|
|
148
|
-
const c = BuilderCondition.fromDto(dto, universe);
|
|
149
|
-
expect(c.variable).toBeInstanceOf(QuestionVariable);
|
|
150
|
-
expect(c.validate().isValid).toBe(false);
|
|
151
|
-
expect(c.value).toBe(false);
|
|
152
|
-
});
|
|
153
|
-
test("Condition is invalid if value (from dto) is not found in variable", () => {
|
|
154
|
-
const universe = RuleBuilderTestUtils.createBuilderVariables_A_H();
|
|
155
|
-
const dto: BuilderConditionDto = {
|
|
156
|
-
kind: "condition",
|
|
157
|
-
name: "invalid variable name in dto",
|
|
158
|
-
variableId: "a",
|
|
159
|
-
operator: "equal",
|
|
160
|
-
value: 7,
|
|
161
|
-
};
|
|
162
|
-
const c = BuilderCondition.fromDto(dto, universe);
|
|
163
|
-
expect(c.variable).toBeInstanceOf(QuestionVariable);
|
|
164
|
-
expect(c.operator).toBe("equal");
|
|
165
|
-
expect(c.operatorsSelectItems.length).toBe(BuilderCondition.NUMBER_OPERATORS.length);
|
|
166
|
-
expect(c.value).toBe(false);
|
|
167
|
-
expect(c.validate().isValid).toBe(false);
|
|
168
|
-
});
|
|
169
|
-
});
|
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
import { BuilderObject } from "../BuilderObject";
|
|
2
|
-
import { BuilderOperator } from "./Builder-operator";
|
|
3
|
-
import type { BuilderVariable, BuilderVariableOption } from "./RuleVariable";
|
|
4
|
-
import { OperatorSelectItem, RuleOptionSelectItem, RuleVariableSelectItem } from "./SingleSelectItem";
|
|
5
|
-
export interface BuilderConditionDto {
|
|
6
|
-
readonly kind: "condition";
|
|
7
|
-
readonly operator: BuilderOperator | "";
|
|
8
|
-
readonly name: string;
|
|
9
|
-
readonly variableId: string;
|
|
10
|
-
readonly value: number | string | boolean;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class BuilderCondition extends BuilderObject<"builder-condition", BuilderConditionDto> {
|
|
14
|
-
readonly objectType: "builder-condition" = "builder-condition";
|
|
15
|
-
public static readonly NUMBER_OPERATORS: ReadonlyArray<OperatorSelectItem> = [
|
|
16
|
-
OperatorSelectItem.EQ,
|
|
17
|
-
OperatorSelectItem.NOT_EQ,
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
private initialDto: BuilderConditionDto;
|
|
21
|
-
name = "";
|
|
22
|
-
|
|
23
|
-
public static create = (variableList: ReadonlyArray<BuilderVariable>) => {
|
|
24
|
-
const condition = new BuilderCondition(
|
|
25
|
-
{
|
|
26
|
-
kind: "condition",
|
|
27
|
-
name: "",
|
|
28
|
-
operator: "",
|
|
29
|
-
variableId: "",
|
|
30
|
-
value: "",
|
|
31
|
-
},
|
|
32
|
-
variableList
|
|
33
|
-
);
|
|
34
|
-
return condition;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
public static fromDto = (dto: BuilderConditionDto, variables: ReadonlyArray<BuilderVariable>) => {
|
|
38
|
-
const _dto: BuilderConditionDto = {
|
|
39
|
-
kind: "condition",
|
|
40
|
-
name: dto.name ?? "",
|
|
41
|
-
value: dto.value ?? "",
|
|
42
|
-
operator: dto.operator ?? "",
|
|
43
|
-
variableId: dto.variableId ?? "",
|
|
44
|
-
};
|
|
45
|
-
const instance = new BuilderCondition(_dto, variables);
|
|
46
|
-
return instance;
|
|
47
|
-
};
|
|
48
|
-
private _variable: BuilderVariable | false = false;
|
|
49
|
-
private _operator: BuilderOperator | "" = "";
|
|
50
|
-
private _value: BuilderVariableOption | false = false;
|
|
51
|
-
private _variableList: ReadonlyArray<BuilderVariable> = [];
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Can only set variables that exist in variableList.
|
|
55
|
-
* @param variable
|
|
56
|
-
*/
|
|
57
|
-
set variable(variable: BuilderVariable | false) {
|
|
58
|
-
if (variable === this._variable) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this._variable = variable;
|
|
62
|
-
this._operator = "";
|
|
63
|
-
this._value = false;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get variable() {
|
|
67
|
-
return this._variable;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
set value(variableValue: BuilderVariableOption | false) {
|
|
71
|
-
this._value = variableValue;
|
|
72
|
-
}
|
|
73
|
-
get value() {
|
|
74
|
-
return this._value;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
validate(): { isValid: true } | { isValid: false; message: string } {
|
|
78
|
-
if (this._variableList.length === 0) {
|
|
79
|
-
return {
|
|
80
|
-
isValid: false,
|
|
81
|
-
message: "Has no variableList to check dto against.",
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
if (!this._variable) {
|
|
85
|
-
return {
|
|
86
|
-
isValid: false,
|
|
87
|
-
message: "Variable has not been initialized from variableList.",
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (!this._operator) {
|
|
92
|
-
return { isValid: false, message: "Operator has not been initialized" };
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (!this._value) {
|
|
96
|
-
return {
|
|
97
|
-
isValid: false,
|
|
98
|
-
message: "Value (BuilderVariableOption) is not initialized",
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return { isValid: true };
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private findVariableInUniverse(variableId: string): BuilderVariable | false {
|
|
106
|
-
const v = this._variableList.find((v) => v.varId === variableId);
|
|
107
|
-
return v ?? false;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
set operator(operator: BuilderOperator | "") {
|
|
111
|
-
if (BuilderOperator.is(operator)) {
|
|
112
|
-
this._operator = operator;
|
|
113
|
-
} else {
|
|
114
|
-
this._operator = "";
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
get operator() {
|
|
119
|
-
return this._operator;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private constructor(dto: BuilderConditionDto, variables: ReadonlyArray<BuilderVariable>) {
|
|
123
|
-
super(dto);
|
|
124
|
-
this.initialDto = dto;
|
|
125
|
-
this.name = dto.name;
|
|
126
|
-
this._setVariableList(variables);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
get variableSelectItemsInUniverse(): ReadonlyArray<RuleVariableSelectItem> {
|
|
130
|
-
return this._variableList.map(RuleVariableSelectItem.create);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
get operatorsSelectItems(): ReadonlyArray<OperatorSelectItem> {
|
|
134
|
-
return this._variable ? BuilderCondition.NUMBER_OPERATORS : [];
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
get selectValueItems(): ReadonlyArray<RuleOptionSelectItem> {
|
|
138
|
-
if (!this._variable) {
|
|
139
|
-
return [];
|
|
140
|
-
}
|
|
141
|
-
const opt = this._variable.options.map(RuleOptionSelectItem.create);
|
|
142
|
-
return opt;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
clone(): BuilderConditionDto {
|
|
146
|
-
return this.toJson();
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
private _setVariableList(variables: ReadonlyArray<BuilderVariable>): boolean {
|
|
150
|
-
this._variableList = variables;
|
|
151
|
-
const v = this._variableList.find((v) => v.varId === this.originalDto.variableId);
|
|
152
|
-
if (!v) {
|
|
153
|
-
this._variable = false;
|
|
154
|
-
this._operator = "";
|
|
155
|
-
this._value = false;
|
|
156
|
-
return false;
|
|
157
|
-
}
|
|
158
|
-
this._variable = v;
|
|
159
|
-
const op = this.originalDto.operator;
|
|
160
|
-
|
|
161
|
-
if (!BuilderOperator.is(op)) {
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
this._operator = op;
|
|
165
|
-
const maybeOption = v.options.find((op) => op.value === this.originalDto.value);
|
|
166
|
-
if (!maybeOption) {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
this._value = maybeOption;
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
toJson(): BuilderConditionDto {
|
|
174
|
-
const name = this.name;
|
|
175
|
-
const variableId = this._variable ? this._variable.varId : "";
|
|
176
|
-
const operator = this._operator ? this._operator : "";
|
|
177
|
-
const value = this._value ? this._value.value : "";
|
|
178
|
-
return {
|
|
179
|
-
kind: "condition",
|
|
180
|
-
name,
|
|
181
|
-
operator,
|
|
182
|
-
variableId,
|
|
183
|
-
value,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|