@openinc/parse-server-opendash 4.32.6 → 4.33.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.
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Source that bounds the plan with the given id: its own, otherwise the one of
3
+ * the nearest parent plan. Undefined when no plan up the tree has a source.
4
+ */
5
+ export declare function planScope(planId: string | undefined): Promise<string | undefined>;
6
+ /** Whether `sourceId` is the source `scopeId` or lies somewhere below it. */
7
+ export declare function isWithinSource(sourceId: string, scopeId: string): Promise<boolean>;
8
+ /**
9
+ * Rejects the save unless every source lies within the scope. `what` names
10
+ * the object in the message, e.g. "A production order".
11
+ */
12
+ export declare function assertWithinScope(sourceIds: string[], scopeId: string | undefined, what: string): Promise<void>;
13
+ /**
14
+ * Rejects a new scope of an existing plan (another source or another parent)
15
+ * that would leave sub plans, production orders or work orders below the
16
+ * plan outside of it.
17
+ */
18
+ export declare function assertContentWithinScope(planId: string, scopeId: string | undefined): Promise<void>;
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.planScope = planScope;
7
+ exports.isWithinSource = isWithinSource;
8
+ exports.assertWithinScope = assertWithinScope;
9
+ exports.assertContentWithinScope = assertContentWithinScope;
10
+ const node_1 = __importDefault(require("parse/node"));
11
+ const index_js_1 = require("../../../types/index.js");
12
+ /**
13
+ * Order plans (operational orders) are bound to the source tree: everything
14
+ * below a plan — sub plans, production orders and work orders — has to run on
15
+ * the plan's source or on a source below it. A plan without a source of its
16
+ * own inherits the one of the nearest parent plan that has one; a tree
17
+ * without any source is not bound.
18
+ */
19
+ /** Upper bound for walking the source and order trees; guards against corrupt data. */
20
+ const MAX_DEPTH = 50;
21
+ /**
22
+ * Source that bounds the plan with the given id: its own, otherwise the one of
23
+ * the nearest parent plan. Undefined when no plan up the tree has a source.
24
+ */
25
+ async function planScope(planId) {
26
+ let id = planId;
27
+ for (let depth = 0; id && depth < MAX_DEPTH; depth++) {
28
+ const plan = await new node_1.default.Query(index_js_1.MES_OrderPlan)
29
+ .select("source", "parentOrder")
30
+ .get(id, { useMasterKey: true })
31
+ .catch(() => undefined);
32
+ if (!plan)
33
+ return undefined;
34
+ const sourceId = plan.get("source")?.id;
35
+ if (sourceId)
36
+ return sourceId;
37
+ id = plan.get("parentOrder")?.id;
38
+ }
39
+ return undefined;
40
+ }
41
+ /** Whether `sourceId` is the source `scopeId` or lies somewhere below it. */
42
+ async function isWithinSource(sourceId, scopeId) {
43
+ let id = sourceId;
44
+ for (let depth = 0; id && depth < MAX_DEPTH; depth++) {
45
+ if (id === scopeId)
46
+ return true;
47
+ const source = await new node_1.default.Query(index_js_1.Source)
48
+ .select("parent")
49
+ .get(id, { useMasterKey: true })
50
+ .catch(() => undefined);
51
+ id = source?.get("parent")?.id;
52
+ }
53
+ return false;
54
+ }
55
+ /**
56
+ * Rejects the save unless every source lies within the scope. `what` names
57
+ * the object in the message, e.g. "A production order".
58
+ */
59
+ async function assertWithinScope(sourceIds, scopeId, what) {
60
+ if (!scopeId)
61
+ return;
62
+ for (const sourceId of new Set(sourceIds)) {
63
+ if (!(await isWithinSource(sourceId, scopeId))) {
64
+ throw new node_1.default.Error(node_1.default.Error.VALIDATION_ERROR, `${what} has to run on the source "${await sourceLabel(scopeId)}" of its order plan or on a source below it, not on "${await sourceLabel(sourceId)}".`);
65
+ }
66
+ }
67
+ }
68
+ /**
69
+ * Rejects a new scope of an existing plan (another source or another parent)
70
+ * that would leave sub plans, production orders or work orders below the
71
+ * plan outside of it.
72
+ */
73
+ async function assertContentWithinScope(planId, scopeId) {
74
+ if (!scopeId)
75
+ return;
76
+ const allowed = await sourceSubtree(scopeId);
77
+ const plans = [
78
+ index_js_1.MES_OrderPlan.createWithoutData(planId),
79
+ ...(await planSubtree(planId)),
80
+ ];
81
+ const outsidePlans = plans.filter((plan) => {
82
+ const sourceId = plan.get("source")?.id;
83
+ return sourceId && !allowed.has(sourceId);
84
+ });
85
+ const allowedSources = [...allowed].map((id) => index_js_1.Source.createWithoutData(id));
86
+ const outsideOrders = await new node_1.default.Query(index_js_1.MES_Order)
87
+ .containedIn("orderPlan", plans)
88
+ .exists("source")
89
+ .notContainedIn("source", allowedSources)
90
+ .count({ useMasterKey: true });
91
+ // `sources` is a relation: match work orders with any source outside.
92
+ // Counts 0 when the BDE feature (and with it the class) is disabled.
93
+ const outsideWorkOrders = await new node_1.default.Query(index_js_1.BDE_WorkOrder)
94
+ .containedIn("orderPlan", plans)
95
+ .notEqualTo("isDeleted", true)
96
+ .matchesQuery("sources", new node_1.default.Query(index_js_1.Source).notContainedIn("objectId", [...allowed]))
97
+ .count({ useMasterKey: true })
98
+ .catch(() => 0);
99
+ if (outsidePlans.length + outsideOrders + outsideWorkOrders > 0) {
100
+ throw new node_1.default.Error(node_1.default.Error.VALIDATION_ERROR, `The order plan cannot be bound to the source "${await sourceLabel(scopeId)}": ${outsidePlans.length} sub order(s), ${outsideOrders} production order(s) and ${outsideWorkOrders} work order(s) below it run on sources outside of it.`);
101
+ }
102
+ }
103
+ /** Ids of the source and of all sources below it. */
104
+ async function sourceSubtree(scopeId) {
105
+ const ids = new Set([scopeId]);
106
+ let frontier = [scopeId];
107
+ for (let depth = 0; frontier.length && depth < MAX_DEPTH; depth++) {
108
+ const children = await new node_1.default.Query(index_js_1.Source)
109
+ .containedIn("parent", frontier.map((id) => index_js_1.Source.createWithoutData(id)))
110
+ .select("parent")
111
+ .limit(10000)
112
+ .find({ useMasterKey: true });
113
+ frontier = children
114
+ .map((child) => child.id)
115
+ .filter((id) => !!id && !ids.has(id));
116
+ frontier.forEach((id) => ids.add(id));
117
+ }
118
+ return ids;
119
+ }
120
+ /** Plans below the plan (not the plan itself), with their own source. */
121
+ async function planSubtree(planId) {
122
+ const seen = new Set([planId]);
123
+ const result = [];
124
+ let frontier = [planId];
125
+ for (let depth = 0; frontier.length && depth < MAX_DEPTH; depth++) {
126
+ const children = await new node_1.default.Query(index_js_1.MES_OrderPlan)
127
+ .containedIn("parentOrder", frontier.map((id) => index_js_1.MES_OrderPlan.createWithoutData(id)))
128
+ .select("source", "parentOrder")
129
+ .limit(10000)
130
+ .find({ useMasterKey: true });
131
+ const added = children.filter((child) => !!child.id && !seen.has(child.id));
132
+ frontier = added.map((child) => child.id);
133
+ frontier.forEach((id) => seen.add(id));
134
+ result.push(...added);
135
+ }
136
+ return result;
137
+ }
138
+ async function sourceLabel(id) {
139
+ const source = await new node_1.default.Query(index_js_1.Source)
140
+ .select("name", "tag")
141
+ .get(id, { useMasterKey: true })
142
+ .catch(() => undefined);
143
+ return source?.get("name") || source?.get("tag") || id;
144
+ }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.init = init;
4
4
  const index_js_1 = require("../features/schema/index.js");
