@frontegg/entitlements-javascript-commons 1.0.1 → 1.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/src/plans/index.ts +2 -0
- package/src/plans/plan.evaluator.ts +20 -0
- package/src/plans/tests/plans.evaluator.spec.ts +121 -0
- package/src/plans/types.ts +11 -0
package/docs/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.1.0-alpha.1](https://github.com/frontegg/entitlements-javascript-commons/compare/v-1.0.1...v-1.1.0-alpha.1) (2023-12-31)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **plans:** add plan evaluator ([8bf997a](https://github.com/frontegg/entitlements-javascript-commons/commit/8bf997aad41335e7f07e7ddcd88fcaedfcd318fa))
|
|
7
|
+
|
|
1
8
|
## [1.0.1](https://github.com/frontegg/entitlements-javascript-commons/compare/v-1.0.0...v-1.0.1) (2023-11-06)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Rule, RuleEvaluationResultEnum, createRuleEvaluator } from '../rules';
|
|
2
|
+
import { Plan, PlanEvaluationResult } from './types';
|
|
3
|
+
|
|
4
|
+
export function evaluatePlan(plan: Plan, attributes: Record<string, unknown>): PlanEvaluationResult {
|
|
5
|
+
const treatableRule = findTreatableRule(plan, attributes);
|
|
6
|
+
if (treatableRule) {
|
|
7
|
+
return { treatment: treatableRule.treatment };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return { treatment: plan.defaultTreatment };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function findTreatableRule(plan: Plan, attributes: Record<string, unknown>): Rule | undefined {
|
|
14
|
+
return plan.rules?.find((rule) => {
|
|
15
|
+
const evaluator = createRuleEvaluator({ rule });
|
|
16
|
+
const result = evaluator(attributes);
|
|
17
|
+
|
|
18
|
+
return result === RuleEvaluationResultEnum.Treatable;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { ConditionLogicEnum, TreatmentEnum } from '../../rules';
|
|
2
|
+
import { OperationEnum } from '../../operations/types';
|
|
3
|
+
import { Plan } from '../types';
|
|
4
|
+
import { evaluatePlan } from '../plan.evaluator';
|
|
5
|
+
|
|
6
|
+
type PlanCreator = (data?: Partial<Plan>) => Plan;
|
|
7
|
+
const planFactory: PlanCreator = (data?: Partial<Plan>) => ({
|
|
8
|
+
defaultTreatment: TreatmentEnum.False,
|
|
9
|
+
...data,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('evaluatePlan', () => {
|
|
13
|
+
describe('default treatment', () => {
|
|
14
|
+
it('should return false when no rule is valid and defaultTreatment is false', () => {
|
|
15
|
+
const plan: Plan = planFactory({
|
|
16
|
+
rules: [
|
|
17
|
+
{
|
|
18
|
+
treatment: TreatmentEnum.True,
|
|
19
|
+
conditions: [
|
|
20
|
+
{
|
|
21
|
+
attribute: 'test',
|
|
22
|
+
op: 'not supported' as any,
|
|
23
|
+
value: { list: ['test'] },
|
|
24
|
+
negate: false,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
conditionLogic: ConditionLogicEnum.And,
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const result = evaluatePlan(plan, {});
|
|
33
|
+
|
|
34
|
+
expect(result).toEqual({ treatment: TreatmentEnum.False });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return true when no rule is valid and defaultTreatment is true', () => {
|
|
38
|
+
const plan: Plan = planFactory({
|
|
39
|
+
defaultTreatment: TreatmentEnum.True,
|
|
40
|
+
rules: [
|
|
41
|
+
{
|
|
42
|
+
treatment: TreatmentEnum.True,
|
|
43
|
+
conditions: [
|
|
44
|
+
{
|
|
45
|
+
attribute: 'test',
|
|
46
|
+
op: 'not supported' as any,
|
|
47
|
+
value: { list: ['test'] },
|
|
48
|
+
negate: false,
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
conditionLogic: ConditionLogicEnum.And,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const result = evaluatePlan(plan, {});
|
|
57
|
+
|
|
58
|
+
expect(result).toEqual({ treatment: TreatmentEnum.True });
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('complete evaluation', () => {
|
|
63
|
+
it('should return false according to first treatable rule', () => {
|
|
64
|
+
const plan: Plan = planFactory({
|
|
65
|
+
rules: [
|
|
66
|
+
{
|
|
67
|
+
treatment: TreatmentEnum.False,
|
|
68
|
+
conditions: [
|
|
69
|
+
{
|
|
70
|
+
attribute: 'attribute',
|
|
71
|
+
op: OperationEnum.InList,
|
|
72
|
+
value: { list: ['test'] },
|
|
73
|
+
negate: false,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
conditionLogic: ConditionLogicEnum.And,
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const result = evaluatePlan(plan, { attribute: 'test' });
|
|
82
|
+
|
|
83
|
+
expect(result).toEqual({ treatment: TreatmentEnum.False });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should return true according to first treatable rule', () => {
|
|
87
|
+
const plan: Plan = planFactory({
|
|
88
|
+
rules: [
|
|
89
|
+
{
|
|
90
|
+
treatment: TreatmentEnum.False,
|
|
91
|
+
conditions: [
|
|
92
|
+
{
|
|
93
|
+
attribute: 'test',
|
|
94
|
+
op: 'not supported' as any,
|
|
95
|
+
value: { list: ['test'] },
|
|
96
|
+
negate: false,
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
conditionLogic: ConditionLogicEnum.And,
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
treatment: TreatmentEnum.True,
|
|
103
|
+
conditions: [
|
|
104
|
+
{
|
|
105
|
+
attribute: 'attribute',
|
|
106
|
+
op: OperationEnum.InList,
|
|
107
|
+
value: { list: ['test'] },
|
|
108
|
+
negate: false,
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
conditionLogic: ConditionLogicEnum.And,
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const result = evaluatePlan(plan, { attribute: 'test' });
|
|
117
|
+
|
|
118
|
+
expect(result).toEqual({ treatment: TreatmentEnum.True });
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|