@openinc/parse-server-opendash 4.26.0 → 4.27.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.
@@ -18,3 +18,21 @@ import { Service_FormConfig, type Tenant } from "../../../types/index.js";
18
18
  * key, "all configs" would be every tenant's.
19
19
  */
20
20
  export declare function applicableFormConfig(tenant: Tenant | undefined, user: Parse.User | undefined): Promise<Service_FormConfig | undefined>;
21
+ /**
22
+ * The config a save names in its context (`{ formConfig: <id> }`): the one
23
+ * the form that saves was showing — the ticket forms of the frontend send it.
24
+ * `undefined` when the save names none, or one that does not count:
25
+ *
26
+ * - read with the caller's own rights, so only a config the caller may read
27
+ * (its audience, the tenant's global default, an admin) — the same check
28
+ * the QR flow makes in the browser;
29
+ * - of the ticket's own tenant: a cross-tenant admin may read another
30
+ * tenant's configs, which say nothing about this ticket.
31
+ *
32
+ * Read by id rather than resolved: one query, and exactly the config the form
33
+ * showed, in its current version.
34
+ */
35
+ export declare function configFromContext(id: unknown, tenant: Tenant | undefined, { master, sessionToken, }: {
36
+ master: boolean;
37
+ sessionToken: string | undefined;
38
+ }): Promise<Service_FormConfig | undefined>;
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.applicableFormConfig = applicableFormConfig;
7
+ exports.configFromContext = configFromContext;
7
8
  // Which ticket form config applies to a user. The frontend asks this through
8
9
  // `openinc-openservice-form-config`, and the ticket hook reads the priority
9
10
  // formula out of the same answer, so both always work with the same config.
@@ -72,3 +73,29 @@ async function findRoleMatch(configs, user) {
72
73
  .map((role) => role.id));
73
74
  return configs.find((c) => (0, openservice_shared_1.refIds)(c.roles).some((id) => heldIds.has(id)));
74
75
  }
