@openinc/parse-server-opendash 4.32.6 → 4.34.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.
- package/dist/features/openservice/demoSeed/demoData.d.ts +51 -0
- package/dist/features/openservice/demoSeed/demoData.js +305 -0
- package/dist/features/openservice/demoSeed/demoSeed.d.ts +82 -0
- package/dist/features/openservice/demoSeed/demoSeed.js +424 -0
- package/dist/features/openware/helper/mesSourceScope.d.ts +18 -0
- package/dist/features/openware/helper/mesSourceScope.js +144 -0
- package/dist/functions/openinc-openservice-demo-seed.d.ts +21 -0
- package/dist/functions/openinc-openservice-demo-seed.js +63 -0
- package/dist/hooks/BDE_WorkOrder.js +26 -1
- package/dist/hooks/MES_Order.js +12 -0
- package/dist/hooks/MES_OrderPlan.js +17 -0
- package/dist/hooks/Service_Schedule.js +3 -2
- package/dist/types/Service_Schedule.d.ts +0 -3
- package/dist/types/Service_Schedule.js +0 -6
- package/package.json +1 -1
- package/schema/BDE_ShiftException.json +10 -10
- package/schema/BDE_ShiftModel.json +7 -7
- package/schema/BDE_WorkOrder.json +6 -6
- package/schema/Chat_Message.json +6 -6
- package/schema/Core_RequestTemplate.json +33 -28
- package/schema/MES_RessourceOverview.json +74 -0
- package/schema/OWPlcDeviceTemplate.json +44 -0
- package/schema/RAG_Data.json +0 -5
- package/schema/Service_Schedule.json +0 -3
|
@@ -0,0 +1,424 @@
|
|
|
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.DEMO_SEED_CONTEXT_FLAG = exports.DEMO_SEED_CONFIG_KEY = void 0;
|
|
7
|
+
exports.demoSeedStatus = demoSeedStatus;
|
|
8
|
+
exports.resolveTenant = resolveTenant;
|
|
9
|
+
exports.demoSeedUsers = demoSeedUsers;
|
|
10
|
+
exports.resolveUser = resolveUser;
|
|
11
|
+
exports.seedDemoData = seedDemoData;
|
|
12
|
+
exports.resetDemoData = resetDemoData;
|
|
13
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
14
|
+
const index_js_1 = require("../../../types/index.js");
|
|
15
|
+
const demoData_js_1 = require("./demoData.js");
|
|
16
|
+
/**
|
|
17
|
+
* Where a tenant's demo seed remembers what it created.
|
|
18
|
+
*
|
|
19
|
+
* None of the seeded classes carries a marker column — a demo feature is no
|
|
20
|
+
* reason for a schema change. Instead the ids of everything seeded are kept in
|
|
21
|
+
* one config row per tenant, readable by `od-admin` only. A reset removes
|
|
22
|
+
* exactly those objects (plus what can only go together with them, see
|
|
23
|
+
* {@link resetDemoData}), so a tenant that also holds real data keeps it.
|
|
24
|
+
*/
|
|
25
|
+
exports.DEMO_SEED_CONFIG_KEY = "OPENSERVICE_DEMO_SEED";
|
|
26
|
+
/**
|
|
27
|
+
* Context flag on every write the seed makes. The plan hook skips the Outlook
|
|
28
|
+
* sync for it: demo plans belong to a demo tenant, not in anybody's calendar.
|
|
29
|
+
*/
|
|
30
|
+
exports.DEMO_SEED_CONTEXT_FLAG = "fromDemoSeed";
|
|
31
|
+
/** Reads. */
|
|
32
|
+
const MK = { useMasterKey: true };
|
|
33
|
+
/**
|
|
34
|
+
* Writes and deletes. `skipChangelog`: the seed's own rows would all read
|
|
35
|
+
* "created by master key, today" — noise in every history view, and rows the
|
|
36
|
+
* reset would have to chase afterwards.
|
|
37
|
+
*/
|
|
38
|
+
const WRITE = {
|
|
39
|
+
useMasterKey: true,
|
|
40
|
+
context: { skipChangelog: true, [exports.DEMO_SEED_CONTEXT_FLAG]: true },
|
|
41
|
+
};
|
|
42
|
+
const SEEDED_CLASSES = [
|
|
43
|
+
index_js_1.Service_Equipment.className,
|
|
44
|
+
index_js_1.Service_Schedule.className,
|
|
45
|
+
index_js_1.Service_Schedule_Template.className,
|
|
46
|
+
index_js_1.Service_Schedule_Execution.className,
|
|
47
|
+
];
|
|
48
|
+
/** Every tenant with the date of its last demo seed, if any. */
|
|
49
|
+
async function demoSeedStatus() {
|
|
50
|
+
const [tenants, rows] = await Promise.all([
|
|
51
|
+
new node_1.default.Query(index_js_1.Tenant).ascending("label").limit(1000).find(MK),
|
|
52
|
+
new node_1.default.Query(index_js_1.Config)
|
|
53
|
+
.equalTo("key", exports.DEMO_SEED_CONFIG_KEY)
|
|
54
|
+
.limit(1000)
|
|
55
|
+
.find(MK),
|
|
56
|
+
]);
|
|
57
|
+
const seededAt = new Map();
|
|
58
|
+
for (const row of rows) {
|
|
59
|
+
const tenantId = row.get("tenant")?.id;
|
|
60
|
+
const record = parseRecord(row.get("value"));
|
|
61
|
+
if (tenantId && record)
|
|
62
|
+
seededAt.set(tenantId, record.seededAt);
|
|
63
|
+
}
|
|
64
|
+
return tenants.map((tenant) => ({
|
|
65
|
+
tenantId: tenant.id,
|
|
66
|
+
label: tenant.get("label") || tenant.id,
|
|
67
|
+
seededAt: seededAt.get(tenant.id),
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
async function resolveTenant(tenantId) {
|
|
71
|
+
if (typeof tenantId !== "string" || !tenantId) {
|
|
72
|
+
throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, "tenantId is required");
|
|
73
|
+
}
|
|
74
|
+
return new node_1.default.Query(index_js_1.Tenant).get(tenantId, MK);
|
|
75
|
+
}
|
|
76
|
+
/** The tenant's users, as candidates for the `user` field of the demo data. */
|
|
77
|
+
async function demoSeedUsers(tenant) {
|
|
78
|
+
const users = await new node_1.default.Query(node_1.default.User)
|
|
79
|
+
.equalTo("tenant", tenant)
|
|
80
|
+
.notEqualTo("tenantBanned", true)
|
|
81
|
+
.limit(1000)
|
|
82
|
+
.find(MK);
|
|
83
|
+
return users
|
|
84
|
+
.map((user) => ({ userId: user.id, label: userLabel(user) }))
|
|
85
|
+
.sort((a, b) => a.label.localeCompare(b.label));
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The user the demo data is attributed to — only one of the chosen tenant's,
|
|
89
|
+
* otherwise a demo plan would hand a foreign user read+write on it.
|
|
90
|
+
*/
|
|
91
|
+
async function resolveUser(userId, tenant) {
|
|
92
|
+
if (userId == null || userId === "")
|
|
93
|
+
return undefined;
|
|
94
|
+
if (typeof userId !== "string") {
|
|
95
|
+
throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, "userId must be a string");
|
|
96
|
+
}
|
|
97
|
+
const user = (await new node_1.default.Query(node_1.default.User)
|
|
98
|
+
.equalTo("objectId", userId)
|
|
99
|
+
.equalTo("tenant", tenant)
|
|
100
|
+
.first(MK));
|
|
101
|
+
if (!user) {
|
|
102
|
+
throw new node_1.default.Error(node_1.default.Error.OBJECT_NOT_FOUND, `User ${userId} does not belong to tenant ${tenant.id}`);
|
|
103
|
+
}
|
|
104
|
+
return user;
|
|
105
|
+
}
|
|
106
|
+
function userLabel(user) {
|
|
107
|
+
const full = [user.get("firstname"), user.get("lastname")]
|
|
108
|
+
.filter(Boolean)
|
|
109
|
+
.join(" ");
|
|
110
|
+
return (user.get("name") ||
|
|
111
|
+
full ||
|
|
112
|
+
user.get("username") ||
|
|
113
|
+
user.get("email") ||
|
|
114
|
+
user.id);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Replace the tenant's demo data with a fresh set.
|
|
118
|
+
*
|
|
119
|
+
* Removes the previous seed first, so pressing the button twice does not
|
|
120
|
+
* leave two copies of Halle B.
|
|
121
|
+
*
|
|
122
|
+
* `user` becomes the `user` of every plan, template and execution: the owner
|
|
123
|
+
* of a plan (the ACL grants them read+write) and the author of the rest —
|
|
124
|
+
* and the `performedBy` of every execution.
|
|
125
|
+
* Without one the fields stay empty, as before.
|
|
126
|
+
*/
|
|
127
|
+
async function seedDemoData(tenant, user) {
|
|
128
|
+
const removed = await resetDemoData(tenant);
|
|
129
|
+
// Written after every step, so a failure halfway still leaves a record the
|
|
130
|
+
// next reset can clean up from.
|
|
131
|
+
const record = {
|
|
132
|
+
seededAt: new Date().toISOString(),
|
|
133
|
+
equipment: [],
|
|
134
|
+
templates: [],
|
|
135
|
+
schedules: [],
|
|
136
|
+
executions: [],
|
|
137
|
+
};
|
|
138
|
+
// ── Equipment ──────────────────────────────────────────────────────────────
|
|
139
|
+
const hall = new index_js_1.Service_Equipment();
|
|
140
|
+
hall.set("label", demoData_js_1.DEMO_HALL);
|
|
141
|
+
hall.set("deleted", false);
|
|
142
|
+
hall.set("tenant", tenant);
|
|
143
|
+
await hall.save(null, WRITE);
|
|
144
|
+
record.equipment.push(hall.id);
|
|
145
|
+
await writeRecord(tenant, record);
|
|
146
|
+
const machines = demoData_js_1.DEMO_MACHINES.map((label) => {
|
|
147
|
+
const machine = new index_js_1.Service_Equipment();
|
|
148
|
+
machine.set("label", label);
|
|
149
|
+
machine.set("deleted", false);
|
|
150
|
+
machine.set("tenant", tenant);
|
|
151
|
+
machine.set("parent", hall);
|
|
152
|
+
return machine;
|
|
153
|
+
});
|
|
154
|
+
await node_1.default.Object.saveAll(machines, WRITE);
|
|
155
|
+
record.equipment.push(...machines.map((m) => m.id));
|
|
156
|
+
await writeRecord(tenant, record);
|
|
157
|
+
// ── Templates ──────────────────────────────────────────────────────────────
|
|
158
|
+
const templates = (0, demoData_js_1.demoTemplates)().map((data) => {
|
|
159
|
+
const template = new index_js_1.Service_Schedule_Template();
|
|
160
|
+
template.set("title", data.title);
|
|
161
|
+
template.set("description", data.description);
|
|
162
|
+
template.set("cadence", data.cadence);
|
|
163
|
+
template.set("steps", data.steps);
|
|
164
|
+
template.set("serviceProviders", []);
|
|
165
|
+
template.set("tenant", tenant);
|
|
166
|
+
if (user)
|
|
167
|
+
template.set("user", user);
|
|
168
|
+
return template;
|
|
169
|
+
});
|
|
170
|
+
await node_1.default.Object.saveAll(templates, WRITE);
|
|
171
|
+
record.templates = templates.map((t) => t.id);
|
|
172
|
+
await writeRecord(tenant, record);
|
|
173
|
+
// ── Plans ──────────────────────────────────────────────────────────────────
|
|
174
|
+
const plans = (0, demoData_js_1.demoSchedules)();
|
|
175
|
+
const schedules = plans.map((plan) => {
|
|
176
|
+
const schedule = new index_js_1.Service_Schedule();
|
|
177
|
+
schedule.set("title", plan.title);
|
|
178
|
+
schedule.set("active", true);
|
|
179
|
+
schedule.set("cadence", plan.cadence);
|
|
180
|
+
schedule.set("steps", plan.steps);
|
|
181
|
+
schedule.set("serviceProviders", []);
|
|
182
|
+
schedule.set("tenant", tenant);
|
|
183
|
+
if (user)
|
|
184
|
+
schedule.set("user", user);
|
|
185
|
+
schedule.set("equipment", machines[plan.machine]);
|
|
186
|
+
// Optional columns only when used: an explicit undefined would be written
|
|
187
|
+
// as null and muddle "not configured".
|
|
188
|
+
if (plan.color)
|
|
189
|
+
schedule.set("color", plan.color);
|
|
190
|
+
if (plan.leadDays != null)
|
|
191
|
+
schedule.set("leadDays", plan.leadDays);
|
|
192
|
+
if (plan.graceDays != null)
|
|
193
|
+
schedule.set("graceDays", plan.graceDays);
|
|
194
|
+
if (plan.escalationAfterDays != null) {
|
|
195
|
+
schedule.set("escalationAfterDays", plan.escalationAfterDays);
|
|
196
|
+
}
|
|
197
|
+
if (plan.pausedUntil)
|
|
198
|
+
schedule.set("pausedUntil", plan.pausedUntil);
|
|
199
|
+
if (plan.origin)
|
|
200
|
+
schedule.set("origin", plan.origin);
|
|
201
|
+
if (plan.asset)
|
|
202
|
+
schedule.set("asset", plan.asset);
|
|
203
|
+
return schedule;
|
|
204
|
+
});
|
|
205
|
+
await node_1.default.Object.saveAll(schedules, WRITE);
|
|
206
|
+
record.schedules = schedules.map((s) => s.id);
|
|
207
|
+
await writeRecord(tenant, record);
|
|
208
|
+
// Plans are private by default (`denyTenantUserRead`), and a master-key
|
|
209
|
+
// write has no creator to grant. Assigning the tenant's user role is what
|
|
210
|
+
// makes them visible to everybody logging into the demo — the afterSave
|
|
211
|
+
// hook rebuilds the ACL from the relation.
|
|
212
|
+
const userRole = await new node_1.default.Query(node_1.default.Role)
|
|
213
|
+
.equalTo("name", `od-tenant-user-${tenant.id}`)
|
|
214
|
+
.first(MK);
|
|
215
|
+
if (userRole) {
|
|
216
|
+
for (const schedule of schedules) {
|
|
217
|
+
schedule.relation("assignedRoles").add(userRole);
|
|
218
|
+
}
|
|
219
|
+
await node_1.default.Object.saveAll(schedules, WRITE);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
console.warn(`[DemoSeed] Tenant ${tenant.id} has no od-tenant-user role — demo plans stay visible to admins only`);
|
|
223
|
+
}
|
|
224
|
+
// ── Executions, from each plan's history ───────────────────────────────────
|
|
225
|
+
// Completed executions only: one without `completedAt` is still being worked
|
|
226
|
+
// through, and seeding those would make every plan look like somebody is
|
|
227
|
+
// mid-job on it. Every step is ticked, so the protocols read as complete.
|
|
228
|
+
// The step snapshot (`steps`) is frozen on by the execution hook.
|
|
229
|
+
const executions = plans.flatMap((plan, index) => plan.history.map((completedDaysAgo) => {
|
|
230
|
+
const completedAt = (0, demoData_js_1.demoDaysAgo)(completedDaysAgo);
|
|
231
|
+
const execution = new index_js_1.Service_Schedule_Execution();
|
|
232
|
+
execution.set("schedule", schedules[index]);
|
|
233
|
+
execution.set("tenant", tenant);
|
|
234
|
+
if (user) {
|
|
235
|
+
execution.set("user", user);
|
|
236
|
+
// Who carried it out — what the protocol shows as "durchgeführt von".
|
|
237
|
+
execution.set("performedBy", user);
|
|
238
|
+
}
|
|
239
|
+
execution.set("completedAt", completedAt);
|
|
240
|
+
execution.set("stepResults", plan.steps.map((s) => ({
|
|
241
|
+
stepId: s.id,
|
|
242
|
+
done: true,
|
|
243
|
+
completedAt: completedAt.toISOString(),
|
|
244
|
+
})));
|
|
245
|
+
return execution;
|
|
246
|
+
}));
|
|
247
|
+
await node_1.default.Object.saveAll(executions, WRITE);
|
|
248
|
+
record.executions = executions.map((e) => e.id);
|
|
249
|
+
await writeRecord(tenant, record);
|
|
250
|
+
return {
|
|
251
|
+
tenantId: tenant.id,
|
|
252
|
+
removed,
|
|
253
|
+
created: {
|
|
254
|
+
equipment: record.equipment.length,
|
|
255
|
+
schedules: schedules.length,
|
|
256
|
+
templates: templates.length,
|
|
257
|
+
executions: executions.length,
|
|
258
|
+
},
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Remove what the last seed created in this tenant.
|
|
263
|
+
*
|
|
264
|
+
* Beyond the recorded ids, three things go with them because they cannot stay
|
|
265
|
+
* behind on their own:
|
|
266
|
+
* - executions somebody recorded by hand on a demo plan or demo machine — a
|
|
267
|
+
* plan with executions cannot be deleted, and they would point at nothing;
|
|
268
|
+
* - the role/user assignments of the demo plans (Parse leaves relation rows
|
|
269
|
+
* in `_Join:*` behind when the owning object is deleted);
|
|
270
|
+
* - changelog rows about demo objects, written by edits during the demo.
|
|
271
|
+
*
|
|
272
|
+
* Equipment that tickets or non-demo equipment point at is archived rather
|
|
273
|
+
* than deleted.
|
|
274
|
+
*/
|
|
275
|
+
async function resetDemoData(tenant) {
|
|
276
|
+
const row = await findRecordRow(tenant);
|
|
277
|
+
const record = parseRecord(row?.get("value"));
|
|
278
|
+
const schedules = await byIds(tenant, index_js_1.Service_Schedule, record?.schedules);
|
|
279
|
+
const templates = await byIds(tenant, index_js_1.Service_Schedule_Template, record?.templates);
|
|
280
|
+
const equipment = await byIds(tenant, index_js_1.Service_Equipment, record?.equipment);
|
|
281
|
+
const recordedExecutions = await byIds(tenant, index_js_1.Service_Schedule_Execution, record?.executions);
|
|
282
|
+
// Executions point at plans (or, for a direct maintenance, at equipment);
|
|
283
|
+
// they have to go before what they point at.
|
|
284
|
+
const executions = uniqueById([
|
|
285
|
+
...recordedExecutions,
|
|
286
|
+
...(schedules.length
|
|
287
|
+
? await new node_1.default.Query(index_js_1.Service_Schedule_Execution)
|
|
288
|
+
.equalTo("tenant", tenant)
|
|
289
|
+
.containedIn("schedule", schedules)
|
|
290
|
+
.limit(10000)
|
|
291
|
+
.find(MK)
|
|
292
|
+
: []),
|
|
293
|
+
...(equipment.length
|
|
294
|
+
? await new node_1.default.Query(index_js_1.Service_Schedule_Execution)
|
|
295
|
+
.equalTo("tenant", tenant)
|
|
296
|
+
.containedIn("equipment", equipment)
|
|
297
|
+
.doesNotExist("schedule")
|
|
298
|
+
.limit(10000)
|
|
299
|
+
.find(MK)
|
|
300
|
+
: []),
|
|
301
|
+
]);
|
|
302
|
+
await node_1.default.Object.destroyAll(executions, WRITE);
|
|
303
|
+
await clearRelations(schedules, [
|
|
304
|
+
"assignedRoles",
|
|
305
|
+
"escalationRoles",
|
|
306
|
+
"assignedUsers",
|
|
307
|
+
]);
|
|
308
|
+
await clearRelations(templates, ["assignedRoles"]);
|
|
309
|
+
await node_1.default.Object.destroyAll(schedules, WRITE);
|
|
310
|
+
await node_1.default.Object.destroyAll(templates, WRITE);
|
|
311
|
+
// Machines before the hall that is their parent; one at a time, because a
|
|
312
|
+
// refused delete must not take the rest of the batch with it.
|
|
313
|
+
let deletedEquipment = 0;
|
|
314
|
+
let archivedEquipment = 0;
|
|
315
|
+
const childrenFirst = [...equipment].sort((a, b) => Number(!a.get("parent")) - Number(!b.get("parent")));
|
|
316
|
+
for (const item of childrenFirst) {
|
|
317
|
+
// Somebody hung their own equipment under Halle B: deleting it would leave
|
|
318
|
+
// that child pointing at nothing. The equipment hook only checks tickets.
|
|
319
|
+
const children = await new node_1.default.Query(index_js_1.Service_Equipment)
|
|
320
|
+
.equalTo("parent", item)
|
|
321
|
+
.count(MK);
|
|
322
|
+
try {
|
|
323
|
+
if (children > 0)
|
|
324
|
+
throw new Error("has children");
|
|
325
|
+
await item.destroy(WRITE);
|
|
326
|
+
deletedEquipment++;
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
item.set("deleted", true);
|
|
330
|
+
await item.save(null, WRITE);
|
|
331
|
+
archivedEquipment++;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
// History of edits made during the demo. Only rows about objects that are
|
|
335
|
+
// actually gone — an archived machine keeps its history.
|
|
336
|
+
const goneIds = [
|
|
337
|
+
...executions,
|
|
338
|
+
...schedules,
|
|
339
|
+
...templates,
|
|
340
|
+
...equipment.filter((item) => !item.get("deleted")),
|
|
341
|
+
].map((o) => o.id);
|
|
342
|
+
await deleteChangelog(goneIds);
|
|
343
|
+
if (row)
|
|
344
|
+
await row.destroy(WRITE);
|
|
345
|
+
return {
|
|
346
|
+
executions: executions.length,
|
|
347
|
+
schedules: schedules.length,
|
|
348
|
+
templates: templates.length,
|
|
349
|
+
equipment: deletedEquipment,
|
|
350
|
+
archivedEquipment,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
/** Recorded objects that still exist — scoped to the tenant, always. */
|
|
354
|
+
async function byIds(tenant, cls, ids) {
|
|
355
|
+
if (!ids?.length)
|
|
356
|
+
return [];
|
|
357
|
+
return new node_1.default.Query(cls.className)
|
|
358
|
+
.containedIn("objectId", ids)
|
|
359
|
+
.equalTo("tenant", tenant)
|
|
360
|
+
.limit(ids.length)
|
|
361
|
+
.find(MK);
|
|
362
|
+
}
|
|
363
|
+
async function clearRelations(objects, keys) {
|
|
364
|
+
const changed = [];
|
|
365
|
+
for (const object of objects) {
|
|
366
|
+
let dirty = false;
|
|
367
|
+
for (const key of keys) {
|
|
368
|
+
const relation = object.relation(key);
|
|
369
|
+
const members = await relation.query().limit(1000).find(MK);
|
|
370
|
+
if (members.length) {
|
|
371
|
+
relation.remove(members);
|
|
372
|
+
dirty = true;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (dirty)
|
|
376
|
+
changed.push(object);
|
|
377
|
+
}
|
|
378
|
+
if (changed.length)
|
|
379
|
+
await node_1.default.Object.saveAll(changed, WRITE);
|
|
380
|
+
}
|
|
381
|
+
async function deleteChangelog(ids) {
|
|
382
|
+
if (!ids.length)
|
|
383
|
+
return;
|
|
384
|
+
const rows = await new node_1.default.Query(index_js_1.Changelog)
|
|
385
|
+
.containedIn("nameOfClass", SEEDED_CLASSES)
|
|
386
|
+
.containedIn("changedObject", ids)
|
|
387
|
+
.limit(10000)
|
|
388
|
+
.find(MK);
|
|
389
|
+
await node_1.default.Object.destroyAll(rows, WRITE);
|
|
390
|
+
}
|
|
391
|
+
function uniqueById(objects) {
|
|
392
|
+
return [...new Map(objects.map((o) => [o.id, o])).values()];
|
|
393
|
+
}
|
|
394
|
+
function findRecordRow(tenant) {
|
|
395
|
+
return new node_1.default.Query(index_js_1.Config)
|
|
396
|
+
.equalTo("key", exports.DEMO_SEED_CONFIG_KEY)
|
|
397
|
+
.equalTo("tenant", tenant)
|
|
398
|
+
.first(MK);
|
|
399
|
+
}
|
|
400
|
+
async function writeRecord(tenant, record) {
|
|
401
|
+
let row = await findRecordRow(tenant);
|
|
402
|
+
if (!row) {
|
|
403
|
+
row = new index_js_1.Config();
|
|
404
|
+
row.set("key", exports.DEMO_SEED_CONFIG_KEY);
|
|
405
|
+
row.set("tenant", tenant);
|
|
406
|
+
row.set("priority", 0);
|
|
407
|
+
const acl = new node_1.default.ACL();
|
|
408
|
+
acl.setRoleReadAccess("od-admin", true);
|
|
409
|
+
acl.setRoleWriteAccess("od-admin", true);
|
|
410
|
+
row.setACL(acl);
|
|
411
|
+
}
|
|
412
|
+
row.set("value", JSON.stringify(record));
|
|
413
|
+
await row.save(null, WRITE);
|
|
414
|
+
}
|
|
415
|
+
function parseRecord(value) {
|
|
416
|
+
if (typeof value !== "string" || !value)
|
|
417
|
+
return undefined;
|
|
418
|
+
try {
|
|
419
|
+
return JSON.parse(value);
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
return undefined;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Seed or remove the open.SERVICE demo data set (Halle B, ten plans, four
|
|
3
|
+
* templates, their history) in a tenant of the caller's choice.
|
|
4
|
+
*
|
|
5
|
+
* Actions:
|
|
6
|
+
* - `status` (default) — whether the caller may use this at all, and every
|
|
7
|
+
* tenant with the date of its last seed. A caller who may not gets
|
|
8
|
+
* `{ allowed: false }` instead of an error, so the setup page can simply
|
|
9
|
+
* leave the section out.
|
|
10
|
+
* - `users` — the chosen tenant's users, for the `user` field of the data.
|
|
11
|
+
* - `seed` — replace the tenant's demo data with a fresh set, optionally
|
|
12
|
+
* attributed to `userId` (one of that tenant's users). Dates are relative
|
|
13
|
+
* to now, which is why re-seeding is the way to refresh it.
|
|
14
|
+
* - `reset` — remove what the last seed created, and nothing else.
|
|
15
|
+
*
|
|
16
|
+
* **Global admins only (`od-admin`).** Unlike the setup function, the tenant
|
|
17
|
+
* is a parameter here — that is the point of it — so a tenant admin must not
|
|
18
|
+
* be able to call it: they could write into, or wipe the demo data of, a
|
|
19
|
+
* tenant that is not theirs.
|
|
20
|
+
*/
|
|
21
|
+
export declare function init(name: string): Promise<void>;
|
|
@@ -0,0 +1,63 @@
|
|
|
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.init = init;
|
|
7
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const demoSeed_js_1 = require("../features/openservice/demoSeed/demoSeed.js");
|
|
9
|
+
/**
|
|
10
|
+
* Seed or remove the open.SERVICE demo data set (Halle B, ten plans, four
|
|
11
|
+
* templates, their history) in a tenant of the caller's choice.
|
|
12
|
+
*
|
|
13
|
+
* Actions:
|
|
14
|
+
* - `status` (default) — whether the caller may use this at all, and every
|
|
15
|
+
* tenant with the date of its last seed. A caller who may not gets
|
|
16
|
+
* `{ allowed: false }` instead of an error, so the setup page can simply
|
|
17
|
+
* leave the section out.
|
|
18
|
+
* - `users` — the chosen tenant's users, for the `user` field of the data.
|
|
19
|
+
* - `seed` — replace the tenant's demo data with a fresh set, optionally
|
|
20
|
+
* attributed to `userId` (one of that tenant's users). Dates are relative
|
|
21
|
+
* to now, which is why re-seeding is the way to refresh it.
|
|
22
|
+
* - `reset` — remove what the last seed created, and nothing else.
|
|
23
|
+
*
|
|
24
|
+
* **Global admins only (`od-admin`).** Unlike the setup function, the tenant
|
|
25
|
+
* is a parameter here — that is the point of it — so a tenant admin must not
|
|
26
|
+
* be able to call it: they could write into, or wipe the demo data of, a
|
|
27
|
+
* tenant that is not theirs.
|
|
28
|
+
*/
|
|
29
|
+
async function init(name) {
|
|
30
|
+
node_1.default.Cloud.define(name, async (request) => {
|
|
31
|
+
const allowed = request.master || (await isGlobalAdmin(request.user));
|
|
32
|
+
const { action, tenantId, userId } = request.params;
|
|
33
|
+
if ((action ?? "status") === "status") {
|
|
34
|
+
return allowed
|
|
35
|
+
? { allowed: true, tenants: await (0, demoSeed_js_1.demoSeedStatus)() }
|
|
36
|
+
: { allowed: false, tenants: [] };
|
|
37
|
+
}
|
|
38
|
+
if (!allowed) {
|
|
39
|
+
throw new node_1.default.Error(node_1.default.Error.OPERATION_FORBIDDEN, `${name}: only global administrators may seed demo data`);
|
|
40
|
+
}
|
|
41
|
+
const tenant = await (0, demoSeed_js_1.resolveTenant)(tenantId);
|
|
42
|
+
switch (action) {
|
|
43
|
+
case "users":
|
|
44
|
+
return (0, demoSeed_js_1.demoSeedUsers)(tenant);
|
|
45
|
+
case "seed":
|
|
46
|
+
return (0, demoSeed_js_1.seedDemoData)(tenant, await (0, demoSeed_js_1.resolveUser)(userId, tenant));
|
|
47
|
+
case "reset":
|
|
48
|
+
return { tenantId: tenant.id, removed: await (0, demoSeed_js_1.resetDemoData)(tenant) };
|
|
49
|
+
default:
|
|
50
|
+
throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, `${name}: unknown action "${action}"`);
|
|
51
|
+
}
|
|
52
|
+
}, { requireUser: true });
|
|
53
|
+
}
|
|
54
|
+
/** Direct member of `od-admin` — the role every ACL in this server grants. */
|
|
55
|
+
async function isGlobalAdmin(user) {
|
|
56
|
+
if (!user)
|
|
57
|
+
return false;
|
|
58
|
+
const role = await new node_1.default.Query(node_1.default.Role)
|
|
59
|
+
.equalTo("name", "od-admin")
|
|
60
|
+
.equalTo("users", user)
|
|
61
|
+
.first({ useMasterKey: true });
|
|
62
|
+
return !!role;
|
|
63
|
+
}
|