5
+ const mesSourceScope_js_1 = require("../features/openware/helper/mesSourceScope.js");
5
6
  const index_js_2 = require("../types/index.js");
6
7
  async function init() {
7
8
  (0, index_js_1.beforeSaveHook)(index_js_2.BDE_WorkOrder, async (request) => {
@@ -10,7 +11,31 @@ async function init() {
10
11
  // Workers at the BDE terminal set the status when they start, pause or
11
12
  // complete a work order.
12
13
  await (0, index_js_1.defaultAclHandler)(request, { allowTenantUserWrite: true });
13
- // TODO
14
+ // A work order of an order plan runs on the plan's source or below it.
15
+ const planId = object.get("orderPlan")?.id;
16
+ if (planId) {
17
+ // `sources` is a relation: the save carries what is added and removed.
18
+ const op = object.op("sources");
19
+ let sourceIds = op?.relationsToAdd ?? [];
20
+ // Moved to another plan: the sources it already has count as well.
21
+ if (original && original.get("orderPlan")?.id !== planId) {
22
+ const removed = new Set(op?.relationsToRemove ?? []);
23
+ const current = await object
24
+ .relation("sources")
25
+ .query()
26
+ .limit(1000)
27
+ .find({ useMasterKey: true });
28
+ sourceIds = [
29
+ ...sourceIds,
30
+ ...current
31
+ .map((source) => source.id)
32
+ .filter((id) => !!id && !removed.has(id)),
33
+ ];
34
+ }
35
+ if (sourceIds.length) {
36
+ await (0, mesSourceScope_js_1.assertWithinScope)(sourceIds, await (0, mesSourceScope_js_1.planScope)(planId), "A work order");
37
+ }
38
+ }
14
39
  });
15
40
  (0, index_js_1.afterSaveHook)(index_js_2.BDE_WorkOrder, async (request) => {
16
41
  const { object, original, user } = request;
@@ -41,6 +41,7 @@ const node_1 = __importDefault(require("parse/node"));
41
41
  const cron = __importStar(require("cron"));
42
42
  const cron_1 = require("cron");
43
43
  const index_js_1 = require("../features/openware/index.js");
44
+ const mesSourceScope_js_1 = require("../features/openware/helper/mesSourceScope.js");
44
45
  const index_js_2 = require("../features/schema/index.js");
45
46
  const index_js_3 = require("../types/index.js");
46
47
  // Tolerance for client/server clock skew when comparing `start` against `now`.
@@ -85,6 +86,17 @@ async function init() {
85
86
  allowCustomACL: true,
86
87
  allowTenantUserWrite: true,
87
88
  });
89
+ // A production order of an order plan runs on the plan's source or below
90
+ // it. Checked first: further down, a running order ends other orders.
91
+ const planId = object.get("orderPlan")?.id;
92
+ const sourceId = object.get("source")?.id;
93
+ if (planId &&
94
+ sourceId &&
95
+ (!original ||
96
+ original.get("orderPlan")?.id !== planId ||
97
+ original.get("source")?.id !== sourceId)) {
98
+ await (0, mesSourceScope_js_1.assertWithinScope)([sourceId], await (0, mesSourceScope_js_1.planScope)(planId), "A production order");
99
+ }
88
100
  if (!object.tag) {
89
101
  const source = await object.get("source")?.fetch({ useMasterKey: true });
90
102
  if (!source || !source.tag) {
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.init = init;
7
7
  const node_1 = __importDefault(require("parse/node"));
8
8
  const index_js_1 = require("../features/schema/index.js");
9
+ const mesSourceScope_js_1 = require("../features/openware/helper/mesSourceScope.js");
9
10
  const index_js_2 = require("../types/index.js");
10
11
  /** Upper bound for walking up the order tree; guards against corrupt data. */
11
12
  const MAX_DEPTH = 50;
@@ -20,6 +21,22 @@ async function init() {
20
21
  if (object.dirty("parentOrder")) {
21
22
  await assertNoCycle(object);
22
23
  }
24
+ // A sub order stays within the source of its parent plan, and a plan's
25
+ // new source (or parent) still has to hold everything below it.
26
+ const sourceId = object.get("source")?.id;
27
+ const parentId = object.get("parentOrder")?.id;
28
+ const sourceChanged = !original || original.get("source")?.id !== sourceId;
29
+ const parentChanged = !original || original.get("parentOrder")?.id !== parentId;
30
+ if (sourceChanged || parentChanged) {
31
+ const parentScope = await (0, mesSourceScope_js_1.planScope)(parentId);
32
+ if (sourceId) {
33
+ await (0, mesSourceScope_js_1.assertWithinScope)([sourceId], parentScope, "A sub order");
34
+ }
35
+ // With a source of its own, a new parent leaves the scope unchanged.
36
+ if (original && object.id && (sourceChanged || !sourceId)) {
37
+ await (0, mesSourceScope_js_1.assertContentWithinScope)(object.id, sourceId ?? parentScope);
38
+ }
39
+ }
23
40
  });
24
41
  (0, index_js_1.afterSaveHook)(index_js_2.MES_OrderPlan, async (request) => {
25
42
  const { object, original, user } = request;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.32.6",
3
+ "version": "4.33.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.16.1",
6
6
  "keywords": [
@@ -1,26 +1,26 @@
1
1
  {
2
2
  "fields": {
3
- "label": {
4
- "type": "String",
5
- "required": false
3
+ "end": {
4
+ "type": "Date",
5
+ "required": true
6
6
  },
7
7
  "kind": {
8
8
  "type": "String",
9
9
  "required": true
10
10
  },
11
- "start": {
12
- "type": "Date",
13
- "required": true
14
- },
15
- "end": {
16
- "type": "Date",
17
- "required": true
11
+ "label": {
12
+ "type": "String",
13
+ "required": false
18
14
  },
19
15
  "sources": {
20
16
  "type": "Array",
21
17
  "required": false,
22
18
  "defaultValue": []
23
19
  },
20
+ "start": {
21
+ "type": "Date",
22
+ "required": true
23
+ },
24
24
  "tenant": {
25
25
  "type": "Pointer",
26
26
  "targetClass": "{{PREFIX}}Tenant",
@@ -14,23 +14,23 @@
14
14
  "required": false,
15
15
  "defaultValue": []
16
16
  },
17
- "validFrom": {
18
- "type": "Date",
17
+ "tenant": {
18
+ "type": "Pointer",
19
+ "targetClass": "{{PREFIX}}Tenant",
19
20
  "required": false
20
21
  },
21
22
  "timezone": {
22
23
  "type": "String",
23
24
  "required": false
24
25
  },
25
- "tenant": {
26
- "type": "Pointer",
27
- "targetClass": "{{PREFIX}}Tenant",
28
- "required": false
29
- },
30
26
  "user": {
31
27
  "type": "Pointer",
32
28
  "targetClass": "_User",
33
29
  "required": false
30
+ },
31
+ "validFrom": {
32
+ "type": "Date",
33
+ "required": false
34
34
  }
35
35
  },
36
36
  "classLevelPermissions": {
@@ -56,12 +56,6 @@
56
56
  }
57
57
  },
58
58
  "classLevelPermissions": {
59
- "ACL": {
60
- "*": {
61
- "read": true,
62
- "write": true
63
- }
64
- },
65
59
  "find": {
66
60
  "*": true
67
61
  },
@@ -85,6 +79,12 @@
85
79
  },
86
80
  "protectedFields": {
87
81
  "*": []
82
+ },
83
+ "ACL": {
84
+ "*": {
85
+ "read": true,
86
+ "write": true
87
+ }
88
88
  }
89
89
  }
90
90
  }
@@ -54,12 +54,6 @@
54
54
  }
55
55
  },
56
56
  "classLevelPermissions": {
57
- "ACL": {
58
- "*": {
59
- "read": true,
60
- "write": true
61
- }
62
- },
63
57
  "find": {
64
58
  "*": true
65
59
  },
@@ -83,6 +77,12 @@
83
77
  },
84
78
  "protectedFields": {
85
79
  "*": []
80
+ },
81
+ "ACL": {
82
+ "*": {
83
+ "read": true,
84
+ "write": true
85
+ }
86
86
  }
87
87
  }
88
88
  }
@@ -1,19 +1,27 @@
1
1
  {
2
2
  "fields": {
3
- "identifier": {
4
- "type": "String",
5
- "required": true
3
+ "allowedMethods": {
4
+ "type": "Array",
5
+ "required": false
6
6
  },
7
- "priority": {
8
- "type": "Number",
9
- "required": true,
10
- "defaultValue": 0
7
+ "allowPath": {
8
+ "type": "Boolean",
9
+ "required": false,
10
+ "defaultValue": false
11
+ },
12
+ "body": {
13
+ "type": "Object",
14
+ "required": false
11
15
  },
12
16
  "description": {
13
17
  "type": "String",
14
18
  "required": false
15
19
  },
16
- "url": {
20
+ "headers": {
21
+ "type": "Object",
22
+ "required": false
23
+ },
24
+ "identifier": {
17
25
  "type": "String",
18
26
  "required": true
19
27
  },
@@ -21,39 +29,31 @@
21
29
  "type": "String",
22
30
  "required": false
23
31
  },
24
- "allowedMethods": {
25
- "type": "Array",
26
- "required": false
27
- },
28
- "allowPath": {
29
- "type": "Boolean",
30
- "required": false,
31
- "defaultValue": false
32
- },
33
- "headers": {
34
- "type": "Object",
35
- "required": false
32
+ "priority": {
33
+ "type": "Number",
34
+ "required": true,
35
+ "defaultValue": 0
36
36
  },
37
37
  "query": {
38
38
  "type": "Object",
39
39
  "required": false
40
40
  },
41
- "body": {
41
+ "secrets": {
42
42
  "type": "Object",
43
43
  "required": false
44
44
  },
45
- "secrets": {
46
- "type": "Object",
45
+ "tenant": {
46
+ "type": "Pointer",
47
+ "targetClass": "{{PREFIX}}Tenant",
47
48
  "required": false
48
49
  },
49
50
  "timeout": {
50
51
  "type": "Number",
51
52
  "required": false
52
53
  },
53
- "tenant": {
54
- "type": "Pointer",
55
- "targetClass": "{{PREFIX}}Tenant",
56
- "required": false
54
+ "url": {
55
+ "type": "String",
56
+ "required": true
57
57
  },
58
58
  "user": {
59
59
  "type": "Pointer",
@@ -82,7 +82,12 @@
82
82
  },
83
83
  "addField": {},
84
84
  "protectedFields": {
85
- "*": ["secrets", "headers", "query", "body"],
85
+ "*": [
86
+ "secrets",
87
+ "headers",
88
+ "query",
89
+ "body"
90
+ ],
86
91
  "role:od-admin": []
87
92
  }
88
93
  }
@@ -0,0 +1,74 @@
1
+ {
2
+ "fields": {
3
+ "description": {
4
+ "type": "String",
5
+ "required": false
6
+ },
7
+ "image": {
8
+ "type": "File",
9
+ "required": false
10
+
11
+ },
12
+ "kpis": {
13
+ "type": "Array",
14
+ "required": false,
15
+ "defaultValue": []
16
+ },
17
+ "overviewDashboard": {
18
+ "type": "Pointer",
19
+ "targetClass": "{{PREFIX}}Dashboard",
20
+ "required": false
21
+ },
22
+ "priority": {
23
+ "type": "Number",
24
+ "required": true,
25
+ "defaultValue": 0
26
+ },
27
+ "source": {
28
+ "type": "Pointer",
29
+ "targetClass": "{{PREFIX}}Source",
30
+ "required": true
31
+ },
32
+ "stateConfing": {
33
+ "type": "Object",
34
+ "required": false
35
+ },
36
+ "tenant": {
37
+ "type": "Pointer",
38
+ "targetClass": "{{PREFIX}}Tenant",
39
+ "required": false
40
+ }
41
+ },
42
+ "classLevelPermissions": {
43
+ "ACL": {
44
+ "*": {
45
+ "read": true,
46
+ "write": true
47
+ }
48
+ },
49
+ "find": {
50
+ "*": true
51
+ },
52
+ "count": {
53
+ "*": true
54
+ },
55
+ "get": {
56
+ "*": true
57
+ },
58
+ "create": {
59
+ "*": true
60
+ },
61
+ "update": {
62
+ "*": true
63
+ },
64
+ "delete": {
65
+ "*": true
66
+ },
67
+ "addField": {
68
+ "*": true
69
+ },
70
+ "protectedFields": {
71
+ "*": []
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,44 @@
1
+ {
2
+ "fields": {
3
+ "description": {
4
+ "type": "String"
5
+ },
6
+ "device": {
7
+ "type": "Object",
8
+ "required": true
9
+ },
10
+ "items": {
11
+ "type": "Array",
12
+ "required": true,
13
+ "defaultValue": []
14
+ },
15
+ "name": {
16
+ "type": "String",
17
+ "required": true
18
+ }
19
+ },
20
+ "classLevelPermissions": {
21
+ "find": {
22
+ "requiresAuthentication": true
23
+ },
24
+ "count": {
25
+ "requiresAuthentication": true
26
+ },
27
+ "get": {
28
+ "requiresAuthentication": true
29
+ },
30
+ "create": {
31
+ "requiresAuthentication": true
32
+ },
33
+ "update": {
34
+ "requiresAuthentication": true
35
+ },
36
+ "delete": {
37
+ "requiresAuthentication": true
38
+ },
39
+ "addField": {},
40
+ "protectedFields": {
41
+ "*": []
42
+ }
43
+ }
44
+ }
@@ -46,11 +46,6 @@
46
46
  "type": "String",
47
47
  "required": false
48
48
  },
49
- "parseClassRelated": {
50
- "type": "Array",
51
- "required": false,
52
- "defaultValue": []
53
- },
54
49
  "text": {
55
50
  "type": "String",
56
51
  "required": false