@openinc/parse-server-opendash 4.24.0 → 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.
Files changed (58) hide show
  1. package/dist/features/config/helper/getConfig.js +2 -1
  2. package/dist/features/config/types/ConfigKeys.d.ts +1 -0
  3. package/dist/features/config/types/ConfigKeys.js +10 -25
  4. package/dist/features/openservice/alarms/tenantForSecret.js +3 -1
  5. package/dist/features/openservice/alarms/ticket/AlarmTicketTypes.d.ts +0 -4
  6. package/dist/features/openservice/alarms/ticket/AlarmTicketTypes.js +1 -5
  7. package/dist/features/openservice/alarms/ticket/createAlarmTicket.js +10 -6
  8. package/dist/features/openservice/init.d.ts +12 -6
  9. package/dist/features/openservice/init.js +29 -21
  10. package/dist/features/openservice/tickets/ticketAccess.d.ts +40 -0
  11. package/dist/features/openservice/tickets/ticketAccess.js +105 -0
  12. package/dist/features/openservice/tickets/ticketCleanup.d.ts +25 -0
  13. package/dist/features/openservice/tickets/ticketCleanup.js +65 -0
  14. package/dist/features/openservice/tickets/ticketNotifications.d.ts +2 -0
  15. package/dist/features/openservice/tickets/ticketNotifications.js +242 -0
  16. package/dist/features/openservice/tickets/ticketNumber.d.ts +12 -0
  17. package/dist/features/openservice/tickets/ticketNumber.js +75 -0
  18. package/dist/features/openservice/tickets/ticketReferences.d.ts +61 -0
  19. package/dist/features/openservice/tickets/ticketReferences.js +229 -0
  20. package/dist/features/permissions/helper/hasPermission.d.ts +10 -1
  21. package/dist/features/permissions/helper/hasPermission.js +20 -3
  22. package/dist/features/permissions/helper/requirePermission.d.ts +2 -1
  23. package/dist/features/permissions/helper/requirePermission.js +3 -2
  24. package/dist/features/permissions/types/Permissions.d.ts +1 -0
  25. package/dist/features/permissions/types/Permissions.js +5 -2
  26. package/dist/features/schema/indexes.js +24 -0
  27. package/dist/features/schema/services/beforeSaveHook.js +54 -0
  28. package/dist/functions/openinc-backfill-ticket-sort-keys.d.ts +3 -2
  29. package/dist/functions/openinc-backfill-ticket-sort-keys.js +3 -2
  30. package/dist/functions/openinc-config.js +2 -22
  31. package/dist/helper/aclAccess.d.ts +19 -0
  32. package/dist/helper/aclAccess.js +57 -0
  33. package/dist/helper/getUserLanguage.d.ts +15 -0
  34. package/dist/helper/getUserLanguage.js +28 -4
  35. package/dist/helper/roleNames.d.ts +7 -0
  36. package/dist/helper/roleNames.js +21 -0
  37. package/dist/helper/ticketSortKeys.d.ts +8 -9
  38. package/dist/helper/ticketSortKeys.js +21 -23
  39. package/dist/helper/updateWithoutTriggers.d.ts +22 -0
  40. package/dist/helper/updateWithoutTriggers.js +57 -0
  41. package/dist/hooks/Chat_Message.js +119 -41
  42. package/dist/hooks/Config_open_service.js +30 -4
  43. package/dist/hooks/Meta_Entry.js +75 -14
  44. package/dist/hooks/Service_Equipment.js +6 -6
  45. package/dist/hooks/Service_FormConfig.js +19 -7
  46. package/dist/hooks/Service_Project.js +15 -1
  47. package/dist/hooks/Service_Status.js +51 -2
  48. package/dist/hooks/Service_Tag.js +51 -11
  49. package/dist/hooks/Service_Ticket.js +57 -626
  50. package/dist/index.js +7 -2
  51. package/dist/scripts/migration/lib/ptr.d.ts +1 -1
  52. package/dist/scripts/migration/lib/ptr.js +3 -5
  53. package/dist/scripts/migration/lib/ticket.js +3 -2
  54. package/dist/scripts/repair-ticket-acl.d.ts +1 -0
  55. package/dist/scripts/repair-ticket-acl.js +135 -0
  56. package/package.json +4 -1
  57. package/dist/helper/pointerIds.d.ts +0 -17
  58. package/dist/helper/pointerIds.js +0 -45
