@openinc/parse-server-opendash 4.34.0 → 4.36.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 (39) hide show
  1. package/dist/featuremap.json +0 -1
  2. package/dist/features/openservice/alarms/ensureAlarmActionRow.d.ts +32 -0
  3. package/dist/features/openservice/alarms/ensureAlarmActionRow.js +67 -0
  4. package/dist/features/openservice/alarms/ensureOpenwareWebhook.d.ts +22 -0
  5. package/dist/features/openservice/alarms/ensureOpenwareWebhook.js +81 -0
  6. package/dist/features/openservice/alarms/index.d.ts +9 -1
  7. package/dist/features/openservice/alarms/index.js +17 -2
  8. package/dist/features/openservice/alarms/openwareAlarm.d.ts +76 -0
  9. package/dist/features/openservice/alarms/openwareAlarm.js +96 -0
  10. package/dist/features/openservice/alarms/readAlarmCall.d.ts +26 -0
  11. package/dist/features/openservice/alarms/readAlarmCall.js +67 -0
  12. package/dist/features/openservice/alarms/resolveAlarmUser.d.ts +16 -0
  13. package/dist/features/openservice/alarms/resolveAlarmUser.js +35 -0
  14. package/dist/features/openservice/alarms/resolveEquipment.js +4 -1
  15. package/dist/features/openservice/alarms/schedule/createAlarmSchedule.js +2 -8
  16. package/dist/features/openservice/alarms/schedule/ensureAlarmScheduleAction.d.ts +3 -36
  17. package/dist/features/openservice/alarms/schedule/ensureAlarmScheduleAction.js +12 -140
  18. package/dist/features/openservice/alarms/ticket/AlarmTicketTypes.d.ts +14 -0
  19. package/dist/features/openservice/alarms/ticket/alarmTicketDefaults.d.ts +34 -0
  20. package/dist/features/openservice/alarms/ticket/alarmTicketDefaults.js +80 -0
  21. package/dist/features/openservice/alarms/ticket/createAlarmTicket.js +72 -7
  22. package/dist/features/openservice/alarms/ticket/ensureAlarmTicketAction.d.ts +3 -19
  23. package/dist/features/openservice/alarms/ticket/ensureAlarmTicketAction.js +33 -91
  24. package/dist/features/openservice/init.js +11 -3
  25. package/dist/functions/openinc-openservice-alarm-create-schedule.d.ts +7 -9
  26. package/dist/functions/openinc-openservice-alarm-create-schedule.js +11 -31
  27. package/dist/functions/openinc-openservice-alarm-create-ticket.d.ts +7 -9
  28. package/dist/functions/openinc-openservice-alarm-create-ticket.js +11 -31
  29. package/dist/types/Service_Tag.d.ts +3 -0
  30. package/dist/types/Service_Tag.js +6 -0
  31. package/dist/types/index.d.ts +0 -2
  32. package/dist/types/index.js +1 -3
  33. package/package.json +1 -1
  34. package/schema/Service_Tag.json +3 -0
  35. package/dist/hooks/Service_EquipmentAnnotation.d.ts +0 -1
  36. package/dist/hooks/Service_EquipmentAnnotation.js +0 -15
  37. package/dist/types/Service_EquipmentAnnotation.d.ts +0 -35
  38. package/dist/types/Service_EquipmentAnnotation.js +0 -57
  39. package/schema/Service_EquipmentAnnotation.json +0 -57
@@ -1,39 +1,6 @@
1
1
  /**
2
- * The HTTP call that carries the action out.
3
- *
4
- * `type: "webhook"` means the executor runs an `OD3_AlarmWebhook` row rather
5
- * than calling anything itself. That row used to be created by hand — and a
6
- * hand-made one is how an installation ends up with `payload: "{}"`, which
7
- * reaches the endpoint carrying neither secret nor event, so nothing happens
8
- * and nothing says why.
9
- *
10
- * Found again by `url`, not by name: the url is what makes two rows the same
11
- * webhook, a name is free text somebody renames.
12
- *
13
- * ⚠️ **The url is relative.** That is what the existing hand-made rows use and
14
- * what this server answers, but whether the executor resolves it against the
15
- * Parse base or needs an absolute address is open.WARE's business. If a fired
16
- * alarm never arrives, check this first.
17
- */
18
- export declare function ensureAlarmScheduleWebhook(): Promise<void>;
19
- /**
20
- * Make "schedule a maintenance appointment" selectable in the alarm dialog.
21
- *
22
- * Alarm actions are **data**, not code: `OD3_AlarmAction` rows that the
23
- * monitoring adapter loads into the frontend, where the chosen row's
24
- * `formFields` are rendered as the action's settings form (see
25
- * `plugins/monitoring/src/components/AlarmCreate.tsx`). There is no plugin-side
26
- * `registerAlarmAction`, so the row is seeded here — find-or-create by `topic`,
27
- * like the ticket action and the other open.SERVICE bootstraps.
28
- *
29
- * The row is **global** (no tenant): the action makes sense for every tenant,
30
- * and the tenant of a fired alarm is established by its secret, not by this row.
31
- *
32
- * **How it is carried out.** `type: "webhook"` means the executor runs an
33
- * `OD3_AlarmWebhook` row, which POSTs to a URL. That row is created by hand for
34
- * now (an admin UI can come later) and must point at
35
- * `/parse/functions/openinc-openservice-alarm-create-schedule` with the payload
36
- * below. The `target` here names the same path so the two are readable side by
37
- * side, and so a webhook-less executor that reads `target` directly still works.
2
+ * Make "schedule a maintenance appointment" selectable in the alarm dialog and
3
+ * executable by open.WARE: the `OD3_AlarmAction` row and the
4
+ * `OD3_AlarmWebhook` row it targets (see `ensureAlarmActionRow`).
38
5
  */
