@openinc/parse-server-opendash 4.40.0 → 4.41.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.
@@ -31,7 +31,8 @@ async function init() {
31
31
  // the colleague on the next shift — are not necessarily the person who
32
32
  // carried it out. Write is what gets restricted, below and by Parse.
33
33
  await (0, index_js_1.defaultAclHandler)(request, { allowCustomACL: true });
34
- // Freeze the plan's work steps onto the execution as it is created.
34
+ // Freeze what the protocol reads from its plan onto the execution as it is
35
+ // created: the work steps, the machine and the name.
35
36
  //
36
37
  // Deliberately **before** the migration/master escape below: this is not an
37
38
  // authorisation question but a data one. A protocol written by the seed
@@ -41,11 +42,18 @@ async function init() {
41
42
  //
42
43
  // Without it a protocol shows whatever the plan says *now*: renaming a step
43
44
  // rewrites every protocol that referenced it, deleting one turns its
44
- // results into numbered orphans (GRENZEN §5).
45
+ // results into numbered orphans (GRENZEN §5). The same goes for the plan
46
+ // itself — moved to another machine, every old protocol would move with it
47
+ // (machine book, KPIs, the execution list); renamed, every old protocol
48
+ // would be renamed too. It also makes "executions of machine X" one query
49
+ // on `equipment`, whatever created the run — the open.WARE automation has
50
+ // always set it, the UI never did.
45
51
  //
46
- // A direct maintenance (no plan) brings its own steps and is left alone.
47
- if (object.isNew() && !object.get("steps")) {
48
- await freezeSteps(object);
52
+ // Only fills what is missing: a direct maintenance (no plan) brings all of
53
+ // it itself, and an explicit value — the automation's `title`, a migrated
54
+ // v1 title — is what was actually recorded.
55
+ if (object.isNew()) {
56
+ await freezePlanSnapshot(object);
49
57
  }
50
58
  // A protocol has to be about something: the plan it performs, or — for a
51
59
  // direct maintenance — the machine it was done on. With neither, no view
@@ -81,9 +89,9 @@ async function init() {
81
89
  // Without this, anyone holding `can-perform-execution` could record a run
82
90
  // of a plan they were never assigned to, given its objectId (which every
83
91
  // tenant-readable execution of that plan carries). It would count towards
84
- // the plan's due dates, and `freezeSteps` above — which reads with the
85
- // master key — would copy the private plan's steps into a record the whole
86
- // tenant can read.
92
+ // the plan's due dates, and `freezePlanSnapshot` above — which reads with
93
+ // the master key — would copy the private plan's steps and title into a
94
+ // record the whole tenant can read.
87
95
  //
88
96
  // Checked whenever `schedule` changes, not only on create: re-pointing an
89
97
  // open execution at another plan is the same question.
@@ -161,27 +169,46 @@ async function requireReadableSchedule(execution, sessionToken) {
161
169
  throw new node_1.default.Error(node_1.default.Error.OPERATION_FORBIDDEN, "Maintenance plan not found, or not assigned to you.");
162
170
  }
163
171
  }
172
+ /** What an execution copies from its plan on create — see the hook. */
173
+ const SNAPSHOT_FIELDS = ["steps", "equipment", "title"];
164
174
  /**
165
- * Copy the plan's steps onto the execution.
175
+ * Copy the plan's steps, machine and title onto the execution, where it does
176
+ * not carry its own.
166
177
  *
167
178
  * Failure is logged, not thrown: a missing snapshot degrades the protocol to
168
179
  * what it was before this existed — readable, just not frozen — while throwing
169
180
  * would stop somebody from recording maintenance they actually performed.
170
181
  */
171
- async function freezeSteps(execution) {
182
+ async function freezePlanSnapshot(execution) {
172
183
  const schedule = execution.get("schedule");
173
184
  if (!schedule?.id)
174
185
  return;
186
+ const missing = SNAPSHOT_FIELDS.filter((field) => !present(execution.get(field)));
187
+ if (missing.length === 0)
188
+ return;
175
189
  try {
176
190
  const plan = await new node_1.default.Query(index_js_3.Service_Schedule)
177
191
  .equalTo("objectId", schedule.id)
178
192
  .first({ useMasterKey: true });
179
- const steps = plan?.get("steps");
180
- if (Array.isArray(steps) && steps.length > 0) {
181
- execution.set("steps", steps);
193
+ if (!plan)
194
+ return;
195
+ for (const field of missing) {
196
+ const value = plan.get(field);
197
+ if (present(value))
198
+ execution.set(field, value);
182
199
  }
183
200
  }
184
201
  catch (error) {
185
- console.error(`[ServiceScheduleExecution] Could not snapshot steps of plan ${schedule.id}:`, error);
202
+ console.error(`[ServiceScheduleExecution] Could not snapshot plan ${schedule.id}:`, error);
186
203
  }
187
204
  }
205
+ /** Empty steps or a blank title count as missing — on either side. */
206
+ function present(value) {
207
+ if (value === undefined || value === null)
208
+ return false;
209
+ if (Array.isArray(value))
210
+ return value.length > 0;
211
+ if (typeof value === "string")
212
+ return value.trim().length > 0;
213
+ return true;
214
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,124 @@
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
+ const node_1 = __importDefault(require("parse/node"));
7
+ const index_js_1 = require("../types/index.js");
8
+ /**
9
+ * One-off backfill for `OD3_Service_Schedule_Execution.equipment` and `title`
10
+ * on plan runs.
11
+ *
12
+ * From here on the execution hook copies both from the plan as a run is
13
+ * created (next to the steps), so a protocol stays on the machine and under the
14
+ * name it was performed with. Runs created before that carry neither — except
15
+ * the ones the open.WARE automation or the v1 migration wrote. This fills the
16
+ * gap from the plan as it reads **today**: the best that is known, and the
17
+ * value every view already showed for them.
18
+ *
19
+ * PARSE_URL=… PARSE_APP_ID=… PARSE_MASTER_KEY=… \
20
+ * node dist/scripts/backfill-execution-snapshot.js [--dry-run]
21
+ *
22
+ * Only fills what is missing; an existing value is never overwritten. Direct
23
+ * maintenance (no plan) is skipped — it brings both itself. Completed
24
+ * protocols are written too: the master key passes the hook's "completed is
25
+ * closed" guard, and this adds what the protocol already showed rather than
26
+ * changing it. No changelog entry (`skipChangelog`). Safe to re-run.
27
+ *
28
+ * Steps are deliberately left alone: a run without its own steps predates the
29
+ * snapshot (2026-09-21), and its results may reference steps the plan has
30
+ * since changed — today's steps would be a guess presented as a record.
31
+ */
32
+ const BATCH_SIZE = 500;
33
+ function requireEnv(name) {
34
+ const value = process.env[name];
35
+ if (!value) {
36
+ console.error(`Missing required environment variable ${name}.`);
37
+ process.exit(1);
38
+ }
39
+ return value;
40
+ }
41
+ /** Iterate the plan runs in `createdAt` order, page by page. */
42
+ async function* pages() {
43
+ let after;
44
+ for (;;) {
45
+ const query = new node_1.default.Query(index_js_1.Service_Schedule_Execution)
46
+ .exists("schedule")
47
+ .select("schedule", "equipment", "title")
48
+ .ascending("createdAt")
49
+ .limit(BATCH_SIZE);
50
+ // `createdAt` is not part of the generic attribute map, hence the cast.
51
+ if (after)
52
+ query.greaterThan("createdAt", after);
53
+ const results = await query.find({ useMasterKey: true });
54
+ if (results.length === 0)
55
+ return;
56
+ yield results;
57
+ after = results[results.length - 1].createdAt;
58
+ if (results.length < BATCH_SIZE)
59
+ return;
60
+ }
61
+ }
62
+ /** The plans of one page — archived ones included (master key). */
63
+ async function plansOf(executions) {
64
+ const ids = [
65
+ ...new Set(executions
66
+ .map((e) => e.get("schedule")?.id)
67
+ .filter((id) => !!id)),
68
+ ];
69
+ const plans = await new node_1.default.Query(index_js_1.Service_Schedule)
70
+ .containedIn("objectId", ids)
71
+ .select("equipment", "title")
72
+ .limit(ids.length)
73
+ .find({ useMasterKey: true });
74
+ return new Map(plans.map((plan) => [plan.id, plan]));
75
+ }
76
+ async function main() {
77
+ const dryRun = process.argv.includes("--dry-run");
78
+ node_1.default.initialize(requireEnv("PARSE_APP_ID"), undefined, requireEnv("PARSE_MASTER_KEY"));
79
+ node_1.default.serverURL =
80
+ process.env.PARSE_URL ?? "http://localhost:1337/parse";
81
+ let scanned = 0;
82
+ let equipmentSet = 0;
83
+ let titleSet = 0;
84
+ let planMissing = 0;
85
+ for await (const page of pages()) {
86
+ const plans = await plansOf(page);
87
+ for (const execution of page) {
88
+ scanned++;
89
+ const planId = execution.get("schedule")
90
+ ?.id;
91
+ const plan = planId ? plans.get(planId) : undefined;
92
+ if (!plan) {
93
+ planMissing++;
94
+ continue;
95
+ }
96
+ let dirty = false;
97
+ if (!execution.get("equipment") && plan.get("equipment")) {
98
+ execution.set("equipment", plan.get("equipment"));
99
+ equipmentSet++;
100
+ dirty = true;
101
+ }
102
+ const title = plan.get("title");
103
+ if (!execution.get("title")?.trim() && title?.trim()) {
104
+ execution.set("title", title);
105
+ titleSet++;
106
+ dirty = true;
107
+ }
108
+ if (!dirty || dryRun)
109
+ continue;
110
+ await execution.save(null, {
111
+ useMasterKey: true,
112
+ context: { skipChangelog: true },
113
+ });
114
+ }
115
+ }
116
+ console.log(`${dryRun ? "[dry run] " : ""}scanned ${scanned} plan runs, ` +
117
+ `equipment ${dryRun ? "to set" : "set"} on ${equipmentSet}, ` +
118
+ `title ${dryRun ? "to set" : "set"} on ${titleSet}` +
119
+ (planMissing ? `, ${planMissing} with a plan that no longer exists` : ""));
120
+ }
121
+ main().catch((error) => {
122
+ console.error(error);
123
+ process.exit(1);
124
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.40.0",
3
+ "version": "4.41.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.21.0",
6
6
  "keywords": [
@@ -69,6 +69,7 @@
69
69
  "backfill:meta-contexts": "node dist/scripts/backfill-meta-contexts.js",
70
70
  "backfill:changelog-search": "node dist/scripts/backfill-changelog-search.js",
71
71
  "backfill:stock-alert": "node dist/scripts/backfill-stock-alert.js",
72
+ "backfill:execution-snapshot": "node dist/scripts/backfill-execution-snapshot.js",
72
73
  "repair:ticket-acl": "node dist/scripts/repair-ticket-acl.js",
73
74
  "test:integration": "node --test --test-concurrency=1 \"test/integration/**/*.test.mjs\"",
74
75
  "test:unit": "node --test \"test/unit/**/*.test.mjs\"",