Skip to content

Commit 26dd72e

Browse files
authored
Merge pull request OneUptime#1648 from OneUptime/add-more-conds
refactor: Add support for additional condition operators in IfElse co…
2 parents 5662d45 + 462105c commit 26dd72e

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

Common/Server/Types/Workflow/Components/Conditions/IfElse.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import ReturnResult from "Common/Types/IsolatedVM/ReturnResult";
55
import { JSONObject, JSONValue } from "Common/Types/JSON";
66
import ComponentMetadata, { Port } from "Common/Types/Workflow/Component";
77
import ComponentID from "Common/Types/Workflow/ComponentID";
8-
import Components from "Common/Types/Workflow/Components/Condition";
8+
import Components, {
9+
ConditionOperator,
10+
} from "Common/Types/Workflow/Components/Condition";
911

1012
export default class IfElse extends ComponentCode {
1113
public constructor() {
@@ -87,7 +89,7 @@ export default class IfElse extends ComponentCode {
8789
return arg;
8890
};
8991

90-
const code: string = `
92+
let code: string = `
9193
const input1 = ${
9294
serialize(args["input-1"] as string) || ""
9395
};
@@ -96,9 +98,19 @@ export default class IfElse extends ComponentCode {
9698
serialize(args["input-2"] as string) || ""
9799
};
98100
99-
return input1 ${
100-
(args["operator"] as string) || "=="
101-
} input2`;
101+
`;
102+
103+
if (args["operator"] === ConditionOperator.Contains) {
104+
code += `return input1.includes(input2);`;
105+
} else if (args["operator"] === ConditionOperator.DoesNotContain) {
106+
code += `return !input1.includes(input2);`;
107+
} else if (args["operator"] === ConditionOperator.StartsWith) {
108+
code += `return input1.startsWith(input2);`;
109+
} else if (args["operator"] === ConditionOperator.EndsWith) {
110+
code += `return input1.endsWith(input2);`;
111+
} else {
112+
code += `return input1 ${(args["operator"] as string) || "=="} input2;`;
113+
}
102114

103115
const returnResult: ReturnResult = await VMUtil.runCodeInSandbox({
104116
code,

Common/Types/Workflow/Components/Condition.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ import ComponentMetadata, {
55
ComponentType,
66
} from "./../Component";
77

8+
export enum ConditionOperator {
9+
EqualTo = "==",
10+
NotEqualTo = "!=",
11+
GreaterThan = ">",
12+
GreaterThanOrEqualTo = ">=",
13+
LessThan = "<",
14+
LessThanOrEqualTo = "<=",
15+
Contains = "contains",
16+
DoesNotContain = "does not contain",
17+
StartsWith = "starts with",
18+
EndsWith = "ends with",
19+
}
20+
821
const components: Array<ComponentMetadata> = [
922
{
1023
id: ComponentID.IfElse,

Common/UI/Components/Workflow/Utils.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ComponentMetadata, {
99
import Components, { Categories } from "Common/Types/Workflow/Components";
1010
import BaseModelComponentFactory from "Common/Types/Workflow/Components/BaseModel";
1111
import Entities from "Common/Models/DatabaseModels/Index";
12+
import { ConditionOperator } from "../../../Types/Workflow/Components/Condition";
1213

1314
type LoadComponentsAndCategoriesFunction = () => {
1415
components: Array<ComponentMetadata>;
@@ -192,27 +193,43 @@ export const componentInputTypeToFormFieldType: ComponentInputTypeToFormFieldTyp
192193
dropdownOptions: [
193194
{
194195
label: "Equal To",
195-
value: "==",
196+
value: ConditionOperator.EqualTo,
196197
},
197198
{
198199
label: "Not Equal To",
199-
value: "!=",
200+
value: ConditionOperator.NotEqualTo,
200201
},
201202
{
202203
label: "Greater Than",
203-
value: ">",
204+
value: ConditionOperator.GreaterThan,
204205
},
205206
{
206207
label: "Less Than",
207-
value: "<",
208+
value: ConditionOperator.LessThan,
208209
},
209210
{
210211
label: "Greater Than or Equal",
211-
value: ">=",
212+
value: ConditionOperator.GreaterThanOrEqualTo,
212213
},
213214
{
214215
label: "Less Than or Equal",
215-
value: "<=",
216+
value: ConditionOperator.LessThanOrEqualTo,
217+
},
218+
{
219+
label: "Contains",
220+
value: ConditionOperator.Contains,
221+
},
222+
{
223+
label: "Does Not Contain",
224+
value: ConditionOperator.DoesNotContain,
225+
},
226+
{
227+
label: "Starts With",
228+
value: ConditionOperator.StartsWith,
229+
},
230+
{
231+
label: "Ends With",
232+
value: ConditionOperator.EndsWith,
216233
},
217234
],
218235
};

0 commit comments

Comments
 (0)