@frontegg/entitlements-javascript-commons 1.0.0-alpha.7 → 1.0.0-alpha.9
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/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/operations/numeric/index.d.ts +1 -1
- package/dist/operations/numeric/index.js +3 -3
- package/dist/operations/numeric/index.js.map +1 -1
- package/dist/user-entitlements/is-entitled.evaluator.d.ts +11 -3
- package/dist/user-entitlements/is-entitled.evaluator.js +27 -5
- package/dist/user-entitlements/is-entitled.evaluator.js.map +1 -1
- package/dist/user-entitlements/types.d.ts +11 -0
- package/dist/user-entitlements/types.js.map +1 -1
- package/docs/CHANGELOG.md +15 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/operations/numeric/index.ts +4 -4
- package/src/user-entitlements/is-entitled.evaluator.ts +34 -6
- package/src/user-entitlements/tests/is-entitled.evaluator.spec.ts +25 -1
- package/src/user-entitlements/types.ts +10 -0
package/dist/index.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export { FeatureFlagEvaluationResult, FeatureFlag, evaluateFeatureFlag } from '.
|
|
|
2
2
|
export { TreatmentEnum, Rule } from './rules';
|
|
3
3
|
export { Condition } from './conditions';
|
|
4
4
|
export { OperationEnum, ConditionValue } from './operations/types';
|
|
5
|
-
export { evaluateIsEntitledToFeature, evaluateIsEntitledToPermissions, CustomAttributes, NotEntitledJustification, UserEntitlementsContext, EntitlementResult, } from './user-entitlements';
|
|
5
|
+
export { evaluateIsEntitledToFeature, evaluateIsEntitledToPermissions, CustomAttributes, FronteggAttributes, NotEntitledJustification, UserEntitlementsContext, EntitlementResult, } from './user-entitlements';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAAgG;AAA7C,oHAAA,mBAAmB,OAAA;AACtE,iCAA8C;AAArC,sGAAA,aAAa,OAAA;AAEtB,4CAAmE;AAA1D,sGAAA,aAAa,OAAA;AACtB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iDAAgG;AAA7C,oHAAA,mBAAmB,OAAA;AACtE,iCAA8C;AAArC,sGAAA,aAAa,OAAA;AAEtB,4CAAmE;AAA1D,sGAAA,aAAa,OAAA;AACtB,yDAQ6B;AAP3B,gIAAA,2BAA2B,OAAA;AAC3B,oIAAA,+BAA+B,OAAA;AAG/B,6HAAA,wBAAwB,OAAA"}
|
|
@@ -17,9 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
exports.NumericOperationsMapper = void 0;
|
|
18
18
|
const types_1 = require("../types");
|
|
19
19
|
const operations_1 = require("./operations");
|
|
20
|
-
__exportStar(require("./operations"), exports);
|
|
21
|
-
__exportStar(require("./sanitizers"), exports);
|
|
22
|
-
__exportStar(require("./types"), exports);
|
|
23
20
|
exports.NumericOperationsMapper = {
|
|
24
21
|
[types_1.OperationEnum.Equal]: (value) => (0, operations_1.useEqualsOperation)(value),
|
|
25
22
|
[types_1.OperationEnum.GreaterThan]: (value) => (0, operations_1.useGreaterThanOperation)(value),
|
|
@@ -28,4 +25,7 @@ exports.NumericOperationsMapper = {
|
|
|
28
25
|
[types_1.OperationEnum.LesserThanEqual]: (value) => (0, operations_1.useLesserThanEqualOperation)(value),
|
|
29
26
|
[types_1.OperationEnum.BetweenNumeric]: (value) => (0, operations_1.useBetweenNumericOperation)(value),
|
|
30
27
|
};
|
|
28
|
+
__exportStar(require("./operations"), exports);
|
|
29
|
+
__exportStar(require("./sanitizers"), exports);
|
|
30
|
+
__exportStar(require("./types"), exports);
|
|
31
31
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/operations/numeric/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oCAA2D;AAC3D,6CAOsB;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/operations/numeric/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,oCAA2D;AAC3D,6CAOsB;AAGT,QAAA,uBAAuB,GAAqB;IACvD,CAAC,qBAAa,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,+BAAkB,EAAC,KAAsC,CAAC;IAC5F,CAAC,qBAAa,CAAC,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,oCAAuB,EAAC,KAAsC,CAAC;IACvG,CAAC,qBAAa,CAAC,gBAAgB,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,yCAA4B,EAAC,KAAsC,CAAC;IACjH,CAAC,qBAAa,CAAC,UAAU,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,mCAAsB,EAAC,KAAsC,CAAC;IACrG,CAAC,qBAAa,CAAC,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,wCAA2B,EAAC,KAAsC,CAAC;IAC/G,CAAC,qBAAa,CAAC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,uCAA0B,EAAC,KAAuC,CAAC;CAC/G,CAAC;AAEF,+CAA6B;AAC7B,+CAA6B;AAC7B,0CAAwB"}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare function evaluateIsEntitledToFeature(featureKey: string, userEntitlementsContext: UserEntitlementsContext, attributes?:
|
|
3
|
-
export declare function evaluateIsEntitledToPermissions(permissionKey: string, userEntitlementsContext: UserEntitlementsContext, attributes?:
|
|
1
|
+
import { EntitlementResult, UserEntitlementsContext, Attributes } from './types';
|
|
2
|
+
export declare function evaluateIsEntitledToFeature(featureKey: string, userEntitlementsContext: UserEntitlementsContext, attributes?: Attributes): EntitlementResult;
|
|
3
|
+
export declare function evaluateIsEntitledToPermissions(permissionKey: string, userEntitlementsContext: UserEntitlementsContext, attributes?: Attributes): EntitlementResult;
|
|
4
|
+
/**
|
|
5
|
+
* Merges the `custom` and `frontegg` Records into a single Record,
|
|
6
|
+
* Alters the `frontegg` Record keys with a prefix
|
|
7
|
+
* Example:
|
|
8
|
+
* Input: { 'custom': { 'customAttribute': 'someValue' }, 'frontegg': { 'email': 'user@email.com' } }
|
|
9
|
+
* Output: { 'customAttribute': 'someValue', 'imported.email': 'user@email.com' }
|
|
10
|
+
*/
|
|
11
|
+
export declare function prepareAttributes(attributes?: Attributes): Record<string, unknown>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.evaluateIsEntitledToPermissions = exports.evaluateIsEntitledToFeature = void 0;
|
|
3
|
+
exports.prepareAttributes = exports.evaluateIsEntitledToPermissions = exports.evaluateIsEntitledToFeature = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const feature_flags_1 = require("../feature-flags");
|
|
6
6
|
const rules_1 = require("../rules");
|
|
7
|
-
function evaluateIsEntitledToFeature(featureKey, userEntitlementsContext, attributes
|
|
7
|
+
function evaluateIsEntitledToFeature(featureKey, userEntitlementsContext, attributes) {
|
|
8
8
|
const feature = userEntitlementsContext.features[featureKey];
|
|
9
9
|
let hasExpired = false;
|
|
10
10
|
if (feature && feature.expireTime !== null) {
|
|
@@ -14,15 +14,18 @@ function evaluateIsEntitledToFeature(featureKey, userEntitlementsContext, attrib
|
|
|
14
14
|
}
|
|
15
15
|
}
|
|
16
16
|
if (feature && feature.featureFlag) {
|
|
17
|
-
const { treatment } = (0, feature_flags_1.evaluateFeatureFlag)(feature.featureFlag, attributes);
|
|
17
|
+
const { treatment } = (0, feature_flags_1.evaluateFeatureFlag)(feature.featureFlag, prepareAttributes(attributes));
|
|
18
18
|
if (treatment === rules_1.TreatmentEnum.True) {
|
|
19
19
|
return { isEntitled: true };
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
|
-
return {
|
|
22
|
+
return {
|
|
23
|
+
isEntitled: false,
|
|
24
|
+
justification: hasExpired ? types_1.NotEntitledJustification.BUNDLE_EXPIRED : types_1.NotEntitledJustification.MISSING_FEATURE,
|
|
25
|
+
};
|
|
23
26
|
}
|
|
24
27
|
exports.evaluateIsEntitledToFeature = evaluateIsEntitledToFeature;
|
|
25
|
-
function evaluateIsEntitledToPermissions(permissionKey, userEntitlementsContext, attributes
|
|
28
|
+
function evaluateIsEntitledToPermissions(permissionKey, userEntitlementsContext, attributes) {
|
|
26
29
|
const permission = userEntitlementsContext.permissions[permissionKey];
|
|
27
30
|
if (!permission) {
|
|
28
31
|
return { isEntitled: false, justification: types_1.NotEntitledJustification.MISSING_PERMISSION };
|
|
@@ -50,4 +53,23 @@ exports.evaluateIsEntitledToPermissions = evaluateIsEntitledToPermissions;
|
|
|
50
53
|
function getLinkedFeatures(permissionKey, userEntitlementsContext) {
|
|
51
54
|
return Object.keys(userEntitlementsContext.features).filter((featureKey) => userEntitlementsContext.features[featureKey].linkedPermissions.includes(permissionKey));
|
|
52
55
|
}
|
|
56
|
+
/**
|
|
57
|
+
* Merges the `custom` and `frontegg` Records into a single Record,
|
|
58
|
+
* Alters the `frontegg` Record keys with a prefix
|
|
59
|
+
* Example:
|
|
60
|
+
* Input: { 'custom': { 'customAttribute': 'someValue' }, 'frontegg': { 'email': 'user@email.com' } }
|
|
61
|
+
* Output: { 'customAttribute': 'someValue', 'imported.email': 'user@email.com' }
|
|
62
|
+
*/
|
|
63
|
+
function prepareAttributes(attributes = {}) {
|
|
64
|
+
const { custom = {}, frontegg = {} } = attributes;
|
|
65
|
+
const importedAttributesPrefix = 'imported.'; // Not Final
|
|
66
|
+
return {
|
|
67
|
+
...custom,
|
|
68
|
+
...Object.keys(frontegg).reduce((modifiedImportedAttributes, key) => {
|
|
69
|
+
modifiedImportedAttributes[`${importedAttributesPrefix}${key}`] = frontegg[key];
|
|
70
|
+
return modifiedImportedAttributes;
|
|
71
|
+
}, {}),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
exports.prepareAttributes = prepareAttributes;
|
|
53
75
|
//# sourceMappingURL=is-entitled.evaluator.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is-entitled.evaluator.js","sourceRoot":"","sources":["../../src/user-entitlements/is-entitled.evaluator.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"is-entitled.evaluator.js","sourceRoot":"","sources":["../../src/user-entitlements/is-entitled.evaluator.ts"],"names":[],"mappings":"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"}
|
|
@@ -17,4 +17,15 @@ export declare enum NotEntitledJustification {
|
|
|
17
17
|
BUNDLE_EXPIRED = "BUNDLE_EXPIRED"
|
|
18
18
|
}
|
|
19
19
|
export type CustomAttributes = Record<string, string | number | boolean | Date>;
|
|
20
|
+
export type FronteggAttributes = {
|
|
21
|
+
tenantId?: string;
|
|
22
|
+
userId?: string;
|
|
23
|
+
email?: string;
|
|
24
|
+
email_verified?: boolean;
|
|
25
|
+
[unmappedAttribute: string]: unknown;
|
|
26
|
+
};
|
|
27
|
+
export type Attributes = {
|
|
28
|
+
custom?: CustomAttributes;
|
|
29
|
+
frontegg?: FronteggAttributes;
|
|
30
|
+
};
|
|
20
31
|
export declare const NO_EXPIRATION_TIME = -1;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/user-entitlements/types.ts"],"names":[],"mappings":";;;AAkBA,IAAY,wBAIX;AAJD,WAAY,wBAAwB;IAClC,+DAAmC,CAAA;IACnC,qEAAyC,CAAA;IACzC,6DAAiC,CAAA;AACnC,CAAC,EAJW,wBAAwB,GAAxB,gCAAwB,KAAxB,gCAAwB,QAInC;
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/user-entitlements/types.ts"],"names":[],"mappings":";;;AAkBA,IAAY,wBAIX;AAJD,WAAY,wBAAwB;IAClC,+DAAmC,CAAA;IACnC,qEAAyC,CAAA;IACzC,6DAAiC,CAAA;AACnC,CAAC,EAJW,wBAAwB,GAAxB,gCAAwB,KAAxB,gCAAwB,QAInC;AAcY,QAAA,kBAAkB,GAAG,CAAC,CAAC,CAAC"}
|
package/docs/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
# [1.0.0-alpha.9](https://github.com/frontegg/entitlements-javascript-commons/compare/v-1.0.0-alpha.8...v-1.0.0-alpha.9) (2023-10-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* **user-entitlements:** add frontegg attributes ([878f4aa](https://github.com/frontegg/entitlements-javascript-commons/commit/878f4aa5f284fc2dd27ffae63ed9c0c74b2c8adb))
|
|
7
|
+
* **user-entitlements:** add frontegg attributes ([b1c3e30](https://github.com/frontegg/entitlements-javascript-commons/commit/b1c3e30e420e8972167087c5745ef49ac0c6b859))
|
|
8
|
+
|
|
9
|
+
# [1.0.0-alpha.8](https://github.com/frontegg/entitlements-javascript-commons/compare/v-1.0.0-alpha.7...v-1.0.0-alpha.8) (2023-10-11)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* fix export order ([#16](https://github.com/frontegg/entitlements-javascript-commons/issues/16)) ([d7fafa4](https://github.com/frontegg/entitlements-javascript-commons/commit/d7fafa427e5f99fffe541f9fa78fd9b7507045c3))
|
|
15
|
+
|
|
1
16
|
# [1.0.0-alpha.7](https://github.com/frontegg/entitlements-javascript-commons/compare/v-1.0.0-alpha.6...v-1.0.0-alpha.7) (2023-10-11)
|
|
2
17
|
|
|
3
18
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -9,10 +9,6 @@ import {
|
|
|
9
9
|
} from './operations';
|
|
10
10
|
import { BetweenNumericOperationPayload, SingleNumericOperationPayload } from './types';
|
|
11
11
|
|
|
12
|
-
export * from './operations';
|
|
13
|
-
export * from './sanitizers';
|
|
14
|
-
export * from './types';
|
|
15
|
-
|
|
16
12
|
export const NumericOperationsMapper: OperationsMapper = {
|
|
17
13
|
[OperationEnum.Equal]: (value) => useEqualsOperation(value as SingleNumericOperationPayload),
|
|
18
14
|
[OperationEnum.GreaterThan]: (value) => useGreaterThanOperation(value as SingleNumericOperationPayload),
|
|
@@ -21,3 +17,7 @@ export const NumericOperationsMapper: OperationsMapper = {
|
|
|
21
17
|
[OperationEnum.LesserThanEqual]: (value) => useLesserThanEqualOperation(value as SingleNumericOperationPayload),
|
|
22
18
|
[OperationEnum.BetweenNumeric]: (value) => useBetweenNumericOperation(value as BetweenNumericOperationPayload),
|
|
23
19
|
};
|
|
20
|
+
|
|
21
|
+
export * from './operations';
|
|
22
|
+
export * from './sanitizers';
|
|
23
|
+
export * from './types';
|
|
@@ -1,13 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
EntitlementResult,
|
|
3
|
+
NotEntitledJustification,
|
|
4
|
+
NO_EXPIRATION_TIME,
|
|
5
|
+
UserEntitlementsContext,
|
|
6
|
+
Attributes,
|
|
7
|
+
} from './types';
|
|
2
8
|
import { evaluateFeatureFlag } from '../feature-flags';
|
|
3
9
|
import { TreatmentEnum } from '../rules';
|
|
4
10
|
export function evaluateIsEntitledToFeature(
|
|
5
11
|
featureKey: string,
|
|
6
12
|
userEntitlementsContext: UserEntitlementsContext,
|
|
7
|
-
attributes
|
|
13
|
+
attributes?: Attributes,
|
|
8
14
|
): EntitlementResult {
|
|
9
15
|
const feature = userEntitlementsContext.features[featureKey];
|
|
10
|
-
|
|
11
16
|
let hasExpired = false;
|
|
12
17
|
if (feature && feature.expireTime !== null) {
|
|
13
18
|
hasExpired = feature.expireTime !== NO_EXPIRATION_TIME && feature.expireTime < Date.now();
|
|
@@ -18,19 +23,22 @@ export function evaluateIsEntitledToFeature(
|
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
if (feature && feature.featureFlag) {
|
|
21
|
-
const { treatment } = evaluateFeatureFlag(feature.featureFlag, attributes);
|
|
26
|
+
const { treatment } = evaluateFeatureFlag(feature.featureFlag, prepareAttributes(attributes));
|
|
22
27
|
if (treatment === TreatmentEnum.True) {
|
|
23
28
|
return { isEntitled: true };
|
|
24
29
|
}
|
|
25
30
|
}
|
|
26
31
|
|
|
27
|
-
return {
|
|
32
|
+
return {
|
|
33
|
+
isEntitled: false,
|
|
34
|
+
justification: hasExpired ? NotEntitledJustification.BUNDLE_EXPIRED : NotEntitledJustification.MISSING_FEATURE,
|
|
35
|
+
};
|
|
28
36
|
}
|
|
29
37
|
|
|
30
38
|
export function evaluateIsEntitledToPermissions(
|
|
31
39
|
permissionKey: string,
|
|
32
40
|
userEntitlementsContext: UserEntitlementsContext,
|
|
33
|
-
attributes
|
|
41
|
+
attributes?: Attributes,
|
|
34
42
|
): EntitlementResult {
|
|
35
43
|
const permission = userEntitlementsContext.permissions[permissionKey];
|
|
36
44
|
|
|
@@ -69,3 +77,23 @@ function getLinkedFeatures(permissionKey: string, userEntitlementsContext: UserE
|
|
|
69
77
|
userEntitlementsContext.features[featureKey].linkedPermissions.includes(permissionKey),
|
|
70
78
|
);
|
|
71
79
|
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Merges the `custom` and `frontegg` Records into a single Record,
|
|
83
|
+
* Alters the `frontegg` Record keys with a prefix
|
|
84
|
+
* Example:
|
|
85
|
+
* Input: { 'custom': { 'customAttribute': 'someValue' }, 'frontegg': { 'email': 'user@email.com' } }
|
|
86
|
+
* Output: { 'customAttribute': 'someValue', 'imported.email': 'user@email.com' }
|
|
87
|
+
*/
|
|
88
|
+
export function prepareAttributes(attributes: Attributes = {}): Record<string, unknown> {
|
|
89
|
+
const { custom = {}, frontegg = {} } = attributes;
|
|
90
|
+
const importedAttributesPrefix = 'imported.'; // Not Final
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
...custom,
|
|
94
|
+
...Object.keys(frontegg).reduce((modifiedImportedAttributes, key) => {
|
|
95
|
+
modifiedImportedAttributes[`${importedAttributesPrefix}${key}`] = frontegg[key];
|
|
96
|
+
return modifiedImportedAttributes;
|
|
97
|
+
}, {}),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import * as FeatureFlags from '../../feature-flags/feature-flag.evaluator';
|
|
2
2
|
import * as IsEntitledEvaluators from '../is-entitled.evaluator';
|
|
3
3
|
import { TreatmentEnum } from '../../rules';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
EntitlementResult,
|
|
6
|
+
NotEntitledJustification,
|
|
7
|
+
NO_EXPIRATION_TIME,
|
|
8
|
+
UserEntitlementsContext,
|
|
9
|
+
Attributes,
|
|
10
|
+
} from '../types';
|
|
5
11
|
import { FeatureFlag } from '../../feature-flags/types';
|
|
6
12
|
const mockFeatureFlag: FeatureFlag = {
|
|
7
13
|
on: true,
|
|
@@ -289,3 +295,21 @@ describe('evaluateIsEntitledToPermission', () => {
|
|
|
289
295
|
expect(result).toEqual(falsyEntitlementResultBundleExpired);
|
|
290
296
|
});
|
|
291
297
|
});
|
|
298
|
+
|
|
299
|
+
describe('prepareAttributes', () => {
|
|
300
|
+
test('given custom & frontegg attributes, expected is merged Record, frontegg attributes altered with prefix', async () => {
|
|
301
|
+
const attributes: Attributes = {
|
|
302
|
+
custom: { testAttribute: 'testValue' },
|
|
303
|
+
frontegg: { email: 'test@email.com', unknownFronteggAttribute: 'unknownFronteggAttribute' },
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const expectedPreparedAttributes: Record<string, unknown> = {
|
|
307
|
+
testAttribute: 'testValue',
|
|
308
|
+
'imported.email': 'test@email.com',
|
|
309
|
+
'imported.unknownFronteggAttribute': 'unknownFronteggAttribute',
|
|
310
|
+
};
|
|
311
|
+
const preparedAttributes = IsEntitledEvaluators.prepareAttributes(attributes);
|
|
312
|
+
|
|
313
|
+
expect(preparedAttributes).toEqual(expectedPreparedAttributes);
|
|
314
|
+
});
|
|
315
|
+
});
|
|
@@ -24,4 +24,14 @@ export enum NotEntitledJustification {
|
|
|
24
24
|
|
|
25
25
|
export type CustomAttributes = Record<string, string | number | boolean | Date>;
|
|
26
26
|
|
|
27
|
+
export type FronteggAttributes = {
|
|
28
|
+
tenantId?: string;
|
|
29
|
+
userId?: string;
|
|
30
|
+
email?: string;
|
|
31
|
+
email_verified?: boolean;
|
|
32
|
+
[unmappedAttribute: string]: unknown;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type Attributes = { custom?: CustomAttributes; frontegg?: FronteggAttributes };
|
|
36
|
+
|
|
27
37
|
export const NO_EXPIRATION_TIME = -1;
|