76
+ /**
77
+ * The config a save names in its context (`{ formConfig: <id> }`): the one
78
+ * the form that saves was showing — the ticket forms of the frontend send it.
79
+ * `undefined` when the save names none, or one that does not count:
80
+ *
81
+ * - read with the caller's own rights, so only a config the caller may read
82
+ * (its audience, the tenant's global default, an admin) — the same check
83
+ * the QR flow makes in the browser;
84
+ * - of the ticket's own tenant: a cross-tenant admin may read another
85
+ * tenant's configs, which say nothing about this ticket.
86
+ *
87
+ * Read by id rather than resolved: one query, and exactly the config the form
88
+ * showed, in its current version.
89
+ */
90
+ async function configFromContext(id, tenant, { master, sessionToken, }) {
91
+ if (typeof id !== "string" || !id)
92
+ return undefined;
93
+ if (!master && !sessionToken)
94
+ return undefined;
95
+ const config = await new node_1.default.Query(index_js_1.Service_FormConfig)
96
+ .equalTo("objectId", id)
97
+ .first(master ? { useMasterKey: true } : { sessionToken });
98
+ if (!config)
99
+ return undefined;
100
+ return config.tenant?.id === tenant?.id ? config : undefined;
101
+ }
@@ -28,20 +28,21 @@ export declare function priorityFormulaOf(config: Service_FormConfig | undefined
28
28
  * `undefined` as well.
29
29
  */
30
30
  export declare function computePriority(formula: PriorityFormula, values: (key: string) => unknown): number | undefined;
31
+ /** Whether this save changes one of the inputs a priority formula reads. */
32
+ export declare function changesPriorityInputs(object: Parse.Object, original: Parse.Object | undefined): boolean;
31
33
  /**
32
- * Sets the computed priority on a ticket being saved, under the form config
33
- * that applies to the user saving it (see `applicableFormConfig`).
34
+ * Sets the computed priority on a ticket being saved, under `config` — the
35
+ * form config that applies to the user saving (see `applicableFormConfig`).
34
36
  *
35
37
  * Only when this save changes one of the inputs — a ticket the save does not
36
38
  * reassess keeps its priority, also when the formula has changed since — and
37
- * only when the caller does not set the priority itself in the same save:
38
- * the ticket forms send the value they computed and showed, and an import
39
- * keeps the priority its file states.
39
+ * only when the caller does not set the priority itself in the same save: an
40
+ * import keeps the priority its file states.
40
41
  *
41
42
  * "Changes" and "sets" are judged against `original`. The admin table sends
42
43
  * every field of its form, the unchanged priority included; counting that as
43
44
  * set by hand would leave the old priority after every factor edit there —
44
45
  * which is the case this exists for.
45
46
  */
46
- export declare function applyComputedPriority(object: Parse.Object, original: Parse.Object | undefined, user: Parse.User | undefined): Promise<void>;
47
+ export declare function applyComputedPriority(object: Parse.Object, original: Parse.Object | undefined, config: Service_FormConfig | undefined): void;
47
48
  export {};
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PRIORITY_FORMULA_INPUTS = void 0;
4
4
  exports.priorityFormulaOf = priorityFormulaOf;
5
5
  exports.computePriority = computePriority;
6
+ exports.changesPriorityInputs = changesPriorityInputs;
6
7
  exports.applyComputedPriority = applyComputedPriority;
7
8
  const ruleset_core_1 = require("@opendash/ruleset-core");
8
- const ticketFormConfig_js_1 = require("./ticketFormConfig.js");
9
9
  /**
10
10
  * The fields a priority formula may read, e.g.
11
11
  * `max(downtime, restriction) * 0.7 + frequency * 0.3`. Mirrors
@@ -71,30 +71,36 @@ function computePriority(formula, values) {
71
71
  return undefined;
72
72
  return Math.round(Math.min(100, Math.max(0, result)));
73
73
  }
74
+ /** Whether this save changes `key`, judged against the stored ticket. */
75
+ function changes(object, original, key) {
76
+ return original
77
+ ? object.get(key) !== original.get(key)
78
+ : object.get(key) != null;
79
+ }
80
+ /** Whether this save changes one of the inputs a priority formula reads. */
81
+ function changesPriorityInputs(object, original) {
82
+ return exports.PRIORITY_FORMULA_INPUTS.some((key) => changes(object, original, key));
83
+ }
74
84
  /**
75
- * Sets the computed priority on a ticket being saved, under the form config
76
- * that applies to the user saving it (see `applicableFormConfig`).
85
+ * Sets the computed priority on a ticket being saved, under `config` — the
86
+ * form config that applies to the user saving (see `applicableFormConfig`).
77
87
  *
78
88
  * Only when this save changes one of the inputs — a ticket the save does not
79
89
  * reassess keeps its priority, also when the formula has changed since — and
80
- * only when the caller does not set the priority itself in the same save:
81
- * the ticket forms send the value they computed and showed, and an import
82
- * keeps the priority its file states.
90
+ * only when the caller does not set the priority itself in the same save: an
91
+ * import keeps the priority its file states.
83
92
  *
84
93
  * "Changes" and "sets" are judged against `original`. The admin table sends
85
94
  * every field of its form, the unchanged priority included; counting that as
86
95
  * set by hand would leave the old priority after every factor edit there —
87
96
  * which is the case this exists for.
88
97
  */
89
- async function applyComputedPriority(object, original, user) {
90
- const before = (key) => original?.get(key);
91
- const changes = (key) => object.isNew() ? object.get(key) != null : object.get(key) !== before(key);
92
- if (!exports.PRIORITY_FORMULA_INPUTS.some(changes))
98
+ function applyComputedPriority(object, original, config) {
99
+ if (!changesPriorityInputs(object, original))
93
100
  return;
94
- if (changes("priority"))
101
+ if (changes(object, original, "priority"))
95
102
  return;
96
- const tenant = object.get("tenant");
97
- const formula = priorityFormulaOf(await (0, ticketFormConfig_js_1.applicableFormConfig)(tenant, user));
103
+ const formula = priorityFormulaOf(config);
98
104
  if (!formula)
99
105
  return;
100
106
  const priority = computePriority(formula, (key) => object.get(key));
@@ -0,0 +1,18 @@
1
+ /** The zone ticket rules are evaluated in. */
2
+ export declare const RULE_TIME_ZONE: string;
3
+ /** Units of a relative date value. Mirrors `RelativeUnit` of the frontend. */
4
+ export type RelativeUnit = "minutes" | "hours" | "days" | "weeks";
5
+ /**
6
+ * `instant` as a `Date` whose *local* getters read the zone's wall clock.
7
+ *
8
+ * `@opendash/ruleset-core` compares dates in local time — a rule's
9
+ * "YYYY-MM-DD" is local midnight, "equals" compares local calendar days. Fed
10
+ * these, it compares on the zone's calendar whatever the process runs in.
11
+ */
12
+ export declare function inRuleZone(instant: Date): Date;
13
+ /**
14
+ * `now` moved by a relative rule value, the way dayjs does it in the browser:
15
+ * minutes and hours as elapsed time, days and weeks on the zone's calendar —
16
+ * "in 1 day" across a daylight-saving change keeps the time of day.
17
+ */
18
+ export declare function addRelative(now: Date, amount: number, unit: RelativeUnit): Date;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ // Time for the ticket rules: the browser evaluates and computes them in the
3
+ // user's local time, so the server does the same in the zone the users live
4
+ // in rather than in the container's UTC.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RULE_TIME_ZONE = void 0;
7
+ exports.inRuleZone = inRuleZone;
8
+ exports.addRelative = addRelative;
9
+ /** The zone ticket rules are evaluated in. */
10
+ exports.RULE_TIME_ZONE = process.env.OPENSERVICE_TIME_ZONE || "Europe/Berlin";
11
+ const formatter = new Intl.DateTimeFormat("en-US", {
12
+ timeZone: exports.RULE_TIME_ZONE,
13
+ hourCycle: "h23",
14
+ year: "numeric",
15
+ month: "2-digit",
16
+ day: "2-digit",
17
+ hour: "2-digit",
18
+ minute: "2-digit",
19
+ second: "2-digit",
20
+ });
21
+ /** What a clock in {@link RULE_TIME_ZONE} shows at `instant`. */
22
+ function wallClock(instant) {
23
+ const parts = formatter.formatToParts(instant);
24
+ const part = (type) => Number(parts.find((p) => p.type === type)?.value);
25
+ return {
26
+ year: part("year"),
27
+ month: part("month"),
28
+ day: part("day"),
29
+ hour: part("hour"),
30
+ minute: part("minute"),
31
+ second: part("second"),
32
+ ms: instant.getUTCMilliseconds(),
33
+ };
34
+ }
35
+ /** How far the zone is ahead of UTC at `instant`, in ms. */
36
+ function zoneOffset(instant) {
37
+ const w = wallClock(instant);
38
+ return (Date.UTC(w.year, w.month - 1, w.day, w.hour, w.minute, w.second, w.ms) -
39
+ instant.getTime());
40
+ }
41
+ /**
42
+ * `instant` as a `Date` whose *local* getters read the zone's wall clock.
43
+ *
44
+ * `@opendash/ruleset-core` compares dates in local time — a rule's
45
+ * "YYYY-MM-DD" is local midnight, "equals" compares local calendar days. Fed
46
+ * these, it compares on the zone's calendar whatever the process runs in.
47
+ */
48
+ function inRuleZone(instant) {
49
+ const w = wallClock(instant);
50
+ return new Date(w.year, w.month - 1, w.day, w.hour, w.minute, w.second, w.ms);
51
+ }
52
+ /**
53
+ * `now` moved by a relative rule value, the way dayjs does it in the browser:
54
+ * minutes and hours as elapsed time, days and weeks on the zone's calendar —
55
+ * "in 1 day" across a daylight-saving change keeps the time of day.
56
+ */
57
+ function addRelative(now, amount, unit) {
58
+ if (unit === "minutes")
59
+ return new Date(now.getTime() + amount * 60_000);
60
+ if (unit === "hours")
61
+ return new Date(now.getTime() + amount * 3_600_000);
62
+ const days = unit === "weeks" ? amount * 7 : amount;
63
+ const w = wallClock(now);
64
+ const target = Date.UTC(w.year, w.month - 1, w.day + days, w.hour, w.minute, w.second, w.ms);
65
+ // The target's offset may differ from today's; one correction settles it.
66
+ const first = target - zoneOffset(new Date(target));
67
+ return new Date(target - zoneOffset(new Date(first)));
68
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * When a rule acts. Mirrors `RuleTrigger` of the frontend.
3
+ *
4
+ * - `create`: when the ticket is created.
5
+ * - `becomes`: when its condition starts to hold — on creation if it holds,
6
+ * later when a save makes it hold that did not before.
7
+ * - `always`: on every save while the condition holds. A rule stored without
8
+ * a trigger predates the choice and keeps this behaviour.
9
+ */
10
+ export type RuleTrigger = "create" | "becomes" | "always";
11
+ /**
12
+ * What a form config does to a ticket on save, in the order the frontend used
13
+ * to apply it: the computed priority, the field value rules, the action rules
14
+ * — so a rule on the priority sees the computed one.
15
+ *
16
+ * Which config: the one the form that saves was showing, when it names it in
17
+ * the save's context (`{ formConfig: <id> }`, see `configFromContext`) — a
18
+ * form opened by QR code shows another config than the user's own, and its
19
+ * rules are what the user saw. Otherwise (the admin table, the REST API, an
20
+ * alarm) the one that applies to the user saving (`applicableFormConfig`).
21
+ *
22
+ * The rules act on a save somebody makes — a person, an integration, an alarm
23
+ * creating a ticket. Not on the server's own bookkeeping: a master-key update
24
+ * (sort keys pushed down, the scheduler archiving) would otherwise re-assign
25
+ * people that were taken off by hand, through an "always" rule. The priority
26
+ * follows its inputs on any save.
27
+ */
28
+ export declare function applyFormConfigRules(object: Parse.Object, original: Parse.Object | undefined, user: Parse.User | undefined, { master, sessionToken, context, }: {
29
+ master: boolean;
30
+ sessionToken: string | undefined;
31
+ context: Record<string, unknown> | undefined;
32
+ }): Promise<void>;
@@ -0,0 +1,225 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyFormConfigRules = applyFormConfigRules;
4
+ const openservice_shared_1 = require("@opendash/openservice-shared");
5
+ const ruleset_core_1 = require("@opendash/ruleset-core");
6
+ const index_js_1 = require("../../../types/index.js");
7
+ const ticketFormConfig_js_1 = require("./ticketFormConfig.js");
8
+ const ticketPriority_js_1 = require("./ticketPriority.js");
9
+ const ticketRuleTime_js_1 = require("./ticketRuleTime.js");
10
+ /**
11
+ * Columns a rule never writes: the server's and the archive's own, and the
12
+ * priority a formula computes. Mirrors `PROTECTED_TICKET_FIELDS` of the
13
+ * frontend, plus the derived columns only the server knows about.
14
+ */
15
+ const PROTECTED_FIELDS = new Set([
16
+ "deleted",
17
+ "tenant",
18
+ "number",
19
+ "createdBy",
20
+ "closedAt",
21
+ "archiveReason",
22
+ "useForSimilarCases",
23
+ "origin",
24
+ "statusOrder",
25
+ "projectSortKey",
26
+ "equipmentSortKey",
27
+ ]);
28
+ /** Pointer columns a rule may write, with the class they point at. */
29
+ const POINTER_TARGETS = {
30
+ status: index_js_1.Service_Status,
31
+ project: index_js_1.Service_Project,
32
+ equipment: index_js_1.Service_Equipment,
33
+ parent: index_js_1.Service_Ticket,
34
+ };
35
+ /** Columns holding a list of ids (stored as plain id strings). */
36
+ const REF_LIST_FIELDS = new Set([
37
+ "tags",
38
+ "assignedUsers",
39
+ "assignedRoles",
40
+ ]);
41
+ function parseJson(raw) {
42
+ try {
43
+ const parsed = JSON.parse(raw ?? "[]");
44
+ return Array.isArray(parsed) ? parsed : [];
45
+ }
46
+ catch {
47
+ return [];
48
+ }
49
+ }
50
+ /** A rule set without a single rule always holds — the editor starts empty. */
51
+ function holds(condition, values) {
52
+ if (!condition || !hasRules(condition))
53
+ return true;
54
+ return (0, ruleset_core_1.applyRuleSet)(condition, values);
55
+ }
56
+ function hasRules(group) {
57
+ return (group.kind === "group" &&
58
+ group.rules.some((rule) => rule.kind === "group" ? hasRules(rule) : true));
59
+ }
60
+ /** Reads a ticket's values, dates on the rule zone's calendar. */
61
+ function readerOf(object) {
62
+ return (field) => {
63
+ const value = object.get(field);
64
+ return value instanceof Date ? (0, ticketRuleTime_js_1.inRuleZone)(value) : value;
65
+ };
66
+ }
67
+ function isEmpty(value) {
68
+ if (value == null || value === "")
69
+ return true;
70
+ return Array.isArray(value) && value.length === 0;
71
+ }
72
+ /** Whether a rule with `on` acts in this save. */
73
+ function fires(on, creating, becameTrue) {
74
+ switch (on ?? "always") {
75
+ case "create":
76
+ return creating;
77
+ case "becomes":
78
+ return becameTrue;
79
+ default:
80
+ return true;
81
+ }
82
+ }
83
+ /** The first value rule that has a value and whose condition holds. */
84
+ function firstMatch(entry, values) {
85
+ return entry.valueRules?.find((rule) => isComplete(rule.value) && holds(rule.when, values));
86
+ }
87
+ function isComplete(value) {
88
+ if (!value)
89
+ return false;
90
+ if (value.kind === "relative")
91
+ return typeof value.amount === "number" && !!value.unit;
92
+ return value.value !== undefined;
93
+ }
94
+ /**
95
+ * A rule's value in the column's stored shape — what the frontend's
96
+ * `buildCreateValue` sends: a pointer for a reference, plain id strings for a
97
+ * list of references, a `Date` for a date.
98
+ */
99
+ function storedValue(key, value, now) {
100
+ if (value.kind === "relative") {
101
+ return (0, ticketRuleTime_js_1.addRelative)(now, value.amount, value.unit);
102
+ }
103
+ const raw = value.value;
104
+ if (REF_LIST_FIELDS.has(key))
105
+ return (0, openservice_shared_1.refIds)(raw);
106
+ const target = POINTER_TARGETS[key];
107
+ if (target) {
108
+ const id = (0, openservice_shared_1.refIds)([raw])[0];
109
+ return id ? target.createWithoutData(id) : undefined;
110
+ }
111
+ return raw;
112
+ }
113
+ /**
114
+ * The field value rules of `config`, in form order; each sees what the ones
115
+ * before it wrote. Mirrors `applyValueRules` of the frontend.
116
+ *
117
+ * - `prefill`: on creation, into an empty field.
118
+ * - `set`: by its trigger, overriding. `becomes` acts when the rule becomes
119
+ * the field's first matching rule — so lowering a priority from "critical"
120
+ * to "high" applies the "high" rule's due date.
121
+ */
122
+ function applyValueRules(entries, object, original, now) {
123
+ const creating = !original;
124
+ const after = readerOf(object);
125
+ const before = original ? readerOf(original) : undefined;
126
+ for (const entry of entries) {
127
+ if (!entry?.key || PROTECTED_FIELDS.has(entry.key))
128
+ continue;
129
+ const rule = firstMatch(entry, after);
130
+ if (!rule?.value)
131
+ continue;
132
+ if (rule.mode === "prefill") {
133
+ if (!creating || !isEmpty(object.get(entry.key)))
134
+ continue;
135
+ }
136
+ else {
137
+ // A relative date set on every save would move with each one; the
138
+ // editor does not offer it, and a hand-written config does not get it.
139
+ if (rule.value.kind === "relative" && (rule.on ?? "always") === "always")
140
+ continue;
141
+ const becameTrue = !before || firstMatch(entry, before)?.id !== rule.id;
142
+ if (!fires(rule.on, creating, becameTrue))
143
+ continue;
144
+ }
145
+ const value = storedValue(entry.key, rule.value, now);
146
+ if (value === undefined)
147
+ continue;
148
+ object.set(entry.key, value);
149
+ }
150
+ }
151
+ /** Union of id lists, de-duplicated, in order. */
152
+ function withIds(existing, ids) {
153
+ return [...new Set([...(0, openservice_shared_1.refIds)(existing), ...ids])];
154
+ }
155
+ /**
156
+ * The action rules of `config`. Mirrors `applyTicketRules` and the "assign"
157
+ * handler of the frontend: the users and roles are added to the assignees.
158
+ * Who was assigned this way is notified by the ticket's afterSave like anyone
159
+ * else newly assigned.
160
+ */
161
+ function applyActionRules(rules, object, original) {
162
+ const creating = !original;
163
+ const after = readerOf(object);
164
+ const before = original ? readerOf(original) : undefined;
165
+ for (const rule of rules) {
166
+ if (rule?.function !== "assign" || !rule.ruleset)
167
+ continue;
168
+ if (!holds(rule.ruleset, after))
169
+ continue;
170
+ const becameTrue = !before || !holds(rule.ruleset, before);
171
+ if (!fires(rule.params?.on, creating, becameTrue))
172
+ continue;
173
+ const assignees = Array.isArray(rule.params?.assignees)
174
+ ? rule.params.assignees.filter((a) => typeof a === "string")
175
+ : [];
176
+ const users = assignees
177
+ .filter((a) => a.startsWith("user:"))
178
+ .map((a) => a.slice("user:".length));
179
+ const roles = assignees
180
+ .filter((a) => a.startsWith("role:"))
181
+ .map((a) => a.slice("role:".length));
182
+ if (users.length > 0) {
183
+ const next = withIds(object.get("assignedUsers"), users);
184
+ if (next.length !== (0, openservice_shared_1.refIds)(object.get("assignedUsers")).length)
185
+ object.set("assignedUsers", next);
186
+ }
187
+ if (roles.length > 0) {
188
+ const next = withIds(object.get("assignedRoles"), roles);
189
+ if (next.length !== (0, openservice_shared_1.refIds)(object.get("assignedRoles")).length)
190
+ object.set("assignedRoles", next);
191
+ }
192
+ }
193
+ }
194
+ /**
195
+ * What a form config does to a ticket on save, in the order the frontend used
196
+ * to apply it: the computed priority, the field value rules, the action rules
197
+ * — so a rule on the priority sees the computed one.
198
+ *
199
+ * Which config: the one the form that saves was showing, when it names it in
200
+ * the save's context (`{ formConfig: <id> }`, see `configFromContext`) — a
201
+ * form opened by QR code shows another config than the user's own, and its
202
+ * rules are what the user saw. Otherwise (the admin table, the REST API, an
203
+ * alarm) the one that applies to the user saving (`applicableFormConfig`).
204
+ *
205
+ * The rules act on a save somebody makes — a person, an integration, an alarm
206
+ * creating a ticket. Not on the server's own bookkeeping: a master-key update
207
+ * (sort keys pushed down, the scheduler archiving) would otherwise re-assign
208
+ * people that were taken off by hand, through an "always" rule. The priority
209
+ * follows its inputs on any save.
210
+ */
211
+ async function applyFormConfigRules(object, original, user, { master, sessionToken, context, }) {
212
+ const runRules = !original || !master;
213
+ if (!runRules && !(0, ticketPriority_js_1.changesPriorityInputs)(object, original))
214
+ return;
215
+ const tenant = object.get("tenant");
216
+ const config = (await (0, ticketFormConfig_js_1.configFromContext)(context?.formConfig, tenant, {
217
+ master,
218
+ sessionToken,
219
+ })) ?? (await (0, ticketFormConfig_js_1.applicableFormConfig)(tenant, user));
220
+ (0, ticketPriority_js_1.applyComputedPriority)(object, original, config);
221
+ if (!runRules || !config)
222
+ return;
223
+ applyValueRules(parseJson(config.fields), object, original, new Date());
224
+ applyActionRules(parseJson(config.rules), object, original);
225
+ }
@@ -3,7 +3,10 @@
3
3
  * the detail edit and the import of `@opendash/plugin-openservice-ticket`.
4
4
  *
5
5
  * Params: `{ tenant?: string }`, the active tenant (default: the caller's
6
- * own). Returns `{ id: string | null }`; `null` means the built-in form.
6
+ * own). Returns `{ id: string | null, appliesRules: true }`; `null` means the
7
+ * built-in form. `appliesRules` tells the frontend that the ticket hook
8
+ * applies the config's computed priority and save-time rules itself, so the
9
+ * browser must not apply them again on save (see `applyFormConfigRules`).
7
10
  *
8
11
  * **Why a function.** Resolution goes by the caller's roles, inherited ones
9
12
  * included, and the browser cannot resolve them: it knows its roles by name, a
@@ -12,7 +12,10 @@ const index_js_1 = require("../types/index.js");
12
12
  * the detail edit and the import of `@opendash/plugin-openservice-ticket`.
13
13
  *
14
14
  * Params: `{ tenant?: string }`, the active tenant (default: the caller's
15
- * own). Returns `{ id: string | null }`; `null` means the built-in form.
15
+ * own). Returns `{ id: string | null, appliesRules: true }`; `null` means the
16
+ * built-in form. `appliesRules` tells the frontend that the ticket hook
17
+ * applies the config's computed priority and save-time rules itself, so the
18
+ * browser must not apply them again on save (see `applyFormConfigRules`).
16
19
  *
17
20
  * **Why a function.** Resolution goes by the caller's roles, inherited ones
18
21
  * included, and the browser cannot resolve them: it knows its roles by name, a
@@ -42,10 +45,10 @@ async function init(name) {
42
45
  : user.get("tenant");
43
46
  const config = await (0, ticketFormConfig_js_1.applicableFormConfig)(tenant, user);
44
47
  if (!config?.id)
45
- return { id: null };
48
+ return { id: null, appliesRules: true };
46
49
  const readable = await new node_1.default.Query(index_js_1.Service_FormConfig)
47
50
  .equalTo("objectId", config.id)
48
51
  .first({ sessionToken });
49
- return { id: readable?.id ?? null };
52
+ return { id: readable?.id ?? null, appliesRules: true };
50
53
  }, { requireUser: true });
51
54
  }