39
6
  export declare function ensureAlarmScheduleAction(): Promise<void>;
@@ -1,150 +1,22 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ensureAlarmScheduleWebhook = ensureAlarmScheduleWebhook;
7
3
  exports.ensureAlarmScheduleAction = ensureAlarmScheduleAction;
8
- const node_1 = __importDefault(require("parse/node"));
9
- const index_js_1 = require("../../../../types/index.js");
4
+ const ensureAlarmActionRow_js_1 = require("../ensureAlarmActionRow.js");
10
5
  const AlarmScheduleTypes_js_1 = require("./AlarmScheduleTypes.js");
11
- /** Name of the webhook row. Free text — the url is what identifies it. */
12
- const WEBHOOK_NAME = "open.SERVICE: Wartungstermin anlegen";
13
6
  /**
14
- * What the executor posts. Placeholders are substituted by open.WARE.
15
- *
16
- * Kept identical to the action row's own `payload`: the two are the same
17
- * contract written down twice, and a difference between them is a bug waiting
18
- * for whichever one the executor happens to read.
19
- */
20
- const WEBHOOK_PAYLOAD = JSON.stringify({
21
- secret: "{{secret}}",
22
- event: {
23
- alarmId: "{{alarm.id}}",
24
- name: "{{alarm.name}}",
25
- userId: "{{alarm.user}}",
26
- item_id: "{{item.id}}",
27
- item_source: "{{item.source}}",
28
- item_dimension: "{{item.dimension}}",
29
- value: "{{value}}",
30
- condition: "{{condition}}",
31
- firedAt: "{{timestamp}}",
32
- },
33
- params: "{{action}}",
34
- });
35
- /**
36
- * The HTTP call that carries the action out.
37
- *
38
- * `type: "webhook"` means the executor runs an `OD3_AlarmWebhook` row rather
39
- * than calling anything itself. That row used to be created by hand — and a
40
- * hand-made one is how an installation ends up with `payload: "{}"`, which
41
- * reaches the endpoint carrying neither secret nor event, so nothing happens
42
- * and nothing says why.
43
- *
44
- * Found again by `url`, not by name: the url is what makes two rows the same
45
- * webhook, a name is free text somebody renames.
46
- *
47
- * ⚠️ **The url is relative.** That is what the existing hand-made rows use and
48
- * what this server answers, but whether the executor resolves it against the
49
- * Parse base or needs an absolute address is open.WARE's business. If a fired
50
- * alarm never arrives, check this first.
51
- */
52
- async function ensureAlarmScheduleWebhook() {
53
- const url = "/parse/functions/openinc-openservice-alarm-create-schedule";
54
- const existing = await new node_1.default.Query(index_js_1.AlarmWebhook)
55
- .equalTo("url", url)
56
- .first({ useMasterKey: true });
57
- if (existing) {
58
- // An empty payload is the failure this function exists to prevent, so it
59
- // is repaired. One somebody deliberately customised is left alone.
60
- const payload = existing.get("payload");
61
- if (!payload || payload.trim() === "" || payload.trim() === "{}") {
62
- existing.set("payload", WEBHOOK_PAYLOAD);
63
- await existing.save(null, { useMasterKey: true });
64
- console.log(`[AlarmSchedule] Filled in the empty payload of "${url}"`);
65
- }
66
- return;
67
- }
68
- const webhook = new index_js_1.AlarmWebhook();
69
- webhook.set("name", WEBHOOK_NAME);
70
- webhook.set("url", url);
71
- webhook.set("method", "POST");
72
- webhook.set("payload", WEBHOOK_PAYLOAD);
73
- webhook.set("header", { "Content-Type": "application/json" });
74
- webhook.set("extraOptions", {});
75
- await webhook.save(null, { useMasterKey: true });
76
- console.log(`[AlarmSchedule] Registered webhook "${WEBHOOK_NAME}"`);
77
- }
78
- /**
79
- * Make "schedule a maintenance appointment" selectable in the alarm dialog.
80
- *
81
- * Alarm actions are **data**, not code: `OD3_AlarmAction` rows that the
82
- * monitoring adapter loads into the frontend, where the chosen row's
83
- * `formFields` are rendered as the action's settings form (see
84
- * `plugins/monitoring/src/components/AlarmCreate.tsx`). There is no plugin-side
85
- * `registerAlarmAction`, so the row is seeded here — find-or-create by `topic`,
86
- * like the ticket action and the other open.SERVICE bootstraps.
87
- *
88
- * The row is **global** (no tenant): the action makes sense for every tenant,
89
- * and the tenant of a fired alarm is established by its secret, not by this row.
90
- *
91
- * **How it is carried out.** `type: "webhook"` means the executor runs an
92
- * `OD3_AlarmWebhook` row, which POSTs to a URL. That row is created by hand for
93
- * now (an admin UI can come later) and must point at
94
- * `/parse/functions/openinc-openservice-alarm-create-schedule` with the payload
95
- * below. The `target` here names the same path so the two are readable side by
96
- * side, and so a webhook-less executor that reads `target` directly still works.
7
+ * Make "schedule a maintenance appointment" selectable in the alarm dialog and
8
+ * executable by open.WARE: the `OD3_AlarmAction` row and the
9
+ * `OD3_AlarmWebhook` row it targets (see `ensureAlarmActionRow`).
97
10
  */
