Skip to content

Commit

Permalink
fix(filters/continue): cover multiple conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
barinali committed May 9, 2023
1 parent 1f1b3a3 commit a8823c3
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions packages/backend/src/apps/filter/actions/continue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,44 @@ type TGroupItem = {
type TGroup = Record<'and', TGroupItem[]>;

const isEqual = (a: string, b: string) => a === b;
const isNotEqual = (a: string, b: string) => !isEqual(a, b)
const isNotEqual = (a: string, b: string) => !isEqual(a, b);
const isGreaterThan = (a: string, b: string) => Number(a) > Number(b);
const isLessThan = (a: string, b: string) => Number(a) < Number(b);
const isGreaterThanOrEqual = (a: string, b: string) => Number(a) >= Number(b);
const isLessThanOrEqual = (a: string, b: string) => Number(a) <= Number(b);
const contains = (a: string, b: string) => a.includes(b);
const doesNotContain = (a: string, b: string) => !contains(a, b);

const shouldContinue = (orGroups: TGroup[]) => {
let atLeastOneGroupMatches = false;

for (const group of orGroups) {
let groupMatches = true;

for (const condition of group.and) {
const conditionMatches = operate(
condition.operator,
condition.key,
condition.value
);

if (!conditionMatches) {
groupMatches = false;

break;
}
}

if (groupMatches) {
atLeastOneGroupMatches = true;

break;
}
}

return atLeastOneGroupMatches;
}

type TOperatorFunc = (a: string, b: string) => boolean;

type TOperators = {
Expand Down Expand Up @@ -66,7 +96,7 @@ export default defineAction({
return groups;
}, []);

if (matchingGroups.length === 0) {
if (!shouldContinue(orGroups)) {
$.execution.exit();
}

Expand Down

0 comments on commit a8823c3

Please sign in to comment.