Skip to content

The open alternative to Salesforce, designed for AI.

fix(filter): guard isMatchingDateFilter against empty date values (#22029)

Charles Bochet5ec98d3d84cf
diff --git a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingDateFilter.test.ts b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingDateFilter.test.ts
index d359bc34a64..9b6f7c44efe 100644
--- a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingDateFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingDateFilter.test.ts
@@ -1,3 +1,4 @@
+import { type DateFilter } from '@/types';
import { isMatchingDateFilter } from '@/utils/filter/utils/isMatchingDateFilter';
describe('isMatchingDateFilter', () => {
@@ -82,6 +83,45 @@ describe('isMatchingDateFilter', () => {
});
});
+ describe('null or undefined value', () => {
+ it.each([null, undefined])(
+ 'does not throw and returns false for comparison operators (value: %s)',
+ (value) => {
+ const comparisonFilters: DateFilter[] = [
+ { eq: testDate },
+ { neq: testDate },
+ { gt: testDate },
+ { gte: testDate },
+ { lt: testDate },
+ { lte: testDate },
+ { in: [testDate] },
+ ];
+
+ for (const dateFilter of comparisonFilters) {
+ expect(isMatchingDateFilter({ dateFilter, value })).toBe(false);
+ }
+ },
+ );
+
+ it.each([null, undefined])(
+ 'matches "is: NULL" for an empty value (value: %s)',
+ (value) => {
+ expect(
+ isMatchingDateFilter({ dateFilter: { is: 'NULL' }, value }),
+ ).toBe(true);
+ },
+ );
+
+ it.each([null, undefined])(
+ 'does not match "is: NOT_NULL" for an empty value (value: %s)',
+ (value) => {
+ expect(
+ isMatchingDateFilter({ dateFilter: { is: 'NOT_NULL' }, value }),
+ ).toBe(false);
+ },
+ );
+ });
+
describe('gt', () => {
it('value is greater than gt filter', () => {
expect(
diff --git a/packages/twenty-shared/src/utils/filter/utils/isMatchingDateFilter.ts b/packages/twenty-shared/src/utils/filter/utils/isMatchingDateFilter.ts
index 78492810a89..823e4405f76 100644
--- a/packages/twenty-shared/src/utils/filter/utils/isMatchingDateFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/isMatchingDateFilter.ts
@@ -1,4 +1,5 @@
import { type DateFilter } from '@/types';
+import { isDefined } from '@/utils';
import { isAfter, isBefore, isEqual, parseISO } from 'date-fns';
export const isMatchingDateFilter = ({
@@ -6,8 +7,12 @@ export const isMatchingDateFilter = ({
value,
}: {
dateFilter: DateFilter;
- value: string;
+ value: string | null | undefined;
}) => {
+ if (!isDefined(value)) {
+ return dateFilter.is === 'NULL';
+ }
+
switch (true) {
case dateFilter.eq !== undefined: {
return isEqual(parseISO(value), parseISO(dateFilter.eq));