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 d359bc34a6..9b6f7c44ef 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 78492810a8..823e4405f7 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));