@openinc/parse-server-opendash 4.7.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.
Files changed (33) hide show
  1. package/dist/features/schema/helper/migrationMode.js +2 -0
  2. package/dist/hooks/Service_Schedule.js +7 -0
  3. package/dist/hooks/Service_Schedule_Template.js +5 -0
  4. package/dist/scripts/migration/lib/cadence.d.ts +75 -0
  5. package/dist/scripts/migration/lib/cadence.js +246 -0
  6. package/dist/scripts/migration/lib/env.d.ts +5 -0
  7. package/dist/scripts/migration/lib/env.js +23 -0
  8. package/dist/scripts/migration/lib/schedule.d.ts +306 -0
  9. package/dist/scripts/migration/lib/schedule.js +613 -0
  10. package/dist/scripts/migration/passes/03-equipment.js +11 -4
  11. package/dist/scripts/migration/passes/13-schedule-templates.d.ts +15 -0
  12. package/dist/scripts/migration/passes/13-schedule-templates.js +163 -0
  13. package/dist/scripts/migration/passes/14-schedules.d.ts +30 -0
  14. package/dist/scripts/migration/passes/14-schedules.js +239 -0
  15. package/dist/scripts/migration/passes/15-executions.d.ts +34 -0
  16. package/dist/scripts/migration/passes/15-executions.js +244 -0
  17. package/dist/scripts/migration/passes/16-schedule-meta.d.ts +23 -0
  18. package/dist/scripts/migration/passes/16-schedule-meta.js +308 -0
  19. package/dist/scripts/migration/passes/17-schedule-assets.d.ts +30 -0
  20. package/dist/scripts/migration/passes/17-schedule-assets.js +240 -0
  21. package/dist/scripts/migration/passes/18-calendar-links.d.ts +25 -0
  22. package/dist/scripts/migration/passes/18-calendar-links.js +132 -0
  23. package/dist/scripts/migration/passes/19-schedule-history.d.ts +18 -0
  24. package/dist/scripts/migration/passes/19-schedule-history.js +186 -0
  25. package/dist/scripts/migration/run.js +20 -9
  26. package/dist/scripts/migration/verify.js +294 -11
  27. package/dist/scripts/seed-v1-testdata.d.ts +2 -0
  28. package/dist/scripts/seed-v1-testdata.js +392 -6
  29. package/dist/types/Service_Schedule.d.ts +3 -0
  30. package/dist/types/Service_Schedule.js +6 -0
  31. package/dist/types/Service_Schedule_Execution.d.ts +1 -1
  32. package/package.json +1 -1
  33. package/schema/Service_Schedule.json +4 -0
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const node_1 = __importDefault(require("parse/node"));
7
+ const serviceCalendarMeta_js_1 = require("../../features/openservice/serviceSchedules/calendarSync/serviceCalendarMeta.js");
7
8
  const index_js_1 = require("../../types/index.js");
8
9
  const articleMeta_js_1 = require("./lib/articleMeta.js");
9
10
  const equipmentMeta_js_1 = require("./lib/equipmentMeta.js");
@@ -11,6 +12,7 @@ const env_js_1 = require("./lib/env.js");
11
12
  const message_js_1 = require("./lib/message.js");
12
13
  const pass_js_1 = require("./lib/pass.js");
13
14
  const ptr_js_1 = require("./lib/ptr.js");
15
+ const schedule_js_1 = require("./lib/schedule.js");
14
16
  /** Pointer/array-of-ids fields to resolve, and the class they must resolve into. */
15
17
  const REFERENCE_FIELDS = [
16
18
  { field: "status", className: index_js_1.Service_Status.className },
@@ -68,6 +70,75 @@ function comparable(value) {
68
70
  };
69
71
  return JSON.stringify(norm(value));
70
72
  }
