@openinc/parse-server-opendash 4.19.0 → 4.21.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.
@@ -25,6 +25,7 @@ export declare namespace Permissions {
25
25
  ticket_access = "openservice-ticket:can-access-ticket-plugin",
26
26
  ticket_create = "openservice-ticket:can-create-ticket",
27
27
  ticket_delete = "openservice-ticket:can-delete-ticket",
28
+ ticket_analytics_view = "openservice-ticket:can-view-analytics",
28
29
  schedule_access = "openservice-schedule:can-access-schedule-plugin",
29
30
  schedule_create = "openservice-schedule:can-create-schedule",
30
31
  schedule_delete = "openservice-schedule:can-delete-schedule",
@@ -54,6 +54,11 @@ var Permissions;
54
54
  SERVICE["ticket_access"] = "openservice-ticket:can-access-ticket-plugin";
55
55
  SERVICE["ticket_create"] = "openservice-ticket:can-create-ticket";
56
56
  SERVICE["ticket_delete"] = "openservice-ticket:can-delete-ticket";
57
+ // The ticket evaluations page. Its own key rather than the schedule
58
+ // plugin's `analytics_view`: a tenant may well want its shift leads to see
59
+ // ticket throughput without opening up maintenance compliance, and the two
60
+ // pages belong to two plugins that are deployed independently.
61
+ SERVICE["ticket_analytics_view"] = "openservice-ticket:can-view-analytics";
57
62
  // Schedule-Plugin
58
63
  //
59
64
  // No `read` or `edit` here on purpose: both are decided per record by the
@@ -83,7 +83,85 @@ async function applyLinkedObjectTenant(request) {
83
83
  console.error("[@openinc/parse-server-opendash][Meta_Entry] Failed to resolve the linked object's tenant", className, objectId, error);
84
84
  }
85
85
  }
