@openinc/parse-server-opendash 4.6.0 → 4.8.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/schema/helper/migrationMode.js +2 -0
- package/dist/hooks/Service_Schedule.js +7 -0
- package/dist/hooks/Service_Schedule_Template.js +5 -0
- package/dist/scripts/migration/lib/articleMeta.d.ts +34 -10
- package/dist/scripts/migration/lib/articleMeta.js +58 -11
- package/dist/scripts/migration/lib/cadence.d.ts +75 -0
- package/dist/scripts/migration/lib/cadence.js +246 -0
- package/dist/scripts/migration/lib/env.d.ts +5 -0
- package/dist/scripts/migration/lib/env.js +23 -0
- package/dist/scripts/migration/lib/equipmentMeta.d.ts +65 -0
- package/dist/scripts/migration/lib/equipmentMeta.js +155 -0
- package/dist/scripts/migration/lib/schedule.d.ts +306 -0
- package/dist/scripts/migration/lib/schedule.js +613 -0
- package/dist/scripts/migration/passes/03-equipment.js +181 -4
- package/dist/scripts/migration/passes/05-articles.d.ts +12 -15
- package/dist/scripts/migration/passes/05-articles.js +83 -33
- package/dist/scripts/migration/passes/13-schedule-templates.d.ts +15 -0
- package/dist/scripts/migration/passes/13-schedule-templates.js +163 -0
- package/dist/scripts/migration/passes/14-schedules.d.ts +30 -0
- package/dist/scripts/migration/passes/14-schedules.js +239 -0
- package/dist/scripts/migration/passes/15-executions.d.ts +34 -0
- package/dist/scripts/migration/passes/15-executions.js +244 -0
- package/dist/scripts/migration/passes/16-schedule-meta.d.ts +23 -0
- package/dist/scripts/migration/passes/16-schedule-meta.js +308 -0
- package/dist/scripts/migration/passes/17-schedule-assets.d.ts +30 -0
- package/dist/scripts/migration/passes/17-schedule-assets.js +240 -0
- package/dist/scripts/migration/passes/18-calendar-links.d.ts +25 -0
- package/dist/scripts/migration/passes/18-calendar-links.js +132 -0
- package/dist/scripts/migration/passes/19-schedule-history.d.ts +18 -0
- package/dist/scripts/migration/passes/19-schedule-history.js +186 -0
- package/dist/scripts/migration/run.js +20 -9
- package/dist/scripts/migration/verify.js +450 -12
- package/dist/scripts/seed-v1-testdata.d.ts +2 -0
- package/dist/scripts/seed-v1-testdata.js +449 -10
- package/dist/types/Service_Schedule.d.ts +3 -0
- package/dist/types/Service_Schedule.js +6 -0
- package/dist/types/Service_Schedule_Execution.d.ts +1 -1
- package/package.json +1 -1
- package/schema/Service_Schedule.json +4 -0
|
@@ -0,0 +1,308 @@
|
|
|
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.pass16 = void 0;
|
|
7
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const index_js_1 = require("../../../types/index.js");
|
|
9
|
+
const acl_js_1 = require("../lib/acl.js");
|
|
10
|
+
const pass_js_1 = require("../lib/pass.js");
|
|
11
|
+
const ptr_js_1 = require("../lib/ptr.js");
|
|
12
|
+
const schedule_js_1 = require("../lib/schedule.js");
|
|
13
|
+
const _07_meta_configs_js_1 = require("./07-meta-configs.js");
|
|
14
|
+
/**
|
|
15
|
+
* The field v1's per-step comment lands in.
|
|
16
|
+
*
|
|
17
|
+
* v1 had a comment box on every execution step; v2 has none. A comment is
|
|
18
|
+
* part of what a technician recorded, so it becomes a captured value like any
|
|
19
|
+
* other — and a capture field on the plan step it belongs to, so the protocol
|
|
20
|
+
* renders it without a config-drift warning and the next run can fill it in.
|
|
21
|
+
* The name is deliberately unlike anything a customer would define.
|
|
22
|
+
*/
|
|
23
|
+
const COMMENT_FIELD = {
|
|
24
|
+
name: "legacyComment",
|
|
25
|
+
label: "Kommentar",
|
|
26
|
+
type: "string",
|
|
27
|
+
multiline: true,
|
|
28
|
+
};
|
|
29
|
+
/** Fields by name, first definition wins. */
|
|
30
|
+
function unionFields(...lists) {
|
|
31
|
+
const byName = new Map();
|
|
32
|
+
for (const list of lists) {
|
|
33
|
+
for (const field of list) {
|
|
34
|
+
if (field?.name && !byName.has(field.name))
|
|
35
|
+
byName.set(field.name, field);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return [...byName.values()];
|
|
39
|
+
}
|
|
40
|
+
/** Era-B field configs (`{ name, type, … }`) in a v1 `fields` / `meta` array. */
|
|
41
|
+
function typedFields(raw) {
|
|
42
|
+
if (!Array.isArray(raw))
|
|
43
|
+
return [];
|
|
44
|
+
return raw
|
|
45
|
+
.filter((field) => field && !(0, schedule_js_1.isStaticField)(field) && field.type)
|
|
46
|
+
.map((field) => (0, _07_meta_configs_js_1.toMetaFieldConfig)(field))
|
|
47
|
+
.filter((field) => !!field);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Pass 16 — step capture fields → `OD3_Meta_Config`, captured values →
|
|
51
|
+
* `OD3_Meta_Entry`.
|
|
52
|
+
*
|
|
53
|
+
* v2 keys both by step id: the config under `stepMetaContext(stepId)`, one
|
|
54
|
+
* entry per execution under the same context with `entryClassName` =
|
|
55
|
+
* `OD3_Service_Schedule_Execution`. v1 went through three shapes, and all of
|
|
56
|
+
* them end up there:
|
|
57
|
+
*
|
|
58
|
+
* | v1 era | fields (config) | values |
|
|
59
|
+
* | ------------------- | ----------------------------------- | --------------------------------- |
|
|
60
|
+
* | A (… 2025-08) | static admin values — pass 13/14 put them into the step description | none captured |
|
|
61
|
+
* | B (2025-08 … 02-26) | `Schedule_Step.fields` configs | `Execution_Step.meta` `[{…, value}]` |
|
|
62
|
+
* | C (2026-02 …) | `Meta_Config` `["schedule","step","meta",id]` | `Meta_Entry` on the execution step |
|
|
63
|
+
*
|
|
64
|
+
* Plus v1's step comment, see {@link COMMENT_FIELD}.
|
|
65
|
+
*
|
|
66
|
+
* The step ids come from `lib/schedule.ts` — the same functions passes 13–15
|
|
67
|
+
* used, which is what makes an entry written here land on the step the
|
|
68
|
+
* protocol shows.
|
|
69
|
+
*/
|
|
70
|
+
exports.pass16 = {
|
|
71
|
+
id: "16",
|
|
72
|
+
title: "schedule-meta",
|
|
73
|
+
needs: ["13", "14", "15"],
|
|
74
|
+
async run({ config, map, report, entry }) {
|
|
75
|
+
const { batchSize } = config;
|
|
76
|
+
// ── v1 era C: configs and entries, keyed by v1 ids ────────────────────
|
|
77
|
+
const legacyConfigs = new Map();
|
|
78
|
+
for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Meta_Config).startsWith("context", schedule_js_1.LEGACY_STEP_META_PREFIX), batchSize)) {
|
|
79
|
+
for (const row of page) {
|
|
80
|
+
const stepId = (0, schedule_js_1.stepIdOfContext)(row.get("context") ?? "");
|
|
81
|
+
const fields = row.get("config")?.fields;
|
|
82
|
+
if (stepId && fields?.length)
|
|
83
|
+
legacyConfigs.set(stepId, fields);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const legacyEntries = new Map();
|
|
87
|
+
for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Meta_Entry)
|
|
88
|
+
.startsWith("context", schedule_js_1.LEGACY_STEP_META_PREFIX)
|
|
89
|
+
.equalTo("entryClassName", index_js_1.Maintenance_Schedule_Execution_Step.className), batchSize)) {
|
|
90
|
+
for (const row of page) {
|
|
91
|
+
const objectId = row.get("entryObjectId");
|
|
92
|
+
if (objectId)
|
|
93
|
+
legacyEntries.set(objectId, row);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// ── what already exists in v2, so a re-run updates ────────────────────
|
|
97
|
+
const existingConfigs = new Map();
|
|
98
|
+
for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Meta_Config).startsWith("context", schedule_js_1.STEP_META_PREFIX), batchSize)) {
|
|
99
|
+
for (const row of page) {
|
|
100
|
+
const context = row.get("context");
|
|
101
|
+
if (context)
|
|
102
|
+
existingConfigs.set(context, row);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const existingEntries = new Map();
|
|
106
|
+
for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Meta_Entry)
|
|
107
|
+
.startsWith("context", schedule_js_1.STEP_META_PREFIX)
|
|
108
|
+
.equalTo("entryClassName", index_js_1.Service_Schedule_Execution.className), batchSize)) {
|
|
109
|
+
for (const row of page) {
|
|
110
|
+
existingEntries.set(`${row.get("context")}|${row.get("entryObjectId")}`, row);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// ── 1. captured values, collected first ───────────────────────────────
|
|
114
|
+
// Before the configs, because a plan step needs the comment field exactly
|
|
115
|
+
// when one of its executions left a comment.
|
|
116
|
+
const plans = await schedule_js_1.PlanIndex.load(config);
|
|
117
|
+
const stepsByExecution = await (0, schedule_js_1.executionStepsByExecution)(config);
|
|
118
|
+
const pending = [];
|
|
119
|
+
const commentedSteps = new Set();
|
|
120
|
+
for await (const page of (0, pass_js_1.pages)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule_Execution), config.tenantId), batchSize)) {
|
|
121
|
+
for (const execution of page) {
|
|
122
|
+
if (!execution.id)
|
|
123
|
+
continue;
|
|
124
|
+
// Not migrated (abandoned, or failed in pass 15) — nothing to attach to.
|
|
125
|
+
const executionId = map.get(index_js_1.Maintenance_Schedule_Execution.className, execution.id);
|
|
126
|
+
if (!executionId)
|
|
127
|
+
continue;
|
|
128
|
+
const rows = stepsByExecution.get(execution.id) ?? [];
|
|
129
|
+
const ids = (0, schedule_js_1.executionStepIds)(rows, plans.resolve(execution));
|
|
130
|
+
for (const row of rows) {
|
|
131
|
+
const stepId = row.id ? ids.get(row.id) : undefined;
|
|
132
|
+
if (!stepId)
|
|
133
|
+
continue;
|
|
134
|
+
const legacy = legacyEntries.get(row.id);
|
|
135
|
+
const legacyValues = legacy?.get("values") ??
|
|
136
|
+
{};
|
|
137
|
+
// The entry's own snapshot, else the step's config it was captured
|
|
138
|
+
// against — without either the values would have no labels.
|
|
139
|
+
const snapshot = legacy?.get("config")?.fields;
|
|
140
|
+
const v1StepId = (0, pass_js_1.refId)(row.get("step"));
|
|
141
|
+
const legacyFields = snapshot?.length
|
|
142
|
+
? snapshot
|
|
143
|
+
: legacy && v1StepId
|
|
144
|
+
? (legacyConfigs.get(v1StepId) ?? [])
|
|
145
|
+
: [];
|
|
146
|
+
// Era B: the config entries with their value next to them. `meta`
|
|
147
|
+
// can hold `undefined` holes — v1's direct maintenance pushed
|
|
148
|
+
// unfiltered.
|
|
149
|
+
const eraB = row.get("meta") ?? [];
|
|
150
|
+
const eraBValues = {};
|
|
151
|
+
for (const field of eraB) {
|
|
152
|
+
const name = field?.name?.trim();
|
|
153
|
+
if (name && field.value !== undefined && field.value !== null) {
|
|
154
|
+
eraBValues[name] = field.value;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const comment = String(row.get("comment") ?? "").trim();
|
|
158
|
+
const values = {
|
|
159
|
+
...eraBValues,
|
|
160
|
+
...legacyValues,
|
|
161
|
+
...(comment ? { [COMMENT_FIELD.name]: comment } : {}),
|
|
162
|
+
};
|
|
163
|
+
if (Object.keys(values).length === 0)
|
|
164
|
+
continue;
|
|
165
|
+
if (comment)
|
|
166
|
+
commentedSteps.add(stepId);
|
|
167
|
+
pending.push({
|
|
168
|
+
context: (0, ptr_js_1.contextToString)((0, schedule_js_1.stepMetaContext)(stepId)),
|
|
169
|
+
executionId,
|
|
170
|
+
tenantId: (0, pass_js_1.refId)(row.get("tenant")) ?? (0, pass_js_1.refId)(execution.get("tenant")),
|
|
171
|
+
values,
|
|
172
|
+
fields: unionFields(legacyFields, typedFields(eraB), comment ? [COMMENT_FIELD] : []),
|
|
173
|
+
createdAt: row.createdAt,
|
|
174
|
+
updatedAt: row.updatedAt,
|
|
175
|
+
label: `execution step ${row.id}`,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// ── 2. capture fields per plan / template step ────────────────────────
|
|
181
|
+
const steps = new schedule_js_1.StepSource(batchSize);
|
|
182
|
+
const configIds = new Map();
|
|
183
|
+
let configsWritten = 0;
|
|
184
|
+
let commentFields = 0;
|
|
185
|
+
// The v2 owner's tenant, as pass 13/14 resolved it — a v1 plan may have
|
|
186
|
+
// none of its own.
|
|
187
|
+
const tenantOfOwner = new Map();
|
|
188
|
+
await tenantsOf(index_js_1.Service_Schedule, batchSize, tenantOfOwner);
|
|
189
|
+
await tenantsOf(index_js_1.Service_Schedule_Template, batchSize, tenantOfOwner);
|
|
190
|
+
const writeConfig = async (row, stepId, ownerId, label) => {
|
|
191
|
+
const fields = unionFields(legacyConfigs.get(row.id) ?? typedFields(row.get("fields")), commentedSteps.has(stepId) ? [COMMENT_FIELD] : []);
|
|
192
|
+
if (fields.length === 0)
|
|
193
|
+
return;
|
|
194
|
+
if (commentedSteps.has(stepId))
|
|
195
|
+
commentFields += 1;
|
|
196
|
+
const context = (0, ptr_js_1.contextToString)((0, schedule_js_1.stepMetaContext)(stepId));
|
|
197
|
+
const existing = existingConfigs.get(context);
|
|
198
|
+
const tenantId = tenantOfOwner.get(ownerId);
|
|
199
|
+
const target = existing ?? new index_js_1.Meta_Config();
|
|
200
|
+
target.set("context", context);
|
|
201
|
+
target.set("config", { fields });
|
|
202
|
+
if (tenantId)
|
|
203
|
+
target.set("tenant", (0, ptr_js_1.tenantPtr)(tenantId));
|
|
204
|
+
// What `defaultAclHandler` gives a Meta_Config: the tenant reads it,
|
|
205
|
+
// its admins edit it.
|
|
206
|
+
if (!existing)
|
|
207
|
+
target.setACL((0, acl_js_1.migrationAcl)({ tenantId }));
|
|
208
|
+
if (config.dryRun) {
|
|
209
|
+
existing ? entry.updated++ : entry.created++;
|
|
210
|
+
configsWritten += 1;
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
const id = await (0, acl_js_1.saveMigrated)(config, target, {
|
|
215
|
+
className: index_js_1.Meta_Config.className,
|
|
216
|
+
isNew: !existing,
|
|
217
|
+
createdAt: row.createdAt,
|
|
218
|
+
updatedAt: row.updatedAt,
|
|
219
|
+
});
|
|
220
|
+
configIds.set(context, id);
|
|
221
|
+
existing ? entry.updated++ : entry.created++;
|
|
222
|
+
configsWritten += 1;
|
|
223
|
+
}
|
|
224
|
+
catch (error) {
|
|
225
|
+
report.error(entry, `${label} config failed: ${error.message}`);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
for (const template of await (0, pass_js_1.all)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule_Template), config.tenantId), batchSize)) {
|
|
229
|
+
const ownerId = template.id
|
|
230
|
+
? map.get(index_js_1.Maintenance_Schedule_Template.className, template.id)
|
|
231
|
+
: undefined;
|
|
232
|
+
if (!ownerId)
|
|
233
|
+
continue;
|
|
234
|
+
for (const row of await steps.of(template)) {
|
|
235
|
+
await writeConfig(row, (0, schedule_js_1.templateStepId)(template.id, row.id), ownerId, `template ${template.id} step ${row.id}`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
for (const plan of await (0, pass_js_1.all)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule), config.tenantId), batchSize)) {
|
|
239
|
+
const ownerId = plan.id
|
|
240
|
+
? map.get(index_js_1.Maintenance_Schedule.className, plan.id)
|
|
241
|
+
: undefined;
|
|
242
|
+
if (!ownerId)
|
|
243
|
+
continue;
|
|
244
|
+
const { rows } = await steps.ofPlan(plan);
|
|
245
|
+
for (const row of rows) {
|
|
246
|
+
await writeConfig(row, (0, schedule_js_1.planStepId)(plan.id, row.id), ownerId, `plan ${plan.id} step ${row.id}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// ── 3. the entries ────────────────────────────────────────────────────
|
|
250
|
+
let entriesWritten = 0;
|
|
251
|
+
for (const item of pending) {
|
|
252
|
+
const key = `${item.context}|${item.executionId}`;
|
|
253
|
+
const existing = existingEntries.get(key);
|
|
254
|
+
const target = existing ?? new index_js_1.Meta_Entry();
|
|
255
|
+
target.set("context", item.context);
|
|
256
|
+
target.set("entryClassName", index_js_1.Service_Schedule_Execution.className);
|
|
257
|
+
target.set("entryObjectId", item.executionId);
|
|
258
|
+
target.set("values", item.values);
|
|
259
|
+
// An entry carries a snapshot of the fields it was captured against,
|
|
260
|
+
// so it still renders after the plan's fields change.
|
|
261
|
+
target.set("config", { fields: item.fields });
|
|
262
|
+
const originId = configIds.get(item.context) ?? existingConfigs.get(item.context)?.id;
|
|
263
|
+
const origin = (0, ptr_js_1.maybePtr)(index_js_1.Meta_Config.className, originId);
|
|
264
|
+
if (origin)
|
|
265
|
+
target.set("origin", origin);
|
|
266
|
+
if (item.tenantId)
|
|
267
|
+
target.set("tenant", (0, ptr_js_1.tenantPtr)(item.tenantId));
|
|
268
|
+
if (!existing) {
|
|
269
|
+
target.setACL((0, acl_js_1.migrationAcl)({ tenantId: item.tenantId, allowTenantUserWrite: true }));
|
|
270
|
+
}
|
|
271
|
+
if (config.dryRun) {
|
|
272
|
+
existing ? entry.updated++ : entry.created++;
|
|
273
|
+
entriesWritten += 1;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
await (0, acl_js_1.saveMigrated)(config, target, {
|
|
278
|
+
className: index_js_1.Meta_Entry.className,
|
|
279
|
+
isNew: !existing,
|
|
280
|
+
createdAt: item.createdAt,
|
|
281
|
+
updatedAt: item.updatedAt,
|
|
282
|
+
});
|
|
283
|
+
existing ? entry.updated++ : entry.created++;
|
|
284
|
+
entriesWritten += 1;
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
report.error(entry, `${item.label} values failed: ${error.message}`);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
report.info(entry, `${configsWritten} step field config(s), ${entriesWritten} captured ` +
|
|
291
|
+
`value set(s); ${commentedSteps.size} step(s) carry v1 comments ` +
|
|
292
|
+
`(${commentFields} plan step(s) gained a "${COMMENT_FIELD.label}" field)`);
|
|
293
|
+
if (legacyEntries.size > 0 || legacyConfigs.size > 0) {
|
|
294
|
+
report.info(entry, `read ${legacyConfigs.size} v1 step config(s) and ${legacyEntries.size} ` +
|
|
295
|
+
`v1 value set(s) from the 2026 meta store`);
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
};
|
|
299
|
+
/** objectId → tenant id of every row of a v2 class. */
|
|
300
|
+
async function tenantsOf(cls, batchSize, into) {
|
|
301
|
+
for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(cls), batchSize)) {
|
|
302
|
+
for (const row of page) {
|
|
303
|
+
const tenantId = (0, pass_js_1.refId)(row.get("tenant"));
|
|
304
|
+
if (row.id && tenantId)
|
|
305
|
+
into.set(row.id, tenantId);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Pass } from "../lib/pass.js";
|
|
2
|
+
/**
|
|
3
|
+
* Pass 17 — files of plans, steps and executions → the v2 asset contexts.
|
|
4
|
+
*
|
|
5
|
+
* v2 keeps two kinds of file for maintenance, both in `OD3_Assets`:
|
|
6
|
+
*
|
|
7
|
+
* - **machine documents** — manuals, instructions — under the *machine*
|
|
8
|
+
* (`od3-service-equipment-<id>`, per plan when it has no machine). v1's plan
|
|
9
|
+
* documents (`schedule-media-<plan>`) and step attachments
|
|
10
|
+
* (`schedule-step-media-<step>`, and the older `Schedule_Step.media`
|
|
11
|
+
* relation) are instructions, so they go there. v2 has no per-step files.
|
|
12
|
+
* - **execution media** — photos of the work — under the execution
|
|
13
|
+
* (`od3-service-execution-<id>`). v1's `schedule-execution-<id>` and the
|
|
14
|
+
* older `Execution.media` relation.
|
|
15
|
+
*
|
|
16
|
+
* Existing asset rows are re-addressed in place, like pass 11 does for
|
|
17
|
+
* tickets: the file stays where it is, only `context` changes. A
|
|
18
|
+
* `Maintenance_Media` file has no asset row yet and gets one.
|
|
19
|
+
*
|
|
20
|
+
* Not read: `Execution_Step.media`. v1 filled it only by copying the step's
|
|
21
|
+
* own media when a run started (`hooks/Maintenance_Schedule_Execution.ts`),
|
|
22
|
+
* so it holds the step's instructions a second time, not photos.
|
|
23
|
+
*
|
|
24
|
+
* A v1 step can belong to several plans on different machines, but an asset
|
|
25
|
+
* row has one context. It goes to the first plan's machine; the others are
|
|
26
|
+
* named in a warning. Copying the row would not help: deleting an asset
|
|
27
|
+
* destroys its file (`hooks/Assets.ts`), so two rows on one file means one
|
|
28
|
+
* deletion breaks the other.
|
|
29
|
+
*/
|
|
30
|
+
export declare const pass17: Pass;
|
|
@@ -0,0 +1,240 @@
|
|
|
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.pass17 = void 0;
|
|
7
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const index_js_1 = require("../../../types/index.js");
|
|
9
|
+
const acl_js_1 = require("../lib/acl.js");
|
|
10
|
+
const latest_js_1 = require("../lib/latest.js");
|
|
11
|
+
const pass_js_1 = require("../lib/pass.js");
|
|
12
|
+
const ptr_js_1 = require("../lib/ptr.js");
|
|
13
|
+
const schedule_js_1 = require("../lib/schedule.js");
|
|
14
|
+
/** v1's asset contexts (`plugins/openservice/.../parse-config.tsx`). */
|
|
15
|
+
const PLAN_PREFIX = "schedule-media-";
|
|
16
|
+
const STEP_PREFIX = "schedule-step-media-";
|
|
17
|
+
const EXECUTION_PREFIX = "schedule-execution-";
|
|
18
|
+
/**
|
|
19
|
+
* Pass 17 — files of plans, steps and executions → the v2 asset contexts.
|
|
20
|
+
*
|
|
21
|
+
* v2 keeps two kinds of file for maintenance, both in `OD3_Assets`:
|
|
22
|
+
*
|
|
23
|
+
* - **machine documents** — manuals, instructions — under the *machine*
|
|
24
|
+
* (`od3-service-equipment-<id>`, per plan when it has no machine). v1's plan
|
|
25
|
+
* documents (`schedule-media-<plan>`) and step attachments
|
|
26
|
+
* (`schedule-step-media-<step>`, and the older `Schedule_Step.media`
|
|
27
|
+
* relation) are instructions, so they go there. v2 has no per-step files.
|
|
28
|
+
* - **execution media** — photos of the work — under the execution
|
|
29
|
+
* (`od3-service-execution-<id>`). v1's `schedule-execution-<id>` and the
|
|
30
|
+
* older `Execution.media` relation.
|
|
31
|
+
*
|
|
32
|
+
* Existing asset rows are re-addressed in place, like pass 11 does for
|
|
33
|
+
* tickets: the file stays where it is, only `context` changes. A
|
|
34
|
+
* `Maintenance_Media` file has no asset row yet and gets one.
|
|
35
|
+
*
|
|
36
|
+
* Not read: `Execution_Step.media`. v1 filled it only by copying the step's
|
|
37
|
+
* own media when a run started (`hooks/Maintenance_Schedule_Execution.ts`),
|
|
38
|
+
* so it holds the step's instructions a second time, not photos.
|
|
39
|
+
*
|
|
40
|
+
* A v1 step can belong to several plans on different machines, but an asset
|
|
41
|
+
* row has one context. It goes to the first plan's machine; the others are
|
|
42
|
+
* named in a warning. Copying the row would not help: deleting an asset
|
|
43
|
+
* destroys its file (`hooks/Assets.ts`), so two rows on one file means one
|
|
44
|
+
* deletion breaks the other.
|
|
45
|
+
*/
|
|
46
|
+
exports.pass17 = {
|
|
47
|
+
id: "17",
|
|
48
|
+
title: "schedule-assets",
|
|
49
|
+
needs: ["03", "14", "15"],
|
|
50
|
+
async run({ config, map, report, entry }) {
|
|
51
|
+
const { batchSize } = config;
|
|
52
|
+
// ── targets: plans, and the plans each step belongs to ────────────────
|
|
53
|
+
const plans = await (0, pass_js_1.all)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule), config.tenantId), batchSize);
|
|
54
|
+
const planTargets = new Map();
|
|
55
|
+
const stepTargets = new Map();
|
|
56
|
+
const stepRows = new Map();
|
|
57
|
+
const steps = new schedule_js_1.StepSource(batchSize);
|
|
58
|
+
for (const plan of plans) {
|
|
59
|
+
const planId = plan.id
|
|
60
|
+
? map.get(index_js_1.Maintenance_Schedule.className, plan.id)
|
|
61
|
+
: undefined;
|
|
62
|
+
if (!planId)
|
|
63
|
+
continue;
|
|
64
|
+
const v1SourceId = (0, pass_js_1.refId)(plan.get("source"));
|
|
65
|
+
const tenantId = (0, pass_js_1.refId)(plan.get("tenant"));
|
|
66
|
+
const target = {
|
|
67
|
+
context: (0, schedule_js_1.machineDocsContext)({
|
|
68
|
+
equipmentId: v1SourceId
|
|
69
|
+
? map.get(index_js_1.Source.className, v1SourceId)
|
|
70
|
+
: undefined,
|
|
71
|
+
scheduleId: planId,
|
|
72
|
+
}),
|
|
73
|
+
tenantId,
|
|
74
|
+
creatorId: (0, schedule_js_1.readV1Access)(plan, tenantId).creatorId,
|
|
75
|
+
};
|
|
76
|
+
planTargets.set(plan.id, target);
|
|
77
|
+
for (const row of (await steps.ofPlan(plan)).rows) {
|
|
78
|
+
stepRows.set(row.id, row);
|
|
79
|
+
const list = stepTargets.get(row.id) ?? [];
|
|
80
|
+
if (!list.some((t) => t.context === target.context))
|
|
81
|
+
list.push(target);
|
|
82
|
+
stepTargets.set(row.id, list);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const executionTarget = (v1ExecutionId) => {
|
|
86
|
+
const id = map.get(index_js_1.Maintenance_Schedule_Execution.className, v1ExecutionId);
|
|
87
|
+
return id ? (0, schedule_js_1.executionMediaContext)(id) : undefined;
|
|
88
|
+
};
|
|
89
|
+
// Steps whose files had more than one machine to go to — noted only when a
|
|
90
|
+
// file actually moves, not for every shared step.
|
|
91
|
+
const shared = new Set();
|
|
92
|
+
const stepTarget = (v1StepId) => stepTargets.get(v1StepId)?.[0];
|
|
93
|
+
const noteShared = (v1StepId) => {
|
|
94
|
+
if ((stepTargets.get(v1StepId)?.length ?? 0) > 1)
|
|
95
|
+
shared.add(v1StepId);
|
|
96
|
+
};
|
|
97
|
+
// ── 1. asset rows on v1 contexts: re-address ──────────────────────────
|
|
98
|
+
// Read completely before writing: rewriting `context` takes a row out of
|
|
99
|
+
// the `startsWith` result, and paging a shrinking result skips rows (see
|
|
100
|
+
// pass 11).
|
|
101
|
+
const assets = await (0, pass_js_1.all)(() => new node_1.default.Query(index_js_1.Assets).startsWith("context", "schedule-"), batchSize);
|
|
102
|
+
let unmapped = 0;
|
|
103
|
+
let rewritten = 0;
|
|
104
|
+
for (const asset of assets) {
|
|
105
|
+
const context = asset.get("context") ?? "";
|
|
106
|
+
let next;
|
|
107
|
+
if (context.startsWith(STEP_PREFIX)) {
|
|
108
|
+
const v1StepId = context.slice(STEP_PREFIX.length);
|
|
109
|
+
next = stepTarget(v1StepId)?.context;
|
|
110
|
+
if (next)
|
|
111
|
+
noteShared(v1StepId);
|
|
112
|
+
}
|
|
113
|
+
else if (context.startsWith(PLAN_PREFIX)) {
|
|
114
|
+
next = planTargets.get(context.slice(PLAN_PREFIX.length))?.context;
|
|
115
|
+
}
|
|
116
|
+
else if (context.startsWith(EXECUTION_PREFIX)) {
|
|
117
|
+
next = executionTarget(context.slice(EXECUTION_PREFIX.length));
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
continue; // some other feature's "schedule-…" context
|
|
121
|
+
}
|
|
122
|
+
if (!next) {
|
|
123
|
+
// Template-only steps, plans or executions outside --tenant, or an
|
|
124
|
+
// execution pass 15 did not migrate (abandoned).
|
|
125
|
+
unmapped += 1;
|
|
126
|
+
entry.skipped++;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (config.dryRun) {
|
|
130
|
+
entry.updated++;
|
|
131
|
+
rewritten += 1;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
asset.set("context", next);
|
|
135
|
+
try {
|
|
136
|
+
// Only the context changes; file, owner, ACL and timestamps stay.
|
|
137
|
+
await asset.save(null, acl_js_1.MIGRATION_SAVE);
|
|
138
|
+
entry.updated++;
|
|
139
|
+
rewritten += 1;
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
report.error(entry, `asset ${asset.id} context rewrite failed: ${error.message}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// ── 2. Maintenance_Media files: new asset rows ────────────────────────
|
|
146
|
+
let created = 0;
|
|
147
|
+
// One asset per file, also in a dry run (where the map records nothing).
|
|
148
|
+
const handled = new Set();
|
|
149
|
+
const createAsset = async (media, context, owner, label) => {
|
|
150
|
+
if (!media.id || handled.has(media.id))
|
|
151
|
+
return;
|
|
152
|
+
handled.add(media.id);
|
|
153
|
+
if (map.get(index_js_1.Maintenance_Media.className, media.id))
|
|
154
|
+
return;
|
|
155
|
+
const file = media.get("media");
|
|
156
|
+
if (!file?.url()) {
|
|
157
|
+
report.warn(entry, `${label}: media ${media.id} has no file — skipped`);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (!owner.userId) {
|
|
161
|
+
// `OD3_Assets.user` is required.
|
|
162
|
+
report.warn(entry, `${label}: media ${media.id} has nobody to own it (Assets.user is ` +
|
|
163
|
+
`required) — left in OD3_Maintenance_Media`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const tenantId = owner.tenantId ?? (0, pass_js_1.refId)(media.get("tenant"));
|
|
167
|
+
if (config.dryRun) {
|
|
168
|
+
entry.created++;
|
|
169
|
+
created += 1;
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const asset = new index_js_1.Assets();
|
|
173
|
+
asset.set("context", context);
|
|
174
|
+
asset.set("description", media.get("description") || file.name() || "Datei");
|
|
175
|
+
asset.set("file", file);
|
|
176
|
+
asset.set("user", (0, ptr_js_1.userPtr)(owner.userId));
|
|
177
|
+
if (tenantId)
|
|
178
|
+
asset.set("tenant", (0, ptr_js_1.tenantPtr)(tenantId));
|
|
179
|
+
asset.setACL((0, acl_js_1.migrationAcl)({
|
|
180
|
+
tenantId,
|
|
181
|
+
userId: owner.userId,
|
|
182
|
+
allowTenantUserWrite: true,
|
|
183
|
+
}));
|
|
184
|
+
try {
|
|
185
|
+
const id = await (0, acl_js_1.saveMigrated)(config, asset, {
|
|
186
|
+
className: index_js_1.Assets.className,
|
|
187
|
+
isNew: true,
|
|
188
|
+
createdAt: media.createdAt,
|
|
189
|
+
updatedAt: media.updatedAt,
|
|
190
|
+
});
|
|
191
|
+
await map.record(index_js_1.Maintenance_Media.className, media.id, index_js_1.Assets.className, id);
|
|
192
|
+
entry.created++;
|
|
193
|
+
created += 1;
|
|
194
|
+
}
|
|
195
|
+
catch (error) {
|
|
196
|
+
report.error(entry, `${label}: media ${media.id} failed: ${error.message}`);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
// Step attachments, onto the machine of the (first) plan with the step.
|
|
200
|
+
for (const [v1StepId, row] of stepRows) {
|
|
201
|
+
const target = stepTarget(v1StepId);
|
|
202
|
+
if (!target)
|
|
203
|
+
continue;
|
|
204
|
+
const files = (await (0, latest_js_1.relationObjects)(row, "media", batchSize));
|
|
205
|
+
if (files.length)
|
|
206
|
+
noteShared(v1StepId);
|
|
207
|
+
for (const media of files) {
|
|
208
|
+
await createAsset(media, target.context, { tenantId: target.tenantId, userId: target.creatorId }, `step ${v1StepId}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// Execution photos. The scheduler's `convertMediaToAssets` job moved most
|
|
212
|
+
// of these already and emptied the relation; this is what it left.
|
|
213
|
+
for await (const page of (0, pass_js_1.pages)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule_Execution), config.tenantId), batchSize)) {
|
|
214
|
+
for (const execution of page) {
|
|
215
|
+
const context = execution.id
|
|
216
|
+
? executionTarget(execution.id)
|
|
217
|
+
: undefined;
|
|
218
|
+
if (!context)
|
|
219
|
+
continue;
|
|
220
|
+
for (const media of (await (0, latest_js_1.relationObjects)(execution, "media", batchSize))) {
|
|
221
|
+
await createAsset(media, context, {
|
|
222
|
+
tenantId: (0, pass_js_1.refId)(execution.get("tenant")),
|
|
223
|
+
userId: (0, pass_js_1.refId)(execution.get("user")),
|
|
224
|
+
}, `execution ${execution.id}`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
report.info(entry, `${rewritten} asset(s) re-addressed, ${created} Maintenance_Media file(s) ` +
|
|
229
|
+
`became assets`);
|
|
230
|
+
if (unmapped > 0) {
|
|
231
|
+
report.info(entry, `${unmapped} asset(s) on a v1 schedule context with no migrated owner ` +
|
|
232
|
+
`(template-only steps, other tenants, abandoned runs) — left as they are`);
|
|
233
|
+
}
|
|
234
|
+
if (shared.size > 0) {
|
|
235
|
+
report.warn(entry, `${shared.size} step(s) with files belong to plans on different ` +
|
|
236
|
+
`machines — their files went to the first plan's machine only: ` +
|
|
237
|
+
`${[...shared].slice(0, 10).join(", ")}`);
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Pass } from "../lib/pass.js";
|
|
2
|
+
/**
|
|
3
|
+
* Pass 18 — hand each v1 plan's Outlook event over to its v2 plan.
|
|
4
|
+
*
|
|
5
|
+
* Both generations remember "this plan is that calendar event" as a
|
|
6
|
+
* `Meta_Entry` — v1 under `["schedule","sync","microsoft"]` for
|
|
7
|
+
* `OD3_Maintenance_Schedule`, v2 under `["service","schedule","sync",
|
|
8
|
+
* "microsoft"]` for `OD3_Service_Schedule`. One Outlook connection serves
|
|
9
|
+
* both, and the event itself stays where it is.
|
|
10
|
+
*
|
|
11
|
+
* Without this pass a migrated plan has no event as far as v2 knows, so the
|
|
12
|
+
* first edit in v2 (`syncPlanToCalendar`) creates a *second* event next to
|
|
13
|
+
* the one v1 made, and the v2 poller, once re-enabled, imports v1's event as
|
|
14
|
+
* a third plan. Carrying the id, the `changeKey` and the event snapshot over
|
|
15
|
+
* makes the next v2 save update the existing event instead.
|
|
16
|
+
*
|
|
17
|
+
* An event already linked to a *different* v2 plan (the v2 poller imported
|
|
18
|
+
* it before this ran) is not relinked: that is a duplicate plan for a human
|
|
19
|
+
* to merge, and it is reported.
|
|
20
|
+
*
|
|
21
|
+
* Must run after the plans exist. Needs `OD_MIGRATION_MODE` on the server
|
|
22
|
+
* like every pass — here also because it keeps the plan afterSave from
|
|
23
|
+
* pushing migrated plans to Outlook before their link arrives.
|
|
24
|
+
*/
|
|
25
|
+
export declare const pass18: Pass;
|