Skip to content

The open alternative to Salesforce, designed for AI.

Fix JSON filter (#26589)

BOHEUSaba884f6e07d
diff --git a/packages/twenty-server/src/engine/api/common/common-args-processors/filter-arg-processor/constants/filter-operators.constant.ts b/packages/twenty-server/src/engine/api/common/common-args-processors/filter-arg-processor/constants/filter-operators.constant.ts
index cec02355c60..adae42c0fb1 100644
--- a/packages/twenty-server/src/engine/api/common/common-args-processors/filter-arg-processor/constants/filter-operators.constant.ts
+++ b/packages/twenty-server/src/engine/api/common/common-args-processors/filter-arg-processor/constants/filter-operators.constant.ts
@@ -75,6 +75,10 @@ export const ENUM_FILTER_OPERATORS: FilterOperator[] = [
'isEmptyArray',
];
-export const RAW_JSON_FILTER_OPERATORS: FilterOperator[] = ['is', 'like'];
+export const RAW_JSON_FILTER_OPERATORS: FilterOperator[] = [
+ 'is',
+ 'like',
+ 'ilike',
+];
export const RICH_TEXT_FILTER_OPERATORS: FilterOperator[] = ['ilike'];
diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/graphql-types/input/raw-json-filter.input-type.ts b/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/graphql-types/input/raw-json-filter.input-type.ts
index 6be4b09e66f..502a1ca3040 100644
--- a/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/graphql-types/input/raw-json-filter.input-type.ts
+++ b/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/graphql-types/input/raw-json-filter.input-type.ts
@@ -10,5 +10,10 @@ export const RawJsonFilterType = new GraphQLInputObjectType({
type: GraphQLString,
description: 'Pattern match with % wildcard (e.g. %value%)',
},
+ ilike: {
+ type: GraphQLString,
+ description:
+ 'Case-insensitive pattern match with % wildcard (e.g. %value%)',
+ },
},
});
diff --git a/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/field-filters.json-schema.ts b/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/field-filters.json-schema.ts
index 57a83113699..39227194ca1 100644
--- a/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/field-filters.json-schema.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/field-filters.json-schema.ts
@@ -30,6 +30,8 @@ const SHARED_FILTER_NAME_BY_FIELD_TYPE: Partial<
[FieldMetadataType.EMAILS]: 'EmailsFilter',
[FieldMetadataType.PHONES]: 'PhonesFilter',
[FieldMetadataType.LINKS]: 'LinksFilter',
+ [FieldMetadataType.RAW_JSON]: 'RawJsonFilter',
+ [FieldMetadataType.FILES]: 'RawJsonFilter',
};
const referenceSharedFilter = ({
diff --git a/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/shared-filter-definitions.json-schema.ts b/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/shared-filter-definitions.json-schema.ts
index 070151683ad..f48a0125eb5 100644
--- a/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/shared-filter-definitions.json-schema.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-crud/json-schemas/shared-filter-definitions.json-schema.ts
@@ -118,6 +118,18 @@ export const SHARED_FILTER_JSON_SCHEMAS = {
},
},
DefaultFilter: textFilter(),
+ RawJsonFilter: {
+ type: 'object',
+ properties: {
+ like: stringOperator({
+ description: 'LIKE on the JSON text (% wildcard)',
+ }),
+ ilike: stringOperator({
+ description: 'ILIKE on the JSON text (% wildcard, case-insensitive)',
+ }),
+ is: NULL_CHECK_REFERENCE,
+ },
+ },
ArrayFilter: {
type: 'object',
properties: {
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 79ff82ef012..9cb3053c44d 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
@@ -1480,6 +1480,53 @@ describe('evaluateFilterConditions', () => {
});
});
+ describe('raw JSON operands', () => {
+ const jsonValue = { name: 'Acme', tags: ['vip-gold'] };
+
+ const evaluateRawJson = (
+ operand: ViewFilterOperand,
+ leftOperand: unknown,
+ rightOperand: unknown = '',
+ ) =>
+ evaluateFilterConditions({
+ filters: [
+ createFilter(operand, leftOperand, rightOperand, 'RAW_JSON'),
+ ],
+ });
+
+ it('should search inside JSON values case-insensitively', () => {
+ expect(
+ evaluateRawJson(ViewFilterOperand.CONTAINS, jsonValue, 'acme'),
+ ).toBe(true);
+ expect(
+ evaluateRawJson(
+ ViewFilterOperand.CONTAINS,
+ JSON.stringify(jsonValue),
+ 'VIP',
+ ),
+ ).toBe(true);
+ expect(
+ evaluateRawJson(
+ ViewFilterOperand.DOES_NOT_CONTAIN,
+ jsonValue,
+ 'Acme',
+ ),
+ ).toBe(false);
+ });
+
+ it('should treat missing values and a stringified null as empty', () => {
+ expect(evaluateRawJson(ViewFilterOperand.IS_EMPTY, 'null')).toBe(true);
+ expect(evaluateRawJson(ViewFilterOperand.IS_EMPTY, null)).toBe(true);
+ expect(evaluateRawJson(ViewFilterOperand.IS_EMPTY, '')).toBe(true);
+ expect(evaluateRawJson(ViewFilterOperand.IS_NOT_EMPTY, jsonValue)).toBe(
+ true,
+ );
+ expect(
+ evaluateRawJson(ViewFilterOperand.DOES_NOT_CONTAIN, null, 'Acme'),
+ ).toBe(true);
+ });
+ });
+
describe('error cases', () => {
it('should throw error for unknown operand', () => {
const filter = createFilter(
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 fe2e6105bca..c0300626f07 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
@@ -69,12 +69,13 @@ function evaluateFilter(
case 'LINKS':
case 'ARRAY':
case 'array':
- case 'RAW_JSON':
return evaluateTextAndArrayFilter(
filterWithConvertedOperand,
filter.type,
filter.compositeFieldSubFieldName,
);
+ case 'RAW_JSON':
+ return evaluateRawJsonFilter(filterWithConvertedOperand);
case 'SELECT':
return evaluateSelectFilter(filterWithConvertedOperand);
case 'BOOLEAN':
@@ -202,6 +203,44 @@ function evaluateTextAndArrayFilter(
}
}
+function parseRawJsonOperand(leftOperand: unknown): unknown {
+ if (!isString(leftOperand)) {
+ return leftOperand;
+ }
+
+ if (leftOperand.trim() === 'null') {
+ return null;
+ }
+
+ return parseJson<unknown>(leftOperand) ?? leftOperand;
+}
+
+function evaluateRawJsonFilter(filter: ResolvedFilter): boolean {
+ const jsonValue = parseRawJsonOperand(filter.leftOperand);
+ const isEmpty = !isDefined(jsonValue) || jsonValue === '';
+
+ const containsSearchValue = () =>
+ !isEmpty &&
+ JSON.stringify(jsonValue)
+ .toLowerCase()
+ .includes(String(filter.rightOperand ?? '').toLowerCase());
+
+ switch (filter.operand) {
+ case ViewFilterOperand.CONTAINS:
+ return containsSearchValue();
+ case ViewFilterOperand.DOES_NOT_CONTAIN:
+ return !containsSearchValue();
+ case ViewFilterOperand.IS_EMPTY:
+ return isEmpty;
+ case ViewFilterOperand.IS_NOT_EMPTY:
+ return !isEmpty;
+ default:
+ throw new Error(
+ `Operand ${filter.operand} not supported for raw JSON filter`,
+ );
+ }
+}
+
function isNotEmptyTextOrArray(value: unknown): boolean {
return isNonEmptyString(value) || isNonEmptyArray(value);
}
diff --git a/packages/twenty-server/test/integration/ai/suites/__snapshots__/mcp-database-tool-input-schemas.integration-spec.ts.snap b/packages/twenty-server/test/integration/ai/suites/__snapshots__/mcp-database-tool-input-schemas.integration-spec.ts.snap
index 91612d4446d..0699a7ba667 100644
--- a/packages/twenty-server/test/integration/ai/suites/__snapshots__/mcp-database-tool-input-schemas.integration-spec.ts.snap
+++ b/packages/twenty-server/test/integration/ai/suites/__snapshots__/mcp-database-tool-input-schemas.integration-spec.ts.snap
@@ -2012,12 +2012,8 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
},
"extraData": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -2029,11 +2025,7 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -3373,12 +3365,8 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
},
"extraData": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -3390,11 +3378,7 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -5100,12 +5084,8 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
},
"extraData": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -5117,11 +5097,7 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -5959,12 +5935,8 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
},
"extraData": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -5976,11 +5948,7 @@ exports[`MCP database tool input schemas should list the input schemas of the pe
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -7564,12 +7532,8 @@ exports[`MCP database tool input schemas should list the input schemas of the su
},
"files": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -7581,11 +7545,7 @@ exports[`MCP database tool input schemas should list the input schemas of the su
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -8199,12 +8159,8 @@ exports[`MCP database tool input schemas should list the input schemas of the su
},
"files": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -8216,11 +8172,7 @@ exports[`MCP database tool input schemas should list the input schemas of the su
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -8947,12 +8899,8 @@ exports[`MCP database tool input schemas should list the input schemas of the su
},
"files": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -8964,11 +8912,7 @@ exports[`MCP database tool input schemas should list the input schemas of the su
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
@@ -9341,12 +9285,8 @@ exports[`MCP database tool input schemas should list the input schemas of the su
},
"files": {
"properties": {
- "eq": {
- "description": "Equals",
- "type": "string",
- },
"ilike": {
- "description": "ILIKE (% wildcard, case-insensitive)",
+ "description": "ILIKE on the JSON text (% wildcard, case-insensitive)",
"type": "string",
},
"is": {
@@ -9358,11 +9298,7 @@ exports[`MCP database tool input schemas should list the input schemas of the su
"type": "string",
},
"like": {
- "description": "LIKE (% wildcard)",
- "type": "string",
- },
- "neq": {
- "description": "Not equals",
+ "description": "LIKE on the JSON text (% wildcard)",
"type": "string",
},
},
diff --git a/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/__snapshots__/files-field-filter-input-validation.integration-spec.ts.snap b/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/__snapshots__/files-field-filter-input-validation.integration-spec.ts.snap
index 46fd2c0a249..3201900551f 100644
--- a/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/__snapshots__/files-field-filter-input-validation.integration-spec.ts.snap
+++ b/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/__snapshots__/files-field-filter-input-validation.integration-spec.ts.snap
@@ -1,12 +1,12 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
-exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":[]}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like"`;
+exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":[]}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like, ilike"`;
-exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":{}}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like"`;
+exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":{}}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like, ilike"`;
-exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":2}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like"`;
+exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":2}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like, ilike"`;
-exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":true}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like"`;
+exports[`Filter input validation - FILES Gql filter input - failure FILES field type - should fail with filter : {"filesField":{"containsIlike":true}} 1`] = `"Operator "containsIlike" is not valid for field "filesField" of type FILES - Allowed operators: is, like, ilike"`;
exports[`Filter input validation - FILES Rest filter input - failure FILES field type - should fail with filter : "filesField[containsAny]:\\"[]\\"" 1`] = `
[
diff --git a/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/constants/successful-filter-input-by-field-metadata-type.constant.ts b/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/constants/successful-filter-input-by-field-metadata-type.constant.ts
index 2daca44d374..8e625df75a8 100644
--- a/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/constants/successful-filter-input-by-field-metadata-type.constant.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/inputs-validation/filter-validation/constants/successful-filter-input-by-field-metadata-type.constant.ts
@@ -951,6 +951,15 @@ export const successfulFilterInputByFieldMetadataType: {
return JSON.stringify(record.rawJsonField).includes('test');
},
},
+ {
+ gqlFilterInput: { rawJsonField: { ilike: '%TEST%' } },
+ restFilterInput: 'rawJsonField[ilike]:"%TEST%"',
+ validateFilter: (record: Record<string, any>) => {
+ return JSON.stringify(record.rawJsonField)
+ .toLowerCase()
+ .includes('test');
+ },
+ },
],
[FieldMetadataType.ARRAY]: [
{
@@ -1000,5 +1009,17 @@ export const successfulFilterInputByFieldMetadataType: {
return Array.isArray(record.filesField) && record.filesField.length > 0;
},
},
+ {
+ gqlFilterInput: { filesField: { ilike: '%document.PDF%' } },
+ restFilterInput: 'filesField[ilike]:"%document.PDF%"',
+ validateFilter: (record: Record<string, any>) => {
+ return (
+ Array.isArray(record.filesField) &&
+ record.filesField.some(
+ (file: Record<string, any>) => file.label === 'Document.pdf',
+ )
+ );
+ },
+ },
],
};
diff --git a/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts b/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts
index 4d1f773f60b..cab079cc0ba 100644
--- a/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts
+++ b/packages/twenty-shared/src/types/RecordGqlOperationFilter.ts
@@ -154,11 +154,13 @@ export type ArrayFilter = {
export type RawJsonFilter = {
like?: string;
+ ilike?: string;
is?: IsFilter;
};
export type FilesFilter = {
like?: string;
+ ilike?: string;
is?: IsFilter;
};
diff --git a/packages/twenty-shared/src/utils/filter/__tests__/turnRecordFilterIntoGqlOperationFilter.test.ts b/packages/twenty-shared/src/utils/filter/__tests__/turnRecordFilterIntoGqlOperationFilter.test.ts
index 8db2569dcd3..8dd118a1718 100644
--- a/packages/twenty-shared/src/utils/filter/__tests__/turnRecordFilterIntoGqlOperationFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/__tests__/turnRecordFilterIntoGqlOperationFilter.test.ts
@@ -717,7 +717,7 @@ describe('turnRecordFilterIntoRecordGqlOperationFilter', () => {
fieldMetadataItemById,
});
- expect(result).toEqual({ metadata: { like: '%test%' } });
+ expect(result).toEqual({ metadata: { ilike: '%test%' } });
});
it('should handle DOES_NOT_CONTAIN operand', () => {
@@ -731,7 +731,12 @@ describe('turnRecordFilterIntoRecordGqlOperationFilter', () => {
fieldMetadataItemById,
});
- expect(result).toEqual({ not: { metadata: { like: '%test%' } } });
+ expect(result).toEqual({
+ or: [
+ { not: { metadata: { ilike: '%test%' } } },
+ { metadata: { is: 'NULL' } },
+ ],
+ });
});
});
@@ -747,7 +752,26 @@ describe('turnRecordFilterIntoRecordGqlOperationFilter', () => {
fieldMetadataItemById,
});
- expect(result).toEqual({ attachments: { like: '%doc%' } });
+ expect(result).toEqual({ attachments: { ilike: '%doc%' } });
+ });
+
+ it('should handle DOES_NOT_CONTAIN operand', () => {
+ const result = turnRecordFilterIntoRecordGqlOperationFilter({
+ filterValueDependencies,
+ recordFilter: makeFilter(
+ 'f-files',
+ RecordFilterOperand.DOES_NOT_CONTAIN,
+ 'doc',
+ ),
+ fieldMetadataItemById,
+ });
+
+ expect(result).toEqual({
+ or: [
+ { not: { attachments: { ilike: '%doc%' } } },
+ { attachments: { is: 'NULL' } },
+ ],
+ });
});
});
diff --git a/packages/twenty-shared/src/utils/filter/turnRecordFilterIntoGqlOperationFilter.ts b/packages/twenty-shared/src/utils/filter/turnRecordFilterIntoGqlOperationFilter.ts
index 0da8e075d1f..8cd91f34f86 100644
--- a/packages/twenty-shared/src/utils/filter/turnRecordFilterIntoGqlOperationFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/turnRecordFilterIntoGqlOperationFilter.ts
@@ -318,16 +318,23 @@ const buildDirectFieldGqlOperationFilter = ({
case RecordFilterOperand.CONTAINS:
return {
[fieldMetadataItem.name]: {
- like: `%${recordFilter.value}%`,
+ ilike: `%${recordFilter.value}%`,
} as RawJsonFilter,
};
case RecordFilterOperand.DOES_NOT_CONTAIN:
return {
- not: {
- [fieldMetadataItem.name]: {
- like: `%${recordFilter.value}%`,
- } as RawJsonFilter,
- },
+ or: [
+ {
+ not: {
+ [fieldMetadataItem.name]: {
+ ilike: `%${recordFilter.value}%`,
+ } as RawJsonFilter,
+ },
+ },
+ {
+ [fieldMetadataItem.name]: { is: 'NULL' } as RawJsonFilter,
+ },
+ ],
};
default:
throw new Error(
@@ -339,16 +346,23 @@ const buildDirectFieldGqlOperationFilter = ({
case RecordFilterOperand.CONTAINS:
return {
[fieldMetadataItem.name]: {
- like: `%${recordFilter.value}%`,
+ ilike: `%${recordFilter.value}%`,
} as FilesFilter,
};
case RecordFilterOperand.DOES_NOT_CONTAIN:
return {
- not: {
- [fieldMetadataItem.name]: {
- like: `%${recordFilter.value}%`,
- } as FilesFilter,
- },
+ or: [
+ {
+ not: {
+ [fieldMetadataItem.name]: {
+ ilike: `%${recordFilter.value}%`,
+ } as FilesFilter,
+ },
+ },
+ {
+ [fieldMetadataItem.name]: { is: 'NULL' } as FilesFilter,
+ },
+ ],
};
default:
throw new Error(
diff --git a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingFilesFilter.test.ts b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingFilesFilter.test.ts
index ee35b794ead..d23a8f4017e 100644
--- a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingFilesFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingFilesFilter.test.ts
@@ -104,7 +104,7 @@ describe('isMatchingFilesFilter', () => {
).toBe(false);
});
- it('should be case insensitive', () => {
+ it('should be case sensitive', () => {
expect(
isMatchingFilesFilter({
filesFilter: { like: '%FILE.PDF%' },
@@ -117,6 +117,22 @@ describe('isMatchingFilesFilter', () => {
},
],
}),
+ ).toBe(false);
+ });
+
+ it('should be case insensitive with ilike', () => {
+ expect(
+ isMatchingFilesFilter({
+ filesFilter: { ilike: '%FILE.PDF%' },
+ value: [
+ {
+ fileId: '1',
+ label: 'file.pdf',
+ url: 'http://example.com/file.pdf',
+ extension: 'pdf',
+ },
+ ],
+ }),
).toBe(true);
});
diff --git a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRawJsonFilter.test.ts b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRawJsonFilter.test.ts
index 15ac77b6a76..b98769474ad 100644
--- a/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRawJsonFilter.test.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/__tests__/isMatchingRawJsonFilter.test.ts
@@ -43,11 +43,31 @@ describe('isMatchingRawJsonFilter', () => {
).toBe(false);
});
- it('should match across lines', () => {
+ it('should be case sensitive', () => {
expect(
isMatchingRawJsonFilter({
- rawJsonFilter: { like: '%value%' },
- value: { key: 'value' } as any,
+ rawJsonFilter: { like: '%acme%' },
+ value: { name: 'Acme' },
+ }),
+ ).toBe(false);
+ });
+
+ it('should not match a null value', () => {
+ expect(
+ isMatchingRawJsonFilter({
+ rawJsonFilter: { like: '%null%' },
+ value: null,
+ }),
+ ).toBe(false);
+ });
+ });
+
+ describe('ilike', () => {
+ it('should match case insensitively', () => {
+ expect(
+ isMatchingRawJsonFilter({
+ rawJsonFilter: { ilike: '%acme%' },
+ value: { name: 'Acme' },
}),
).toBe(true);
});
diff --git a/packages/twenty-shared/src/utils/filter/utils/isMatchingFilesFilter.ts b/packages/twenty-shared/src/utils/filter/utils/isMatchingFilesFilter.ts
index 2c0739a6c84..a2debc0ab87 100644
--- a/packages/twenty-shared/src/utils/filter/utils/isMatchingFilesFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/isMatchingFilesFilter.ts
@@ -1,4 +1,5 @@
import { type FilesFilter } from '@/types';
+import { isMatchingRawJsonFilter } from '@/utils/filter/utils/isMatchingRawJsonFilter';
export const isMatchingFilesFilter = ({
filesFilter,
@@ -9,16 +10,16 @@ export const isMatchingFilesFilter = ({
}) => {
switch (true) {
case filesFilter.like !== undefined: {
- const escapedPattern = filesFilter.like.replace(
- /[.*+?^${}()|[\]\\]/g,
- '\\$&',
- );
- const regexPattern = escapedPattern.replace(/%/g, '.*');
- const regexCaseInsensitive = new RegExp(`^${regexPattern}$`, 'is');
-
- const stringValue = JSON.stringify(value, null, 1);
-
- return regexCaseInsensitive.test(stringValue);
+ return isMatchingRawJsonFilter({
+ rawJsonFilter: { like: filesFilter.like },
+ value,
+ });
+ }
+ case filesFilter.ilike !== undefined: {
+ return isMatchingRawJsonFilter({
+ rawJsonFilter: { ilike: filesFilter.ilike },
+ value,
+ });
}
case filesFilter.is !== undefined: {
if (filesFilter.is === 'NULL') {
diff --git a/packages/twenty-shared/src/utils/filter/utils/isMatchingRawJsonFilter.ts b/packages/twenty-shared/src/utils/filter/utils/isMatchingRawJsonFilter.ts
index cac715034bb..bcc9bbcdfe9 100644
--- a/packages/twenty-shared/src/utils/filter/utils/isMatchingRawJsonFilter.ts
+++ b/packages/twenty-shared/src/utils/filter/utils/isMatchingRawJsonFilter.ts
@@ -1,29 +1,53 @@
import { type RawJsonFilter } from '@/types';
+import { isDefined } from '@/utils';
import { convertLikePatternToRegexOrThrow } from '@/utils/filter/utils/convertLikePatternToRegexOrThrow';
+const isMatchingLikePattern = ({
+ pattern,
+ value,
+ isCaseInsensitive,
+}: {
+ pattern: string;
+ value: unknown;
+ isCaseInsensitive: boolean;
+}) => {
+ if (!isDefined(value)) {
+ return false;
+ }
+
+ return convertLikePatternToRegexOrThrow({
+ pattern,
+ isCaseInsensitive,
+ }).test(JSON.stringify(value));
+};
+
export const isMatchingRawJsonFilter = ({
rawJsonFilter,
value,
}: {
rawJsonFilter: RawJsonFilter;
- value: string;
+ value: unknown;
}) => {
switch (true) {
case rawJsonFilter.like !== undefined: {
- const regexCaseInsensitive = convertLikePatternToRegexOrThrow({
+ return isMatchingLikePattern({
pattern: rawJsonFilter.like,
+ value,
+ isCaseInsensitive: false,
+ });
+ }
+ case rawJsonFilter.ilike !== undefined: {
+ return isMatchingLikePattern({
+ pattern: rawJsonFilter.ilike,
+ value,
isCaseInsensitive: true,
});
-
- const stringValue = JSON.stringify(value, null, 1);
-
- return regexCaseInsensitive.test(stringValue);
}
case rawJsonFilter.is !== undefined: {
if (rawJsonFilter.is === 'NULL') {
- return value === null;
+ return !isDefined(value);
} else {
- return value !== null;
+ return isDefined(value);
}
}
default: {