Skip to content

The open alternative to Salesforce, designed for AI.

feat: support gt/lt comparisons on select and rating filters (#24116)

Thomas Trompetteae5987df6b98
diff --git a/packages/twenty-front/src/modules/object-record/record-filter/utils/isRecordMatchingFilter.ts b/packages/twenty-front/src/modules/object-record/record-filter/utils/isRecordMatchingFilter.ts
index 0db95a3f556..67e4195fd77 100644
--- a/packages/twenty-front/src/modules/object-record/record-filter/utils/isRecordMatchingFilter.ts
+++ b/packages/twenty-front/src/modules/object-record/record-filter/utils/isRecordMatchingFilter.ts
@@ -309,6 +309,7 @@ export const isRecordMatchingFilter = ({
return isMatchingRatingFilter({
ratingFilter: filterValue as RatingFilter,
value: record[filterKey],
+ options: objectMetadataField.options,
});
case FieldMetadataType.TEXT: {
return isMatchingStringFilter({
@@ -326,6 +327,7 @@ export const isRecordMatchingFilter = ({
return isMatchingSelectFilter({
selectFilter: filterValue as SelectFilter,
value: record[filterKey],
+ options: objectMetadataField.options,
});
case FieldMetadataType.MULTI_SELECT:
return isMatchingMultiSelectFilter({
diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/utils/create-gql-enum-filter-type.util.ts b/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/utils/create-gql-enum-filter-type.util.ts
index 442a06e0b29..c6a6cee41bf 100644
--- a/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/utils/create-gql-enum-filter-type.util.ts
+++ b/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/utils/create-gql-enum-filter-type.util.ts
@@ -20,6 +20,10 @@ export const createGqlEnumFilterType = (
containsAny: { type: new GraphQLList(enumType) },
is: { type: FilterIs },
isEmptyArray: { type: GraphQLBoolean },
+ gt: { type: enumType },
+ gte: { type: enumType },
+ lt: { type: enumType },
+ lte: { type: enumType },
}),
});
};
diff --git a/packages/twenty-server/src/engine/twenty-orm/utils/is-record-matching-rls-row-level-permission-predicate.util.ts b/packages/twenty-server/src/engine/twenty-orm/utils/is-record-matching-rls-row-level-permission-predicate.util.ts
index 39070baae94..676b8754f33 100644
--- a/packages/twenty-server/src/engine/twenty-orm/utils/is-record-matching-rls-row-level-permission-predicate.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/utils/is-record-matching-rls-row-level-permission-predicate.util.ts
@@ -238,6 +238,7 @@ export const isRecordMatchingRLSRowLevelPermissionPredicate = ({
return isMatchingRatingFilter({
ratingFilter: filterValue as RatingFilter,
value: recordFieldValue,
+ options: objectMetadataField.options,
});
case FieldMetadataType.TEXT: {
return isMatchingStringFilter({
@@ -255,6 +256,7 @@ export const isRecordMatchingRLSRowLevelPermissionPredicate = ({
return isMatchingSelectFilter({
selectFilter: filterValue as SelectFilter,
value: recordFieldValue,
+ options: objectMetadataField.options,
});
case FieldMetadataType.MULTI_SELECT:
return isMatchingMultiSelectFilter({
diff --git a/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts b/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts
index 88bebe7d3cf..d189fbd9f68 100644
--- a/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts
+++ b/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts
@@ -43,6 +43,10 @@ export type RatingFilter = {
eq?: string;
in?: string[];
is?: IsFilter;
+ gt?: string;
+ gte?: string;
+ lt?: string;
+ lte?: string;
};
export type FloatFilter = {
@@ -130,6 +134,10 @@ export type SelectFilter = {
in?: string[];
eq?: string;
neq?: string;
+ gt?: string;
+ gte?: string;
+ lt?: string;
+ lte?: string;
};
export type MultiSelectFilter = {
diff --git a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRatingFilter.test.ts b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRatingFilter.test.ts
index 9ec9dba2291..7e648893aa8 100644
--- a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRatingFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRatingFilter.test.ts
@@ -61,6 +61,51 @@ describe('isMatchingRatingFilter', () => {
});
});
+ describe('comparison operators', () => {
+ const options = [
+ 'RATING_1',
+ 'RATING_2',
+ 'RATING_3',
+ 'RATING_4',
+ 'RATING_5',
+ ].map((value, position) => ({ value, position }));
+
+ it('should compare by option position', () => {
+ expect(
+ isMatchingRatingFilter({
+ ratingFilter: { gt: 'RATING_2' },
+ value: 'RATING_4',
+ options,
+ }),
+ ).toBe(true);
+
+ expect(
+ isMatchingRatingFilter({
+ ratingFilter: { lte: 'RATING_2' },
+ value: 'RATING_4',
+ options,
+ }),
+ ).toBe(false);
+ });
+
+ it('should never match a null value or missing option values', () => {
+ expect(
+ isMatchingRatingFilter({
+ ratingFilter: { gt: 'RATING_2' },
+ value: null,
+ options,
+ }),
+ ).toBe(false);
+
+ expect(
+ isMatchingRatingFilter({
+ ratingFilter: { gt: 'RATING_2' },
+ value: 'RATING_4',
+ }),
+ ).toBe(false);
+ });
+ });
+
describe('default', () => {
it('should throw for unexpected filter', () => {
expect(() =>
diff --git a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingSelectFilter.test.ts b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingSelectFilter.test.ts
index 112fee91081..0fe0e51c42b 100644
--- a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingSelectFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingSelectFilter.test.ts
@@ -26,7 +26,7 @@ describe('isMatchingSelectFilter', () => {
expect(
isMatchingSelectFilter({
selectFilter: { is: 'NULL' },
- value: null as any,
+ value: null,
}),
).toBe(true);
});
@@ -70,6 +70,84 @@ describe('isMatchingSelectFilter', () => {
}),
).toBe(true);
});
+
+ it('should not match a null value, mirroring SQL semantics', () => {
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { neq: 'ACTIVE' },
+ value: null,
+ }),
+ ).toBe(false);
+ });
+ });
+
+ describe('comparison operators', () => {
+ const options = ['NEW', 'SCREENING', 'MEETING', 'PROPOSAL', 'CUSTOMER'].map(
+ (value, position) => ({ value, position }),
+ );
+
+ it('should compare by option position, not lexically', () => {
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { gt: 'NEW' },
+ value: 'MEETING',
+ options,
+ }),
+ ).toBe(true);
+
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { lt: 'MEETING' },
+ value: 'CUSTOMER',
+ options,
+ }),
+ ).toBe(false);
+ });
+
+ it('should handle gte and lte inclusively', () => {
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { gte: 'MEETING' },
+ value: 'MEETING',
+ options,
+ }),
+ ).toBe(true);
+
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { lte: 'MEETING' },
+ value: 'PROPOSAL',
+ options,
+ }),
+ ).toBe(false);
+ });
+
+ it('should never match a null value', () => {
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { gt: 'NEW' },
+ value: null,
+ options,
+ }),
+ ).toBe(false);
+ });
+
+ it('should never match when option values are unknown or missing', () => {
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { gt: 'NEW' },
+ value: 'DELETED_OPTION',
+ options,
+ }),
+ ).toBe(false);
+
+ expect(
+ isMatchingSelectFilter({
+ selectFilter: { gt: 'NEW' },
+ value: 'MEETING',
+ }),
+ ).toBe(false);
+ });
});
describe('default', () => {
diff --git a/packages/twenty-shared/src/utils/filter/utils/compareSelectOptionValues.ts b/packages/twenty-shared/src/utils/filter/utils/compareSelectOptionValues.ts
new file mode 100644
index 00000000000..a056f1d7841
--- /dev/null
+++ b/packages/twenty-shared/src/utils/filter/utils/compareSelectOptionValues.ts
@@ -0,0 +1,32 @@
+import { isDefined } from '@/utils/validation/isDefined';
+
+// Select and rating columns are Postgres enums: the server compares them by
+// enum definition order, which is the option position, not lexical order.
+// Returns null when the comparison cannot be evaluated (null value, unknown
+// option, missing options), mirroring SQL NULL comparison semantics.
+export const compareSelectOptionValues = ({
+ value,
+ comparisonValue,
+ options,
+}: {
+ value: string | null | undefined;
+ comparisonValue: string;
+ options: { value: string; position: number }[] | null | undefined;
+}): number | null => {
+ if (!isDefined(value) || !isDefined(options)) {
+ return null;
+ }
+
+ const orderedOptionValues = [...options]
+ .sort((optionA, optionB) => optionA.position - optionB.position)
+ .map((option) => option.value);
+
+ const valueIndex = orderedOptionValues.indexOf(value);
+ const comparisonValueIndex = orderedOptionValues.indexOf(comparisonValue);
+
+ if (valueIndex === -1 || comparisonValueIndex === -1) {
+ return null;
+ }
+
+ return valueIndex - comparisonValueIndex;
+};
diff --git a/packages/twenty-shared/src/utils/filter/utils/isMatchingRatingFilter.ts b/packages/twenty-shared/src/utils/filter/utils/isMatchingRatingFilter.ts
index faf12164cac..371b7ab2b12 100644
--- a/packages/twenty-shared/src/utils/filter/utils/isMatchingRatingFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/isMatchingRatingFilter.ts
@@ -1,11 +1,15 @@
import { type RatingFilter } from '@/types';
+import { compareSelectOptionValues } from './compareSelectOptionValues';
+
export const isMatchingRatingFilter = ({
ratingFilter,
value,
+ options,
}: {
ratingFilter: RatingFilter;
value: string | null;
+ options?: { value: string; position: number }[] | null;
}) => {
switch (true) {
case ratingFilter.eq !== undefined: {
@@ -21,6 +25,42 @@ export const isMatchingRatingFilter = ({
return value !== null;
}
}
+ case ratingFilter.gt !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: ratingFilter.gt,
+ options,
+ });
+
+ return comparison !== null && comparison > 0;
+ }
+ case ratingFilter.gte !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: ratingFilter.gte,
+ options,
+ });
+
+ return comparison !== null && comparison >= 0;
+ }
+ case ratingFilter.lt !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: ratingFilter.lt,
+ options,
+ });
+
+ return comparison !== null && comparison < 0;
+ }
+ case ratingFilter.lte !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: ratingFilter.lte,
+ options,
+ });
+
+ return comparison !== null && comparison <= 0;
+ }
default: {
throw new Error(
`Unexpected value for rating filter : ${JSON.stringify(ratingFilter)}`,
diff --git a/packages/twenty-shared/src/utils/filter/utils/isMatchingSelectFilter.ts b/packages/twenty-shared/src/utils/filter/utils/isMatchingSelectFilter.ts
index 468c75162d7..2eb1b22e65d 100644
--- a/packages/twenty-shared/src/utils/filter/utils/isMatchingSelectFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/isMatchingSelectFilter.ts
@@ -1,15 +1,19 @@
import { type SelectFilter } from '@/types';
+import { compareSelectOptionValues } from './compareSelectOptionValues';
+
export const isMatchingSelectFilter = ({
selectFilter,
value,
+ options,
}: {
selectFilter: SelectFilter;
- value: string;
+ value: string | null;
+ options?: { value: string; position: number }[] | null;
}) => {
switch (true) {
case selectFilter.in !== undefined: {
- return selectFilter.in.includes(value);
+ return value !== null && selectFilter.in.includes(value);
}
case selectFilter.is !== undefined: {
if (selectFilter.is === 'NULL') {
@@ -22,7 +26,43 @@ export const isMatchingSelectFilter = ({
return value === selectFilter.eq;
}
case selectFilter.neq !== undefined: {
- return value !== selectFilter.neq;
+ return value !== null && value !== selectFilter.neq;
+ }
+ case selectFilter.gt !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: selectFilter.gt,
+ options,
+ });
+
+ return comparison !== null && comparison > 0;
+ }
+ case selectFilter.gte !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: selectFilter.gte,
+ options,
+ });
+
+ return comparison !== null && comparison >= 0;
+ }
+ case selectFilter.lt !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: selectFilter.lt,
+ options,
+ });
+
+ return comparison !== null && comparison < 0;
+ }
+ case selectFilter.lte !== undefined: {
+ const comparison = compareSelectOptionValues({
+ value,
+ comparisonValue: selectFilter.lte,
+ options,
+ });
+
+ return comparison !== null && comparison <= 0;
}
default: {
throw new Error(
diff --git a/packages/twenty-shared/src/utils/index.ts b/packages/twenty-shared/src/utils/index.ts
index c05c3ce4853..46a6263a376 100644
--- a/packages/twenty-shared/src/utils/index.ts
+++ b/packages/twenty-shared/src/utils/index.ts
@@ -125,6 +125,7 @@ export { turnRecordFilterGroupsIntoGqlOperationFilter } from './filter/turnRecor
export type { FieldShared } from './filter/turnRecordFilterIntoGqlOperationFilter';
export { turnRecordFilterIntoRecordGqlOperationFilter } from './filter/turnRecordFilterIntoGqlOperationFilter';
export { combineFilters } from './filter/utils/combineFilters';
+export { compareSelectOptionValues } from './filter/utils/compareSelectOptionValues';
export { COMPOSITE_FIELD_FILTER_OPERANDS_MAP } from './filter/utils/compositeFieldFilterOperandsMap';
export { convertViewFilterOperandToCoreOperand } from './filter/utils/convert-view-filter-operand-to-core-operand.util';
export { convertViewFilterValueToString } from './filter/utils/convertViewFilterValueToString';