Skip to content

The open alternative to Salesforce, designed for AI.

fix(workflow): match select filter options by exact value, not substring (#25828)

brekenb5bcf499bdd7
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/__tests__/evaluate-filter-conditions.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/__tests__/evaluate-filter-conditions.util.spec.ts
index 805d01b52b8..397194ba33d 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/__tests__/evaluate-filter-conditions.util.spec.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/__tests__/evaluate-filter-conditions.util.spec.ts
@@ -513,6 +513,109 @@ describe('evaluateFilterConditions', () => {
expect(result).toBe(true);
});
+ it('should return false when the selected value is a substring of an option (IS)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS,
+ 'INVALID',
+ '["VALID"]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(false);
+ });
+
+ it('should return true when the selected value matches an option exactly (IS)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS,
+ 'VALID',
+ '["VALID"]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(true);
+ });
+
+ it('should return true when the selected value is a substring of an option (IsNot)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS_NOT,
+ 'INVALID',
+ '["VALID"]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(true);
+ });
+
+ it('should return false when the selected value matches an option exactly (IsNot)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS_NOT,
+ 'VALID',
+ '["VALID"]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(false);
+ });
+
+ it('should match scalar option values that are valid JSON (IS)', () => {
+ const filter = createFilter(ViewFilterOperand.IS, '1', '1', 'SELECT');
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(true);
+ });
+
+ it('should match scalar option values that are valid JSON (IsNot)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS_NOT,
+ '1',
+ '1',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(false);
+ });
+
+ it('should match any of several selected options (IS)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS,
+ 'PENDING',
+ '["VALID","PENDING"]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(true);
+ });
+
+ it('should treat an empty option as a null value match (IS)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS,
+ null,
+ '[""]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(true);
+ });
+
+ it('should not match a non-null value against an empty option (IS)', () => {
+ const filter = createFilter(
+ ViewFilterOperand.IS,
+ 'VALID',
+ '[""]',
+ 'SELECT',
+ );
+ const result = evaluateFilterConditions({ filters: [filter] });
+
+ expect(result).toBe(false);
+ });
+
it('should return true when there are no values (IsEmpty)', () => {
const filter = createFilter(
ViewFilterOperand.IS_EMPTY,
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/evaluate-filter-conditions.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/evaluate-filter-conditions.util.ts
index 17edd2712fa..274a7c5c17d 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/evaluate-filter-conditions.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/filter/utils/evaluate-filter-conditions.util.ts
@@ -15,6 +15,8 @@ import {
import {
convertViewFilterOperandToCoreOperand as convertViewFilterOperandDeprecated,
isDefined,
+ isMatchingMultiSelectFilter,
+ isMatchingSelectFilter,
isSamePlainDate,
parseToInstantOrThrow,
} from 'twenty-shared/utils';
@@ -485,12 +487,68 @@ function evaluateDefaultFilter(filter: ResolvedFilter): boolean {
}
}
+function parseSelectFilterOptions(rightOperand: unknown): string[] {
+ if (Array.isArray(rightOperand)) {
+ return rightOperand.filter(isString);
+ }
+
+ if (isString(rightOperand)) {
+ try {
+ const parsedRightOperand: unknown = JSON.parse(rightOperand);
+
+ if (Array.isArray(parsedRightOperand)) {
+ return parsedRightOperand.filter(isString);
+ }
+
+ return [rightOperand];
+ } catch {
+ return [rightOperand];
+ }
+ }
+
+ return [];
+}
+
+function isMatchingSelectedOption(
+ leftOperand: unknown,
+ options: string[],
+): boolean {
+ const nonEmptyOptions = options.filter((option) => option !== '');
+ const hasEmptyOption = nonEmptyOptions.length !== options.length;
+
+ if (Array.isArray(leftOperand)) {
+ return (
+ isMatchingMultiSelectFilter({
+ multiSelectFilter: { containsAny: nonEmptyOptions },
+ value: leftOperand,
+ }) ||
+ (hasEmptyOption && leftOperand.length === 0)
+ );
+ }
+
+ const value = isString(leftOperand) ? leftOperand : null;
+
+ return (
+ isMatchingSelectFilter({
+ selectFilter: { in: nonEmptyOptions },
+ value,
+ }) ||
+ (hasEmptyOption && value === null)
+ );
+}
+
function evaluateSelectFilter(filter: ResolvedFilter): boolean {
switch (filter.operand) {
case ViewFilterOperand.IS:
- return contains(filter.leftOperand, filter.rightOperand);
+ return isMatchingSelectedOption(
+ filter.leftOperand,
+ parseSelectFilterOptions(filter.rightOperand),
+ );
case ViewFilterOperand.IS_NOT:
- return !contains(filter.leftOperand, filter.rightOperand);
+ return !isMatchingSelectedOption(
+ filter.leftOperand,
+ parseSelectFilterOptions(filter.rightOperand),
+ );
case ViewFilterOperand.IS_EMPTY:
return !isNotEmptyTextOrArray(filter.leftOperand);