98
11
  async function ensureAlarmScheduleAction() {
99
- const existing = await new node_1.default.Query(index_js_1.AlarmAction)
100
- .equalTo("topic", AlarmScheduleTypes_js_1.ALARM_SCHEDULE_ACTION_ID)
101
- .first({ useMasterKey: true });
102
- if (existing) {
103
- // The form definition is code, not customer configuration, so it is kept
104
- // up to date rather than frozen at whatever the first boot wrote. Without
105
- // this a fix to `FORM_FIELDS` would only ever reach a fresh database —
106
- // which is how every existing installation ended up with pickers that
107
- // store whole objects instead of ids.
108
- if (JSON.stringify(existing.get("formFields")) !== JSON.stringify(FORM_FIELDS)) {
109
- existing.set("formFields", FORM_FIELDS);
110
- await existing.save(null, { useMasterKey: true });
111
- console.log(`[AlarmSchedule] Updated form fields of "${AlarmScheduleTypes_js_1.ALARM_SCHEDULE_ACTION_ID}"`);
112
- }
113
- return;
114
- }
115
- const action = new index_js_1.AlarmAction();
116
- action.set("label", "Wartungstermin anlegen (open.SERVICE)");
117
- action.set("type", "webhook");
118
- action.set("topic", AlarmScheduleTypes_js_1.ALARM_SCHEDULE_ACTION_ID);
119
- action.set("target", "/parse/functions/openinc-openservice-alarm-create-schedule");
120
- action.set("templateType", "json");
121
- action.set("payload", JSON.stringify({
122
- secret: "{{secret}}",
123
- event: {
124
- alarmId: "{{alarm.id}}",
125
- name: "{{alarm.name}}",
126
- userId: "{{alarm.user}}",
127
- item_id: "{{item.id}}",
128
- item_source: "{{item.source}}",
129
- item_dimension: "{{item.dimension}}",
130
- value: "{{value}}",
131
- condition: "{{condition}}",
132
- firedAt: "{{timestamp}}",
133
- },
134
- params: "{{action}}",
135
- }));
136
- // Wear is not only a number: a boolean "filter clogged" or a string state
137
- // code is just as good a reason to schedule maintenance.
138
- action.set("supportedTypes", [
139
- "Number",
140
- "String",
141
- "Boolean",
142
- "Geo",
143
- "Object",
144
- ]);
145
- action.set("formFields", FORM_FIELDS);
146
- await action.save(null, { useMasterKey: true });
147
- console.log(`[AlarmSchedule] Registered alarm action "${AlarmScheduleTypes_js_1.ALARM_SCHEDULE_ACTION_ID}"`);
12
+ await (0, ensureAlarmActionRow_js_1.ensureAlarmActionRow)({
13
+ topic: AlarmScheduleTypes_js_1.ALARM_SCHEDULE_ACTION_ID,
14
+ label: "Wartungstermin anlegen (open.SERVICE)",
15
+ functionName: "openinc-openservice-alarm-create-schedule",
16
+ webhookName: "open.SERVICE: Wartungstermin anlegen",
17
+ webhookComment: "Von open.SERVICE angelegt und gepflegt — Änderungen werden beim Serverstart überschrieben.",
18
+ formFields: FORM_FIELDS,
19
+ });
148
20
  }
