@raytio/core 9.0.3 → 10.1.0

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.
Files changed (145) hide show
  1. package/CHANGELOG.md +161 -0
  2. package/README.md +77 -4
  3. package/dist/accessApplication/createAA.d.ts +17 -17
  4. package/dist/accessApplication/createAA.js +71 -71
  5. package/dist/accessApplication/index.d.ts +1 -1
  6. package/dist/accessApplication/index.js +17 -13
  7. package/dist/crypto/cognitoAttributes.d.ts +15 -15
  8. package/dist/crypto/cognitoAttributes.js +33 -33
  9. package/dist/crypto/decryptKeys.d.ts +1 -1
  10. package/dist/crypto/decryptKeys.js +33 -33
  11. package/dist/crypto/decryptSharedData.d.ts +26 -26
  12. package/dist/crypto/decryptSharedData.js +38 -38
  13. package/dist/crypto/getAADecryptor.d.ts +16 -11
  14. package/dist/crypto/getAADecryptor.js +20 -20
  15. package/dist/crypto/helpers.d.ts +18 -18
  16. package/dist/crypto/helpers.js +26 -26
  17. package/dist/crypto/index.d.ts +4 -4
  18. package/dist/crypto/index.js +21 -17
  19. package/dist/general/conditional.d.ts +17 -7
  20. package/dist/general/conditional.js +32 -22
  21. package/dist/general/date.d.ts +2 -0
  22. package/dist/general/date.js +18 -0
  23. package/dist/general/index.d.ts +4 -3
  24. package/dist/general/index.js +20 -15
  25. package/dist/general/password.d.ts +10 -8
  26. package/dist/general/password.js +31 -29
  27. package/dist/general/types.d.ts +6 -6
  28. package/dist/general/types.js +13 -13
  29. package/dist/index.d.ts +6 -6
  30. package/dist/index.js +22 -18
  31. package/dist/rules/calculateScore.d.ts +23 -11
  32. package/dist/rules/calculateScore.js +76 -52
  33. package/dist/rules/convertInstanceToRuleInput.d.ts +3 -3
  34. package/dist/rules/convertInstanceToRuleInput.js +139 -139
  35. package/dist/rules/evaluateScoreCondition.d.ts +2 -2
  36. package/dist/rules/evaluateScoreCondition.js +45 -45
  37. package/dist/rules/helpers/addInfiniteThresholdBoundaries.d.ts +8 -8
  38. package/dist/rules/helpers/addInfiniteThresholdBoundaries.js +11 -11
  39. package/dist/rules/helpers/checkTypeofValue.d.ts +9 -3
  40. package/dist/rules/helpers/checkTypeofValue.js +55 -43
  41. package/dist/rules/helpers/getLhsTypeofValue.d.ts +0 -0
  42. package/dist/rules/helpers/getLhsTypeofValue.js +1 -0
  43. package/dist/rules/helpers/getValueFromPath.d.ts +2 -0
  44. package/dist/rules/helpers/getValueFromPath.js +35 -0
  45. package/dist/rules/helpers/getValuesFromPath.d.ts +3 -3
  46. package/dist/rules/helpers/getValuesFromPath.js +50 -50
  47. package/dist/rules/helpers/index.d.ts +5 -5
  48. package/dist/rules/helpers/index.js +21 -17
  49. package/dist/rules/helpers/rebuildInfiniteValues.d.ts +3 -0
  50. package/dist/rules/helpers/rebuildInfiniteValues.js +6 -0
  51. package/dist/rules/helpers/symbols.d.ts +2 -2
  52. package/dist/rules/helpers/symbols.js +5 -5
  53. package/dist/rules/helpers/thresholds.d.ts +5 -5
  54. package/dist/rules/helpers/thresholds.js +47 -47
  55. package/dist/rules/index.d.ts +7 -6
  56. package/dist/rules/index.js +25 -20
  57. package/dist/rules/isScoreResultValid.d.ts +2 -0
  58. package/dist/rules/isScoreResultValid.js +9 -0
  59. package/dist/rules/operators/array.d.ts +2 -0
  60. package/dist/rules/operators/array.js +74 -0
  61. package/dist/rules/operators/bool.d.ts +2 -2
  62. package/dist/rules/operators/bool.js +17 -17
  63. package/dist/rules/operators/date.d.ts +2 -2
  64. package/dist/rules/operators/date.js +91 -91
  65. package/dist/rules/operators/hfield.d.ts +2 -2
  66. package/dist/rules/operators/hfield.js +33 -33
  67. package/dist/rules/operators/hschema.d.ts +2 -2
  68. package/dist/rules/operators/hschema.js +21 -21
  69. package/dist/rules/operators/index.d.ts +3 -3
  70. package/dist/rules/operators/index.js +11 -11
  71. package/dist/rules/operators/number.d.ts +2 -2
  72. package/dist/rules/operators/number.js +41 -41
  73. package/dist/rules/operators/string.d.ts +2 -2
  74. package/dist/rules/operators/string.js +58 -58
  75. package/dist/rules/types/config.d.ts +91 -86
  76. package/dist/rules/types/config.js +2 -2
  77. package/dist/rules/types/dataValueTypes.d.ts +19 -19
  78. package/dist/rules/types/dataValueTypes.js +4 -4
  79. package/dist/rules/types/index.d.ts +3 -3
  80. package/dist/rules/types/index.js +19 -15
  81. package/dist/rules/types/internal.d.ts +20 -20
  82. package/dist/rules/types/internal.js +2 -2
  83. package/dist/rules/types/operators.d.ts +19 -0
  84. package/dist/rules/types/operators.js +2 -0
  85. package/dist/rules/types/score.d.ts +50 -0
  86. package/dist/rules/types/score.js +2 -0
  87. package/dist/rules/validate.d.ts +6 -0
  88. package/dist/rules/validate.js +14 -0
  89. package/dist/rules/version.d.ts +1 -0
  90. package/dist/rules/version.js +5 -0
  91. package/dist/schema/index.d.ts +1 -1
  92. package/dist/schema/index.js +17 -13
  93. package/dist/schema/labels.d.ts +2 -2
  94. package/dist/schema/labels.js +20 -20
  95. package/dist/util/canonicalJsonify.d.ts +1 -1
  96. package/dist/util/canonicalJsonify.js +50 -50
  97. package/dist/util/conditional.d.ts +7 -0
  98. package/dist/util/conditional.js +15 -0
  99. package/dist/util/handleResponse.d.ts +1 -1
  100. package/dist/util/handleResponse.js +21 -21
  101. package/dist/util/hash.d.ts +1 -0
  102. package/dist/util/hash.js +11 -0
  103. package/dist/util/index.d.ts +2 -2
  104. package/dist/util/index.js +18 -14
  105. package/dist/verifications/checkSingleVerification.d.ts +9 -0
  106. package/dist/verifications/checkSingleVerification.js +29 -0
  107. package/dist/verifications/checkVerifications.d.ts +1 -0
  108. package/dist/verifications/checkVerifications.js +16 -0
  109. package/dist/verifications/checkVerificationsNew.d.ts +1 -0
  110. package/dist/verifications/checkVerificationsNew.js +16 -0
  111. package/dist/verifications/cleanInstance.d.ts +9 -9
  112. package/dist/verifications/cleanInstance.js +15 -15
  113. package/dist/verifications/getOwnRealVerifications.d.ts +14 -0
  114. package/dist/verifications/getOwnRealVerifications.js +64 -0
  115. package/dist/verifications/getPOVerification.d.ts +16 -16
  116. package/dist/verifications/getPOVerification.js +94 -94
  117. package/dist/verifications/getRealVerifications.d.ts +22 -0
  118. package/dist/verifications/getRealVerifications.js +71 -0
  119. package/dist/verifications/getVerifiedBy.d.ts +1 -1
  120. package/dist/verifications/getVerifiedBy.js +19 -19
  121. package/dist/verifications/index.d.ts +5 -5
  122. package/dist/verifications/index.js +21 -17
  123. package/dist/verifications/maybeRereference.d.ts +1 -1
  124. package/dist/verifications/maybeRereference.js +10 -10
  125. package/dist/verifications/safeHarbour.d.ts +20 -20
  126. package/dist/verifications/safeHarbour.js +74 -74
  127. package/dist/verifications/verifyCheck/getOwnRealVerifications.d.ts +13 -13
  128. package/dist/verifications/verifyCheck/getOwnRealVerifications.js +64 -63
  129. package/dist/verifications/verifyCheck/getSomeoneElsesRealVerifications.d.ts +19 -19
  130. package/dist/verifications/verifyCheck/getSomeoneElsesRealVerifications.js +72 -71
  131. package/dist/verifications/verifyCheck/index.d.ts +3 -3
  132. package/dist/verifications/verifyCheck/index.js +21 -17
  133. package/dist/verifications/verifyCheck/operations/checkOwnVerification.d.ts +15 -15
  134. package/dist/verifications/verifyCheck/operations/checkOwnVerification.js +39 -39
  135. package/dist/verifications/verifyCheck/operations/checkSomeoneElsesVerifications.d.ts +1 -1
  136. package/dist/verifications/verifyCheck/operations/checkSomeoneElsesVerifications.js +16 -16
  137. package/dist/verifications/verifyCheck/operations/index.d.ts +2 -2
  138. package/dist/verifications/verifyCheck/operations/index.js +18 -14
  139. package/dist/verifications/verifyCheck/util/canonicalJsonify.d.ts +1 -0
  140. package/dist/verifications/verifyCheck/util/canonicalJsonify.js +48 -0
  141. package/dist/verifications/verifyCheck/util/getValuesForAField.d.ts +2 -0
  142. package/dist/verifications/verifyCheck/util/getValuesForAField.js +9 -0
  143. package/dist/verifications/verifyCheck/util/index.d.ts +2 -0
  144. package/dist/verifications/verifyCheck/util/index.js +14 -0
  145. package/package.json +6 -5
