@openinc/parse-server-opendash 4.24.1 → 4.25.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/config/helper/getConfig.js +2 -1
- package/dist/features/config/types/ConfigKeys.js +8 -26
- package/dist/features/openservice/alarms/ticket/AlarmTicketTypes.d.ts +0 -4
- package/dist/features/openservice/alarms/ticket/AlarmTicketTypes.js +1 -5
- package/dist/features/openservice/alarms/ticket/createAlarmTicket.js +6 -5
- package/dist/features/openservice/tickets/ticketAccess.d.ts +40 -0
- package/dist/features/openservice/tickets/ticketAccess.js +105 -0
- package/dist/features/openservice/tickets/ticketCleanup.d.ts +25 -0
- package/dist/features/openservice/tickets/ticketCleanup.js +65 -0
- package/dist/features/openservice/tickets/ticketNotifications.d.ts +2 -0
- package/dist/features/openservice/tickets/ticketNotifications.js +242 -0
- package/dist/features/openservice/tickets/ticketNumber.d.ts +12 -0
- package/dist/features/openservice/tickets/ticketNumber.js +75 -0
- package/dist/features/openservice/tickets/ticketReferences.d.ts +61 -0
- package/dist/features/openservice/tickets/ticketReferences.js +229 -0
- package/dist/features/permissions/types/Permissions.d.ts +1 -0
- package/dist/features/permissions/types/Permissions.js +5 -2
- package/dist/features/schema/indexes.js +13 -0
- package/dist/functions/openinc-backfill-ticket-sort-keys.d.ts +3 -2
- package/dist/functions/openinc-backfill-ticket-sort-keys.js +3 -2
- package/dist/helper/getUserLanguage.d.ts +3 -5
- package/dist/helper/getUserLanguage.js +7 -16
- package/dist/helper/roleNames.d.ts +7 -0
- package/dist/helper/roleNames.js +21 -0
- package/dist/helper/ticketSortKeys.d.ts +8 -9
- package/dist/helper/ticketSortKeys.js +21 -23
- package/dist/helper/updateWithoutTriggers.d.ts +22 -0
- package/dist/helper/updateWithoutTriggers.js +57 -0
- package/dist/hooks/Chat_Message.js +7 -30
- package/dist/hooks/Meta_Entry.js +6 -1
- package/dist/hooks/Service_Equipment.js +2 -1
- package/dist/hooks/Service_FormConfig.js +6 -5
- package/dist/hooks/Service_Project.js +2 -1
- package/dist/hooks/Service_Status.js +3 -6
- package/dist/hooks/Service_Tag.js +51 -11
- package/dist/hooks/Service_Ticket.js +22 -701
- package/dist/scripts/migration/lib/ptr.d.ts +1 -1
- package/dist/scripts/migration/lib/ptr.js +3 -5
- package/dist/scripts/migration/lib/ticket.js +3 -2
- package/dist/scripts/repair-ticket-acl.js +3 -3
- package/package.json +3 -1
- package/dist/helper/pointerIds.d.ts +0 -17
- package/dist/helper/pointerIds.js +0 -45
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes derived, denormalised columns straight to the database — every row
|
|
3
|
+
* matching `where` at once, with no triggers, no changelog entry and no new
|
|
4
|
+
* `updatedAt`. `where` and `update` are in REST format: pointers as
|
|
5
|
+
* `{ __type: "Pointer", … }`, `{ __op: "Delete" }` to unset a column.
|
|
6
|
+
*
|
|
7
|
+
* For values that only mirror another row: a ticket's sort keys mirror its
|
|
8
|
+
* status, project and equipment, its tag closure mirrors the tag tree. Saving
|
|
9
|
+
* each affected row did three things that are wrong for such a value:
|
|
10
|
+
*
|
|
11
|
+
* - every ticket hook ran once per row, for thousands of rows;
|
|
12
|
+
* - every row got a changelog entry for a change nobody made to it;
|
|
13
|
+
* - every row got a new `updatedAt`, which the auto-archive reads as
|
|
14
|
+
* activity — reordering "Done" postponed the archiving of every finished
|
|
15
|
+
* ticket by a whole period.
|
|
16
|
+
*
|
|
17
|
+
* Returns the number of rows changed, or `undefined` when the database is out
|
|
18
|
+
* of reach; the caller then saves the rows instead.
|
|
19
|
+
*/
|
|
20
|
+
export declare function updateWithoutTriggers(className: string, where: Record<string, unknown>, update: Record<string, unknown>): Promise<number | undefined>;
|
|
21
|
+
/** A pointer in the REST format `updateWithoutTriggers` takes. */
|
|
22
|
+
export declare function restPointer(object: Parse.Object): Record<string, string>;
|
|
@@ -0,0 +1,57 @@
|
|
|
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.updateWithoutTriggers = updateWithoutTriggers;
|
|
7
|
+
exports.restPointer = restPointer;
|
|
8
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
9
|
+
/**
|
|
10
|
+
* The DatabaseController of the running Parse Server, or `undefined` when it
|
|
11
|
+
* is out of reach (a second copy of `parse-server`, whose config cache is
|
|
12
|
+
* empty). Same reach as `ensureIndexes`.
|
|
13
|
+
*/
|
|
14
|
+
function database() {
|
|
15
|
+
try {
|
|
16
|
+
const Config = require("parse-server/lib/Config");
|
|
17
|
+
const db = Config.get(node_1.default.applicationId)?.database;
|
|
18
|
+
return typeof db?.update === "function" ? db : undefined;
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Writes derived, denormalised columns straight to the database — every row
|
|
26
|
+
* matching `where` at once, with no triggers, no changelog entry and no new
|
|
27
|
+
* `updatedAt`. `where` and `update` are in REST format: pointers as
|
|
28
|
+
* `{ __type: "Pointer", … }`, `{ __op: "Delete" }` to unset a column.
|
|
29
|
+
*
|
|
30
|
+
* For values that only mirror another row: a ticket's sort keys mirror its
|
|
31
|
+
* status, project and equipment, its tag closure mirrors the tag tree. Saving
|
|
32
|
+
* each affected row did three things that are wrong for such a value:
|
|
33
|
+
*
|
|
34
|
+
* - every ticket hook ran once per row, for thousands of rows;
|
|
35
|
+
* - every row got a changelog entry for a change nobody made to it;
|
|
36
|
+
* - every row got a new `updatedAt`, which the auto-archive reads as
|
|
37
|
+
* activity — reordering "Done" postponed the archiving of every finished
|
|
38
|
+
* ticket by a whole period.
|
|
39
|
+
*
|
|
40
|
+
* Returns the number of rows changed, or `undefined` when the database is out
|
|
41
|
+
* of reach; the caller then saves the rows instead.
|
|
42
|
+
*/
|
|
43
|
+
async function updateWithoutTriggers(className, where, update) {
|
|
44
|
+
const db = database();
|
|
45
|
+
if (!db)
|
|
46
|
+
return undefined;
|
|
47
|
+
const result = await db.update(className, where, update, { many: true });
|
|
48
|
+
return result?.modifiedCount ?? 0;
|
|
49
|
+
}
|
|
50
|
+
/** A pointer in the REST format `updateWithoutTriggers` takes. */
|
|
51
|
+
function restPointer(object) {
|
|
52
|
+
return {
|
|
53
|
+
__type: "Pointer",
|
|
54
|
+
className: object.className,
|
|
55
|
+
objectId: object.id,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.init = init;
|
|
7
7
|
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const openservice_shared_1 = require("@opendash/openservice-shared");
|
|
8
9
|
const i18next_1 = __importDefault(require("i18next"));
|
|
9
10
|
const index_js_1 = require("../features/schema/index.js");
|
|
10
11
|
const aclAccess_js_1 = require("../helper/aclAccess.js");
|
|
@@ -66,13 +67,12 @@ async function init() {
|
|
|
66
67
|
return;
|
|
67
68
|
throw new node_1.default.Error(node_1.default.Error.OPERATION_FORBIDDEN, "Only the author may delete a message.");
|
|
68
69
|
});
|
|
70
|
+
// Not awaited: Parse holds the save's response until afterSave returns, and
|
|
71
|
+
// mentioning a role notifies — mail and push included — every member.
|
|
69
72
|
(0, index_js_1.afterSaveHook)(index_js_2.Chat_Message, async ({ object, original }) => {
|
|
70
|
-
|
|
71
|
-
await notifyMentions(object, original);
|
|
72
|
-
}
|
|
73
|
-
catch (error) {
|
|
73
|
+
void notifyMentions(object, original).catch((error) => {
|
|
74
74
|
console.error("[@openinc/parse-server-opendash][Chat_Message] Failed to notify mentioned users", error);
|
|
75
|
-
}
|
|
75
|
+
});
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
@@ -119,7 +119,7 @@ function assertAuthorship(object, original, user) {
|
|
|
119
119
|
* `undefined` for any other chat, or a ticket that does not exist.
|
|
120
120
|
*/
|
|
121
121
|
async function ticketOfMessage(object) {
|
|
122
|
-
const ticketId =
|
|
122
|
+
const ticketId = (0, openservice_shared_1.ticketIdFromContext)(object.get("context"));
|
|
123
123
|
if (!ticketId)
|
|
124
124
|
return undefined;
|
|
125
125
|
return new node_1.default.Query(index_js_2.Service_Ticket)
|
|
@@ -164,7 +164,7 @@ async function notifyMentions(object, original) {
|
|
|
164
164
|
const username = object.get("authorName") || "System";
|
|
165
165
|
const messageText = messageSnippet(object.get("text"));
|
|
166
166
|
// The only linkable chat context today is a service ticket's comment section.
|
|
167
|
-
const ticketId =
|
|
167
|
+
const ticketId = (0, openservice_shared_1.ticketIdFromContext)(object.get("context"));
|
|
168
168
|
// Users already handled — seeded with the author so they never self-notify.
|
|
169
169
|
const notified = new Set();
|
|
170
170
|
const authorId = object.get("author")?.id;
|
|
@@ -267,29 +267,6 @@ function normalizeMentions(value) {
|
|
|
267
267
|
function mentionKey(mention) {
|
|
268
268
|
return `${mention.className}:${mention.id}`;
|
|
269
269
|
}
|
|
270
|
-
/**
|
|
271
|
-
* Extracts the ticket a chat belongs to from its `context`. The context is the
|
|
272
|
-
* JSON-stringified address tuple (e.g. `["service","ticket","<id>"]`). Only the
|
|
273
|
-
* service-ticket context is linkable today; anything else yields no ticket.
|
|
274
|
-
*/
|
|
275
|
-
function getTicketId(context) {
|
|
276
|
-
if (typeof context !== "string")
|
|
277
|
-
return undefined;
|
|
278
|
-
try {
|
|
279
|
-
const parts = JSON.parse(context);
|
|
280
|
-
if (Array.isArray(parts) &&
|
|
281
|
-
parts[0] === "service" &&
|
|
282
|
-
parts[1] === "ticket" &&
|
|
283
|
-
typeof parts[2] === "string" &&
|
|
284
|
-
parts[2]) {
|
|
285
|
-
return parts[2];
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
catch {
|
|
289
|
-
// context is not a JSON tuple — not ticket-bound
|
|
290
|
-
}
|
|
291
|
-
return undefined;
|
|
292
|
-
}
|
|
293
270
|
const SNIPPET_MAX_LENGTH = 140;
|
|
294
271
|
/** Inline mention token: `[label](#mention:_User:<id>)` → keep just `label`. */
|
|
295
272
|
const MENTION_TOKEN = /\[([^\]]*)\]\(#mention:[^)]*\)/g;
|
package/dist/hooks/Meta_Entry.js
CHANGED
|
@@ -137,12 +137,17 @@ async function assertMayWriteAbout(request, linked) {
|
|
|
137
137
|
* `SCHEDULE_VIEWS_META_CONTEXT` (openservice v2), `stepMetaContext()`, and the
|
|
138
138
|
* literals the v1 openservice plugin still uses. Adding a context for a class
|
|
139
139
|
* that is not in here leaves its entries behind on delete.
|
|
140
|
+
*
|
|
141
|
+
* Not `Service_Ticket`: deleting a ticket for good removes its entries in the
|
|
142
|
+
* ticket's own cascade (`cleanupTicketRelations` in
|
|
143
|
+
* `features/openservice/tickets/ticketCleanup.ts`),
|
|
144
|
+
* together with their change history. A second cleanup here would race it and
|
|
145
|
+
* write that history back.
|
|
140
146
|
*/
|
|
141
147
|
const META_SUBJECT_CLASSES = [
|
|
142
148
|
node_1.default.User,
|
|
143
149
|
index_js_3.Source,
|
|
144
150
|
index_js_3.BDE_Article,
|
|
145
|
-
index_js_3.Service_Ticket,
|
|
146
151
|
index_js_3.Service_Equipment,
|
|
147
152
|
index_js_3.Service_Schedule_Execution,
|
|
148
153
|
index_js_3.Tenant,
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.init = init;
|
|
7
7
|
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const openservice_shared_1 = require("@opendash/openservice-shared");
|
|
8
9
|
const index_js_1 = require("../features/schema/index.js");
|
|
9
10
|
const index_js_2 = require("../features/permissions/index.js");
|
|
10
11
|
const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
|
|
@@ -45,7 +46,7 @@ async function init() {
|
|
|
45
46
|
(0, index_js_1.afterSaveHook)(index_js_3.Service_Equipment, async ({ object, original }) => {
|
|
46
47
|
if (!(0, ticketSortKeys_js_1.derivedValueChanged)(object, original, "label"))
|
|
47
48
|
return;
|
|
48
|
-
await (0, ticketSortKeys_js_1.pushSortKeyToTickets)("equipment", "equipmentSortKey", object, (0,
|
|
49
|
+
await (0, ticketSortKeys_js_1.pushSortKeyToTickets)("equipment", "equipmentSortKey", object, (0, openservice_shared_1.sortKeyOf)(object.get("label")));
|
|
49
50
|
});
|
|
50
51
|
// Block deleting equipment that tickets still point at.
|
|
51
52
|
(0, index_js_1.beforeDeleteHook)(index_js_3.Service_Equipment, async (request) => {
|
|
@@ -8,7 +8,8 @@ const node_1 = __importDefault(require("parse/node"));
|
|
|
8
8
|
const index_js_1 = require("../features/schema/index.js");
|
|
9
9
|
const index_js_2 = require("../features/permissions/index.js");
|
|
10
10
|
const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
|
|
11
|
-
const
|
|
11
|
+
const openservice_shared_1 = require("@opendash/openservice-shared");
|
|
12
|
+
const roleNames_js_1 = require("../helper/roleNames.js");
|
|
12
13
|
const index_js_3 = require("../types/index.js");
|
|
13
14
|
/**
|
|
14
15
|
* Restricts a form config to its audience.
|
|
@@ -28,8 +29,8 @@ const index_js_3 = require("../types/index.js");
|
|
|
28
29
|
* to change it.
|
|
29
30
|
*/
|
|
30
31
|
async function applyAudienceAcl(object) {
|
|
31
|
-
const userIds = (0,
|
|
32
|
-
const roles = await (0,
|
|
32
|
+
const userIds = (0, openservice_shared_1.refIds)(object.get("users"));
|
|
33
|
+
const roles = await (0, roleNames_js_1.roleNames)(node_1.default, object.get("roles"));
|
|
33
34
|
if (userIds.length === 0 && roles.length === 0)
|
|
34
35
|
return; // global default
|
|
35
36
|
const acl = object.getACL() ?? new node_1.default.ACL();
|
|
@@ -51,8 +52,8 @@ async function init() {
|
|
|
51
52
|
// role via `openinc-openservice-role-members`. Updates are limited to the
|
|
52
53
|
// admins by the ACL already; checking them too keeps both paths alike.
|
|
53
54
|
await (0, index_js_2.requirePermission)(request, Permissions_js_1.Permissions.SERVICE.admin_access, "change a ticket form config", request.original?.get("tenant") ?? object.get("tenant"));
|
|
54
|
-
const hasAudience = (0,
|
|
55
|
-
(0,
|
|
55
|
+
const hasAudience = (0, openservice_shared_1.refIds)(object.get("users")).length > 0 ||
|
|
56
|
+
(0, openservice_shared_1.refIds)(object.get("roles")).length > 0;
|
|
56
57
|
await (0, index_js_1.defaultHandler)(request);
|
|
57
58
|
// A config addressed at specific roles or users is not the tenant's
|
|
58
59
|
// business — only its audience (granted below) and the admins may read it.
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.init = init;
|
|
7
7
|
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const openservice_shared_1 = require("@opendash/openservice-shared");
|
|
8
9
|
const index_js_1 = require("../features/schema/index.js");
|
|
9
10
|
const index_js_2 = require("../features/permissions/index.js");
|
|
10
11
|
const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
|
|
@@ -63,6 +64,6 @@ async function init() {
|
|
|
63
64
|
(0, index_js_1.afterSaveHook)(index_js_3.Service_Project, async ({ object, original }) => {
|
|
64
65
|
if (!(0, ticketSortKeys_js_1.derivedValueChanged)(object, original, "label"))
|
|
65
66
|
return;
|
|
66
|
-
await (0, ticketSortKeys_js_1.pushSortKeyToTickets)("project", "projectSortKey", object, (0,
|
|
67
|
+
await (0, ticketSortKeys_js_1.pushSortKeyToTickets)("project", "projectSortKey", object, (0, openservice_shared_1.sortKeyOf)(object.get("label")));
|
|
67
68
|
});
|
|
68
69
|
}
|
|
@@ -5,11 +5,11 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.init = init;
|
|
7
7
|
const node_1 = __importDefault(require("parse/node"));
|
|
8
|
+
const openservice_shared_1 = require("@opendash/openservice-shared");
|
|
8
9
|
const index_js_1 = require("../features/schema/index.js");
|
|
9
10
|
const index_js_2 = require("../features/permissions/index.js");
|
|
10
11
|
const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
|
|
11
12
|
const ticketSortKeys_js_1 = require("../helper/ticketSortKeys.js");
|
|
12
|
-
const AlarmTicketTypes_js_1 = require("../features/openservice/alarms/ticket/AlarmTicketTypes.js");
|
|
13
13
|
const index_js_3 = require("../types/index.js");
|
|
14
14
|
async function init() {
|
|
15
15
|
// Soft delete: every ticket carries a required `status` pointer, so a status
|
|
@@ -56,8 +56,8 @@ async function init() {
|
|
|
56
56
|
(0, index_js_1.afterSaveHook)(index_js_3.Service_Status, async ({ object, original }) => {
|
|
57
57
|
if (!original)
|
|
58
58
|
return;
|
|
59
|
-
const wasFinished =
|
|
60
|
-
const finished =
|
|
59
|
+
const wasFinished = (0, openservice_shared_1.isDoneCategory)(original.get("category"));
|
|
60
|
+
const finished = (0, openservice_shared_1.isDoneCategory)(object.get("category"));
|
|
61
61
|
if (wasFinished === finished)
|
|
62
62
|
return;
|
|
63
63
|
await restampClosedAt(object, finished);
|
|
@@ -84,9 +84,6 @@ async function init() {
|
|
|
84
84
|
}
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
|
-
function isFinished(category) {
|
|
88
|
-
return (typeof category === "string" && AlarmTicketTypes_js_1.DONE_STATUS_CATEGORIES.includes(category));
|
|
89
|
-
}
|
|
90
87
|
/**
|
|
91
88
|
* Brings `closedAt` of the tickets in `status` in line with its category:
|
|
92
89
|
* stamped now when it became a finished one — the moment they count as closed
|
|
@@ -8,6 +8,7 @@ const node_1 = __importDefault(require("parse/node"));
|
|
|
8
8
|
const index_js_1 = require("../features/schema/index.js");
|
|
9
9
|
const index_js_2 = require("../features/permissions/index.js");
|
|
10
10
|
const Permissions_js_1 = require("../features/permissions/types/Permissions.js");
|
|
11
|
+
const updateWithoutTriggers_js_1 = require("../helper/updateWithoutTriggers.js");
|
|
11
12
|
const index_js_3 = require("../types/index.js");
|
|
12
13
|
async function init() {
|
|
13
14
|
// Soft delete: tags are stored in the ancestor closures on tickets and
|
|
@@ -97,11 +98,25 @@ async function cleanupTagArrays(object) {
|
|
|
97
98
|
removeTagFromClass(index_js_3.Service_Equipment, object),
|
|
98
99
|
]);
|
|
99
100
|
}
|
|
101
|
+
/**
|
|
102
|
+
* Strips a destroyed tag from every row of `ObjectClass` that carries it —
|
|
103
|
+
* archived ones included, a restored ticket must not point at a missing tag.
|
|
104
|
+
*
|
|
105
|
+
* One database update, without triggers, changelog entries or a new
|
|
106
|
+
* `updatedAt` (see {@link updateWithoutTriggers}): the closure is derived from
|
|
107
|
+
* the tag tree, not something anybody did to the row.
|
|
108
|
+
*/
|
|
100
109
|
async function removeTagFromClass(ObjectClass, tag) {
|
|
101
|
-
const query = new node_1.default.Query(ObjectClass);
|
|
102
|
-
query.equalTo("tags", tag.id);
|
|
103
110
|
// A tag can only be referenced by objects of the same tenant, so we narrow
|
|
104
111
|
// the query to avoid scanning (and touching) other tenants' data.
|
|
112
|
+
const changed = await (0, updateWithoutTriggers_js_1.updateWithoutTriggers)(ObjectClass.className, {
|
|
113
|
+
tags: tag.id,
|
|
114
|
+
...(tag.tenant ? { tenant: (0, updateWithoutTriggers_js_1.restPointer)(tag.tenant) } : {}),
|
|
115
|
+
}, { tags: { __op: "Remove", objects: [tag.id] } });
|
|
116
|
+
if (changed !== undefined)
|
|
117
|
+
return;
|
|
118
|
+
const query = new node_1.default.Query(ObjectClass.className);
|
|
119
|
+
query.equalTo("tags", tag.id);
|
|
105
120
|
if (tag.tenant) {
|
|
106
121
|
query.equalTo("tenant", tag.tenant);
|
|
107
122
|
}
|
|
@@ -110,7 +125,10 @@ async function removeTagFromClass(ObjectClass, tag) {
|
|
|
110
125
|
object.remove("tags", tag.id);
|
|
111
126
|
}
|
|
112
127
|
if (affected.length > 0) {
|
|
113
|
-
await node_1.default.Object.saveAll(affected, {
|
|
128
|
+
await node_1.default.Object.saveAll(affected, {
|
|
129
|
+
useMasterKey: true,
|
|
130
|
+
context: { skipChangelog: true },
|
|
131
|
+
});
|
|
114
132
|
}
|
|
115
133
|
}
|
|
116
134
|
function getParentId(tag) {
|
|
@@ -198,23 +216,45 @@ async function reconcileClosures(tag, oldParentId, newParentId) {
|
|
|
198
216
|
reconcileClass(index_js_3.Service_Equipment, tagId, tenant, oldParents, newParents),
|
|
199
217
|
]);
|
|
200
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* Rebuilds the closures of every row of `ObjectClass` carrying `tagId` —
|
|
221
|
+
* archived ones included, a restored ticket must not come back with the old
|
|
222
|
+
* tree's ancestors.
|
|
223
|
+
*
|
|
224
|
+
* Reads only the `tags` column, and writes without triggers, changelog entries
|
|
225
|
+
* or a new `updatedAt` (see {@link updateWithoutTriggers}): rows that end up
|
|
226
|
+
* with the same closure are written in one update, so re-parenting a tag used
|
|
227
|
+
* on thousands of tickets costs a handful of updates, not thousands of saves.
|
|
228
|
+
*/
|
|
201
229
|
async function reconcileClass(ObjectClass, tagId, tenant, oldParents, newParents) {
|
|
202
|
-
const query = new node_1.default.Query(ObjectClass);
|
|
230
|
+
const query = new node_1.default.Query(ObjectClass.className);
|
|
203
231
|
query.equalTo("tags", tagId);
|
|
204
232
|
// A tag is only referenced by objects of the same tenant.
|
|
205
233
|
if (tenant)
|
|
206
234
|
query.equalTo("tenant", tenant);
|
|
235
|
+
query.select("tags");
|
|
207
236
|
const affected = await query.findAll({ useMasterKey: true });
|
|
208
|
-
|
|
237
|
+
// Rebuilt closure → the rows that get it.
|
|
238
|
+
const groups = new Map();
|
|
209
239
|
for (const object of affected) {
|
|
210
240
|
const current = object.get("tags") ?? [];
|
|
211
241
|
const rebuilt = closureOf(deepestOf(current, oldParents), newParents);
|
|
212
|
-
if (
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
242
|
+
if (sameMembers(current, rebuilt))
|
|
243
|
+
continue;
|
|
244
|
+
const key = JSON.stringify(rebuilt);
|
|
245
|
+
const group = groups.get(key) ?? { tags: rebuilt, rows: [] };
|
|
246
|
+
group.rows.push(object);
|
|
247
|
+
groups.set(key, group);
|
|
216
248
|
}
|
|
217
|
-
|
|
218
|
-
await
|
|
249
|
+
for (const { tags, rows } of groups.values()) {
|
|
250
|
+
const changed = await (0, updateWithoutTriggers_js_1.updateWithoutTriggers)(ObjectClass.className, { objectId: { $in: rows.map((row) => row.id) } }, { tags });
|
|
251
|
+
if (changed !== undefined)
|
|
252
|
+
continue;
|
|
253
|
+
for (const row of rows)
|
|
254
|
+
row.set("tags", tags);
|
|
255
|
+
await node_1.default.Object.saveAll(rows, {
|
|
256
|
+
useMasterKey: true,
|
|
257
|
+
context: { skipChangelog: true },
|
|
258
|
+
});
|
|
219
259
|
}
|
|
220
260
|
}
|