package/dist/index.js CHANGED
@@ -87,8 +87,13 @@ async function init() {
87
87
  await (0, index_js_5.initPermissions)();
88
88
  await (0, index_js_3.initNotifications)();
89
89
  await (0, index_js_8.initUserSettings)();
90
- if ((0, index_js_2.isFeatureEnabled)("MAINTENANCE"))
91
- await (0, index_js_4.init)();
90
+ const serviceGenerations = {
91
+ v1: (0, index_js_2.isFeatureEnabled)("MAINTENANCE"),
92
+ v2: (0, index_js_2.isFeatureEnabled)("SERVICE"),
93
+ };
94
+ if (serviceGenerations.v1 || serviceGenerations.v2) {
95
+ await (0, index_js_4.init)(serviceGenerations);
96
+ }
92
97
  if ((0, index_js_2.isFeatureEnabled)("DOCUMENTATION"))
93
98
  await (0, importDocs_js_1.importDocs)();
94
99
  }
@@ -29,4 +29,4 @@ export declare function dateVal(d: Date): {
29
29
  /** JSON-stringified context array, matching how the chat and meta plugins store it. */
30
30
  export declare function contextToString(parts: string[]): string;
31
31
  /** The context every artefact attached to one ticket shares (chat, assets, ...). */
32
- export declare function ticketContext(ticketId: string): string[];
32
+ export { ticketContext } from "@opendash/openservice-shared";
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.rolePtr = exports.userPtr = exports.tenantPtr = void 0;
3
+ exports.ticketContext = exports.rolePtr = exports.userPtr = exports.tenantPtr = void 0;
4
4
  exports.ptr = ptr;
5
5
  exports.maybePtr = maybePtr;
6
6
  exports.dateVal = dateVal;
7
7
  exports.contextToString = contextToString;
8
- exports.ticketContext = ticketContext;
9
8
  function ptr(className, objectId) {
10
9
  return { __type: "Pointer", className, objectId };
11
10
  }
@@ -33,6 +32,5 @@ function contextToString(parts) {
33
32
  return JSON.stringify(parts);
34
33
  }
35
34
  /** The context every artefact attached to one ticket shares (chat, assets, ...). */
36
- function ticketContext(ticketId) {
37
- return ["service", "ticket", ticketId];
38
- }
35
+ var openservice_shared_1 = require("@opendash/openservice-shared");
36
+ Object.defineProperty(exports, "ticketContext", { enumerable: true, get: function () { return openservice_shared_1.ticketContext; } });
@@ -18,8 +18,9 @@ const pass_js_1 = require("./pass.js");
18
18
  * historical `createdAt`, because `RestWrite` replaces the write payload with the
19
19
  * trigger's `Parse.Object` round-trip, which discards the field (see
20
20
  * `features/schema/services/beforeSaveHook.ts`). So the two things that hook did
21
- * are reimplemented here, deliberately mirroring
22
- * `hooks/Service_Ticket.ts` — keep them in sync.
21
+ * are reimplemented here, deliberately mirroring the ticket's `beforeSave`
22
+ * (`hooks/Service_Ticket.ts` and its modules in `features/openservice/tickets/`)
23
+ * — keep them in sync.
23
24
  */
24
25
  // ── Numbering ──────────────────────────────────────────────────────────────
