Skip to content

Commit

Permalink
fixed $nin case
Browse files Browse the repository at this point in the history
  • Loading branch information
EnvBsh committed Dec 12, 2024
1 parent 0b36680 commit 3d234c3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 46 deletions.
63 changes: 19 additions & 44 deletions policy-service/src/policy-engine/blocks/documents-source-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PolicyComponentsUtils } from '../policy-components-utils.js';
import { IPolicyAddonBlock, IPolicyDocument } from '../policy-engine.interface.js';
import { ChildrenType, ControlType } from '../interfaces/block-about.js';
import { PolicyUser } from '../policy-user.js';
import { PolicyUtils } from '../helpers/utils.js';
import { PolicyUtils, QueryType } from '../helpers/utils.js';
import ObjGet from 'lodash.get';
import ObjSet from 'lodash.set';

Expand Down Expand Up @@ -237,40 +237,13 @@ export class DocumentsSourceAddon {
}

for (const filter of ref.options.filters) {
switch (filter.type) {
case 'equal':
filters.push({ $eq: [filter.value, `\$${filter.field}`] });
break;

case 'not_equal':
filters.push({ $ne: [filter.value, `\$${filter.field}`] });
break;

case 'in':
filters.push({ $in: [`\$${filter.field}`, filter.value.split(',')] });
break;

case 'not_in':
filters.push({ $nin: [`\$${filter.field}`, filter.value.split(',')] });
break;

case 'gt':
filters.push({ $gt: [`\$${filter.field}`, filter.value] });
break;

case 'gte':
filters.push({ $gte: [`\$${filter.field}`, filter.value] });
break;

case 'lt':
filters.push({ $lt: [`\$${filter.field}`, filter.value] });
break;

case 'lte':
filters.push({ $lte: [`\$${filter.field}`, filter.value] });
break;
default:
throw new BlockActionError(`Unknown filter type: ${filter.type}`, ref.blockType, ref.uuid);
const queryType = filter.type as QueryType;
const queryValue = PolicyUtils.getQueryValue(queryType, filter.value);
const queryExpression = PolicyUtils.getQueryExpression(queryType, queryValue);
if (queryExpression) {
filters.push(PolicyUtils.getQueryFilter(filter.field, queryExpression));
} else {
throw new BlockActionError(`Unknown filter type: ${filter.type}`, ref.blockType, ref.uuid);
}
}

Expand All @@ -291,15 +264,17 @@ export class DocumentsSourceAddon {
__sourceTag__: {
$cond: {
if: {
$and: [
...filters,
{
$or: [
{ $eq: [null, '$__sourceTag__'] },
{ $not: '$__sourceTag__' }
]
}
]
$expr: {
$and: [
...filters,
{
$or: [
{ $eq: [null, '$__sourceTag__'] },
{ $not: '$__sourceTag__' }
]
}
]
}
},
then: ref.tag,
else: '$__sourceTag__'
Expand Down
14 changes: 12 additions & 2 deletions policy-service/src/policy-engine/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,14 @@ export class PolicyUtils {
//Check number value
const numberValue = PolicyUtils.parseQueryNumberValue(queryValue);
if (numberValue) {
if (queryOperation === '$ne' || queryOperation === '$nin') {
if(queryOperation === '$nin') {
return {
$and: [
{ $not: { $in: [`\$${queryKey}`, numberValue[0]] }},
{ $not: { $in: [`\$${queryKey}`, numberValue[1]] }}
]
}
} else if (queryOperation === '$ne') {
return {
$and: [
{ [`${queryOperation}`]: [`\$${queryKey}`, numberValue[0]] },
Expand All @@ -1356,7 +1363,10 @@ export class PolicyUtils {
}
}
} else {
return { [`${queryOperation}`]: [`\$${queryKey}`, queryValue] };
if(queryOperation === '$nin') {
return { $not: { $in: [`\$${queryKey}`, queryValue] }}
} else
return { [`${queryOperation}`]: [`\$${queryKey}`, queryValue] };
}
}

Expand Down

0 comments on commit 3d234c3

Please sign in to comment.