Skip to content

Commit 6b57318

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 6b57318

File tree

2 files changed

+100
-2
lines changed

2 files changed

+100
-2
lines changed

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

Lines changed: 78 additions & 1 deletion
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

@@ -590,6 +592,81 @@ describe('PoPageDynamicSearchComponent:', () => {
590592
expect(component['setDisclaimers'](filters)).toEqual(result);
591593
});
592594

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