@hestia-earth/api 0.18.21 → 0.18.23-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.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { BaseModel } from '../../db/model.base';
|
|
2
|
+
export { getMainErrorMessage as reconciliationsGetMainErrorMessage, validateProductCorrespondence as reconciliationsValidateProductCorrespondence, validateImpactAssessmentLinks as reconciliationsValidateImpactAssessmentLinks, validateExpectedNodes as reconciliationsValidateExpectedNodes, validatePrivate as reconciliationsValidatePrivate } from './validations';
|
|
2
3
|
export declare enum ReconciliationLevel {
|
|
3
4
|
success = "success",
|
|
4
5
|
warning = "warning",
|
|
@@ -15,8 +15,14 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.Reconciliation = exports.ReconciliationLevel = void 0;
|
|
18
|
+
exports.Reconciliation = exports.ReconciliationLevel = exports.reconciliationsValidatePrivate = exports.reconciliationsValidateExpectedNodes = exports.reconciliationsValidateImpactAssessmentLinks = exports.reconciliationsValidateProductCorrespondence = exports.reconciliationsGetMainErrorMessage = void 0;
|
|
19
19
|
var model_base_1 = require("../../db/model.base");
|
|
20
|
+
var validations_1 = require("./validations");
|
|
21
|
+
Object.defineProperty(exports, "reconciliationsGetMainErrorMessage", { enumerable: true, get: function () { return validations_1.getMainErrorMessage; } });
|
|
22
|
+
Object.defineProperty(exports, "reconciliationsValidateProductCorrespondence", { enumerable: true, get: function () { return validations_1.validateProductCorrespondence; } });
|
|
23
|
+
Object.defineProperty(exports, "reconciliationsValidateImpactAssessmentLinks", { enumerable: true, get: function () { return validations_1.validateImpactAssessmentLinks; } });
|
|
24
|
+
Object.defineProperty(exports, "reconciliationsValidateExpectedNodes", { enumerable: true, get: function () { return validations_1.validateExpectedNodes; } });
|
|
25
|
+
Object.defineProperty(exports, "reconciliationsValidatePrivate", { enumerable: true, get: function () { return validations_1.validatePrivate; } });
|
|
20
26
|
var ReconciliationLevel;
|
|
21
27
|
(function (ReconciliationLevel) {
|
|
22
28
|
ReconciliationLevel["success"] = "success";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NodeType, ICycleJSONLD, JSONLD, JSON, SchemaType, ITermJSONLD, Term } from '@hestia-earth/schema';
|
|
2
|
+
import { INode } from '@hestia-earth/schema-convert';
|
|
3
|
+
export type Node = (INode | JSONLD<NodeType>) & {
|
|
4
|
+
site?: JSON<SchemaType.Site> | JSONLD<NodeType.Site>;
|
|
5
|
+
cycle?: JSON<SchemaType.Cycle> | JSONLD<NodeType.Cycle>;
|
|
6
|
+
};
|
|
7
|
+
export declare const notPrivateError = "expected-not-private";
|
|
8
|
+
export declare const EXTRANEOUS = "extraneous";
|
|
9
|
+
export declare const validateExpectedNodes: (nodes: Node[]) => Error[];
|
|
10
|
+
export declare const validateImpactAssessmentLinks: (nodes: Node[]) => Error[];
|
|
11
|
+
export declare const validatePrivate: (cycle: ICycleJSONLD) => Error[];
|
|
12
|
+
export declare const validateProductCorrespondence: (expectedProducts: (ITermJSONLD | Term)[], recalculatedProducts: (ITermJSONLD | Term)[], idRecalculated: string) => Error[];
|
|
13
|
+
export declare const getMainErrorMessage: (errors: any) => "expected-not-private" | "recalculation-error" | "invalid-nodes";
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.getMainErrorMessage = exports.validateProductCorrespondence = exports.validatePrivate = exports.validateImpactAssessmentLinks = exports.validateExpectedNodes = exports.EXTRANEOUS = exports.notPrivateError = void 0;
|
|
15
|
+
var schema_1 = require("@hestia-earth/schema");
|
|
16
|
+
exports.notPrivateError = 'expected-not-private';
|
|
17
|
+
exports.EXTRANEOUS = 'extraneous';
|
|
18
|
+
var getType = function (node) { return (node === null || node === void 0 ? void 0 : node['@type']) || (node === null || node === void 0 ? void 0 : node.type); };
|
|
19
|
+
var getId = function (node) { return (node === null || node === void 0 ? void 0 : node['@id']) || (node === null || node === void 0 ? void 0 : node.id); };
|
|
20
|
+
var getErrorsFromValidationsObj = function (validations) {
|
|
21
|
+
return Object.entries(validations)
|
|
22
|
+
.filter(function (_a) {
|
|
23
|
+
var _errorMsg = _a[0], validationFailed = _a[1];
|
|
24
|
+
return validationFailed;
|
|
25
|
+
})
|
|
26
|
+
.map(function (_a) {
|
|
27
|
+
var errorMsg = _a[0];
|
|
28
|
+
return new Error(errorMsg);
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
var validateExpectedNodes = function (nodes) {
|
|
32
|
+
var _a;
|
|
33
|
+
var _b;
|
|
34
|
+
var cycles = nodes.filter(function (node) { return getType(node) === schema_1.NodeType.Cycle; });
|
|
35
|
+
var sites = nodes.filter(function (node) { return getType(node) === schema_1.NodeType.Site; });
|
|
36
|
+
var impactAssessments = nodes.filter(function (node) { return getType(node) === schema_1.NodeType.ImpactAssessment; });
|
|
37
|
+
var cycleId = getId(cycles === null || cycles === void 0 ? void 0 : cycles[0]);
|
|
38
|
+
var cycleSiteId = getId((_b = cycles === null || cycles === void 0 ? void 0 : cycles[0]) === null || _b === void 0 ? void 0 : _b.site);
|
|
39
|
+
var siteId = getId(sites === null || sites === void 0 ? void 0 : sites[0]);
|
|
40
|
+
var validations = __assign((_a = {}, _a['More than one Cycle found in expected nodes'] = cycles.length > 1, _a['No Cycle found in expected nodes'] = cycles.length < 1, _a['Cycle missing name field in expected nodes'] = cycles.length && !cycles[0].name, _a['More than one Site found in expected nodes'] = sites.length > 1, _a["Cycle ".concat(cycleId, " is not linked to Site ").concat(siteId, " in expected nodes")] = cycleSiteId !== siteId, _a), impactAssessments.reduce(function (acc, ia) {
|
|
41
|
+
acc["ImpactAssessment ".concat(getId(ia), " is not linked to Cycle ").concat(cycleId, " in expected nodes")] =
|
|
42
|
+
getId(ia === null || ia === void 0 ? void 0 : ia.cycle) !== cycleId;
|
|
43
|
+
return acc;
|
|
44
|
+
}, {}));
|
|
45
|
+
var validationErrors = getErrorsFromValidationsObj(validations);
|
|
46
|
+
return validationErrors;
|
|
47
|
+
};
|
|
48
|
+
exports.validateExpectedNodes = validateExpectedNodes;
|
|
49
|
+
var matchedProduct = function (impacts) { return function (product) {
|
|
50
|
+
return impacts.filter(function (ia) { return getId(product.term) === getId(ia.product); }).length > 1;
|
|
51
|
+
}; };
|
|
52
|
+
var validateImpactAssessmentLinks = function (nodes) {
|
|
53
|
+
var _a;
|
|
54
|
+
var _b, _c;
|
|
55
|
+
var impactAssessments = nodes.filter(function (node) { return getType(node) === schema_1.NodeType.ImpactAssessment; });
|
|
56
|
+
var cycle = nodes.find(function (node) { return getType(node) === schema_1.NodeType.Cycle; });
|
|
57
|
+
var cycleId = getId(cycle);
|
|
58
|
+
var validations = (_a = {},
|
|
59
|
+
_a["Cycle ".concat(cycleId, " has no associated impact assessment")] = !impactAssessments.length,
|
|
60
|
+
_a["Cycle ".concat(cycleId, " has no products")] = !((_b = cycle.products) === null || _b === void 0 ? void 0 : _b.length),
|
|
61
|
+
_a["Cycle ".concat(cycleId, " has a product with more than one linked impact assessment")] = (_c = cycle.products) === null || _c === void 0 ? void 0 : _c.some(matchedProduct(impactAssessments)),
|
|
62
|
+
_a);
|
|
63
|
+
var validationErrors = getErrorsFromValidationsObj(validations);
|
|
64
|
+
return validationErrors;
|
|
65
|
+
};
|
|
66
|
+
exports.validateImpactAssessmentLinks = validateImpactAssessmentLinks;
|
|
67
|
+
var validatePrivate = function (cycle) {
|
|
68
|
+
var _a;
|
|
69
|
+
return getErrorsFromValidationsObj((_a = {}, _a[exports.notPrivateError] = !cycle.dataPrivate, _a));
|
|
70
|
+
};
|
|
71
|
+
exports.validatePrivate = validatePrivate;
|
|
72
|
+
var validateProductCorrespondence = function (expectedProducts, recalculatedProducts, idRecalculated) {
|
|
73
|
+
var missingProducts = expectedProducts.filter(function (pExpected) { return !recalculatedProducts.some(function (pRecalculated) { return getId(pRecalculated) === getId(pExpected); }); });
|
|
74
|
+
var extraneousProducts = recalculatedProducts.filter(function (pRecalculated) { return !expectedProducts.some(function (pExpected) { return getId(pRecalculated) === getId(pExpected); }); });
|
|
75
|
+
return [
|
|
76
|
+
[missingProducts, 'missing'],
|
|
77
|
+
[extraneousProducts, exports.EXTRANEOUS]
|
|
78
|
+
]
|
|
79
|
+
.map(function (_a) {
|
|
80
|
+
var products = _a[0], errorType = _a[1];
|
|
81
|
+
return products.length
|
|
82
|
+
? new Error("Cycle ".concat(idRecalculated, " has ").concat(errorType, " impact assessment for products: ").concat(products
|
|
83
|
+
.map(function (p) { return p['@id']; })
|
|
84
|
+
.join(', ')))
|
|
85
|
+
: null;
|
|
86
|
+
})
|
|
87
|
+
.filter(Boolean);
|
|
88
|
+
};
|
|
89
|
+
exports.validateProductCorrespondence = validateProductCorrespondence;
|
|
90
|
+
var getMainErrorMessage = function (errors) {
|
|
91
|
+
return errors.length && errors.length === 1 && errors[0].message.includes(exports.EXTRANEOUS)
|
|
92
|
+
? 'recalculation-error'
|
|
93
|
+
: errors.some(function (e) { return e.message === exports.notPrivateError; })
|
|
94
|
+
? exports.notPrivateError
|
|
95
|
+
: 'invalid-nodes';
|
|
96
|
+
};
|
|
97
|
+
exports.getMainErrorMessage = getMainErrorMessage;
|