73
+ /** The v2 ids the migration recorded for a v1 class. */
74
+ async function migratedTargets(sourceClass, batchSize) {
75
+ const ids = new Set();
76
+ for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query("OD3_Service_MigrationMap").equalTo("sourceClass", sourceClass), batchSize)) {
77
+ for (const row of page)
78
+ ids.add(row.get("targetId"));
79
+ }
80
+ return ids;
81
+ }
82
+ /** objectId → tenant id of every row of a class. */
83
+ async function tenantsById(cls, batchSize) {
84
+ const tenants = new Map();
85
+ for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(cls), batchSize)) {
86
+ for (const row of page) {
87
+ if (row.id)
88
+ tenants.set(row.id, (0, pass_js_1.refId)(row.get("tenant")));
89
+ }
90
+ }
91
+ return tenants;
92
+ }
93
+ /**
94
+ * What is wrong with a cadence, as the v2 plan editor and `@opendash/schedule`
95
+ * read one. An `RRULE:` prefix in particular computes fine but breaks the
96
+ * editor, which would corrupt the rule on the next save.
97
+ */
98
+ function cadenceProblems(cadence) {
99
+ if (!cadence || typeof cadence !== "object")
100
+ return ["no cadence"];
101
+ const c = cadence;
102
+ if (c.kind === "manual")
103
+ return [];
104
+ if (c.kind !== "rrule")
105
+ return [`cadence kind ${String(c.kind)}`];
106
+ const problems = [];
107
+ const rule = typeof c.rrule === "string" ? c.rrule : "";
108
+ if (!rule.includes("FREQ="))
109
+ problems.push("rrule without FREQ");
110
+ if (/RRULE:|DTSTART/.test(rule))
111
+ problems.push("rrule carries a prefix/DTSTART");
112
+ const isDate = (v) => typeof v === "string" && !Number.isNaN(Date.parse(v));
113
+ if (!isDate(c.dtstart))
114
+ problems.push("dtstart not an ISO date");
115
+ if (c.until !== undefined && !isDate(c.until)) {
116
+ problems.push("until not an ISO date");
117
+ }
118
+ if (typeof c.timezone !== "string")
119
+ problems.push("no timezone");
120
+ return problems;
121
+ }
122
+ /** What is wrong with a `steps` array: ids must be unique, titles present. */
123
+ function stepProblems(steps) {
124
+ if (steps === undefined)
125
+ return [];
126
+ if (!Array.isArray(steps))
127
+ return ["steps not a list"];
128
+ const problems = [];
129
+ const ids = new Set();
130
+ for (const step of steps) {
131
+ if (typeof step?.id !== "string" || !step.id)
132
+ problems.push("step without id");
133
+ else if (ids.has(step.id))
134
+ problems.push(`duplicate step id ${step.id}`);
135
+ else
136
+ ids.add(step.id);
137
+ if (!step?.title)
138
+ problems.push("step without title");
139
+ }
140
+ return problems;
141
+ }
71
142
  const checks = [
72
143
  // ── Counts ───────────────────────────────────────────────────────────────
73
144
  {
@@ -478,24 +549,36 @@ const checks = [
478
549
  // Under the master key Parse silently stamps `createdAt = now`, which would
479
550
  // collapse the whole trail onto the migration date. This is the check that
480
551
  // catches a run that used the wrong key.
552
+ //
553
+ // Judged per class: pass 12 (tickets) and pass 19 (plans, executions)
554
+ // can run separately, and one class with genuine history must not hide
555
+ // another whose history collapsed.
481
556
  const cutoff = Date.now() - 60 * 60 * 1000;
482
- let total = 0;
483
- let recent = 0;
484
- for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Changelog).equalTo("nameOfClass", index_js_1.Service_Ticket.className), batchSize)) {
557
+ const tally = new Map();
558
+ for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Changelog).containedIn("nameOfClass", [
559
+ index_js_1.Service_Ticket.className,
560
+ index_js_1.Service_Schedule.className,
561
+ index_js_1.Service_Schedule_Execution.className,
562
+ ]), batchSize)) {
485
563
  for (const row of page) {
486
564
  const context = row.get("context");
487
565
  if (!context?.migrated)
488
566
  continue;
489
- total += 1;
567
+ const cls = row.get("nameOfClass");
568
+ const counts = tally.get(cls) ?? { total: 0, recent: 0 };
569
+ counts.total += 1;
490
570
  if ((row.createdAt?.getTime() ?? 0) > cutoff)
491
- recent += 1;
571
+ counts.recent += 1;
572
+ tally.set(cls, counts);
492
573
  }
493
574
  }
