Skip to content

Commit

Permalink
parse query numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
EnvBsh committed Dec 12, 2024
1 parent 7c10ef0 commit 0b36680
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,18 @@ export class FiltersAddonBlockComponent implements OnInit {
}

onFilters(event: any) {
if(!this.currentValue) return;

if(this.type === 'datepicker'){
this.currentValue = moment(this.currentValue).format('YYYY-MM-DD');
}

this.loading = true;
const options: any = { filterValue: null };
if (this.queryType === 'user_defined') {
options.filterValue = this.currentType + ':' + this.currentValue;
} else {
options.filterValue = this.currentValue;
if(this.currentValue) {
if (this.queryType === 'user_defined') {
options.filterValue = this.currentType + ':' + this.currentValue;
} else {
options.filterValue = this.currentValue;
}
}
this.policyEngineService
.setBlockData(this.id, this.policyId, options)
Expand Down
55 changes: 51 additions & 4 deletions policy-service/src/policy-engine/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1330,12 +1330,59 @@ export class PolicyUtils {
}

public static getQueryFilter(key: string, value: any) {
const formattedKey = String(key).replace('document.credentialSubject.0', 'firstCredentialSubject');
const queryKey = String(key).replace('document.credentialSubject.0', 'firstCredentialSubject');
let queryOperation: string = '$eq';
let queryValue: any = value;
if (typeof value === 'object') {
const [op, val] = Object.entries(value)[0];
return { [`${op}`]: [`\$${formattedKey}`, val] };
[queryOperation, queryValue] = Object.entries(value)[0];
}

//Check number value
const numberValue = PolicyUtils.parseQueryNumberValue(queryValue);
if (numberValue) {
if (queryOperation === '$ne' || queryOperation === '$nin') {
return {
$and: [
{ [`${queryOperation}`]: [`\$${queryKey}`, numberValue[0]] },
{ [`${queryOperation}`]: [`\$${queryKey}`, numberValue[1]] }
]
}
} else {
return {
$or: [
{ [`${queryOperation}`]: [`\$${queryKey}`, numberValue[0]] },
{ [`${queryOperation}`]: [`\$${queryKey}`, numberValue[1]] }
]
}
}
} else {
return { $eq: [value, `\$${formattedKey}`] };
return { [`${queryOperation}`]: [`\$${queryKey}`, queryValue] };
}
}

public static parseQueryNumberValue(value: any) {
if (Array.isArray(value)) {
if (value.length) {
const stringValue: string[] = [];
const numberValue: number[] = [];
for (const v of value) {
if (isNaN(v)) {
return null;
} else {
stringValue.push(String(value));
numberValue.push(Number(value));
}
}
return [stringValue, numberValue];
} else {
return null;
}
} else {
if (isNaN(value)) {
return null;
} else {
return [String(value), Number(value)];
}
}
}

Expand Down

0 comments on commit 0b36680

Please sign in to comment.