Skip to content

Commit 6d97f4d

Browse files
wsteixeiraarthur-polidorio
authored andcommitted
fix(page-dynamic-search): 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 ceba0b0 commit 6d97f4d

File tree

2 files changed

+110
-4
lines changed

2 files changed

+110
-4
lines changed

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

Lines changed: 83 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,84 @@ 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 = {
622+
name: 'Name1',
623+
genre: 'male',
624+
$filter: `birthdate ge '2020-01-01' and birthdate le '2020-01-31'`
625+
};
626+
627+
expect(component['addComplexFilter'](filters)).toEqual(result);
628+
});
629+
630+
it(`addComplexFilter: should return filter with attribute 'range' and final date 'undefined'`, () => {
631+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate', range: true }];
632+
633+
const filters = { name: 'Name1', genre: 'male', birthdate: { start: '2020-01-01', end: undefined } };
634+
635+
const result = { name: 'Name1', genre: 'male', birthdate: { start: '2020-01-01', end: undefined } };
636+
637+
expect(component['addComplexFilter'](filters)).toEqual(result);
638+
});
639+
640+
it(`addComplexFilter: should return filter with attribute 'range' and initial date 'undefined'`, () => {
641+
component.filters = [{ property: 'name' }, { property: 'genre' }, { property: 'birthdate', range: true }];
642+
643+
const filters = { name: 'Name1', genre: 'male', birthdate: { start: undefined, end: '2020-01-31' } };
644+
645+
const result = { name: 'Name1', genre: 'male', birthdate: { start: undefined, end: '2020-01-31' } };
646+
647+
expect(component['addComplexFilter'](filters)).toEqual(result);
648+
});
649+
650+
it(`addComplexFilter: should return filter with two attribute 'range'`, () => {
651+
component.filters = [
652+
{ property: 'name' },
653+
{ property: 'genre' },
654+
{ property: 'birthdate', range: true },
655+
{ property: 'deathdate', range: true }
656+
];
657+
658+
const filters = {
659+
name: 'Name1',
660+
genre: 'male',
661+
birthdate: { start: '2020-01-01', end: '2020-01-31' },
662+
deathdate: { start: '2021-01-01', end: '2021-01-31' }
663+
};
664+
665+
const result = {
666+
name: 'Name1',
667+
genre: 'male',
668+
$filter: `birthdate ge '2020-01-01' and birthdate le '2020-01-31' and deathdate ge '2021-01-01' and deathdate le '2021-01-31'`
669+
};
670+
671+
expect(component['addComplexFilter'](filters)).toEqual(result);
672+
});
673+
593674
it('getFilterValueToDisclaimer: should return formated date if field type is PoDynamicFieldType.Date', () => {
594675
const field = { type: PoDynamicFieldType.Date, property: '1', label: 'date' };
595676
const value = '2020-08-12';

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export class PoPageDynamicSearchComponent
189189
}
190190

191191
onAdvancedSearch(filteredItems, isAdvancedSearch?) {
192-
const { filter, optionsService } = filteredItems;
192+
const { optionsService } = filteredItems;
193+
let { filter } = filteredItems;
193194

194195
const visibleFilters =
195196
this.visibleFixedFilters === false
@@ -200,6 +201,8 @@ export class PoPageDynamicSearchComponent
200201

201202
this.setFilters(filter);
202203

204+
filter = this.addComplexFilter(filter);
205+
203206
this.advancedSearch.emit(filter);
204207

205208
if (isAdvancedSearch) {
@@ -295,7 +298,7 @@ export class PoPageDynamicSearchComponent
295298
return this.formatValueToCurrency(field, value);
296299
}
297300

298-
if (field.type === PoDynamicFieldType.Date) {
301+
if (field.type === PoDynamicFieldType.Date || field.type?.toLowerCase() === PoDynamicFieldType.DateTime) {
299302
return field.range ? this.formatDate(value.start) + ' - ' + this.formatDate(value.end) : this.formatDate(value);
300303
}
301304

@@ -436,4 +439,26 @@ export class PoPageDynamicSearchComponent
436439

437440
return this.poPageCustomizationService.getCustomOptions(onLoad, originalOption, pageOptionSchema);
438441
}
442+
443+
private addComplexFilter(filter: object): object {
444+
let complexFilter;
445+
446+
Object.keys(filter).forEach(property => {
447+
if (filter[property].start && filter[property].end) {
448+
if (!complexFilter) {
449+
complexFilter = '';
450+
} else {
451+
complexFilter += ' and ';
452+
}
453+
complexFilter += `${property} ge '${filter[property].start}' and ${property} le '${filter[property].end}'`;
454+
delete filter[property];
455+
}
456+
});
457+
458+
if (complexFilter) {
459+
filter = Object.assign(filter, { $filter: complexFilter });
460+
}
461+
462+
return filter;
463+
}
439464
}

0 commit comments

Comments
 (0)