149
21
  /**
150
22
  * What the person configuring an alarm gets to fill in.
@@ -41,6 +41,20 @@ export interface AlarmTicketParams {
41
41
  assignedUserIds?: string[];
42
42
  assignedRoleIds?: string[];
43
43
  tagIds?: string[];
44
+ /**
45
+ * Ticket form config (`OD3_Service_FormConfig`) the ticket is created
46
+ * through: its field defaults pre-fill the ticket, its rules run on save.
47
+ * Unset or not the tenant's: the tenant's default form.
48
+ */
49
+ formConfigId?: string;
50
+ /**
51
+ * The ticket as configured with the ticket form in template mode, under
52
+ * {@link formConfigId}: field values keyed by ticket field, references as
53
+ * ids (see `readTicketDefaults`). Wins over the flat fields above, which
54
+ * alarms configured with the generic form carry; both win over the form
55
+ * config's defaults.
56
+ */
57
+ ticket?: Record<string, unknown>;
44
58
  /**
45
59
  * Reopen behaviour. `false` (default) keeps **one open ticket per alarm**: a
46
60
  * flapping sensor does not produce a hundred tickets, and closing the ticket
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Values the ticket an alarm creates starts out with — the alarm's template
3
+ * (the ticket form in template mode) or what its form config pre-fills
4
+ * (`defaultValue` of its fields). Both in the form's edit shape: field values
5
+ * keyed by ticket field, references as ids.
6
+ *
7
+ * Read here into exactly the fields a default may set. Dates are left out (a
8
+ * fixed date makes no sense for an alarm that fires later; the config's
9
+ * relative value rules still apply on save), and so are the fields the server
10
+ * owns (tenant, number, origin, closedAt …). Title and description come back
11
+ * apart: the alarm's own title wins over them, and the alarm's details are
12
+ * appended to the description (`alarmTicketFields`). Whether a referenced row
13
+ * belongs to the tenant is the `Service_Ticket` hook's call, as on every write
14
+ * path.
15
+ */
16
+ export interface AlarmTicketDefaults {
17
+ title?: string;
18
+ description?: string;
19
+ status?: string;
20
+ equipment?: string;
21
+ project?: string;
22
+ /** The remaining fields, ready for `ticket.set`. */
23
+ values: Record<string, unknown>;
24
+ }
25
+ /**
26
+ * The defaults of a form config's `fields` (its JSON column). Hidden fields
27
+ * count: the create form keeps them as hidden inputs and saves their default.
28
+ *
29
+ * shortcut: `visibleWhen` is not evaluated — a default of a field hidden by
30
+ * condition is applied anyway; read it with the ticket's values if a config
31
+ * ever relies on that.
32
+ */
33
+ export declare function formConfigDefaults(fields: string | undefined): AlarmTicketDefaults;
34
+ export declare function readTicketDefaults(raw: unknown): AlarmTicketDefaults;
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formConfigDefaults = formConfigDefaults;
4
+ exports.readTicketDefaults = readTicketDefaults;
5
+ const actionParams_js_1 = require("../actionParams.js");
6
+ /** Arrays of ids — the schema stores them as plain id strings. */
7
+ const ID_LISTS = ["tags", "assignedUsers", "assignedRoles"];
8
+ const NUMBERS = [
9
+ "priority",
10
+ "downtime",
11
+ "frequency",
12
+ "restriction",
13
+ "estimatedDuration",
14
+ "actualDuration",
15
+ ];
16
+ /** Stored raw, as the ticket form stores them. */
17
+ const JSON_FIELDS = ["checklist", "material"];
18
+ const BOOLEANS = [
19
+ "excludeFromProductionDashboard",
20
+ "useForSimilarCases",
21
+ ];
22
+ /**
23
+ * The defaults of a form config's `fields` (its JSON column). Hidden fields
24
+ * count: the create form keeps them as hidden inputs and saves their default.
25
+ *
26
+ * shortcut: `visibleWhen` is not evaluated — a default of a field hidden by
27
+ * condition is applied anyway; read it with the ticket's values if a config
28
+ * ever relies on that.
29
+ */
30
+ function formConfigDefaults(fields) {
31
+ let entries;
32
+ try {
33
+ entries = JSON.parse(fields ?? "[]");
34
+ }
35
+ catch {
36
+ entries = [];
37
+ }
38
+ const raw = {};
39
+ for (const entry of Array.isArray(entries) ? entries : []) {
40
+ if (typeof entry?.key === "string" && entry.defaultValue != null) {
41
+ raw[entry.key] = entry.defaultValue;
42
+ }
43
+ }
44
+ return readTicketDefaults(raw);
45
+ }
46
+ function readTicketDefaults(raw) {
47
+ const source = raw && typeof raw === "object" && !Array.isArray(raw)
48
+ ? raw
49
+ : {};
50
+ const values = {};
51
+ for (const key of ID_LISTS) {
52
+ const list = Array.isArray(source[key]) ? source[key] : [];
53
+ const ids = list
54
+ .map(actionParams_js_1.paramId)
55
+ .filter((id) => id !== undefined);
56
+ if (ids.length > 0)
57
+ values[key] = ids;
58
+ }
59
+ for (const key of NUMBERS) {
60
+ const value = (0, actionParams_js_1.paramNumber)(source[key]);
61
+ if (value !== undefined)
62
+ values[key] = value;
63
+ }
64
+ for (const key of JSON_FIELDS) {
65
+ if (source[key] != null)
66
+ values[key] = source[key];
67
+ }
68
+ for (const key of BOOLEANS) {
69
+ if (typeof source[key] === "boolean")
70
+ values[key] = source[key];
71
+ }
72
+ return {
73
+ title: (0, actionParams_js_1.paramString)(source.title),
74
+ description: (0, actionParams_js_1.paramString)(source.description),
75
+ status: (0, actionParams_js_1.paramId)(source.status),
76
+ equipment: (0, actionParams_js_1.paramId)(source.equipment),
77
+ project: (0, actionParams_js_1.paramId)(source.project),
78
+ values,
79
+ };
80
+ }
@@ -8,11 +8,14 @@ exports.tenantForAlarmTicket = tenantForAlarmTicket;
8
8
  const node_1 = __importDefault(require("parse/node"));
