Skip to content

Commit

Permalink
fix: wrong operator comparison (#1461)
Browse files Browse the repository at this point in the history
* fix: wrong operator comparison

* fix: ignored operator for graphql date fieldtypes filters
  • Loading branch information
zewa666 committed Apr 9, 2024
1 parent 4c4be44 commit abe772b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packages/graphql/src/services/__tests__/graphql.service.spec.ts
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/src/services/graphql.service.ts
Expand Up @@ -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;
}

Expand Down
16 changes: 16 additions & 0 deletions packages/odata/src/services/__tests__/grid-odata.service.spec.ts
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/odata/src/services/grid-odata.service.ts
Expand Up @@ -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;
}

Expand Down

0 comments on commit abe772b

Please sign in to comment.