494
- if (total === 0)
495
- return undefined;
496
- return recent === total
497
- ? `all ${total} reconstructed entries were created within the last hour — ` +
498
- `timestamps were NOT preserved (was the maintenance key actually used?)`
575
+ const collapsed = [...tally].filter(([, { total, recent }]) => total > 0 && recent === total);
576
+ return collapsed.length
577
+ ? collapsed
578
+ .map(([cls, { total }]) => `all ${total} reconstructed ${cls} entries were created within ` +
579
+ `the last hour`)
580
+ .join("; ") +
581
+ ` — timestamps were NOT preserved (was the maintenance key actually used?)`
499
582
  : undefined;
500
583
  },
501
584
  },
@@ -528,12 +611,15 @@ const checks = [
528
611
  index_js_1.Service_Tag.className,
529
612
  index_js_1.Service_Equipment.className,
530
613
  index_js_1.Service_Project.className,
614
+ index_js_1.Service_Schedule.className,
615
+ index_js_1.Service_Schedule_Template.className,
616
+ index_js_1.Service_Schedule_Execution.className,
531
617
  ])
532
618
  .notEqualTo("masterkey", true), batchSize)) {
533
619
  for (const row of page) {
534
620
  const context = row.get("context");
535
621
  if (context?.migrated)
536
- continue; // written by pass 12, on purpose
622
+ continue; // written by pass 12 / 19, on purpose
537
623
  if (row.get("actingUser"))
538
624
  continue; // a real edit by a real user
539
625
  total += 1;
@@ -678,6 +764,203 @@ const checks = [
678
764
  : undefined;
679
765
  },
680
766
  },
767
+ // ── Schedules ────────────────────────────────────────────────────────────
768
+ {
769
+ name: "every v1 schedule template was migrated",
770
+ hard: true,
771
+ async run(tenantId, batchSize) {
772
+ const [v1, migrated] = await Promise.all([
773
+ (0, pass_js_1.countAll)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule_Template), tenantId), batchSize),
774
+ migratedCount(index_js_1.Maintenance_Schedule_Template.className, batchSize),
775
+ ]);
776
+ return migrated >= v1
777
+ ? undefined
778
+ : `${v1} v1 template(s) but only ${migrated} mapping(s)`;
779
+ },
780
+ },
781
+ {
782
+ name: "every v1 maintenance plan was migrated",
783
+ hard: true,
784
+ async run(tenantId, batchSize) {
785
+ const [v1, migrated] = await Promise.all([
786
+ (0, pass_js_1.countAll)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Maintenance_Schedule), tenantId), batchSize),
787
+ migratedCount(index_js_1.Maintenance_Schedule.className, batchSize),
788
+ ]);
789
+ return migrated >= v1
790
+ ? undefined
791
+ : `${v1} v1 plan(s) but only ${migrated} mapping(s)`;
792
+ },
793
+ },
794
+ {
795
+ name: "every v1 execution was migrated, except the abandoned runs",
796
+ hard: true,
797
+ async run(tenantId, batchSize) {
798
+ // The same classification pass 15 applies — imported, so the check
799
+ // cannot disagree with the pass it checks.
800
+ const scope = { tenantId, batchSize };
801
+ const plans = await schedule_js_1.PlanIndex.load(scope);
802
+ const stepsByExecution = await (0, schedule_js_1.executionStepsByExecution)(scope);
803
+ let expected = 0;
804
+ let abandoned = 0;
805
+ 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), tenantId), batchSize)) {
806
+ for (const execution of page) {
807
+ const fate = (0, schedule_js_1.classifyExecution)(execution, stepsByExecution.get(execution.id ?? "") ?? [], !!plans.resolve(execution));
808
+ if (fate.fate === "abandoned")
809
+ abandoned += 1;
810
+ else
811
+ expected += 1;
812
+ }
813
+ }
814
+ const migrated = await migratedCount(index_js_1.Maintenance_Schedule_Execution.className, batchSize);
815
+ return migrated >= expected
816
+ ? undefined
817
+ : `${expected} v1 execution(s) to migrate (${abandoned} abandoned left ` +
818
+ `out) but only ${migrated} mapping(s)`;
819
+ },
820
+ },
821
+ {
822
+ name: "every migrated plan and template has a readable cadence and steps",
823
+ hard: true,
824
+ async run(tenantId, batchSize) {
825
+ const bad = [];
826
+ const inspect = (row) => {
827
+ const problems = [
828
+ ...cadenceProblems(row.get("cadence")),
829
+ ...stepProblems(row.get("steps")),
830
+ ];
831
+ if (!row.get("title"))
832
+ problems.push("no title");
833
+ if (!(0, pass_js_1.refId)(row.get("tenant")))
834
+ problems.push("no tenant");
835
+ if (problems.length && bad.length < 10) {
836
+ bad.push(`${row.className} ${row.id}: ${problems.join(", ")}`);
837
+ }
838
+ };
839
+ for await (const page of (0, pass_js_1.pages)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Service_Schedule), tenantId).startsWith("origin", "legacy:"), batchSize)) {
840
+ page.forEach(inspect);
841
+ }
842
+ const templates = await migratedTargets(index_js_1.Maintenance_Schedule_Template.className, batchSize);
843
+ for await (const page of (0, pass_js_1.pages)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Service_Schedule_Template), tenantId), batchSize)) {
844
+ page.filter((row) => templates.has(row.id ?? "")).forEach(inspect);
845
+ }
846
+ return bad.length ? `e.g. ${bad.join("; ")}` : undefined;
847
+ },
848
+ },
849
+ {
850
+ name: "every migrated execution has its plan or machine, in its own tenant",
851
+ hard: true,
852
+ async run(tenantId, batchSize) {
853
+ const migrated = await migratedTargets(index_js_1.Maintenance_Schedule_Execution.className, batchSize);
854
+ const plans = await tenantsById(index_js_1.Service_Schedule, batchSize);
855
+ const machines = await tenantsById(index_js_1.Service_Equipment, batchSize);
856
+ const bad = [];
857
+ for await (const page of (0, pass_js_1.pages)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Service_Schedule_Execution), tenantId), batchSize)) {
858
+ for (const execution of page) {
859
+ if (!execution.id || !migrated.has(execution.id))
860
+ continue;
861
+ const own = (0, pass_js_1.refId)(execution.get("tenant"));
862
+ const problems = [];
863
+ const planId = (0, pass_js_1.refId)(execution.get("schedule"));
864
+ const machineId = (0, pass_js_1.refId)(execution.get("equipment"));
865
+ if (!planId && !machineId)
866
+ problems.push("neither schedule nor equipment");
867
+ const references = [
868
+ ["schedule", planId, plans],
869
+ ["equipment", machineId, machines],
870
+ ];
871
+ for (const [what, id, known] of references) {
872
+ if (!id)
873
+ continue;
874
+ if (!known.has(id))
875
+ problems.push(`${what} ${id} does not exist`);
876
+ else if (known.get(id) !== own)
877
+ problems.push(`${what} of another tenant`);
878
+ }
879
+ if (!own)
880
+ problems.push("no tenant");
881
+ if (problems.length && bad.length < 10) {
882
+ bad.push(`${execution.id}: ${problems.join(", ")}`);
883
+ }
884
+ }
885
+ }
886
+ return bad.length ? `e.g. ${bad.join("; ")}` : undefined;
887
+ },
888
+ },
889
+ {
890
+ name: "every migrated execution's results and values belong to its steps",
891
+ hard: true,
892
+ async run(tenantId, batchSize) {
893
+ // The protocol pairs `stepResults` and captured `Meta_Entry` values with
894
+ // `steps` by id. A mismatch renders as a step without its result — the
895
+ // id minting in lib/schedule.ts is what this checks.
896
+ const migrated = await migratedTargets(index_js_1.Maintenance_Schedule_Execution.className, batchSize);
897
+ const stepIds = new Map();
898
+ const bad = [];
899
+ for await (const page of (0, pass_js_1.pages)(() => (0, pass_js_1.scopeToTenant)(new node_1.default.Query(index_js_1.Service_Schedule_Execution), tenantId), batchSize)) {
900
+ for (const execution of page) {
901
+ if (!execution.id || !migrated.has(execution.id))
902
+ continue;
903
+ const steps = (execution.get("steps") ?? []);
904
+ const problems = stepProblems(steps);
905
+ const ids = new Set(steps.map((step) => step.id ?? ""));
906
+ stepIds.set(execution.id, ids);
907
+ const results = (execution.get("stepResults") ?? []);
908
+ for (const result of results) {
909
+ if (!ids.has(result.stepId ?? "")) {
910
+ problems.push(`result for unknown step ${result.stepId}`);
911
+ }
912
+ }
913
+ if (problems.length && bad.length < 10) {
914
+ bad.push(`${execution.id}: ${problems.join(", ")}`);
915
+ }
916
+ }
917
+ }
918
+ for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Meta_Entry)
919
+ .startsWith("context", schedule_js_1.STEP_META_PREFIX)
920
+ .equalTo("entryClassName", index_js_1.Service_Schedule_Execution.className), batchSize)) {
921
+ for (const row of page) {
922
+ const ids = stepIds.get(row.get("entryObjectId") ?? "");
923
+ if (!ids)
924
+ continue; // not a migrated execution, or outside --tenant
925
+ const stepId = (0, schedule_js_1.stepIdOfContext)(row.get("context") ?? "");
926
+ if ((!stepId || !ids.has(stepId)) && bad.length < 10) {
927
+ bad.push(`values ${row.id} name step ${stepId}, which execution ` +
928
+ `${row.get("entryObjectId")} does not have`);
929
+ }
930
+ }
931
+ }
932
+ return bad.length ? `e.g. ${bad.join("; ")}` : undefined;
933
+ },
934
+ },
935
+ {
936
+ name: "no Outlook event is linked to two v2 plans",
937
+ hard: true,
938
+ async run(_tenantId, batchSize) {
939
+ // Two plans on one event means every edit of either rewrites the same
940
+ // appointment, and the v2 poller cannot tell which plan an Outlook change
941
+ // is for.
942
+ const plansByEvent = new Map();
943
+ for await (const page of (0, pass_js_1.pages)(() => new node_1.default.Query(index_js_1.Meta_Entry)
944
+ .equalTo("context", serviceCalendarMeta_js_1.SERVICE_CALENDAR_SYNC_CONTEXT)
945
+ .equalTo("entryClassName", index_js_1.Service_Schedule.className), batchSize)) {
946
+ for (const row of page) {
947
+ const eventId = row.get("values")?.microsoftCalendarEventId;
948
+ if (!eventId)
949
+ continue;
950
+ const plans = plansByEvent.get(eventId) ?? new Set();
951
+ plans.add(row.get("entryObjectId") ?? "");
952
+ plansByEvent.set(eventId, plans);
953
+ }
954
+ }
955
+ const doubled = [...plansByEvent].filter(([, plans]) => plans.size > 1);
956
+ return doubled.length
957
+ ? `e.g. ${doubled
958
+ .slice(0, 5)
959
+ .map(([event, plans]) => `${event} → ${[...plans].join(", ")}`)
960
+ .join("; ")}`
961
+ : undefined;
962
+ },
963
+ },
681
964
  // ── Soft: distribution sanity ────────────────────────────────────────────
682
965
  {
683
966
  name: "ticket status distribution looks plausible",
@@ -37,5 +37,7 @@
37
37
  * - an assignment batch (three rows, one `batchStamp`)
38
38
  * - a message referencing a non-ticket object (orphan skip)
39
39
  * - assets on a real and on a `temp-` media context
40
+ * - maintenance plans, templates and executions in every v1 shape — see
41
+ * `seedSchedules`
40
42
  */
41
43
  export {};