9
9
  const openservice_shared_1 = require("@opendash/openservice-shared");
10
10
  const index_js_1 = require("../../../../types/index.js");
11
+ const ticketFormConfig_js_1 = require("../../tickets/ticketFormConfig.js");
11
12
  const actionParams_js_1 = require("../actionParams.js");
12
13
  const alarmIdentity_js_1 = require("../alarmIdentity.js");
14
+ const resolveAlarmUser_js_1 = require("../resolveAlarmUser.js");
13
15
  const resolveEquipment_js_1 = require("../resolveEquipment.js");
14
16
  const tenantForSecret_js_1 = require("../tenantForSecret.js");
15
17
  const alarmTicketFields_js_1 = require("./alarmTicketFields.js");
18
+ const alarmTicketDefaults_js_1 = require("./alarmTicketDefaults.js");
16
19
  const AlarmTicketTypes_js_1 = require("./AlarmTicketTypes.js");
17
20
  /**
18
21
  * Create the ticket a fired alarm asks for — or return the one that is already
@@ -31,13 +34,25 @@ async function createAlarmTicket(tenant, event, rawParams = {}) {
31
34
  if (!(0, alarmIdentity_js_1.alarmKey)(event)) {
32
35
  throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, "The alarm payload identifies no alarm: pass alarmId, or item_source/item_id.");
33
36
  }
34
- // Same normalisation as the schedule action: the reference fields can arrive
35
- // as whole objects from alarms configured before the form set a `valueField`
36
- // (see `actionParams.ts`).
37
+ // The ticket is created through a form config, as in the frontend. Per
38
+ // field the alarm's template wins (the ticket form in template mode), then
39
+ // the flat fields of the generic action form, then the config's defaults.
40
+ // Same normalisation as the schedule action for the flat ones: references
41
+ // can arrive as whole objects from alarms configured before the form set a
42
+ // `valueField` (see `actionParams.ts`).
43
+ const config = await alarmFormConfig(tenant, (0, actionParams_js_1.paramId)(rawParams.formConfigId));
44
+ const template = (0, alarmTicketDefaults_js_1.readTicketDefaults)(rawParams.ticket);
45
+ const defaults = (0, alarmTicketDefaults_js_1.formConfigDefaults)(config?.fields);
37
46
  const params = {
38
47
  ...rawParams,
39
- equipmentId: (0, actionParams_js_1.paramId)(rawParams.equipmentId),
40
- projectId: (0, actionParams_js_1.paramId)(rawParams.projectId),
48
+ title: template.title ?? (0, actionParams_js_1.paramString)(rawParams.title) ?? defaults.title,
49
+ description: template.description ??
50
+ (0, actionParams_js_1.paramString)(rawParams.description) ??
51
+ defaults.description,
52
+ equipmentId: template.equipment ??
53
+ (0, actionParams_js_1.paramId)(rawParams.equipmentId) ??
54
+ defaults.equipment,
55
+ projectId: template.project ?? (0, actionParams_js_1.paramId)(rawParams.projectId) ?? defaults.project,
41
56
  };
42
57
  const fields = (0, alarmTicketFields_js_1.alarmTicketFields)(event, params);
43
58
  if (!params.duplicate) {
@@ -51,7 +66,8 @@ async function createAlarmTicket(tenant, event, rawParams = {}) {
51
66
  };
52
67
  }
53
68
  }
54
- const status = await resolveDefaultStatus(tenant);
69
+ const status = (await presetStatus(tenant, template.status ?? defaults.status)) ??
70
+ (await resolveDefaultStatus(tenant));
55
71
  if (!status) {
56
72
  throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, `No "${openservice_shared_1.DEFAULT_STATUS_CATEGORY}" status configured for this tenant — cannot create a ticket.`);
57
73
  }
@@ -68,6 +84,16 @@ async function createAlarmTicket(tenant, event, rawParams = {}) {
68
84
  ticket.set("startDate", (0, alarmIdentity_js_1.firedAt)(event));
69
85
  if (equipment)
70
86
  ticket.set("equipment", equipment);
87
+ // The alarm's owner is the ticket's creator: it shows up as such, and the
88
+ // ACL gives them write access to what their alarm raised.
89
+ const creator = await (0, resolveAlarmUser_js_1.resolveAlarmUser)(tenant, event.userId, `[${AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID}]`);
90
+ if (creator)
91
+ ticket.set("createdBy", creator);
92
+ // The config's remaining defaults first: the flat fields below and the
93
+ // template after them override them.
94
+ for (const [key, value] of Object.entries(defaults.values)) {
95
+ ticket.set(key, value);
96
+ }
71
97
  if (params.priority != null)
72
98
  ticket.set("priority", params.priority);
73
99
  if (params.projectId) {
@@ -85,7 +111,16 @@ async function createAlarmTicket(tenant, event, rawParams = {}) {
85
111
  }
86
112
  if (params.tagIds?.length)
87
113
  ticket.set("tags", params.tagIds);
88
- await ticket.save(null, { useMasterKey: true });
114
+ for (const [key, value] of Object.entries(template.values)) {
115
+ ticket.set(key, value);
116
+ }
117
+ // Naming the config makes the `Service_Ticket` hook run exactly its rules
118
+ // (priority formula, value rules, action rules). The hook also rejects master
119
+ // data of another tenant, as on every other way a ticket is written.
120
+ await ticket.save(null, {
121
+ useMasterKey: true,
122
+ context: { formConfig: config?.id },
123
+ });
89
124
  return {
90
125
  ticketId: ticket.id,
91
126
  created: true,
@@ -114,6 +149,36 @@ async function findOpenTicket(tenant, origin) {
114
149
  return !(0, openservice_shared_1.isDoneCategory)(category);
115
150
  });
116
151
  }
152
+ /**
153
+ * The form config the alarm names, if it is the tenant's — else the tenant's
154
+ * default form, the one a save without a user gets (`applicableFormConfig`).
155
+ */
156
+ async function alarmFormConfig(tenant, id) {
157
+ const chosen = await (0, ticketFormConfig_js_1.configFromContext)(id, tenant, {
158
+ master: true,
159
+ sessionToken: undefined,
160
+ });
161
+ if (id && !chosen) {
162
+ console.warn(`[${AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID}] Form config ${id} is not this tenant's — using the tenant's default form`);
163
+ }
164
+ return chosen ?? (await (0, ticketFormConfig_js_1.applicableFormConfig)(tenant, undefined));
165
+ }
166
+ /**
167
+ * The status the template or the form config names, if it is the tenant's or
168
+ * a global one.
169
+ */
170
+ async function presetStatus(tenant, statusId) {
171
+ if (!statusId)
172
+ return undefined;
173
+ const status = await node_1.default.Query.or(new node_1.default.Query(index_js_1.Service_Status).equalTo("tenant", tenant), new node_1.default.Query(index_js_1.Service_Status).doesNotExist("tenant"))
174
+ .equalTo("objectId", statusId)
175
+ .equalTo("deleted", false)
176
+ .first({ useMasterKey: true });
177
+ if (!status) {
178
+ console.warn(`[${AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID}] Preset status ${statusId} is not available — using the inbox`);
179
+ }
180
+ return status;
181
+ }
117
182
  /** The tenant's inbox status, else a global one — never another tenant's. */
