A platform for community discussion. Free, open, simple.
FEATURE: adds gt/gte/lt/lte operators to if and filter nodes (#42097)
Joffrey JAFFEUXaa3c347a7478
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjsindex 4813849c3d7..ec79b031d7d 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs@@ -5,6 +5,7 @@ import { action } from "@ember/object";import { i18n } from "discourse-i18n";import {isSingleValueOperator,+ operatorOptionsForType,operatorsForType,} from "../../../lib/workflows/condition-operators";import { schemaFieldsForNodeInput } from "../../../lib/workflows/data-preview";@@ -150,7 +151,7 @@ function operationSchema(item) {return (OPERATION_SCHEMAS[type] ??= {type: "options",required: true,- options: operatorsForType(type),+ options: operatorOptionsForType(type),ui: { expression: true },});}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-condition-builder.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-condition-builder.gjsindex fd0f3bc8522..143ea2f34ff 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-condition-builder.gjs+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/data-table-condition-builder.gjs@@ -7,6 +7,7 @@ import { i18n } from "discourse-i18n";import {implicitValueFor,isSingleValueOperator,+ operatorOptionsForType,operatorsForType,} from "../../../lib/workflows/condition-operators";import Collection from "./collection";@@ -31,7 +32,7 @@ function conditionSchema(type) {return {type: "options",required: true,- options: operatorsForType(type, { context: "data_table" }),+ options: operatorOptionsForType(type, { context: "data_table" }),};}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/condition-operators.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/condition-operators.jsindex 3a820a4a699..7d897418dd7 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/condition-operators.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/condition-operators.js@@ -1,3 +1,5 @@+import { localeKeyPart } from "./property-engine";+const OPERATORS = {equals: {contexts: {@@ -39,28 +41,28 @@ const OPERATORS = {},gt: {contexts: {- workflow: ["number"],+ workflow: ["number", "string"],data_table: ["string", "number", "date"],},needsValue: true,},lt: {contexts: {- workflow: ["number"],+ workflow: ["number", "string"],data_table: ["string", "number", "date"],},needsValue: true,},gte: {contexts: {- workflow: ["number"],+ workflow: ["number", "string"],data_table: ["string", "number", "date"],},needsValue: true,},lte: {contexts: {- workflow: ["number"],+ workflow: ["number", "string"],data_table: ["string", "number", "date"],},needsValue: true,@@ -83,6 +85,13 @@ export function operatorsForType(type, { context = "workflow" } = {}) {);}+export function operatorOptionsForType(type, { context = "workflow" } = {}) {+ return operatorsForType(type, { context }).map((operation) => ({+ value: operation,+ label_key: `discourse_workflows.if.operators.${localeKeyPart(operation)}`,+ }));+}+export function isSingleValueOperator(operation) {return OPERATORS[operation] !== undefined && !OPERATORS[operation].needsValue;}diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.jsindex 47a618a327a..c5e655f2876 100644--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js@@ -316,6 +316,8 @@ export function propertyOptionLabel(nodeDefinitionOrType, fieldName, option) {translatedOrNull(`${base}.${key}_${valueKey}`) ||translatedOrNull(`${base}.${key}s.${valueKey}`) ||(option.label_key ? translatedOrNull(option.label_key) : null) ||+ translatedOrNull(`${SHARED_FIELDS_BASE}.${key}_${valueKey}`) ||+ translatedOrNull(`${SHARED_FIELDS_BASE}.${key}s.${valueKey}`) ||option.label ||option.name ||option.valuediff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.ymlindex ef4266d42c3..b0964bcdff2 100644--- a/plugins/discourse-workflows/config/locales/client.en.yml+++ b/plugins/discourse-workflows/config/locales/client.en.yml@@ -379,10 +379,6 @@ en:field: "Field"operator: "Operator"value: "Value"- combinator:- title: "Match mode"- all: "All conditions must pass"- any: "Any condition can pass"operators:equals: "equals"not_equals: "does not equal"@@ -661,6 +657,9 @@ en:category_ids: "Categories"category_ids_description: "Only trigger for topics in these categories. Leave empty to match all categories."category_ids_placeholder: "All categories"+ combinator: "Match mode"+ combinator_and: "All conditions must pass"+ combinator_or: "Any condition can pass"include_subcategories: "Include subcategories"dynamic_values:badge_id: "a badge ID"diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/filter_parameter.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/filter_parameter.rbindex fc75e8a0593..2abf79cc427 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/filter_parameter.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/filter_parameter.rb@@ -10,10 +10,10 @@ module DiscourseWorkflows"notContains" => { types: %w[string array].freeze, needs_value: true }.freeze,"empty" => { types: %w[string array].freeze, needs_value: false }.freeze,"notEmpty" => { types: %w[string array].freeze, needs_value: false }.freeze,- "gt" => { types: %w[number].freeze, needs_value: true }.freeze,- "lt" => { types: %w[number].freeze, needs_value: true }.freeze,- "gte" => { types: %w[number].freeze, needs_value: true }.freeze,- "lte" => { types: %w[number].freeze, needs_value: true }.freeze,+ "gt" => { types: %w[number string].freeze, needs_value: true }.freeze,+ "lt" => { types: %w[number string].freeze, needs_value: true }.freeze,+ "gte" => { types: %w[number string].freeze, needs_value: true }.freeze,+ "lte" => { types: %w[number string].freeze, needs_value: true }.freeze,"true" => { types: %w[boolean].freeze, needs_value: false }.freeze,"false" => { types: %w[boolean].freeze, needs_value: false }.freeze,}.freeze@@ -107,11 +107,25 @@ module DiscourseWorkflowsleft.blank?when "notEmpty"left.present?+ when "gt", "lt", "gte", "lte"+ compare_ordered_strings(left, right, operation)elsefalseendend+ def self.compare_ordered_strings(left, right, operation)+ return false if right.nil?++ numeric_left = coerce_number(left)+ numeric_right = coerce_number(right)+ if numeric_left && numeric_right+ return compare_values(numeric_left, numeric_right, operation)+ end++ compare_values(left, right, operation)+ end+def self.evaluate_number(left, right, operation)left = coerce_number(left)right = coerce_number(right)diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/filter_parameter_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/filter_parameter_spec.rbindex 337f9edf5dc..fc13b07e485 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/filter_parameter_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/filter_parameter_spec.rb@@ -122,6 +122,7 @@ RSpec.describe DiscourseWorkflows::Executor::FilterParameter dodescribe ".supported_operation?" doit "uses the runtime type and operation contract" doexpect(described_class.supported_operation?("number", "gte")).to eq(true)+ expect(described_class.supported_operation?("string", "gte")).to eq(true)expect(described_class.supported_operation?("number", "contains")).to eq(false)expect(described_class.supported_operation?("integer", "equals")).to eq(false)expect(described_class.operation_needs_value?("equals")).to eq(true)@@ -193,6 +194,36 @@ RSpec.describe DiscourseWorkflows::Executor::FilterParameter doit "returns false for unknown operations" doexpect(described_class.evaluate_type("string", "a", "b", "unknown", {})).to be(false)end++ it "compares numeric strings numerically" do+ expect(described_class.evaluate_type("string", "10", "9", "gt", {})).to be(true)+ expect(described_class.evaluate_type("string", "9", "10", "gt", {})).to be(false)+ expect(described_class.evaluate_type("string", "9", "10", "lt", {})).to be(true)+ expect(described_class.evaluate_type("string", "10", "10", "gte", {})).to be(true)+ expect(described_class.evaluate_type("string", "10", "10", "lte", {})).to be(true)+ expect(described_class.evaluate_type("string", "10.5", "10", "gt", {})).to be(true)+ end++ it "falls back to lexicographic ordering for non numeric strings" do+ expect(described_class.evaluate_type("string", "2026-02-01", "2026-01-01", "gt", {})).to be(+ true,+ )+ expect(described_class.evaluate_type("string", "2026-01-01", "2026-02-01", "gt", {})).to be(+ false,+ )+ expect(described_class.evaluate_type("string", "apple", "banana", "lt", {})).to be(true)+ end++ it "returns false when ordering against a missing value" do+ expect(described_class.evaluate_type("string", "10", nil, "gt", {})).to be(false)+ end++ it "applies case insensitivity to ordering" do+ options = { "caseSensitive" => false }+ expect(described_class.evaluate_type("string", "Apple", "banana", "lt", options)).to be(+ true,+ )+ endendcontext "with number type" dodiff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-condition-operators-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-condition-operators-test.jsindex 1b46d40e622..4279b26f22f 100644--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-condition-operators-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-condition-operators-test.js@@ -2,6 +2,7 @@ import { module, test } from "qunit";import {implicitValueFor,isSingleValueOperator,+ operatorOptionsForType,operatorsForType,} from "discourse/plugins/discourse-workflows/admin/lib/workflows/condition-operators";@@ -17,6 +18,42 @@ module("Unit | lib | discourse-workflows | condition operators", function () {]);});+ test("offers ordering operators for string values", function (assert) {+ assert.deepEqual(operatorsForType("string"), [+ "equals",+ "notEquals",+ "contains",+ "notContains",+ "empty",+ "notEmpty",+ "gt",+ "lt",+ "gte",+ "lte",+ ]);+ });++ test("builds translatable operator options", function (assert) {+ assert.deepEqual(operatorOptionsForType("boolean"), [+ {+ value: "equals",+ label_key: "discourse_workflows.if.operators.equals",+ },+ {+ value: "notEquals",+ label_key: "discourse_workflows.if.operators.not_equals",+ },+ {+ value: "true",+ label_key: "discourse_workflows.if.operators.true",+ },+ {+ value: "false",+ label_key: "discourse_workflows.if.operators.false",+ },+ ]);+ });+test("returns data-table operators by context", function (assert) {assert.deepEqual(operatorsForType("boolean", { context: "data_table" }), ["empty",diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.jsindex 62e7fa8b03e..c35cc5f63a3 100644--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js@@ -39,6 +39,10 @@ module("Unit | Utility | workflows property engine", function () {test("resolves translated labels, descriptions, and placeholders", function (assert) {assert.strictEqual(propertyLabel("action:topic", "title"), "Title");assert.strictEqual(propertyLabel(ifNodeType, "combinator"), "Match mode");+ assert.strictEqual(+ propertyLabel("condition:filter", "combinator"),+ "Match mode"+ );assert.strictEqual(propertyLabel("trigger:schedule", "minutesInterval"),"Minutes between triggers"@@ -192,6 +196,14 @@ module("Unit | Utility | workflows property engine", function () {propertyOptionLabel("action:http_request", "method", { value: "GET" }),"GET");+ assert.strictEqual(+ propertyOptionLabel(ifNodeType, "combinator", { value: "and" }),+ "All conditions must pass"+ );+ assert.strictEqual(+ propertyOptionLabel("condition:filter", "combinator", { value: "or" }),+ "Any condition can pass"+ );assert.strictEqual(propertyOptionLabel(dataTableNodeType, "operation", {value: "insert",