Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kiryakov <[email protected]>
  • Loading branch information
Stepan-Kirjakov committed Dec 3, 2024
1 parent ab1b940 commit d3c4126
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
<mat-option value="not_equal">Not Equal</mat-option>
<mat-option value="in">In</mat-option>
<mat-option value="not_in">Not In</mat-option>

<mat-option value="gte">gte</mat-option>
<mat-option value="gt">gt</mat-option>
<mat-option value="gte">gte</mat-option>
<mat-option value="lt">lt</mat-option>
<mat-option value="lte">lte</mat-option>

<mat-option value="user_defined">user defined</mat-option>
</mat-select>
</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</div>
<div *ngIf="queryType === 'user_defined'" class="content-type">
<mat-select [(value)]="currentType" (change)="onFilters()">
<mat-option value="equal">Equal</mat-option>
<mat-option value="not_equal">Not Equal</mat-option>
<mat-option value="eq">Equal</mat-option>
<mat-option value="ne">Not Equal</mat-option>
<mat-option value="in">In</mat-option>
<mat-option value="not_in">Not In</mat-option>
<mat-option value="gte">gte</mat-option>
<mat-option value="nin">Not In</mat-option>
<mat-option value="gt">gt</mat-option>
<mat-option value="gte">gte</mat-option>
<mat-option value="lt">lt</mat-option>
<mat-option value="lte">lte</mat-option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class FiltersAddonBlockComponent implements OnInit {
target: any;
filters: any;
currentValue: any;
currentType: string = 'equal';
currentType: string = 'eq';
queryType: string = 'equal';

constructor(
Expand Down Expand Up @@ -86,6 +86,39 @@ export class FiltersAddonBlockComponent implements OnInit {
}
}

private parseFilterValue(value: string): [string, string] {
if (typeof value === 'string') {
if (value.startsWith('eq:')) {
return ['eq', value.substring('eq'.length + 1)];
}
if (value.startsWith('ne:')) {
return ['ne', value.substring('ne'.length + 1)];
}
if (value.startsWith('in:')) {
return ['in', value.substring('in'.length + 1)];
}
if (value.startsWith('nin:')) {
return ['nin', value.substring('nin'.length + 1)];
}
if (value.startsWith('gt:')) {
return ['gt', value.substring('gt'.length + 1)];
}
if (value.startsWith('gte:')) {
return ['gte', value.substring('gte'.length + 1)];
}
if (value.startsWith('lt:')) {
return ['lt', value.substring('lt'.length + 1)];
}
if (value.startsWith('lte:')) {
return ['lte', value.substring('lte'.length + 1)];
}
if (value.startsWith('regex:')) {
return ['eq', value.substring('regex'.length + 1)];
}
}
return ['eq', value];
}

setData(data: any) {
this.currentValue = null;
if (data) {
Expand All @@ -94,13 +127,14 @@ export class FiltersAddonBlockComponent implements OnInit {
this.target = data.targetBlock;
this.content = data.uiMetaData.content;
this.filters = data.filters;
this.currentValue = data.filterValue;
this.queryType = data.queryType;

if (this.queryType === 'user_defined') {
this.currentType = 'equal';
const [type, value] = this.parseFilterValue(data.filterValue);
this.currentType = type;
this.currentValue = value;
} else {
this.currentType = this.queryType || 'equal';
this.currentType = this.queryType || 'eq';
this.currentValue = data.filterValue;
}

if (this.type == 'unelected') {
Expand All @@ -118,7 +152,10 @@ export class FiltersAddonBlockComponent implements OnInit {
if (options) {
for (let i = 0; i < options.length; i++) {
const item = options[i];
this.options.push(item);
this.options.push({
name: item.name,
value: String(item.value)
})
}
}
}
Expand All @@ -136,7 +173,7 @@ export class FiltersAddonBlockComponent implements OnInit {
options.filterValue = this.currentValue;
}
this.policyEngineService
.setBlockData(this.id, this.policyId, { filterValue: this.currentValue })
.setBlockData(this.id, this.policyId, options)
.subscribe(() => {
this.loading = false;
}, (e) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,15 +517,15 @@ a.open-vp {

.active-multiple-document {
transform: translate(0, 0);
z-index: 3;
z-index: 3 !important;
border: 2px solid gray;
}

.second-multiple-document {
position: absolute;
left: 0;
bottom: 0;
z-index: 2;
z-index: 2 !important;
transform: translate(-7px, -7px);
border: 2px solid #9d9d9d;
}
Expand All @@ -534,17 +534,17 @@ a.open-vp {
position: absolute;
left: 0;
bottom: 0;
z-index: 1;
z-index: 1 !important;
transform: translate(-14px, -14px);
border: 2px solid #bfbfbf;
}

.single-multiple-document {
z-index: 1;
z-index: 1 !important;
}

.hide-multiple-document {
display: none;
display: none !important;
}

.multiple-documents-count {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class FiltersAddonBlock {
switch (ref.options.type) {
case 'dropdown':
break;
case 'datepicker':
break;
case 'input':
break;
default:
validator.addError('Option "type" must be a "dropdown"');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class FiltersAddonBlock {
switch (ref.options.type) {
case 'dropdown':
break;
case 'datepicker':
break;
case 'input':
break;
default:
validator.addError('Option "type" must be a "dropdown"');
}
Expand Down
14 changes: 11 additions & 3 deletions policy-service/src/policy-engine/blocks/filters-addon-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export class FiltersAddonBlock {
blockType: 'filtersAddon',
type: ref.options.type,
uiMetaData: ref.options.uiMetaData,
canBeEmpty: ref.options.canBeEmpty
canBeEmpty: ref.options.canBeEmpty,
queryType: ref.options.queryType
};

const data: any[] = await ref.getSources(user, null);
Expand Down Expand Up @@ -206,9 +207,16 @@ export class FiltersAddonBlock {
if (!blockState.lastData) {
await this.getData(user);
}
const selectItem = Array.isArray(blockState.lastData) ? blockState.lastData.find((e: any) => e.value === value) : null;
let itemValue:any = value;
if (ref.options.queryType === 'user_defined') {
const [, userValue] = PolicyUtils.parseFilterValue(value);
itemValue = userValue;
}

// tslint:disable-next-line:triple-equals
const selectItem = Array.isArray(blockState.lastData) ? blockState.lastData.find((e: any) => e.value == itemValue) : null;
if (selectItem) {
this.addQuery(filter, selectItem.value);
this.addQuery(filter, value);
} else if (!ref.options.canBeEmpty) {
throw new BlockActionError(`filter value is unknown`, ref.blockType, ref.uuid)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,58 @@ export function DataSourceBlock(options: Partial<PolicyBlockDecoratorOptions>) {
})
}

private parseFilterValue(filterValue: any) {
if (!filterValue) {
return { operator: null, value: null };
} else if (filterValue.$eq) {
return { operator: '$eq', value: filterValue.$eq };
} else if (filterValue.$ne) {
return { operator: '$ne', value: filterValue.$ne };
} else if (filterValue.$in) {
return { operator: '$in', value: filterValue.$in };
} else if (filterValue.$nin) {
return { operator: '$nin', value: filterValue.$nin };
} else if (filterValue.$gt) {
return { operator: '$gt', value: filterValue.$gt };
} else if (filterValue.$gte) {
return { operator: '$gte', value: filterValue.$gte };
} else if (filterValue.$lt) {
return { operator: '$lt', value: filterValue.$lt };
} else if (filterValue.$lte) {
return { operator: '$lte', value: filterValue.$lte };
} else if (filterValue.$regex) {
return { operator: '$regex', value: filterValue.$regex };
} else {
return { operator: '$eq', value: filterValue };
}
}

private checkNumberValue(key: string, filterValue: any): any {
const { operator, value } = this.parseFilterValue(filterValue);
if (operator) {
const filter: any = {};
if (isNaN(value)) {
filter[key] = {};
filter[key][operator] = value;
} else {
const filter1: any = {};
const filter2: any = {};
filter1[key] = {};
filter1[key][operator] = String(value);
filter2[key] = {};
filter2[key][operator] = Number(value);
if (operator === '$ne' || operator === '$nin') {
filter.$and = [filter1, filter2];
} else {
filter.$or = [filter1, filter2];
}
}
return filter;
} else {
return null;
}
}

/**
* Get global sources
* @param user
Expand All @@ -107,8 +159,13 @@ export function DataSourceBlock(options: Partial<PolicyBlockDecoratorOptions>) {
const dynFilters = {};
for (const child of this.children) {
if (child.blockClassName === 'DataSourceAddon') {
for (const [key, value] of Object.entries(await child.getFilters(user))) {
dynFilters[key] = { $eq: value };
for (const [key, filterValue] of Object.entries(await child.getFilters(user))) {
const { operator, value } = this.parseFilterValue(filterValue);
if (operator) {
dynFilters[key] = {};
dynFilters[key][operator] = value;
}

}
}
}
Expand Down Expand Up @@ -170,25 +227,24 @@ export function DataSourceBlock(options: Partial<PolicyBlockDecoratorOptions>) {
* @param countResult
* @protected
*/
protected async getSources(user: PolicyUser, globalFilters: any, paginationData: any, countResult: boolean = false): Promise<any[] | number> {
protected async getSources(
user: PolicyUser,
globalFilters: any,
paginationData: any,
countResult: boolean = false
): Promise<any[] | number> {
const data = [];
let totalCount = 0;
let currentPosition = 0;

const _globalFilters = {} as any;
for (const key in globalFilters) {
if (!isNaN(globalFilters[key].$eq)) {
if (!_globalFilters.$or) {
_globalFilters.$or = [];
for (const key of Object.keys(globalFilters)) {
const value = this.checkNumberValue(key, globalFilters[key]);
if (value) {
if (!_globalFilters.$and) {
_globalFilters.$and = [];
}
const filter1 = {} as any;
filter1[key] = {$eq: String(globalFilters[key].$eq)};
_globalFilters.$or.push(filter1);
const filter2 = {} as any;
filter2[key] = {$eq: Number(globalFilters[key].$eq)};
_globalFilters.$or.push(filter2);
} else {
_globalFilters[key] = globalFilters[key];
_globalFilters.$and.push(value);
}
}

Expand Down
Loading

0 comments on commit d3c4126

Please sign in to comment.