The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Unify comparison operators for workflow evaluate and group-move validate.
Kiran K34ae9fa886c4
diff --git a/apps/web/lib/api/groups/validate-group-move-rules.ts b/apps/web/lib/api/groups/validate-group-move-rules.tsindex 62c46f9f7a..75842ab31f 100644--- a/apps/web/lib/api/groups/validate-group-move-rules.ts+++ b/apps/web/lib/api/groups/validate-group-move-rules.ts@@ -1,7 +1,7 @@+import { COMPARISON_OPERATORS } from "@/lib/api/workflows/operators";import {GROUP_MOVE_ATTRIBUTE_CONFIG,GROUP_MOVE_OPERATOR_LABELS,- GROUP_MOVE_OPERATOR_VALIDATORS,GroupMoveAttribute,type GroupMoveCondition,type GroupMoveRules,@@ -78,16 +78,16 @@ const validateRule = ({}// Validate value shape/constraints for the selected operator- const validateOperator =- GROUP_MOVE_OPERATOR_VALIDATORS[- rule.operator as keyof typeof GROUP_MOVE_OPERATOR_VALIDATORS- ];+ const operator = COMPARISON_OPERATORS[rule.operator];- if (validateOperator) {- validateOperator({- rule,- ruleIndex,- });+ if (operator) {+ try {+ operator.validate(rule.value);+ } catch (error) {+ throw new Error(+ `Rule ${ruleIndex + 1}: ${error instanceof Error ? error.message : "Invalid value."}`,+ );+ }}// Validate attribute-specific constraints (business rules)diff --git a/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts b/apps/web/lib/api/workflows/evaluate-workflow-conditions.tsindex 641a9f37af..299c81000a 100644--- a/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts+++ b/apps/web/lib/api/workflows/evaluate-workflow-conditions.ts@@ -1,5 +1,5 @@import { WorkflowCondition, WorkflowConditionAttribute } from "@/lib/types";-import { OPERATOR_FUNCTIONS } from "@/lib/zod/schemas/workflows";+import { COMPARISON_OPERATORS } from "./operators";export function evaluateWorkflowConditions({conditions,@@ -11,9 +11,9 @@ export function evaluateWorkflowConditions({if (conditions.length === 0) return false;for (const condition of conditions) {- const operatorFn = OPERATOR_FUNCTIONS[condition.operator];+ const operator = COMPARISON_OPERATORS[condition.operator];- if (!operatorFn) {+ if (!operator) {console.error(`Operator ${condition.operator} is not supported.`);return false;}@@ -25,7 +25,7 @@ export function evaluateWorkflowConditions({return false;}- if (!operatorFn(attributeValue, condition.value)) {+ if (!operator.evaluate(attributeValue, condition.value)) {return false;}}diff --git a/apps/web/lib/api/workflows/operators.ts b/apps/web/lib/api/workflows/operators.tsnew file mode 100644index 0000000000..4113dbf725--- /dev/null+++ b/apps/web/lib/api/workflows/operators.ts@@ -0,0 +1,66 @@+import { WorkflowComparisonOperator } from "@/lib/types";++type ConditionValue = number | { min: number; max?: number };++type ComparisonOperator = {+ validate: (value: ConditionValue) => void;+ evaluate: (attributeValue: number, conditionValue: ConditionValue) => boolean;+};++export const COMPARISON_OPERATORS: Record<+ WorkflowComparisonOperator,+ ComparisonOperator+> = {+ // Greater than or equal to+ gte: {+ validate(value) {+ if (typeof value !== "number" || isNaN(value) || value <= 0) {+ throw new Error("Please enter a value greater than 0.");+ }+ },+ evaluate(attributeValue, conditionValue) {+ if (typeof conditionValue !== "number") {+ return false;+ }++ return attributeValue >= conditionValue;+ },+ },++ // Between (inclusive)+ between: {+ validate(value) {+ if (typeof value !== "object" || value === null) {+ throw new Error("Please enter a valid value.");+ }++ const min = value.min;+ const max = value.max;++ if (min == null || min === undefined || isNaN(min) || min <= 0) {+ throw new Error("Please enter a minimum value greater than 0.");+ }++ if (max == null || max === undefined || isNaN(max) || max <= 0) {+ throw new Error("Please enter a maximum value (limit) greater than 0.");+ }++ if (max <= min) {+ throw new Error("Maximum value must be greater than minimum value.");+ }+ },+ evaluate(attributeValue, conditionValue) {+ if (typeof conditionValue !== "object" || conditionValue === null) {+ return false;+ }++ const { min, max } = conditionValue;++ if (min == null || max == null) {+ return false;+ }++ return attributeValue >= min && attributeValue <= max;+ },+ },+};diff --git a/apps/web/lib/zod/schemas/group-move-workflows.ts b/apps/web/lib/zod/schemas/group-move-workflows.tsindex 2a210287a9..12ec7c097b 100644--- a/apps/web/lib/zod/schemas/group-move-workflows.ts+++ b/apps/web/lib/zod/schemas/group-move-workflows.ts@@ -54,48 +54,6 @@ export const groupMoveConditionSchema = z.object({export const groupMoveRulesSchema = z.array(groupMoveConditionSchema);-export const GROUP_MOVE_OPERATOR_VALIDATORS = {- gte({ rule, ruleIndex }: GroupMoveAttributeValidatorArgs) {- if (- typeof rule.value !== "number" ||- isNaN(rule.value) ||- rule.value <= 0- ) {- throw new Error(- `Rule ${ruleIndex + 1}: Please enter a value greater than 0.`,- );- }- },-- between({ rule, ruleIndex }: GroupMoveAttributeValidatorArgs) {- if (typeof rule.value !== "object" || rule.value === null) {- throw new Error(`Rule ${ruleIndex + 1}: Please enter a valid value.`);- }-- const min = rule.value.min;- const max = rule.value.max;-- if (min == null || min === undefined || isNaN(min) || min <= 0) {- throw new Error(- `Rule ${ruleIndex + 1}: Please enter a minimum value greater than 0.`,- );- }-- if (max == null || max === undefined || isNaN(max) || max <= 0) {- throw new Error(- `Rule ${ruleIndex + 1}: Please enter a maximum value (limit) greater than 0.`,- );- }-- // Ensure max is greater than min- if (max <= min) {- throw new Error(- `Rule ${ruleIndex + 1}: Maximum value must be greater than minimum value.`,- );- }- },-} as const;-export type GroupMoveCondition = z.infer<typeof groupMoveConditionSchema>;export type GroupMoveRules = z.infer<typeof groupMoveRulesSchema>;@@ -109,8 +67,3 @@ export type GroupMoveAttributeConfig = {inputType: "number" | "currency";operators: readonly GroupMoveOperator[];};--type GroupMoveAttributeValidatorArgs = {- rule: GroupMoveCondition;- ruleIndex: number;-};diff --git a/apps/web/lib/zod/schemas/workflows.ts b/apps/web/lib/zod/schemas/workflows.tsindex 749ee39ed5..8db5a8f4c3 100644--- a/apps/web/lib/zod/schemas/workflows.ts+++ b/apps/web/lib/zod/schemas/workflows.ts@@ -1,15 +1,7 @@-import {- WorkflowComparisonOperator,- WorkflowConditionAttribute,-} from "@/lib/types";+import { WorkflowConditionAttribute } from "@/lib/types";import { WorkflowTrigger } from "@prisma/client";import * as z from "zod/v4";-type OperatorFn = (- aV: number,- cV: number | { min: number; max?: number },-) => boolean;-export const WORKFLOW_ATTRIBUTES = ["totalLeads","totalConversions",@@ -41,32 +33,6 @@ export const WORKFLOW_SCHEDULES: Partial<Record<WorkflowTrigger, string>> = {partnerEnrolled: "0 */12 * * *", // every 12 hours};-export const OPERATOR_FUNCTIONS: Record<- WorkflowComparisonOperator,- OperatorFn-> = {- gte: (aV, cV) => {- if (typeof cV !== "number") {- return false;- }-- return aV >= cV;- },- between: (aV, cV) => {- if (typeof cV !== "object" || cV === null) {- return false;- }-- const { min, max } = cV;-- if (min == null || max == null) {- return false;- }-- return aV >= min && aV <= max;- },-};-export enum WORKFLOW_ACTION_TYPES {AwardBounty = "awardBounty",SendCampaign = "sendCampaign",diff --git a/apps/web/tests/workflows/comparison-operators.test.ts b/apps/web/tests/workflows/comparison-operators.test.tsnew file mode 100644index 0000000000..5da71abbc9--- /dev/null+++ b/apps/web/tests/workflows/comparison-operators.test.ts@@ -0,0 +1,101 @@+import { COMPARISON_OPERATORS } from "@/lib/api/workflows/operators";+import { describe, expect, it } from "vitest";++describe("COMPARISON_OPERATORS.gte.evaluate", () => {+ const { evaluate } = COMPARISON_OPERATORS.gte;++ it("returns true when attribute value is greater than or equal to condition", () => {+ expect(evaluate(10, 10)).toBe(true);+ expect(evaluate(11, 10)).toBe(true);+ });++ it("returns false when attribute value is less than condition", () => {+ expect(evaluate(9, 10)).toBe(false);+ });++ it("returns false when condition value is not a number", () => {+ expect(evaluate(10, { min: 1, max: 5 })).toBe(false);+ });+});++describe("COMPARISON_OPERATORS.between.evaluate", () => {+ const { evaluate } = COMPARISON_OPERATORS.between;++ it("returns true when attribute value is within inclusive range", () => {+ expect(evaluate(1, { min: 1, max: 5 })).toBe(true);+ expect(evaluate(3, { min: 1, max: 5 })).toBe(true);+ expect(evaluate(5, { min: 1, max: 5 })).toBe(true);+ });++ it("returns false when attribute value is outside range", () => {+ expect(evaluate(0, { min: 1, max: 5 })).toBe(false);+ expect(evaluate(6, { min: 1, max: 5 })).toBe(false);+ });++ it("returns false when condition value is missing or invalid", () => {+ expect(evaluate(3, 3)).toBe(false);+ expect(evaluate(3, { min: 1 })).toBe(false);+ expect(evaluate(3, null as unknown as { min: number; max: number })).toBe(+ false,+ );+ });+});++describe("COMPARISON_OPERATORS.gte.validate", () => {+ const { validate } = COMPARISON_OPERATORS.gte;++ it("accepts a positive number", () => {+ expect(() => validate(1)).not.toThrow();+ expect(() => validate(100)).not.toThrow();+ });++ it("rejects non-number, NaN, and non-positive values", () => {+ expect(() => validate({ min: 1, max: 5 })).toThrow(+ "Please enter a value greater than 0.",+ );+ expect(() => validate(NaN)).toThrow(+ "Please enter a value greater than 0.",+ );+ expect(() => validate(0)).toThrow("Please enter a value greater than 0.");+ expect(() => validate(-1)).toThrow("Please enter a value greater than 0.");+ });+});++describe("COMPARISON_OPERATORS.between.validate", () => {+ const { validate } = COMPARISON_OPERATORS.between;++ it("accepts a valid min/max range", () => {+ expect(() => validate({ min: 1, max: 5 })).not.toThrow();+ });++ it("rejects an invalid value shape", () => {+ expect(() => validate(5)).toThrow("Please enter a valid value.");+ });++ it("rejects non-positive or missing min", () => {+ expect(() => validate({ min: 0, max: 5 })).toThrow(+ "Please enter a minimum value greater than 0.",+ );+ expect(() => validate({ min: NaN, max: 5 })).toThrow(+ "Please enter a minimum value greater than 0.",+ );+ });++ it("rejects non-positive or missing max", () => {+ expect(() => validate({ min: 1, max: 0 })).toThrow(+ "Please enter a maximum value (limit) greater than 0.",+ );+ expect(() => validate({ min: 1 })).toThrow(+ "Please enter a maximum value (limit) greater than 0.",+ );+ });++ it("rejects when max is less than or equal to min", () => {+ expect(() => validate({ min: 5, max: 5 })).toThrow(+ "Maximum value must be greater than minimum value.",+ );+ expect(() => validate({ min: 5, max: 4 })).toThrow(+ "Maximum value must be greater than minimum value.",+ );+ });+});