Skip to content

The open alternative to Salesforce, designed for AI.

fix(front): record page pagination on views sorted by a boolean field (#26500)

BOHEUS2dd348285e90
diff --git a/packages/twenty-front/src/modules/object-record/graphql/utils/__tests__/computeCursorArgFilter.test.ts b/packages/twenty-front/src/modules/object-record/graphql/utils/__tests__/computeCursorArgFilter.test.ts
index 4fefce88b9b..5f818c39333 100644
--- a/packages/twenty-front/src/modules/object-record/graphql/utils/__tests__/computeCursorArgFilter.test.ts
+++ b/packages/twenty-front/src/modules/object-record/graphql/utils/__tests__/computeCursorArgFilter.test.ts
@@ -177,4 +177,46 @@ describe('computeCursorArgFilter', () => {
expect(result).toEqual({ or: [{ id: { gt: 'record-1' } }] });
});
+
+ it('should compare a BOOLEAN field with eq instead of gt', () => {
+ const result = computeCursorArgFilter({
+ orderBy: [{ isActive: 'AscNullsFirst' }],
+ cursorRecordValues: { isActive: false, id: 'record-1' },
+ isForwardPagination: true,
+ });
+
+ expect(result).toEqual({
+ or: [
+ { isActive: { eq: true } },
+ { and: [{ isActive: { eq: false } }, { id: { gt: 'record-1' } }] },
+ ],
+ });
+ });
+
+ it('should compare a BOOLEAN field with eq instead of lt', () => {
+ const result = computeCursorArgFilter({
+ orderBy: [{ isActive: 'AscNullsFirst' }],
+ cursorRecordValues: { isActive: true, id: 'record-1' },
+ isForwardPagination: false,
+ });
+
+ expect(result).toEqual({
+ or: [
+ { isActive: { eq: false } },
+ { and: [{ isActive: { eq: true } }, { id: { lt: 'record-1' } }] },
+ ],
+ });
+ });
+
+ it('should drop the BOOLEAN branch when no value comes after the cursor', () => {
+ const result = computeCursorArgFilter({
+ orderBy: [{ isActive: 'AscNullsFirst' }],
+ cursorRecordValues: { isActive: true, id: 'record-1' },
+ isForwardPagination: true,
+ });
+
+ expect(result).toEqual({
+ or: [{ and: [{ isActive: { eq: true } }, { id: { gt: 'record-1' } }] }],
+ });
+ });
});
diff --git a/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts b/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts
index 0ce586999a7..f53aee502e0 100644
--- a/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts
+++ b/packages/twenty-front/src/modules/object-record/graphql/utils/computeCursorArgFilter.ts
@@ -1,8 +1,9 @@
+import { isBoolean } from '@sniptt/guards';
import {
type RecordGqlOperationFilter,
type RecordGqlOperationOrderBy,
} from 'twenty-shared/types';
-import { isPlainObject } from 'twenty-shared/utils';
+import { isDefined, isPlainObject } from 'twenty-shared/utils';
import { isOrderByDirection } from '@/object-record/graphql/utils/isOrderByDirection';
@@ -43,6 +44,26 @@ const buildCursorWhereCondition = (
? { [field.fieldName]: { [field.subFieldName]: { [operator]: value } } }
: { [field.fieldName]: { [operator]: value } };
+const buildBooleanCursorWhereCondition = ({
+ field,
+ operator,
+ value,
+}: {
+ field: CursorOrderByField;
+ operator: string;
+ value: boolean;
+}): RecordGqlOperationFilter | undefined => {
+ if (operator === 'gt' && value === true) {
+ return undefined;
+ }
+
+ if (operator === 'lt' && value === false) {
+ return undefined;
+ }
+
+ return buildCursorWhereCondition(field, 'eq', !value);
+};
+
const resolveOrderByFields = (
orderBy: RecordGqlOperationOrderBy,
): CursorOrderByField[] => {
@@ -82,7 +103,7 @@ export const computeCursorArgFilter = ({
}): RecordGqlOperationFilter => {
const fields = resolveOrderByFields(orderBy);
- const cumulativeConditions: RecordGqlOperationFilter[] = fields.map(
+ const cumulativeConditions = fields.flatMap<RecordGqlOperationFilter>(
(field, index) => {
const equalityPrefixes = fields
.slice(0, index)
@@ -96,15 +117,22 @@ export const computeCursorArgFilter = ({
const ascending = isAscendingOrder(field.direction);
const operator = computeOperator(ascending, isForwardPagination);
- const comparison = buildCursorWhereCondition(
- field,
- operator,
- getCursorValue(cursorRecordValues, field),
- );
+ const cursorValue = getCursorValue(cursorRecordValues, field);
+ const comparison = isBoolean(cursorValue)
+ ? buildBooleanCursorWhereCondition({
+ field,
+ operator,
+ value: cursorValue,
+ })
+ : buildCursorWhereCondition(field, operator, cursorValue);
+
+ if (!isDefined(comparison)) {
+ return [];
+ }
const conditions = [...equalityPrefixes, comparison];
- return conditions.length === 1 ? conditions[0] : { and: conditions };
+ return [conditions.length === 1 ? conditions[0] : { and: conditions }];
},
);