@@ -11,7 +11,7 @@ const ticketReferences_js_1 = require("../features/openservice/tickets/ticketRef
11
11
  const ticketAccess_js_1 = require("../features/openservice/tickets/ticketAccess.js");
12
12
  const ticketNotifications_js_1 = require("../features/openservice/tickets/ticketNotifications.js");
13
13
  const ticketArchive_js_1 = require("../features/openservice/tickets/ticketArchive.js");
14
- const ticketPriority_js_1 = require("../features/openservice/tickets/ticketPriority.js");
14
+ const ticketRules_js_1 = require("../features/openservice/tickets/ticketRules.js");
15
15
  async function init() {
16
16
  // Soft delete: a ticket is referenced by chat messages, meta entries,
17
17
  // notifications and child tickets, so the frontend archives instead of
@@ -58,6 +58,20 @@ async function init() {
58
58
  if (isNew && !(0, index_js_1.isMigrationMode)()) {
59
59
  await (0, index_js_2.requirePermission)(request, Permissions_js_1.Permissions.SERVICE.ticket_create, "create a ticket", object.get("tenant"));
60
60
  }
61
+ // What the form config does on save — the one the saving form names in
62
+ // the context, else the user's own: the computed priority, the field value
63
+ // rules, the assignment rules. The forms show it live; this is what holds
64
+ // for every write path. Before everything that follows from those values
65
+ // — the assignable-role check and the ACL (a rule assigns), the tenant
66
+ // check and sort keys (a rule sets a status or project), `closedAt`, the
67
+ // number. A migration replays history as it was.
68
+ if (!(0, index_js_1.isMigrationMode)()) {
69
+ await (0, ticketRules_js_1.applyFormConfigRules)(object, original, user, {
70
+ master: !!request.master,
71
+ sessionToken: request.sessionToken ?? undefined,
72
+ context: request.context,
73
+ });
74
+ }
61
75
  // No `allowTenantUserWrite`: who may edit a ticket is decided per record by
62
76
  // `setTicketACL` below — creator, assigned users, assigned roles — and a
63
77
  // blanket write grant for every user in the tenant would make that decision
@@ -81,12 +95,6 @@ async function init() {
81
95
  (0, ticketReferences_js_1.applyClosedAt)(object, referenced);
82
96
  (0, ticketArchive_js_1.applyArchiveReason)(object, original, !!request.master);
83
97
  (0, ticketArchive_js_1.applySimilarCaseUse)(object, original);
84
- // The priority a form config derives from downtime, restriction and
85
- // frequency. The ticket forms compute it as well, to show it while the
86
- // user types — here it holds for every other write path (the admin table,
87
- // the REST API). A migration replays the stored priority as it was.
88
- if (!(0, index_js_1.isMigrationMode)())
89
- await (0, ticketPriority_js_1.applyComputedPriority)(object, original, user);
90
98
  // Assign a sequential ticket number on creation. Done here (backend) rather
91
99
  // than in the frontend because every write path — the create form, the
92
100
  // cross-plugin service, and the generic entity admin — funnels through this
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openinc/parse-server-opendash",
3
- "version": "4.26.0",
3
+ "version": "4.27.0",
4
4
  "description": "Parse Server Cloud Code for open.INC Stack.",
5
5
  "packageManager": "pnpm@12.8.1",
6
6
  "keywords": [