118
183
  async function resolveDefaultStatus(tenant) {
119
184
  // Only this tenant's and the global ones: across all tenants a plain `find`
@@ -1,22 +1,6 @@
1
1
  /**
2
- * Make "create a ticket" selectable in the alarm dialog.
3
- *
4
- * Alarm actions are **data**, not code: `OD3_AlarmAction` rows that the
5
- * monitoring adapter loads into the frontend, where the chosen row's
6
- * `formFields` are rendered as the action's settings form (see
7
- * `plugins/monitoring/src/components/AlarmCreate.tsx`). There is no plugin-side
8
- * `registerAlarmAction`, so the row is seeded here — find-or-create by `topic`,
9
- * like the other open.SERVICE bootstraps.
10
- *
11
- * The row is **global** (no tenant): the action makes sense for every tenant,
12
- * and the tenant of a fired alarm is established by its secret, not by this
13
- * row.
14
- *
15
- * ⚠️ `type` / `target` / `payload` are the executor's half of the contract —
16
- * the component that evaluates alarms lives outside this server. The values
17
- * below assume a webhook-style executor that POSTs `payload` (placeholders
18
- * filled from the alarm) to `target`. If the executor expects something else,
19
- * this row is where it is corrected; the receiving endpoint accepts any caller
20
- * that brings the secret.
2
+ * Make "create a ticket" selectable in the alarm dialog and executable by
3
+ * open.WARE: the `OD3_AlarmAction` row and the `OD3_AlarmWebhook` row it
4
+ * targets (see `ensureAlarmActionRow`).
21
5
  */
22
6
  export declare function ensureAlarmTicketAction(): Promise<void>;
@@ -1,81 +1,23 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.ensureAlarmTicketAction = ensureAlarmTicketAction;
7
- const node_1 = __importDefault(require("parse/node"));
8
4
  const index_js_1 = require("../../../../types/index.js");
5
+ const ensureAlarmActionRow_js_1 = require("../ensureAlarmActionRow.js");
9
6
  const AlarmTicketTypes_js_1 = require("./AlarmTicketTypes.js");
10
7
  /**
11
- * Make "create a ticket" selectable in the alarm dialog.
12
- *
13
- * Alarm actions are **data**, not code: `OD3_AlarmAction` rows that the
14
- * monitoring adapter loads into the frontend, where the chosen row's
15
- * `formFields` are rendered as the action's settings form (see
16
- * `plugins/monitoring/src/components/AlarmCreate.tsx`). There is no plugin-side
17
- * `registerAlarmAction`, so the row is seeded here — find-or-create by `topic`,
18
- * like the other open.SERVICE bootstraps.
19
- *
20
- * The row is **global** (no tenant): the action makes sense for every tenant,
21
- * and the tenant of a fired alarm is established by its secret, not by this
22
- * row.
23
- *
24
- * ⚠️ `type` / `target` / `payload` are the executor's half of the contract —
25
- * the component that evaluates alarms lives outside this server. The values
26
- * below assume a webhook-style executor that POSTs `payload` (placeholders
27
- * filled from the alarm) to `target`. If the executor expects something else,
28
- * this row is where it is corrected; the receiving endpoint accepts any caller
29
- * that brings the secret.
8
+ * Make "create a ticket" selectable in the alarm dialog and executable by
9
+ * open.WARE: the `OD3_AlarmAction` row and the `OD3_AlarmWebhook` row it
10
+ * targets (see `ensureAlarmActionRow`).
30
11
  */
31
12
  async function ensureAlarmTicketAction() {
32
- const existing = await new node_1.default.Query(index_js_1.AlarmAction)
33
- .equalTo("topic", AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID)
34
- .first({ useMasterKey: true });
35
- if (existing) {
36
- // The form definition is code, not customer configuration, so it is kept
37
- // up to date rather than frozen at whatever the first boot wrote. Without
38
- // this a fix to `FORM_FIELDS` would only ever reach a fresh database —
39
- // which is how every existing installation ended up with pickers that
40
- // store whole objects instead of ids.
41
- if (JSON.stringify(existing.get("formFields")) !== JSON.stringify(FORM_FIELDS)) {
42
- existing.set("formFields", FORM_FIELDS);
43
- await existing.save(null, { useMasterKey: true });
44
- console.log(`[AlarmTicket] Updated form fields of "${AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID}"`);
45
- }
46
- return;
47
- }
48
- const action = new index_js_1.AlarmAction();
49
- action.set("label", "Ticket erstellen (open.SERVICE)");
50
- action.set("type", "webhook");
51
- action.set("topic", AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID);
52
- action.set("target", "/parse/functions/openinc-openservice-alarm-create-ticket");
53
- action.set("templateType", "json");
54
- action.set("payload", JSON.stringify({
55
- secret: "{{secret}}",
56
- event: {
57
- alarmId: "{{alarm.id}}",
58
- name: "{{alarm.name}}",
59
- item_id: "{{item.id}}",
60
- item_source: "{{item.source}}",
61
- item_dimension: "{{item.dimension}}",
62
- value: "{{value}}",
63
- condition: "{{condition}}",
64
- firedAt: "{{timestamp}}",
65
- },
66
- params: "{{action}}",
67
- }));
68
- // Every value type can breach a rule, so every type may raise a ticket.
69
- action.set("supportedTypes", [
70
- "Number",
71
- "String",
72
- "Boolean",
73
- "Geo",
74
- "Object",
75
- ]);
76
- action.set("formFields", FORM_FIELDS);
77
- await action.save(null, { useMasterKey: true });
78
- console.log(`[AlarmTicket] Registered alarm action "${AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID}"`);
13
+ await (0, ensureAlarmActionRow_js_1.ensureAlarmActionRow)({
14
+ topic: AlarmTicketTypes_js_1.ALARM_TICKET_ACTION_ID,
15
+ label: "Ticket erstellen (open.SERVICE)",
16
+ functionName: "openinc-openservice-alarm-create-ticket",
17
+ webhookName: "open.SERVICE: Ticket erstellen",
18
+ webhookComment: "Von open.SERVICE angelegt und gepflegt — Änderungen werden beim Serverstart überschrieben.",
19
+ formFields: FORM_FIELDS,
20
+ });
79
21
  }
80
22
  /**
81
23
  * What the person configuring an alarm gets to fill in.
@@ -85,22 +27,40 @@ async function ensureAlarmTicketAction() {
85
27
  * `AlarmTicketParams` — the executor hands the form's values back to us as
86
28
  * `params`.
87
29
  *
30
+ * The ticket's content comes from a **ticket form** of the administration
31
+ * (its field defaults and rules), so the alarm only adds what is its own:
32
+ * title, note, machine.
33
+ *
88
34
  * The machine is a plain picker rather than something derived from the sensor,
89
35
  * because nothing in the data model links the two. Whoever sets the alarm up
90
36
  * knows which machine it watches; guessing it later is worse than asking now.
91
37
  */
92
38
  const FORM_FIELDS = [
39
+ {
40
+ key: "formConfigId",
41
+ label: "Ticketformular",
42
+ type: "select.parse",
43
+ settings: {
44
+ className: index_js_1.Service_FormConfig.className,
45
+ displayField: "label",
46
+ mode: "single",
47
+ // Without this the picker stores the whole selected object instead of
48
+ // its id — see actionParams.ts.
49
+ valueField: "objectId",
50
+ },
51
+ hint: "Das Ticket entsteht wie über dieses Formular: mit seinen Standardwerten und Regeln. Leer = Standardformular.",
52
+ },
93
53
  {
94
54
  key: "title",
95
55
  label: "Titel des Tickets",
96
56
  type: "input",
97
- hint: "Platzhalter: {{alarm}}, {{sensor}}, {{value}}. Leer = Name des Alarms.",
57
+ hint: "Platzhalter: {{alarm}}, {{sensor}}, {{value}}. Leer = Titel aus dem Formular, sonst Name des Alarms.",
98
58
  },
99
59
  {
100
60
  key: "description",
101
61
  label: "Notiz",
102
62
  type: "textarea",
103
- hint: "Wird dem Ticket vorangestellt; Sensor, Wert, Regel und Zeitpunkt kommen automatisch dazu.",
63
+ hint: "Leer = Beschreibung aus dem Formular. Sensor, Wert, Regel und Zeitpunkt kommen automatisch dazu.",
104
64
  },
105
65
  {
106
66
  key: "equipmentId",
@@ -114,25 +74,7 @@ const FORM_FIELDS = [
114
74
  // its id — see actionParams.ts.
115
75
  valueField: "objectId",
116
76
  },
117
- hint: "Ohne Angabe wird über die Quellen-Tags der Maschine geraten — oder das Ticket bleibt ohne Maschine.",
118
- },
119
- {
120
- key: "projectId",
121
- label: "Projekt",
122
- type: "select.parse",
123
- settings: {
124
- className: "OD3_Service_Project",
125
- displayField: "label",
126
- mode: "single",
127
- // Without this the picker stores the whole selected object instead of
128
- // its id — see actionParams.ts.
129
- valueField: "objectId",
130
- },
131
- },
132
- {
133
- key: "priority",
134
- label: "Priorität",
135
- type: "input.number",
77
+ hint: "Ohne Angabe die Maschine aus dem Formular, sonst wird über die Quellen-Tags der Maschine geraten.",
136
78
  },
137
79
  {
138
80
  key: "duplicate",