From 196b845d51562a23467610d934283026afa562be Mon Sep 17 00:00:00 2001 From: Vildan Softic Date: Tue, 9 Apr 2024 21:56:06 +0200 Subject: [PATCH 1/2] fix: wrong operator comparison --- .../__tests__/grid-odata.service.spec.ts | 16 ++++++++++++++++ .../odata/src/services/grid-odata.service.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/odata/src/services/__tests__/grid-odata.service.spec.ts b/packages/odata/src/services/__tests__/grid-odata.service.spec.ts index e787e2dae..a9e446a11 100644 --- a/packages/odata/src/services/__tests__/grid-odata.service.spec.ts +++ b/packages/odata/src/services/__tests__/grid-odata.service.spec.ts @@ -1338,6 +1338,22 @@ describe('GridOdataService', () => { expect(query).toBe(expectation); }); + it('should return a query with a date operator when only 1 searchTerms', () => { + const expectation = `$top=10&$filter=(contains(Company, 'abc') and UpdatedDate ge 2001-01-20T00:00:00Z)`; + const mockColumnCompany = { id: 'company', field: 'company' } as Column; + const mockColumnUpdated = { id: 'updatedDate', field: 'updatedDate', type: FieldType.date } as Column; + const mockColumnFilters = { + company: { columnId: 'company', columnDef: mockColumnCompany, searchTerms: ['abc'], operator: 'Contains', type: FieldType.string }, + updatedDate: { columnId: 'updatedDate', columnDef: mockColumnUpdated, searchTerms: ['2001-01-20'], operator: '>=', type: FieldType.dateIso }, + } as ColumnFilters; + + service.init(serviceOptions, paginationOptions, gridStub); + service.updateFilters(mockColumnFilters, false); + const query = service.buildQuery(); + + expect(query).toBe(expectation); + }); + it('should return a query without any date filtering when searchTerms is an empty array', () => { const expectation = `$top=10&$filter=(contains(Company, 'abc'))`; const mockColumnCompany = { id: 'company', field: 'company' } as Column; diff --git a/packages/odata/src/services/grid-odata.service.ts b/packages/odata/src/services/grid-odata.service.ts index 90637a209..4af5d1377 100644 --- a/packages/odata/src/services/grid-odata.service.ts +++ b/packages/odata/src/services/grid-odata.service.ts @@ -386,7 +386,7 @@ export class GridOdataService implements BackendService { } // Range with 1 searchterm should lead to equals for a date field. - if ((operator === OperatorType.rangeInclusive || OperatorType.rangeExclusive) && Array.isArray(searchTerms) && searchTerms.length === 1 && fieldType === FieldType.date) { + if ((operator === OperatorType.rangeInclusive || operator === OperatorType.rangeExclusive) && Array.isArray(searchTerms) && searchTerms.length === 1 && fieldType === FieldType.date) { operator = OperatorType.equal; } From ab38b9bad1d83394e5cb85b6899a87ac26927f05 Mon Sep 17 00:00:00 2001 From: Vildan Softic Date: Tue, 9 Apr 2024 22:00:54 +0200 Subject: [PATCH 2/2] fix: ignored operator for graphql date fieldtypes filters --- .../services/__tests__/graphql.service.spec.ts | 16 ++++++++++++++++ packages/graphql/src/services/graphql.service.ts | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/graphql/src/services/__tests__/graphql.service.spec.ts b/packages/graphql/src/services/__tests__/graphql.service.spec.ts index 8ae0a3e64..296cd65a0 100644 --- a/packages/graphql/src/services/__tests__/graphql.service.spec.ts +++ b/packages/graphql/src/services/__tests__/graphql.service.spec.ts @@ -1102,6 +1102,22 @@ describe('GraphqlService', () => { expect(removeSpaces(query)).toBe(removeSpaces(expectation)); }); + it('should return a query with a date operator when only 1 searchTerms', () => { + const expectation = `query{users(first:10,offset:0,filterBy:[{field:company,operator:Contains,value:"abc"},{field:updatedDate,operator:GE,value:"2001-01-20"}]){totalCount,nodes{id,company,gender,name}}}`; + const mockColumnCompany = { id: 'company', field: 'company' } as Column; + const mockColumnUpdated = { id: 'updatedDate', field: 'updatedDate', type: FieldType.date } as Column; + const mockColumnFilters = { + company: { columnId: 'company', columnDef: mockColumnCompany, searchTerms: ['abc'], operator: 'Contains', type: FieldType.string }, + updatedDate: { columnId: 'updatedDate', columnDef: mockColumnUpdated, searchTerms: ['2001-01-20'], operator: '>=', type: FieldType.dateIso }, + } as ColumnFilters; + + service.init(serviceOptions, paginationOptions, gridStub); + service.updateFilters(mockColumnFilters, false); + const query = service.buildQuery(); + + expect(removeSpaces(query)).toBe(removeSpaces(expectation)); + }); + it('should return a query without any date filtering when searchTerms is an empty array', () => { const expectation = `query{users(first:10,offset:0,filterBy:[{field:company,operator:Contains,value:"abc"}]){totalCount,nodes{id,company,gender,name}}}`; const mockColumnCompany = { id: 'company', field: 'company' } as Column; diff --git a/packages/graphql/src/services/graphql.service.ts b/packages/graphql/src/services/graphql.service.ts index 585b6b631..c2dd78fde 100644 --- a/packages/graphql/src/services/graphql.service.ts +++ b/packages/graphql/src/services/graphql.service.ts @@ -472,7 +472,7 @@ export class GraphqlService implements BackendService { } // Range with 1 searchterm should lead to equals for a date field. - if ((operator === OperatorType.rangeInclusive || OperatorType.rangeExclusive) && Array.isArray(searchTerms) && searchTerms.length === 1 && fieldType === FieldType.date) { + if ((operator === OperatorType.rangeInclusive || operator === OperatorType.rangeExclusive) && Array.isArray(searchTerms) && searchTerms.length === 1 && fieldType === FieldType.date) { operator = OperatorType.equal; }