Skip to content

Commit 749a46f

Browse files
committed
fix(page-dynamic-table): ajusta o GET request com atributo range
Foi incluído o `$filter` para filtros complexos no GET Request quando o atributo `range` está com `true` Fixes #1211
1 parent 74e7802 commit 749a46f

File tree

2 files changed

+102
-3
lines changed

2 files changed

+102
-3
lines changed

projects/templates/src/lib/components/po-page-dynamic-search/po-page-dynamic-search.component.spec.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ describe('PoPageDynamicSearchComponent:', () => {
214214
expect(component.poAdvancedFilter.open).toHaveBeenCalled();
215215
});
216216

217-
it(`onAdvancedSearch: should call 'setDisclaimers', 'setFilters' and 'advancedSearch.emit'`, () => {
217+
it(`onAdvancedSearch: should call 'setDisclaimers', 'setFilters', 'addComplexFilter' and 'advancedSearch.emit'`, () => {
218218
const filter = { property: 'value1' };
219219
const optionsService = undefined;
220220

@@ -224,13 +224,15 @@ describe('PoPageDynamicSearchComponent:', () => {
224224
: component.filters;
225225

226226
spyOn(component, <any>'setDisclaimers');
227+
spyOn(component, <any>'addComplexFilter').and.returnValue(filter);
227228
spyOn(component.advancedSearch, 'emit');
228229
spyOn(component, <any>'setFilters');
229230

230231
component.onAdvancedSearch({ filter, optionsService });
231232

232233
expect(component['setDisclaimers']).toHaveBeenCalledWith(filter, optionsService, visibleFilters);
233234
expect(component['setFilters']).toHaveBeenCalledBefore(component.advancedSearch.emit);
235+
expect(component['addComplexFilter']).toHaveBeenCalledWith(filter);
234236
expect(component.advancedSearch.emit).toHaveBeenCalledWith(filter);
235237
});
236238

@@ -285,7 +287,8 @@ describe('PoPageDynamicSearchComponent:', () => {
285287
},
286288
poPageList: {
287289
clearInputSearch: () => {}
288-
}
290+
},
291+
addComplexFilter: () => ({})
289292
};
290293
const filteredItems = { filter: 'fakeFilter', optionsService: 'fakeOptionsService' };
291294
const isAdvancedSearch = true;
@@ -590,6 +593,81 @@ describe('PoPageDynamicSearchComponent:', () => {
590593
expect(component['setDisclaimers'](filters)).toEqual(result);
591594
});
592595

596+
it(`addComplexFilter: should return {}`, () => {
597+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate' }];
598+
599+
const filters = {};
600+
601+
const result = {};
602+
603+
expect(component['addComplexFilter'](filters)).toEqual(result);
604+
});
605+
606+
it(`addComplexFilter: should return filter without attribute 'range'`, () => {
607+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate' }];
608+
609+
const filters = { name: 'Name1', genre: 'male', birthdate: '2020-01-15' };
610+
611+
const result = { name: 'Name1', genre: 'male', birthdate: '2020-01-15' };
612+
613+
expect(component['addComplexFilter'](filters)).toEqual(result);
614+
});
615+
616+
it(`addComplexFilter: should return filter with attribute 'range'`, () => {
617+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate', range: true }];
618+
619+
const filters = { name: 'Name1', genre: 'male', birthdate: { start: '2020-01-01', end: '2020-01-31' } };
620+
621+
const result = { name: 'Name1', genre: 'male', $filter: 'birthdate ge 2020-01-01 and birthdate le 2020-01-31' };
622+
623+
expect(component['addComplexFilter'](filters)).toEqual(result);
624+
});
625+
626+
it(`addComplexFilter: should return filter with attribute 'range' and final date 'undefined'`, () => {
627+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate', range: true }];
628+
629+
const filters = { name: 'Name1', genre: 'male', birthdate: { start: '2020-01-01', end: undefined } };
630+
631+
const result = { name: 'Name1', genre: 'male', birthdate: { start: '2020-01-01', end: undefined } };
632+
633+
expect(component['addComplexFilter'](filters)).toEqual(result);
634+
});
635+
636+
it(`addComplexFilter: should return filter with attribute 'range' and initial date 'undefined'`, () => {
637+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate', range: true }];
638+
639+
const filters = { name: 'Name1', genre: 'male', birthdate: { start: undefined, end: '2020-01-31' } };
640+
641+
const result = { name: 'Name1', genre: 'male', birthdate: { start: undefined, end: '2020-01-31' } };
642+
643+
expect(component['addComplexFilter'](filters)).toEqual(result);
644+
});
645+
646+
it(`addComplexFilter: should return filter with two attribute 'range'`, () => {
647+
component.filters = [
648+
{ property: 'name' },
649+
{ property: 'genre' },
650+
{ property: 'birthdate', range: true },
651+
{ property: 'deathdate', range: true }
652+
];
653+
654+
const filters = {
655+
name: 'Name1',
656+
genre: 'male',
657+
birthdate: { start: '2020-01-01', end: '2020-01-31' },
658+
deathdate: { start: '2021-01-01', end: '2021-01-31' }
659+
};
660+
661+
const result = {
662+
name: 'Name1',
663+
genre: 'male',
664+
$filter:
665+
'birthdate ge 2020-01-01 and birthdate le 2020-01-31 and deathdate ge 2021-01-01 and deathdate le 2021-01-31'
666+
};
667+
668+
expect(component['addComplexFilter'](filters)).toEqual(result);
669+
});
670+
593671
it('getFilterValueToDisclaimer: should return formated date if field type is PoDynamicFieldType.Date', () => {
594672
const field = { type: PoDynamicFieldType.Date, property: '1', label: 'date' };
595673
const value = '2020-08-12';

projects/templates/src/lib/components/po-page-dynamic-search/po-page-dynamic-search.component.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ export class PoPageDynamicSearchComponent
186186
}
187187

188188
onAdvancedSearch(filteredItems, isAdvancedSearch?) {
189-
const { filter, optionsService } = filteredItems;
189+
const { optionsService } = filteredItems;
190+
let { filter } = filteredItems;
190191

191192
const visibleFilters =
192193
this.visibleFixedFilters === false
@@ -197,6 +198,8 @@ export class PoPageDynamicSearchComponent
197198

198199
this.setFilters(filter);
199200

201+
filter = this.addComplexFilter(filter);
202+
200203
this.advancedSearch.emit(filter);
201204

202205
if (isAdvancedSearch) {
@@ -433,4 +436,22 @@ export class PoPageDynamicSearchComponent
433436

434437
return this.poPageCustomizationService.getCustomOptions(onLoad, originalOption, pageOptionSchema);
435438
}
439+
440+
private addComplexFilter(filter: object): object {
441+
let complexFilter;
442+
443+
Object.keys(filter).forEach(property => {
444+
if (filter[property].start && filter[property].end) {
445+
complexFilter = !complexFilter ? '' : (complexFilter += ' and ');
446+
complexFilter += `${property} ge ${filter[property].start} and ${property} le ${filter[property].end}`;
447+
delete filter[property];
448+
}
449+
});
450+
451+
if (complexFilter) {
452+
filter = Object.assign(filter, { $filter: complexFilter });
453+
}
454+
455+
return filter;
456+
}
436457
}

0 commit comments

Comments
 (0)