@@ -1,29 +1,31 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.hashPassword = void 0;
4
- // ⚠ updating this value will lock all existing users out of their accounts
5
- /** @internal */
6
- const SALT = "247 213 94 9 15 236 156 48 194 177 107 216 198 215 169 239";
7
- /**
8
- * AWS Cognito never gets the raw password. We send them
9
- * a hashed verison using PBKDF2 with SHA-256 and 10,000
10
- * iterations.
11
- * @param password The raw password
12
- * @returns Promise resolving to the hashed password
13
- */
14
- async function hashPassword(password) {
15
- const binaryPassword = new TextEncoder().encode(password);
16
- const cryptoKey = await crypto.subtle.importKey("raw", binaryPassword, "PBKDF2", false, ["deriveBits"]);
17
- const options = {
18
- name: "PBKDF2",
19
- hash: "SHA-256",
20
- salt: new Uint8Array(SALT.split(" ").map(Number)),
21
- iterations: 10000,
22
- };
23
- const keyBuffer = new Uint8Array(await crypto.subtle.deriveBits(options, cryptoKey, 256));
24
- const compositeStr = Array.from(keyBuffer)
25
- .map(byte => String.fromCharCode(byte))
26
- .join("");
27
- return btoa(compositeStr);
28
- }
29
- exports.hashPassword = hashPassword;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.hashPassword = void 0;
4
+ // ⚠ updating this value will lock all existing users out of their accounts
5
+ /** @internal */
6
+ const SALT = "247 213 94 9 15 236 156 48 194 177 107 216 198 215 169 239";
7
+ /**
8
+ * @deprecated legacy feature, see #1252
9
+ *
10
+ * AWS Cognito never gets the raw password. We send them
11
+ * a hashed verison using PBKDF2 with SHA-256 and 10,000
12
+ * iterations.
13
+ * @param password The raw password
14
+ * @returns Promise resolving to the hashed password
15
+ */
16
+ async function hashPassword(password) {
17
+ const binaryPassword = new TextEncoder().encode(password);
18
+ const cryptoKey = await crypto.subtle.importKey("raw", binaryPassword, "PBKDF2", false, ["deriveBits"]);
19
+ const options = {
20
+ name: "PBKDF2",
21
+ hash: "SHA-256",
22
+ salt: new Uint8Array(SALT.split(" ").map(Number)),
23
+ iterations: 10000,
24
+ };
25
+ const keyBuffer = new Uint8Array(await crypto.subtle.deriveBits(options, cryptoKey, 256));
26
+ const compositeStr = Array.from(keyBuffer)
27
+ .map(byte => String.fromCharCode(byte))
28
+ .join("");
29
+ return btoa(compositeStr);
30
+ }
31
+ exports.hashPassword = hashPassword;
@@ -1,6 +1,6 @@
1
- /**
2
- * to prevent a key from the JS object prototype (e.g. `__proto__`) from
3
- * returning an unexpected value (see #1162)
4
- * @ignore
5
- */
6
- export declare function assertSafeProperty(key: string): asserts key is string;
1
+ /**
2
+ * to prevent a key from the JS object prototype (e.g. `__proto__`) from
3
+ * returning an unexpected value (see #1162)
4
+ * @ignore
5
+ */
6
+ export declare function assertSafeProperty(key: string): asserts key is string;
@@ -1,13 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertSafeProperty = void 0;
4
- /**
5
- * to prevent a key from the JS object prototype (e.g. `__proto__`) from
6
- * returning an unexpected value (see #1162)
7
- * @ignore
8
- */
9
- function assertSafeProperty(key) {
10
- if (key in {})
11
- throw new Error("Unacceptable object property");
12
- }
13
- exports.assertSafeProperty = assertSafeProperty;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertSafeProperty = void 0;
4
+ /**
5
+ * to prevent a key from the JS object prototype (e.g. `__proto__`) from
6
+ * returning an unexpected value (see #1162)
7
+ * @ignore
8
+ */
9
+ function assertSafeProperty(key) {
10
+ if (key in {})
11
+ throw new Error("Unacceptable object property");
12
+ }
13
+ exports.assertSafeProperty = assertSafeProperty;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- export * from "./accessApplication";
2
- export * from "./crypto";
3
- export * from "./general";
4
- export * from "./rules";
5
- export * from "./schema";
6
- export * from "./verifications";
1
+ export * from "./accessApplication";
2
+ export * from "./crypto";
3
+ export * from "./general";
4
+ export * from "./rules";
5
+ export * from "./schema";
6
+ export * from "./verifications";
package/dist/index.js CHANGED
@@ -1,18 +1,22 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
- };
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- __exportStar(require("./accessApplication"), exports);
14
- __exportStar(require("./crypto"), exports);
15
- __exportStar(require("./general"), exports);
16
- __exportStar(require("./rules"), exports);
17
- __exportStar(require("./schema"), exports);
18
- __exportStar(require("./verifications"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./accessApplication"), exports);
18
+ __exportStar(require("./crypto"), exports);
19
+ __exportStar(require("./general"), exports);
20
+ __exportStar(require("./rules"), exports);
21
+ __exportStar(require("./schema"), exports);
22
+ __exportStar(require("./verifications"), exports);
@@ -1,11 +1,23 @@
1
- import { RuleData, ScoreConfig } from "./types";
2
- /** @ignore */
3
- export declare type ScoreResult = {
4
- score: number;
5
- category: string;
6
- };
7
- /**
8
- * the main function to calculate a score and category.
9
- * Might throw an error.
10
- */
11
- export declare function calculateScore(ruleConfig: ScoreConfig, ruleInput: RuleData): ScoreResult;
1
+ import { RuleData, ScoreConfig } from "./types";
2
+ /** @ignore */
3
+ export declare type ScoreResult = {
4
+ score: number;
5
+ category: string;
6
+ categoryColour: string | undefined;
7
+ diagnostics: {
8
+ version: string;
9
+ config: {
10
+ id: string;
11
+ rules: {
12
+ id: string;
13
+ name: string;
14
+ passed: boolean;
15
+ }[];
16
+ };
17
+ };
18
+ };
19
+ /**
20
+ * the main function to calculate a score and category.
21
+ * Might throw an error.
22
+ */
23
+ export declare function calculateScore(ruleConfig: ScoreConfig, ruleInput: RuleData): Promise<ScoreResult>;
@@ -1,52 +1,76 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.calculateScore = void 0;
4
- const evaluateScoreCondition_1 = require("./evaluateScoreCondition");
5
- const helpers_1 = require("./helpers");
6
- function evaluateASTNode(node, data) {
7
- if (node.type === "LanguageOperator") {
8
- if (!node.children.length) {
9
- // we allow a single child because why not.
10
- throw new Error("LanguageOperator has no children");
11
- }
12
- if (node.operator === "AND") {
13
- return node.children.every(n => evaluateASTNode(n, data));
14
- }
15
- if (node.operator === "OR") {
16
- return node.children.some(n => evaluateASTNode(n, data));
17
- }
18
- throw new Error(`LanguageOperator node has invalid operator '${node.operator}'`);
19
- }
20
- if (node.type === "ScoreCondition") {
21
- return (0, evaluateScoreCondition_1.evaluateScoreCondition)(node, data);
22
- }
23
- throw new Error("ASTNode has invalid type");
24
- }
25
- function evaluateRule(rule, data) {
26
- if (rule.tree.length !== 1) {
27
- throw new Error("A rule may only have 1 root node");
28
- }
29
- const rulePassed = rule.tree.every(n => evaluateASTNode(n, data));
30
- return rulePassed ? rule.trueWeight : rule.falseWeight;
31
- }
32
- /**
33
- * the main function to calculate a score and category.
34
- * Might throw an error.
35
- */
36
- function calculateScore(ruleConfig, ruleInput) {
37
- const result = ruleConfig.rules.map(rule => evaluateRule(rule, ruleInput));
38
- const [addOrMultiply, initialValue] = (0, helpers_1.getCombinator)(ruleConfig.combinator);
39
- const finalScore = result.reduce(addOrMultiply, initialValue);
40
- const matchingCategories = (0, helpers_1.addInfiniteThresholdBoundaries)(ruleConfig.scoreThresholds).filter(threshold => finalScore >= threshold.from && finalScore <= threshold.to);
41
- if (matchingCategories.length === 0) {
42
- throw new Error(`No categories match the final score (${finalScore})`);
43
- }
44
- if (matchingCategories.length !== 1) {
45
- throw new Error(`Multiple categories match the final score (${finalScore})`);
46
- }
47
- return {
48
- score: finalScore,
49
- category: matchingCategories[0].name,
50
- };
51
- }
52
- exports.calculateScore = calculateScore;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateScore = void 0;
4
+ const evaluateScoreCondition_1 = require("./evaluateScoreCondition");
5
+ const helpers_1 = require("./helpers");
6
+ const general_1 = require("../general");
7
+ const version_1 = require("./version");
8
+ function evaluateASTNode(node, data) {
9
+ if (node.type === "LanguageOperator") {
10
+ if (!node.children.length) {
11
+ // we allow a single child because why not.
12
+ throw new Error("LanguageOperator has no children");
13
+ }
14
+ if (node.operator === "AND") {
15
+ return node.children.every(n => evaluateASTNode(n, data));
16
+ }
17
+ if (node.operator === "OR") {
18
+ return node.children.some(n => evaluateASTNode(n, data));
19
+ }
20
+ throw new Error(`LanguageOperator node has invalid operator '${node.operator}'`);
21
+ }
22
+ if (node.type === "ScoreCondition") {
23
+ return (0, evaluateScoreCondition_1.evaluateScoreCondition)(node, data);
24
+ }
25
+ throw new Error("ASTNode has invalid type");
26
+ }
27
+ function evaluateRule(rule, data) {
28
+ if (rule.tree.length !== 1) {
29
+ throw new Error("A rule may only have 1 root node");
30
+ }
31
+ const rulePassed = rule.tree.every(n => evaluateASTNode(n, data));
32
+ return {
33
+ passed: rulePassed,
34
+ score: rulePassed ? rule.trueWeight : rule.falseWeight,
35
+ };
36
+ }
37
+ /**
38
+ * We guarantee that the same configId or ruleId will always produce the same output.
39
+ * This is the closest thing we have to versioning.
40
+ */
41
+ const jsonId = (json) => (0, general_1.hashPassword)(JSON.stringify(json)).then(hash => hash.slice(0, 10));
42
+ /**
43
+ * the main function to calculate a score and category.
44
+ * Might throw an error.
45
+ */
46
+ async function calculateScore(ruleConfig, ruleInput) {
47
+ const result = ruleConfig.rules.map(rule => evaluateRule(rule, ruleInput));
48
+ const [addOrMultiply, initialValue] = (0, helpers_1.getCombinator)(ruleConfig.combinator);
49
+ const finalScore = result
50
+ .map(x => x.score)
51
+ .reduce(addOrMultiply, initialValue);
52
+ const matchingCategories = (0, helpers_1.addInfiniteThresholdBoundaries)(ruleConfig.scoreThresholds).filter(threshold => finalScore >= threshold.from && finalScore <= threshold.to);
53
+ if (matchingCategories.length === 0) {
54
+ throw new Error(`No categories match the final score (${finalScore})`);
55
+ }
56
+ if (matchingCategories.length !== 1) {
57
+ throw new Error(`Multiple categories match the final score (${finalScore})`);
58
+ }
59
+ return {
60
+ score: finalScore,
61
+ category: matchingCategories[0].name,
62
+ categoryColour: matchingCategories[0].colour,
63
+ diagnostics: {
64
+ version: version_1.SUBMISSION_RULES_VERSION,
65
+ config: {
66
+ id: await jsonId(ruleConfig),
67
+ rules: await Promise.all(result.map(async ({ passed }, i) => {
68
+ const r = ruleConfig.rules[i];
69
+ const ruleId = await jsonId(r);
70
+ return { id: ruleId, name: r.name, passed };
71
+ })),
72
+ },
73
+ },
74
+ };
75
+ }
76
+ exports.calculateScore = calculateScore;
@@ -1,3 +1,3 @@
1
- import { Instance, RealVer, Schema } from "@raytio/types";
2
- import { RuleData } from "./types";
3
- export declare const convertInstanceToRuleInput: (instance: Instance, realVers: RealVer[], getSchema: (schemaName: string) => Promise<Schema>) => Promise<RuleData>;
1
+ import { Instance, RealVer, Schema } from "@raytio/types";
2
+ import { RuleData } from "./types";
3
+ export declare const convertInstanceToRuleInput: (instance: Instance, realVers: RealVer[], getSchema: (schemaName: string) => Promise<Schema>) => Promise<RuleData>;
@@ -1,139 +1,139 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertInstanceToRuleInput = void 0;
4
- const ramda_1 = require("ramda");
5
- const verifications_1 = require("../verifications");
6
- const schema_1 = require("../schema");
7
- const helpers_1 = require("./helpers");
8
- // has to be hardcodede since we're in @raytio/core
9
- const VERIFICATION_SCHEMA = "ss_Verification";
10
- /**
11
- * this is only called for field values, so we can easily work out the type
12
- * There are no complications like POVerification or DateUnits
13
- */
14
- function getTypeofFieldValue(value) {
15
- if (Array.isArray(value))
16
- return "Array";
17
- if (typeof value === "boolean")
18
- return "Bool";
19
- if (typeof value === "number")
20
- return "Number";
21
- if (typeof value === "string")
22
- return "String";
23
- return undefined;
24
- }
25
- const isDateField = (schema) => ([fieldName]) => {
26
- var _a;
27
- return (_a = schema.properties[fieldName].tags) === null || _a === void 0 ? void 0 : _a.some(tag => tag.startsWith("group:date_picker"));
28
- };
29
- const convertInstanceToRuleInput = async (instance, realVers, getSchema) => {
30
- const out = {};
31
- // eslint-disable-next-line fp/no-loops
32
- for (const PO of instance.profile_objects) {
33
- const schemaName = (0, schema_1.findSchemaLabel)(PO.labels);
34
- // eslint-disable-next-line no-continue
35
- if (schemaName === VERIFICATION_SCHEMA)
36
- continue;
37
- const schema = await getSchema(schemaName);
38
- const verified = (0, verifications_1.getPOVerification)({ PO, schema, realVers });
39
- // remove anything we can't make rules for (e.g. object), and
40
- // also remove any fields that aren't defined in the schema.
41
- const validFields = Object.entries(PO.properties).filter(([fieldName, value]) => getTypeofFieldValue(value) && schema.properties[fieldName]);
42
- const nonDateProperties = validFields
43
- .filter(d => !isDateField(schema)(d))
44
- .map(([fieldName, value]) => {
45
- var _a;
46
- const verifiedBy = (0, ramda_1.uniq)((_a = realVers
47
- .filter(x => x.belongsToNId === PO.n_id &&
48
- x.fieldName === fieldName &&
49
- x.provider.verifierNId)) === null || _a === void 0 ? void 0 : _a.map(x => x.provider.verifierNId));
50
- const field = {
51
- hField: {
52
- __typeof__: helpers_1.HFieldSymbol,
53
- cameFromSchema: schemaName,
54
- verification: verified.fieldVerifications[fieldName],
55
- verifiedBy,
56
- },
57
- type: getTypeofFieldValue(value),
58
- value,
59
- };
60
- return [fieldName, field];
61
- });
62
- const dateFieldByCategorys = (0, ramda_1.groupBy)(([fieldName]) => schema.properties[fieldName].tags.find(tag => tag.startsWith("group:date_picker")), validFields.filter(isDateField(schema)));
63
- // the three date fields become one pseudo field for the score system
64
- const pseudoDateFields = Object.entries(dateFieldByCategorys).map(([groupTag, members]) => {
65
- var _a;
66
- const groupName = groupTag.split(":")[2] || "NO_NAME";
67
- const memberFieldNames = members.map(x => x[0]);
68
- const worstVer = (0, ramda_1.sort)((a, b) => b - a, // find the highest enum value (that's the worst)
69
- memberFieldNames.map(f => verified.fieldVerifications[f]))[0];
70
- const fieldVers = (0, ramda_1.uniq)((_a = realVers
71
- .filter(x => x.belongsToNId === PO.n_id &&
72
- memberFieldNames.includes(x.fieldName) &&
73
- x.provider.verifierNId)) === null || _a === void 0 ? void 0 : _a.map(v => v.provider.verifierNId).filter((val) => !!val));
74
- const values = Object.fromEntries(members);
75
- const dayFieldName = memberFieldNames.find(f => { var _a; return (_a = schema.properties[f].tags) === null || _a === void 0 ? void 0 : _a.includes("date_component:day"); });
76
- const monthFieldName = memberFieldNames.find(f => { var _a; return (_a = schema.properties[f].tags) === null || _a === void 0 ? void 0 : _a.includes("date_component:month"); });
77
- const yearFieldName = memberFieldNames.find(f => { var _a; return (_a = schema.properties[f].tags) === null || _a === void 0 ? void 0 : _a.includes("date_component:year"); });
78
- if (!dayFieldName || !monthFieldName || !yearFieldName) {
79
- throw new Error("Failed to infer date component field");
80
- }
81
- const isoString = [
82
- values[yearFieldName],
83
- `${values[monthFieldName]}`.padStart(2, "0"),
84
- `${values[dayFieldName]}`.padStart(2, "0"),
85
- ].join("-");
86
- const pseudoField = {
87
- hField: {
88
- __typeof__: helpers_1.HFieldSymbol,
89
- cameFromSchema: schemaName,
90
- verification: worstVer,
91
- verifiedBy: fieldVers,
92
- },
93
- type: "Date",
94
- value: new Date(isoString),
95
- };
96
- return [`__group_${groupName}`, pseudoField];
97
- });
98
- const deepProperties = Object.entries(PO.properties)
99
- .filter(([fieldName, fieldValue]) => {
100
- const field = schema.properties[fieldName];
101
- return (field &&
102
- field.type === "object" &&
103
- field.properties &&
104
- fieldValue &&
105
- typeof fieldValue === "object");
106
- })
107
- .flatMap(([fieldName, fieldValue]) => {
108
- const subFields = Object.keys(schema.properties[fieldName].properties);
109
- return subFields.map(subFieldName => {
110
- const value = fieldValue[subFieldName];
111
- const hsubfield = {
112
- hField: null,
113
- type: getTypeofFieldValue(value),
114
- value,
115
- };
116
- return [`${fieldName}|${subFieldName}`, hsubfield];
117
- });
118
- });
119
- // eslint-disable-next-line fp/no-mutation
120
- out[schemaName] || (out[schemaName] = []);
121
- // eslint-disable-next-line fp/no-mutating-methods
122
- out[schemaName].push({
123
- hSchema: {
124
- __typeof__: helpers_1.HSchemaSymbol,
125
- verification: verified.status,
126
- verifiedBy: verified.details.verifiers
127
- .map(x => x.verifierNId)
128
- .filter((val) => !!val),
129
- },
130
- properties: Object.fromEntries([
131
- ...nonDateProperties,
132
- ...pseudoDateFields,
133
- ...deepProperties,
134
- ]),
135
- });
136
- }
137
- return out;
138
- };
139
- exports.convertInstanceToRuleInput = convertInstanceToRuleInput;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertInstanceToRuleInput = void 0;
4
+ const ramda_1 = require("ramda");
5
+ const verifications_1 = require("../verifications");
6
+ const schema_1 = require("../schema");
7
+ const helpers_1 = require("./helpers");
8
+ // has to be hardcodede since we're in @raytio/core
9
+ const VERIFICATION_SCHEMA = "ss_Verification";
10
+ /**
11
+ * this is only called for field values, so we can easily work out the type
12
+ * There are no complications like POVerification or DateUnits
13
+ */
14
+ function getTypeofFieldValue(value) {
15
+ if (Array.isArray(value))
16
+ return "Array";
17
+ if (typeof value === "boolean")
18
+ return "Bool";
19
+ if (typeof value === "number")
20
+ return "Number";
21
+ if (typeof value === "string")
22
+ return "String";
23
+ return undefined;
24
+ }
25
+ const isDateField = (schema) => ([fieldName]) => {
26
+ var _a;
27
+ return (_a = schema.properties[fieldName].tags) === null || _a === void 0 ? void 0 : _a.some(tag => tag.startsWith("group:date_picker"));
28
+ };
29
+ const convertInstanceToRuleInput = async (instance, realVers, getSchema) => {
30
+ const out = {};
31
+ // eslint-disable-next-line fp/no-loops
32
+ for (const PO of instance.profile_objects) {
33
+ const schemaName = (0, schema_1.findSchemaLabel)(PO.labels);
34
+ // eslint-disable-next-line no-continue
35
+ if (schemaName === VERIFICATION_SCHEMA)
36
+ continue;
37
+ const schema = await getSchema(schemaName);
38
+ const verified = (0, verifications_1.getPOVerification)({ PO, schema, realVers });
39
+ // remove anything we can't make rules for (e.g. object), and
40
+ // also remove any fields that aren't defined in the schema.
41
+ const validFields = Object.entries(PO.properties).filter(([fieldName, value]) => getTypeofFieldValue(value) && schema.properties[fieldName]);
42
+ const nonDateProperties = validFields
43
+ .filter(d => !isDateField(schema)(d))
44
+ .map(([fieldName, value]) => {
45
+ var _a;
46
+ const verifiedBy = (0, ramda_1.uniq)((_a = realVers
47
+ .filter(x => x.belongsToNId === PO.n_id &&
48
+ x.fieldName === fieldName &&
49
+ x.provider.verifierNId)) === null || _a === void 0 ? void 0 : _a.map(x => x.provider.verifierNId));
50
+ const field = {
51
+ hField: {
52
+ __typeof__: helpers_1.HFieldSymbol,
53
+ cameFromSchema: schemaName,
54
+ verification: verified.fieldVerifications[fieldName],
55
+ verifiedBy,
56
+ },
57
+ type: getTypeofFieldValue(value),
58
+ value,
59
+ };
60
+ return [fieldName, field];
61
+ });
62
+ const dateFieldByCategorys = (0, ramda_1.groupBy)(([fieldName]) => schema.properties[fieldName].tags.find(tag => tag.startsWith("group:date_picker")), validFields.filter(isDateField(schema)));
63
+ // the three date fields become one pseudo field for the score system
64
+ const pseudoDateFields = Object.entries(dateFieldByCategorys).map(([groupTag, members]) => {
65
+ var _a;
66
+ const groupName = groupTag.split(":")[2] || "NO_NAME";
67
+ const memberFieldNames = members.map(x => x[0]);
68
+ const worstVer = (0, ramda_1.sort)((a, b) => b - a, // find the highest enum value (that's the worst)
69
+ memberFieldNames.map(f => verified.fieldVerifications[f]))[0];
70
+ const fieldVers = (0, ramda_1.uniq)((_a = realVers
71
+ .filter(x => x.belongsToNId === PO.n_id &&
72
+ memberFieldNames.includes(x.fieldName) &&
73
+ x.provider.verifierNId)) === null || _a === void 0 ? void 0 : _a.map(v => v.provider.verifierNId).filter((val) => !!val));
74
+ const values = Object.fromEntries(members);
75
+ const dayFieldName = memberFieldNames.find(f => { var _a; return (_a = schema.properties[f].tags) === null || _a === void 0 ? void 0 : _a.includes("date_component:day"); });
76
+ const monthFieldName = memberFieldNames.find(f => { var _a; return (_a = schema.properties[f].tags) === null || _a === void 0 ? void 0 : _a.includes("date_component:month"); });
77
+ const yearFieldName = memberFieldNames.find(f => { var _a; return (_a = schema.properties[f].tags) === null || _a === void 0 ? void 0 : _a.includes("date_component:year"); });
78
+ if (!dayFieldName || !monthFieldName || !yearFieldName) {
79
+ throw new Error("Failed to infer date component field");
80
+ }
81
+ const isoString = [
82
+ values[yearFieldName],
83
+ `${values[monthFieldName]}`.padStart(2, "0"),
84
+ `${values[dayFieldName]}`.padStart(2, "0"),
85
+ ].join("-");
86
+ const pseudoField = {
87
+ hField: {
88
+ __typeof__: helpers_1.HFieldSymbol,
89
+ cameFromSchema: schemaName,
90
+ verification: worstVer,
91
+ verifiedBy: fieldVers,
92
+ },
93
+ type: "Date",
94
+ value: new Date(isoString),
95
+ };
96
+ return [`__group_${groupName}`, pseudoField];
97
+ });
98
+ const deepProperties = Object.entries(PO.properties)
99
+ .filter(([fieldName, fieldValue]) => {
100
+ const field = schema.properties[fieldName];
101
+ return (field &&
102
+ field.type === "object" &&
103
+ field.properties &&
104
+ fieldValue &&
105
+ typeof fieldValue === "object");
106
+ })
107
+ .flatMap(([fieldName, fieldValue]) => {
108
+ const subFields = Object.keys(schema.properties[fieldName].properties);
109
+ return subFields.map(subFieldName => {
110
+ const value = fieldValue[subFieldName];
111
+ const hsubfield = {
112
+ hField: null,
113
+ type: getTypeofFieldValue(value),
114
+ value,
115
+ };
116
+ return [`${fieldName}|${subFieldName}`, hsubfield];
117
+ });
118
+ });
119
+ // eslint-disable-next-line fp/no-mutation
120
+ out[schemaName] || (out[schemaName] = []);
121
+ // eslint-disable-next-line fp/no-mutating-methods
122
+ out[schemaName].push({
123
+ hSchema: {
124
+ __typeof__: helpers_1.HSchemaSymbol,
125
+ verification: verified.status,
126
+ verifiedBy: verified.details.verifiers
127
+ .map(x => x.verifierNId)
128
+ .filter((val) => !!val),
129
+ },
130
+ properties: Object.fromEntries([
131
+ ...nonDateProperties,
132
+ ...pseudoDateFields,
133
+ ...deepProperties,
134
+ ]),
135
+ });
136
+ }
137
+ return out;
138
+ };
139
+ exports.convertInstanceToRuleInput = convertInstanceToRuleInput;
@@ -1,2 +1,2 @@
1
- import { ASTScoreConditionNode, RuleData } from "./types";
2
- export declare function evaluateScoreCondition(node: ASTScoreConditionNode, data: RuleData): boolean;
1
+ import { ASTScoreConditionNode, RuleData } from "./types";
2
+ export declare function evaluateScoreCondition(node: ASTScoreConditionNode, data: RuleData): boolean;