25
26
  const COUNTER_CLASS = "OD3_Service_TicketCounter";
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,135 @@
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 openservice_shared_1 = require("@opendash/openservice-shared");
8
+ const index_js_1 = require("../types/index.js");
9
+ /**
10
+ * One-off repair for ticket ACLs written before the `Service_Ticket` hook
11
+ * rebuilt them on every save.
12
+ *
13
+ * The hook used to keep the stored ACL and only ever add to it, so a user taken
14
+ * off a ticket kept read and write, and so did anything a client had slipped
15
+ * into the ACL. Since the fix every save starts from scratch — but a ticket
16
+ * nobody touches keeps its old grants. This walks every ticket, archived ones
17
+ * included, and re-saves those whose ACL differs from what the hook grants
18
+ * today; the hook itself then writes the ACL, so this script only decides
19
+ * *which* tickets to touch.
20
+ *
21
+ * PARSE_URL=… PARSE_APP_ID=… PARSE_MASTER_KEY=… \
22
+ * node dist/scripts/repair-ticket-acl.js [--dry-run]
23
+ *
24
+ * A re-save stamps `updatedAt`, which the scheduler's auto-archive reads as
25
+ * activity: a repaired, finished ticket is archived one period later than it
26
+ * would have been. Tickets whose ACL is already right are not touched. The
27
+ * re-save writes no changelog entry (`skipChangelog`). Safe to re-run.
28
+ */
29
+ const BATCH_SIZE = 200;
30
+ function requireEnv(name) {
31
+ const value = process.env[name];
32
+ if (!value) {
33
+ console.error(`Missing required environment variable ${name}.`);
34
+ process.exit(1);
35
+ }
36
+ return value;
37
+ }
38
+ /** An ACL as a sorted list of `key:r` / `key:w` entries, false grants dropped. */
39
+ function normalise(grants) {
40
+ return Object.entries(grants)
41
+ .flatMap(([key, permissions]) => [
42
+ ...(permissions.read ? [`${key}:r`] : []),
43
+ ...(permissions.write ? [`${key}:w`] : []),
44
+ ])
45
+ .sort()
46
+ .join("|");
47
+ }
48
+ /**
49
+ * What the hook grants a ticket today — `defaultAclHandler` without a custom
50
+ * ACL plus `setTicketACL`. Only used to pick the tickets to re-save; the hook
51
+ * has the last word.
52
+ */
53
+ function expectedGrants(ticket, roleNameById) {
54
+ const grants = {};
55
+ const grant = (key, read, write) => {
56
+ const current = grants[key] ?? {};
57
+ grants[key] = {
58
+ read: current.read || read,
59
+ write: current.write || write,
60
+ };
61
+ };
62
+ grant("role:od-admin", true, true);
63
+ const tenantId = ticket.get("tenant")?.id;
64
+ if (tenantId) {
65
+ grant(`role:od-tenant-user-${tenantId}`, true, false);
66
+ grant(`role:od-tenant-admin-${tenantId}`, true, true);
67
+ }
68
+ const creatorId = ticket.get("createdBy")?.id;
69
+ if (creatorId)
70
+ grant(creatorId, true, true);
71
+ for (const userId of (0, openservice_shared_1.refIds)(ticket.get("assignedUsers"))) {
72
+ grant(userId, true, true);
73
+ }
74
+ for (const roleId of (0, openservice_shared_1.refIds)(ticket.get("assignedRoles"))) {
75
+ const name = roleNameById.get(roleId);
76
+ if (name)
77
+ grant(`role:${name}`, true, true);
78
+ }
79
+ return grants;
80
+ }
81
+ async function main() {
82
+ const dryRun = process.argv.includes("--dry-run");
83
+ node_1.default.initialize(requireEnv("PARSE_APP_ID"), undefined, requireEnv("PARSE_MASTER_KEY"));
84
+ node_1.default.serverURL =
85
+ process.env.PARSE_URL ?? "http://localhost:1337/parse";
86
+ const roles = await new node_1.default.Query(node_1.default.Role).findAll({
87
+ useMasterKey: true,
88
+ });
89
+ const roleNameById = new Map(roles.flatMap((role) => {
90
+ const name = role.getName();
91
+ return role.id && name ? [[role.id, name]] : [];
92
+ }));
93
+ let checked = 0;
94
+ let repaired = 0;
95
+ let failed = 0;
96
+ // The master key bypasses the soft-delete filter: archived tickets are
97
+ // reachable by link and need the repair as much as live ones.
98
+ await new node_1.default.Query(index_js_1.Service_Ticket).eachBatch(async (tickets) => {
99
+ const stale = [];
100
+ for (const ticket of tickets) {
101
+ const expected = expectedGrants(ticket, roleNameById);
102
+ const stored = (ticket.getACL()?.toJSON() ?? {});
103
+ if (normalise(stored) === normalise(expected))
104
+ continue;
105
+ // Gives the save something to send — an unchanged object may not be
106
+ // written at all. The hook rebuilds the ACL from scratch anyway.
107
+ ticket.setACL(new node_1.default.ACL(expected));
108
+ stale.push(ticket);
109
+ }
110
+ checked += tickets.length;
111
+ if (dryRun || stale.length === 0) {
112
+ repaired += stale.length;
113
+ return;
114
+ }
115
+ try {
116
+ await node_1.default.Object.saveAll(stale, {
117
+ useMasterKey: true,
118
+ context: { skipChangelog: true },
119
+ });
120
+ repaired += stale.length;
121
+ }
122
+ catch (error) {
123
+ failed += stale.length;
124
+ console.error(`Could not repair ${stale.length} ticket(s):`, error);
125
+ }
126
+ }, { batchSize: BATCH_SIZE, useMasterKey: true });
127
+ console.log(`${dryRun ? "[dry run] " : ""}Checked ${checked} ticket(s), ` +
128
+ `${dryRun ? "would repair" : "repaired"} ${repaired}` +
129
+ (failed > 0 ? `, failed ${failed}` : "") +
130
+ ".");
131
+ }
132
+ main().catch((error) => {
133
+ console.error(error);
134
+ process.exit(1);
135
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.24.0",
3
+ "version": "4.25.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [
@@ -68,9 +68,12 @@
68
68
  "migrate:verify": "node dist/scripts/migration/verify.js",
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
+ "repair:ticket-acl": "node dist/scripts/repair-ticket-acl.js",
72
+ "test:integration": "node --test --test-concurrency=1 \"test/integration/**/*.test.mjs\"",
71
73
  "seed:v1": "node dist/scripts/seed-v1-testdata.js"
72
74
  },
73
75
  "dependencies": {
76
+ "@opendash/openservice-shared": "nightly",
74
77
  "@opendash/ruleset-core": "nightly",
75
78
  "@openinc/parse-server-schema": "^4.1.8",
76
79
  "amqplib": "^0.10.9",
@@ -1,17 +0,0 @@
1
- /**
2
- * Extracts the object ids from an array-of-ref field, tolerating undefined and
3
- * malformed entries.
4
- *
5
- * The entity adapter stores array-of-ref fields as plain id **strings**, not as
6
- * `Parse.Object`s. Reading `.id` off them returned undefined, so an ACL built
7
- * from such a field granted nobody anything — silently. Every consumer of
8
- * `assignedUsers`, `assignedRoles`, `users` or `roles` has to go through here.
9
- */
10
- export declare function pointerIds(value: unknown): string[];
11
- /**
12
- * Role **names** for an array-of-ref field holding role pointers.
13
- *
14
- * `Parse.ACL` grants roles by name, never by id, so a role reference has to be
15
- * resolved before it can be written into an ACL.
16
- */
17
- export declare function roleNames(Parse: typeof import("parse/node").default, value: unknown): Promise<string[]>;
@@ -1,45 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.pointerIds = pointerIds;
4
- exports.roleNames = roleNames;
5
- /**
6
- * Extracts the object ids from an array-of-ref field, tolerating undefined and
7
- * malformed entries.
8
- *
9
- * The entity adapter stores array-of-ref fields as plain id **strings**, not as
10
- * `Parse.Object`s. Reading `.id` off them returned undefined, so an ACL built
11
- * from such a field granted nobody anything — silently. Every consumer of
12
- * `assignedUsers`, `assignedRoles`, `users` or `roles` has to go through here.
13
- */
14
- function pointerIds(value) {
15
- if (!Array.isArray(value))
16
- return [];
17
- return value
18
- .map((entry) => {
19
- if (typeof entry === "string")
20
- return entry;
21
- if (entry && typeof entry === "object") {
22
- const o = entry;
23
- return o.id ?? o.objectId;
24
- }
25
- return undefined;
26
- })
27
- .filter((id) => typeof id === "string");
28
- }
29
- /**
30
- * Role **names** for an array-of-ref field holding role pointers.
31
- *
32
- * `Parse.ACL` grants roles by name, never by id, so a role reference has to be
33
- * resolved before it can be written into an ACL.
34
- */
35
- async function roleNames(Parse, value) {
36
- const ids = pointerIds(value);
37
- if (ids.length === 0)
38
- return [];
39
- const roles = await new Parse.Query(Parse.Role)
40
- .containedIn("objectId", ids)
41
- .find({ useMasterKey: true });
42
- return roles
43
- .map((role) => role.getName())
44
- .filter((name) => !!name);
45
- }