86
+ /**
87
+ * The classes a meta entry can be *about*, i.e. every class some context
88
+ * addresses by `entryClassName`. Kept here rather than spread over the ten
89
+ * classes' own hook modules: the list is the answer to "who carries meta
90
+ * entries", it belongs next to the class that holds them, and ten copies of the
91
+ * same three lines is how the eleventh context gets forgotten.
92
+ *
93
+ * Derived from the frontend's context constants — `MetaContext` (bde),
94
+ * `metaContext()` / `TICKET_VIEWS_META_CONTEXT` /
95
+ * `SCHEDULE_VIEWS_META_CONTEXT` (openservice v2), `stepMetaContext()`, and the
96
+ * literals the v1 openservice plugin still uses. Adding a context for a class
97
+ * that is not in here leaves its entries behind on delete.
98
+ */
99
+ const META_SUBJECT_CLASSES = [
100
+ node_1.default.User,
101
+ index_js_3.Source,
102
+ index_js_3.BDE_Article,
103
+ index_js_3.Service_Ticket,
104
+ index_js_3.Service_Equipment,
105
+ index_js_3.Service_Schedule_Execution,
106
+ index_js_3.Tenant,
107
+ index_js_3.Maintenance_Ticket,
108
+ index_js_3.Maintenance_Schedule,
109
+ index_js_3.Maintenance_Schedule_Execution_Step,
110
+ ];
111
+ /**
112
+ * Drop the meta entries of an object that was deleted.
113
+ *
114
+ * Nothing did this before: an entry addresses its subject by the two strings
115
+ * `entryClassName` + `entryObjectId` and not by a pointer, so Parse has no idea
116
+ * the two belong together and deleting the subject left the entry sitting there
117
+ * forever. The index this query runs on was already declared for exactly this
118
+ * case — see `od_entryObjectId_entryClassName_context` in `indexes.ts`, whose
119
+ * comment names "cleanup on delete (without `context`)".
120
+ *
121
+ * **`afterDelete`, deliberately not `beforeDelete`.** A throw in `beforeDelete`
122
+ * aborts the delete, which would make a failed bit of housekeeping enough to
123
+ * stop anyone from deleting a ticket. Here the object is already gone and a
124
+ * failure leaves exactly what the situation was before this existed — an
125
+ * orphaned entry — so it is logged and swallowed.
126
+ *
127
+ * Soft delete is unaffected and should be: the archive action stamps a marker
128
+ * column instead of destroying the row, no delete trigger fires, and the
129
+ * entries of an archived object stay with it until it is really deleted.
130
+ *
131
+ * One case this cannot reach: the schedule step contexts carry the step id
132
+ * *inside the context* (`["openservice", "schedule", "step", <stepId>]`) while
133
+ * the entry hangs off the execution. Removing a step from a plan therefore
134
+ * leaves an entry whose subject is still alive, which no `entryObjectId` lookup
135
+ * can find.
136
+ */
137
+ async function removeEntriesOfDeletedObject(className, objectId) {
138
+ try {
139
+ // No `context` constraint: every context's entries for this object go.
140
+ const orphans = await new node_1.default.Query(index_js_3.Meta_Entry)
141
+ .equalTo("entryClassName", className)
142
+ .equalTo("entryObjectId", objectId)
143
+ .find({ useMasterKey: true });
144
+ if (orphans.length === 0)
145
+ return;
146
+ await node_1.default.Object.destroyAll(orphans, { useMasterKey: true });
147
+ }
148
+ catch (error) {
149
+ console.error("[@openinc/parse-server-opendash][Meta_Entry] Failed to remove the meta entries of a deleted object", className, objectId, error);
150
+ }
151
+ }
86
152
  async function init() {
153
+ for (const subjectClass of META_SUBJECT_CLASSES) {
154
+ // `.className` and not a literal: a query on a class name that does not
155
+ // exist returns nothing instead of throwing, so a typo here would be a
156
+ // cleanup that silently never cleans anything up.
157
+ const className = subjectClass
158
+ .className;
159
+ (0, index_js_2.afterDeleteHook)(subjectClass, async ({ object }) => {
160
+ if (!object?.id)
161
+ return;
162
+ await removeEntriesOfDeletedObject(className, object.id);
163
+ });
164
+ }
87
165
  (0, index_js_2.beforeSaveHook)(index_js_3.Meta_Entry, async (request) => {
88
166
  await (0, index_js_2.defaultHandler)(request);
89
167
  // Before the ACL handler: it derives every grant from `tenant`.
@@ -42,6 +42,7 @@ const i18next_1 = __importDefault(require("i18next"));
42
42
  const index_js_1 = require("../features/schema/index.js");
43
43
  const index_js_2 = require("../features/permissions/index.js");
44
44
  const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
45
+ const AlarmTicketTypes_js_1 = require("../features/openservice/alarms/ticket/AlarmTicketTypes.js");
45
46
  const appLinks_js_1 = require("../helper/appLinks.js");
46
47
  const pointerIds_js_1 = require("../helper/pointerIds.js");
47
48
  const getUserLanguage_js_1 = require("../helper/getUserLanguage.js");
@@ -92,6 +93,7 @@ async function init() {
92
93
  const referenced = await loadReferencedRows(object);
93
94
  assertSameTenantReferences(object, referenced);
94
95
  applySortKeys(object, referenced);
96
+ applyClosedAt(object, referenced);
95
97
  // Assign a sequential ticket number on creation. Done here (backend) rather
96
98
  // than in the frontend because every write path — the create form, the
97
99
  // cross-plugin service, and the generic entity admin — funnels through this
@@ -320,8 +322,9 @@ async function loadReferencedRows(object) {
320
322
  for (const [className, ids] of byClass) {
321
323
  const found = await new node_1.default.Query(className)
322
324
  .containedIn("objectId", [...ids])
323
- // `label` and `order` feed the sort keys below; `tenant` the guard.
324
- .select("tenant", "label", "order")
325
+ // `label` and `order` feed the sort keys below, `category` the
326
+ // closing date; `tenant` the guard.
327
+ .select("tenant", "label", "order", "category")
325
328
  .limit(ids.size)
326
329
  .find({ useMasterKey: true });
327
330
  for (const row of found)
@@ -407,6 +410,57 @@ function applySortKeys(object, rows) {
407
410
  object.set(target, value);
408
411
  }
409
412
  }
413
+ /**
414
+ * Stamps and clears `closedAt` from the ticket's status.
415
+ *
416
+ * **Why the server and not the form.** `closedAt` is the only record of when
417
+ * work finished, and every number built on it — throughput, how long a ticket
418
+ * sat open, what a month closed — is wrong if it is missing on some tickets and
419
+ * guessed on others. It was a plain date field nobody filled: the board moves a
420
+ * ticket to "Erledigt" by drag and drop, the detail page by a dropdown, the
421
+ * rules by an action, and none of them went near it. Doing it here covers every
422
+ * one of those paths plus the import and the REST API, which is the whole point
423
+ * of putting it in `beforeSave`.
424
+ *
425
+ * **The status decides, not the field.** A status counts as closing when its
426
+ * `category` is one of {@link DONE_STATUS_CATEGORIES} — the same list the alarm
427
+ * tickets and the frontend read, so "Erledigt", "Abgebrochen" and whatever a
428
+ * tenant calls them agree without anybody configuring it twice.
429
+ *
430
+ * Reopening clears the stamp. A ticket that goes back to "In Bearbeitung" is
431
+ * not closed, and leaving the old date would quietly count it as finished in
432
+ * every evaluation while it is being worked on again.
433
+ *
434
+ * **A value the caller sent wins.** `object.dirty("closedAt")` means somebody
435
+ * named a date in this same save — the migration replaying history, a correction
436
+ * in the admin table — and overwriting it with `now` would be this function
437
+ * deciding it knows better.
438
+ */
439
+ function applyClosedAt(object, rows) {
440
+ // Nothing to react to: the status is unchanged. A ticket whose status stayed
441
+ // put keeps whatever stamp it has, including none.
442
+ if (!object.dirty("status"))
443
+ return;
444
+ // Explicitly set in this save — see the note above.
445
+ if (object.dirty("closedAt"))
446
+ return;
447
+ const status = object.get("status");
448
+ const row = status?.id ? rows.get(status.id) : undefined;
449
+ // The row is only missing when the pointer could not be read. Guessing either
450
+ // way would be worse than leaving the stamp as it is.
451
+ if (!row)
452
+ return;
453
+ const closing = AlarmTicketTypes_js_1.DONE_STATUS_CATEGORIES.includes(row.get("category"));
454
+ if (closing) {
455
+ // Already stamped: the ticket moved between two closing statuses
456
+ // ("Erledigt" → "Abgebrochen"). It closed when it first closed.
457
+ if (!object.get("closedAt"))
458
+ object.set("closedAt", new Date());
459
+ }
460
+ else {
461
+ object.unset("closedAt");
462
+ }
463
+ }
410
464
  /**
411
465
  * Sets the ticket ACL from `createdBy`, `assignedUsers` and `assignedRoles` —
412
466
  * each gets read/write. `assignedUsers`/`assignedRoles` are arrays of Parse
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.19.0",
3
+ "version": "4.21.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [