@openinc/parse-server-opendash 4.38.1 → 4.39.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/openservice/alarms/index.d.ts +5 -2
- package/dist/features/openservice/alarms/index.js +6 -5
- package/dist/features/openservice/alarms/schedule/AlarmScheduleTypes.d.ts +4 -2
- package/dist/features/openservice/alarms/schedule/createAlarmSchedule.d.ts +6 -6
- package/dist/features/openservice/alarms/schedule/createAlarmSchedule.js +36 -139
- package/dist/features/openservice/alarms/schedule/ensureAlarmScheduleAction.js +2 -2
- package/dist/features/openservice/alarms/ticket/createAlarmTicket.d.ts +7 -4
- package/dist/features/openservice/alarms/ticket/createAlarmTicket.js +42 -161
- package/dist/features/openservice/alarms/ticket/ensureAlarmTicketAction.js +2 -2
- package/dist/features/openservice/automation/automationCall.d.ts +56 -0
- package/dist/features/openservice/automation/automationCall.js +61 -0
- package/dist/features/openservice/automation/createSchedule.d.ts +124 -0
- package/dist/features/openservice/automation/createSchedule.js +247 -0
- package/dist/features/openservice/automation/createTicket.d.ts +121 -0
- package/dist/features/openservice/automation/createTicket.js +212 -0
- package/dist/features/openservice/automation/index.d.ts +32 -0
- package/dist/features/openservice/automation/index.js +56 -0
- package/dist/features/openservice/{alarms/actionParams.d.ts → automation/params.d.ts} +15 -1
- package/dist/features/openservice/{alarms/actionParams.js → automation/params.js} +58 -1
- package/dist/features/openservice/automation/recordExecution.d.ts +61 -0
- package/dist/features/openservice/automation/recordExecution.js +111 -0
- package/dist/features/openservice/automation/resolveEquipment.d.ts +16 -0
- package/dist/features/openservice/automation/resolveEquipment.js +43 -0
- package/dist/features/openservice/automation/resolveStatus.d.ts +20 -0
- package/dist/features/openservice/automation/resolveStatus.js +57 -0
- package/dist/features/openservice/automation/resolveTenantUser.d.ts +16 -0
- package/dist/features/openservice/automation/resolveTenantUser.js +35 -0
- package/dist/features/openservice/automation/ticketDefaults.d.ts +34 -0
- package/dist/features/openservice/{alarms/ticket/alarmTicketDefaults.js → automation/ticketDefaults.js} +8 -8
- package/dist/features/openservice/automation/updateTicket.d.ts +65 -0
- package/dist/features/openservice/automation/updateTicket.js +141 -0
- package/dist/features/openservice/index.d.ts +2 -0
- package/dist/features/openservice/index.js +2 -0
- package/dist/functions/openinc-openservice-create-schedule.d.ts +20 -0
- package/dist/functions/openinc-openservice-create-schedule.js +41 -0
- package/dist/functions/openinc-openservice-create-ticket.d.ts +24 -0
- package/dist/functions/openinc-openservice-create-ticket.js +48 -0
- package/dist/functions/openinc-openservice-record-execution.d.ts +17 -0
- package/dist/functions/openinc-openservice-record-execution.js +38 -0
- package/dist/functions/openinc-openservice-update-ticket.d.ts +16 -0
- package/dist/functions/openinc-openservice-update-ticket.js +37 -0
- package/dist/hooks/Service_Schedule.js +5 -0
- package/dist/hooks/Service_Schedule_Execution.js +35 -0
- package/package.json +1 -1
- package/dist/features/openservice/alarms/resolveAlarmUser.d.ts +0 -16
- package/dist/features/openservice/alarms/resolveAlarmUser.js +0 -35
- package/dist/features/openservice/alarms/resolveEquipment.d.ts +0 -17
- package/dist/features/openservice/alarms/resolveEquipment.js +0 -45
- package/dist/features/openservice/alarms/ticket/alarmTicketDefaults.d.ts +0 -34
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Tenant, type _User } from "../../../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* How a record created from outside is written.
|
|
4
|
+
*
|
|
5
|
+
* - **master key** — the server's own write: the hooks skip their permission
|
|
6
|
+
* checks, and the caller names the tenant. What an alarm executor or a
|
|
7
|
+
* trusted integration (the demo Node-RED) uses.
|
|
8
|
+
* - **session** — the write of a (technical) user: every hook applies, so
|
|
9
|
+
* `ticket_create`, `schedule_create`, `execution_perform` and the record ACLs
|
|
10
|
+
* decide exactly as they do for a person in the frontend. What a production
|
|
11
|
+
* integration should use.
|
|
12
|
+
*/
|
|
13
|
+
export type WriteAuth = {
|
|
14
|
+
useMasterKey: true;
|
|
15
|
+
} | {
|
|
16
|
+
sessionToken: string;
|
|
17
|
+
};
|
|
18
|
+
/** The options a `save`/`query` call needs for `auth`. */
|
|
19
|
+
export declare function writeOptions(auth: WriteAuth): Parse.FullOptions;
|
|
20
|
+
/** One authenticated call of an automation endpoint. */
|
|
21
|
+
export interface AutomationCall<P> {
|
|
22
|
+
tenant: Tenant;
|
|
23
|
+
auth: WriteAuth;
|
|
24
|
+
/**
|
|
25
|
+
* The person the call acts as — the session's user, or with the master key
|
|
26
|
+
* the tenant user named in `actingUserId`. Recorded as creator, author or
|
|
27
|
+
* performer; absent when a master-key caller names nobody.
|
|
28
|
+
*/
|
|
29
|
+
actor?: _User;
|
|
30
|
+
params: P;
|
|
31
|
+
}
|
|
32
|
+
/** Parameters every automation endpoint understands besides its own. */
|
|
33
|
+
export interface AutomationCallParams {
|
|
34
|
+
/** Master key only: the tenant to write into. Required then. */
|
|
35
|
+
tenantId?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Master key only: the tenant user the call acts as (creator of a ticket,
|
|
38
|
+
* author of a comment, performer of a maintenance). Ignored with a session —
|
|
39
|
+
* a user cannot act as somebody else.
|
|
40
|
+
*/
|
|
41
|
+
actingUserId?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Authenticate a call of an automation endpoint (`openinc-openservice-*`
|
|
45
|
+
* without `alarm`) and settle tenant, write mode and actor.
|
|
46
|
+
*
|
|
47
|
+
* A browser session is accepted here, unlike on the alarm endpoints: these
|
|
48
|
+
* endpoints create nothing a user could not create in the frontend, and with a
|
|
49
|
+
* session every hook checks exactly what it checks there. The tenant then is
|
|
50
|
+
* the user's own — a `tenantId` naming another one is refused rather than
|
|
51
|
+
* silently ignored, so a misconfigured integration fails loudly.
|
|
52
|
+
*
|
|
53
|
+
* Every failure to authenticate gets the same answer, so a caller guessing
|
|
54
|
+
* ids learns nothing from the difference.
|
|
55
|
+
*/
|
|
56
|
+
export declare function readAutomationCall<P extends AutomationCallParams>(name: string, request: Parse.Cloud.FunctionRequest): Promise<AutomationCall<P>>;
|
|
@@ -0,0 +1,61 @@
|
|
|
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.writeOptions = writeOptions;
|
|
7
|
+
exports.readAutomationCall = readAutomationCall;
|
|
8
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
9
|
+
const index_js_1 = require("../../../types/index.js");
|
|
10
|
+
const params_js_1 = require("./params.js");
|
|
11
|
+
const resolveTenantUser_js_1 = require("./resolveTenantUser.js");
|
|
12
|
+
/** The options a `save`/`query` call needs for `auth`. */
|
|
13
|
+
function writeOptions(auth) {
|
|
14
|
+
return "sessionToken" in auth
|
|
15
|
+
? { sessionToken: auth.sessionToken }
|
|
16
|
+
: { useMasterKey: true };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Authenticate a call of an automation endpoint (`openinc-openservice-*`
|
|
20
|
+
* without `alarm`) and settle tenant, write mode and actor.
|
|
21
|
+
*
|
|
22
|
+
* A browser session is accepted here, unlike on the alarm endpoints: these
|
|
23
|
+
* endpoints create nothing a user could not create in the frontend, and with a
|
|
24
|
+
* session every hook checks exactly what it checks there. The tenant then is
|
|
25
|
+
* the user's own — a `tenantId` naming another one is refused rather than
|
|
26
|
+
* silently ignored, so a misconfigured integration fails loudly.
|
|
27
|
+
*
|
|
28
|
+
* Every failure to authenticate gets the same answer, so a caller guessing
|
|
29
|
+
* ids learns nothing from the difference.
|
|
30
|
+
*/
|
|
31
|
+
async function readAutomationCall(name, request) {
|
|
32
|
+
const params = (request.params ?? {});
|
|
33
|
+
const forbidden = () => new node_1.default.Error(node_1.default.Error.OPERATION_FORBIDDEN, `${name}: unknown tenant or invalid credentials`);
|
|
34
|
+
if (request.master) {
|
|
35
|
+
const tenantId = (0, params_js_1.paramId)(params.tenantId);
|
|
36
|
+
const tenant = tenantId
|
|
37
|
+
? await new node_1.default.Query(index_js_1.Tenant)
|
|
38
|
+
.equalTo("objectId", tenantId)
|
|
39
|
+
.first({ useMasterKey: true })
|
|
40
|
+
: undefined;
|
|
41
|
+
if (!tenant)
|
|
42
|
+
throw forbidden();
|
|
43
|
+
const actor = await (0, resolveTenantUser_js_1.resolveTenantUser)(tenant, params.actingUserId, `[${name}]`);
|
|
44
|
+
return { tenant, auth: { useMasterKey: true }, actor, params };
|
|
45
|
+
}
|
|
46
|
+
const user = request.user;
|
|
47
|
+
const sessionToken = user?.getSessionToken();
|
|
48
|
+
if (!user || !sessionToken)
|
|
49
|
+
throw forbidden();
|
|
50
|
+
const tenant = user.get("tenant") ??
|
|
51
|
+
(await new node_1.default.Query(node_1.default.User)
|
|
52
|
+
.equalTo("objectId", user.id)
|
|
53
|
+
.include("tenant")
|
|
54
|
+
.first({ useMasterKey: true }))?.get("tenant");
|
|
55
|
+
if (!tenant?.id)
|
|
56
|
+
throw forbidden();
|
|
57
|
+
const requested = (0, params_js_1.paramId)(params.tenantId);
|
|
58
|
+
if (requested && requested !== tenant.id)
|
|
59
|
+
throw forbidden();
|
|
60
|
+
return { tenant, auth: { sessionToken }, actor: user, params };
|
|
61
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Service_Schedule, Tenant, type _User } from "../../../types/index.js";
|
|
2
|
+
import { type WriteAuth } from "./automationCall.js";
|
|
3
|
+
/**
|
|
4
|
+
* Context flag that keeps a plan out of the Outlook sync (`Service_Schedule`
|
|
5
|
+
* afterSave). For plans that belong in no calendar — demo data above all.
|
|
6
|
+
*/
|
|
7
|
+
export declare const NO_CALENDAR_SYNC_CONTEXT_FLAG = "skipCalendarSync";
|
|
8
|
+
/** A plan's cadence, as `OD3_Service_Schedule.cadence` stores it. */
|
|
9
|
+
export interface Cadence {
|
|
10
|
+
kind: "rrule" | "manual" | string;
|
|
11
|
+
rrule?: string;
|
|
12
|
+
dtstart?: string;
|
|
13
|
+
timezone?: string;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* What a caller may say about the plan it wants created.
|
|
18
|
+
*
|
|
19
|
+
* **When** — the first that is given wins:
|
|
20
|
+
* 1. `cadence` — the plan's rule as the editor stores it;
|
|
21
|
+
* 2. `due` / `dueInDays` — a single appointment (`COUNT=1`);
|
|
22
|
+
* 3. the template's cadence, starting at `startAt` (default: now);
|
|
23
|
+
* 4. a single appointment, due now.
|
|
24
|
+
*/
|
|
25
|
+
export interface ScheduleCreateParams {
|
|
26
|
+
/** Template to copy steps, cadence and settings from. */
|
|
27
|
+
templateId?: unknown;
|
|
28
|
+
/** Default: the template's title. Required without a template. */
|
|
29
|
+
title?: unknown;
|
|
30
|
+
description?: unknown;
|
|
31
|
+
equipmentId?: unknown;
|
|
32
|
+
/** Fallback for the machine — see `resolveEquipment`. */
|
|
33
|
+
sourceTags?: unknown;
|
|
34
|
+
cadence?: unknown;
|
|
35
|
+
due?: unknown;
|
|
36
|
+
/** Days from now until the single appointment. */
|
|
37
|
+
dueInDays?: unknown;
|
|
38
|
+
/** Start of the template's cadence. */
|
|
39
|
+
startAt?: unknown;
|
|
40
|
+
/**
|
|
41
|
+
* IANA zone of a cadence this call builds (single appointment, template
|
|
42
|
+
* rhythm without a zone of its own). Default `Europe/Berlin` — the
|
|
43
|
+
* frontend's fallback, see {@link oneOffCadence}.
|
|
44
|
+
*/
|
|
45
|
+
timezone?: unknown;
|
|
46
|
+
/** Replaces the template's steps. Steps without an `id` get one. */
|
|
47
|
+
steps?: unknown;
|
|
48
|
+
/** `#rrggbb`; unset derives one from the id. */
|
|
49
|
+
color?: unknown;
|
|
50
|
+
leadDays?: unknown;
|
|
51
|
+
graceDays?: unknown;
|
|
52
|
+
escalationAfterDays?: unknown;
|
|
53
|
+
assignedUserIds?: unknown;
|
|
54
|
+
/** Added to the roles the template assigns. */
|
|
55
|
+
assignedRoleIds?: unknown;
|
|
56
|
+
/** Free-form marker of where the plan comes from — see `Schedule.origin`. */
|
|
57
|
+
origin?: unknown;
|
|
58
|
+
/**
|
|
59
|
+
* With an `origin`: return the existing plan of that origin instead of
|
|
60
|
+
* creating another. A single appointment counts while nobody has performed
|
|
61
|
+
* it; a recurring plan counts while it is not archived.
|
|
62
|
+
*/
|
|
63
|
+
unique?: unknown;
|
|
64
|
+
/** Structured context the plan keeps, e.g. `{ alarm: … }`. */
|
|
65
|
+
asset?: unknown;
|
|
66
|
+
/** `false` keeps the plan out of the Outlook sync. Default `true`. */
|
|
67
|
+
calendarSync?: unknown;
|
|
68
|
+
}
|
|
69
|
+
export interface ScheduleCreateOptions {
|
|
70
|
+
auth: WriteAuth;
|
|
71
|
+
/** Owner of the plan (`user`). With a session the hook sets it anyway. */
|
|
72
|
+
actor?: _User;
|
|
73
|
+
logPrefix: string;
|
|
74
|
+
/** The last word on the title, given the template's — the alarm renders. */
|
|
75
|
+
titleFor?: (templateTitle: string | undefined) => string;
|
|
76
|
+
}
|
|
77
|
+
export interface ScheduleCreateResult {
|
|
78
|
+
scheduleId: string;
|
|
79
|
+
/** `false` when `unique` found an existing plan of the same origin. */
|
|
80
|
+
created: boolean;
|
|
81
|
+
origin?: string;
|
|
82
|
+
/** ISO start of the cadence — the appointment, for a single one. */
|
|
83
|
+
due?: string;
|
|
84
|
+
equipmentId?: string;
|
|
85
|
+
templateId?: string;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a maintenance plan the way the editor does — optionally from a
|
|
89
|
+
* template — or, with `unique`, return the one that already exists.
|
|
90
|
+
*
|
|
91
|
+
* The plan is an ordinary `OD3_Service_Schedule`. Nothing about the calendar,
|
|
92
|
+
* the detail page, the execution runner or the notification worker has to know
|
|
93
|
+
* where it came from; they all read a plan.
|
|
94
|
+
*/
|
|
95
|
+
export declare function createSchedule(tenant: Tenant, params: ScheduleCreateParams, options: ScheduleCreateOptions): Promise<ScheduleCreateResult>;
|
|
96
|
+
/**
|
|
97
|
+
* A `COUNT=1` RRULE at `due` in `timezone` — one appointment, no recurrence.
|
|
98
|
+
*
|
|
99
|
+
* Same shape as the frontend's `oneOffCadence()`
|
|
100
|
+
* (`plugins/openservice-schedule/src/features/schedules/utils/cadence.ts`):
|
|
101
|
+
* `isOneOff()` decides by parsing the string, and a plan the editor does not
|
|
102
|
+
* recognise as one-off would be offered a recurrence editor it has no rule for.
|
|
103
|
+
* The zone is written as there, too: a plan without one drifts by an hour at
|
|
104
|
+
* every daylight saving switch. The server has no browser to ask, so it
|
|
105
|
+
* defaults to the frontend's fallback (`Europe/Berlin`, which is also
|
|
106
|
+
* `DEFAULT_SHIFT_TIMEZONE` of `@opendash/schedule`) — the server process's own
|
|
107
|
+
* zone is usually UTC and would be wrong for the plant.
|
|
108
|
+
*/
|
|
109
|
+
export declare function oneOffCadence(due: Date, timezone?: string): Cadence;
|
|
110
|
+
/**
|
|
111
|
+
* Whether a cadence is a single appointment — answered by `@opendash/schedule`,
|
|
112
|
+
* like in the frontend (`isOneOff` of the schedule plugin) and the scheduler,
|
|
113
|
+
* so the three cannot disagree about whether a plan repeats.
|
|
114
|
+
*/
|
|
115
|
+
export declare function isOneOffCadence(cadence: unknown): boolean;
|
|
116
|
+
/**
|
|
117
|
+
* The plan of this origin that still counts — see `unique`.
|
|
118
|
+
*
|
|
119
|
+
* For a single appointment "still counts" means no execution has been
|
|
120
|
+
* **completed** against it. A running execution counts as unperformed on
|
|
121
|
+
* purpose: the technician is standing at the machine, and a second appointment
|
|
122
|
+
* landing in the calendar mid-job helps nobody.
|
|
123
|
+
*/
|
|
124
|
+
export declare function findOpenSchedule(tenant: Tenant, origin: string): Promise<Service_Schedule | undefined>;
|
|
@@ -0,0 +1,247 @@
|
|
|
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.NO_CALENDAR_SYNC_CONTEXT_FLAG = void 0;
|
|
7
|
+
exports.createSchedule = createSchedule;
|
|
8
|
+
exports.oneOffCadence = oneOffCadence;
|
|
9
|
+
exports.isOneOffCadence = isOneOffCadence;
|
|
10
|
+
exports.findOpenSchedule = findOpenSchedule;
|
|
11
|
+
const node_1 = __importDefault(require("parse/node"));
|
|
12
|
+
const schedule_1 = require("@opendash/schedule");
|
|
13
|
+
const crypto_1 = require("crypto");
|
|
14
|
+
const index_js_1 = require("../../../types/index.js");
|
|
15
|
+
const automationCall_js_1 = require("./automationCall.js");
|
|
16
|
+
const params_js_1 = require("./params.js");
|
|
17
|
+
const resolveEquipment_js_1 = require("./resolveEquipment.js");
|
|
18
|
+
/**
|
|
19
|
+
* Context flag that keeps a plan out of the Outlook sync (`Service_Schedule`
|
|
20
|
+
* afterSave). For plans that belong in no calendar — demo data above all.
|
|
21
|
+
*/
|
|
22
|
+
exports.NO_CALENDAR_SYNC_CONTEXT_FLAG = "skipCalendarSync";
|
|
23
|
+
/**
|
|
24
|
+
* Create a maintenance plan the way the editor does — optionally from a
|
|
25
|
+
* template — or, with `unique`, return the one that already exists.
|
|
26
|
+
*
|
|
27
|
+
* The plan is an ordinary `OD3_Service_Schedule`. Nothing about the calendar,
|
|
28
|
+
* the detail page, the execution runner or the notification worker has to know
|
|
29
|
+
* where it came from; they all read a plan.
|
|
30
|
+
*/
|
|
31
|
+
async function createSchedule(tenant, params, options) {
|
|
32
|
+
const { auth, actor, logPrefix } = options;
|
|
33
|
+
const origin = (0, params_js_1.paramString)(params.origin);
|
|
34
|
+
const templateId = (0, params_js_1.paramId)(params.templateId);
|
|
35
|
+
if (origin && (0, params_js_1.paramBoolean)(params.unique)) {
|
|
36
|
+
const existing = await findOpenSchedule(tenant, origin);
|
|
37
|
+
if (existing) {
|
|
38
|
+
return {
|
|
39
|
+
scheduleId: existing.id,
|
|
40
|
+
created: false,
|
|
41
|
+
origin,
|
|
42
|
+
due: existing.get("cadence")?.dtstart,
|
|
43
|
+
equipmentId: existing.get("equipment")
|
|
44
|
+
?.id,
|
|
45
|
+
templateId,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const template = await resolveTemplate(tenant, templateId, logPrefix);
|
|
50
|
+
const title = options.titleFor?.(template?.get("title")) ??
|
|
51
|
+
(0, params_js_1.paramString)(params.title) ??
|
|
52
|
+
template?.get("title");
|
|
53
|
+
if (!title) {
|
|
54
|
+
throw new node_1.default.Error(node_1.default.Error.INVALID_QUERY, `${logPrefix} A plan needs a title — pass "title" or a template.`);
|
|
55
|
+
}
|
|
56
|
+
const equipment = await (0, resolveEquipment_js_1.resolveEquipment)(tenant, (0, params_js_1.paramId)(params.equipmentId), (0, params_js_1.paramStrings)(params.sourceTags), logPrefix);
|
|
57
|
+
const cadence = cadenceOf(params, template);
|
|
58
|
+
const schedule = new index_js_1.Service_Schedule();
|
|
59
|
+
schedule.set("tenant", tenant);
|
|
60
|
+
schedule.set("active", true);
|
|
61
|
+
schedule.set("title", title);
|
|
62
|
+
schedule.set("cadence", cadence);
|
|
63
|
+
schedule.set("steps", stepsOf(params.steps) ?? template?.get("steps") ?? []);
|
|
64
|
+
if (origin)
|
|
65
|
+
schedule.set("origin", origin);
|
|
66
|
+
const description = (0, params_js_1.paramString)(params.description) ?? template?.get("description");
|
|
67
|
+
if (description)
|
|
68
|
+
schedule.set("description", description);
|
|
69
|
+
if (params.asset && typeof params.asset === "object") {
|
|
70
|
+
schedule.set("asset", params.asset);
|
|
71
|
+
}
|
|
72
|
+
// Settings a template carries over. Copied, not referenced: applying a
|
|
73
|
+
// template is a snapshot everywhere else in this domain, and a later template
|
|
74
|
+
// edit must not rewrite an appointment somebody has already been sent to.
|
|
75
|
+
if (template?.get("notifications")) {
|
|
76
|
+
schedule.set("notifications", template.get("notifications"));
|
|
77
|
+
}
|
|
78
|
+
if (template?.get("serviceProviders")) {
|
|
79
|
+
schedule.set("serviceProviders", template.get("serviceProviders"));
|
|
80
|
+
}
|
|
81
|
+
if (equipment)
|
|
82
|
+
schedule.set("equipment", equipment);
|
|
83
|
+
const color = (0, params_js_1.paramString)(params.color);
|
|
84
|
+
if (color)
|
|
85
|
+
schedule.set("color", color);
|
|
86
|
+
for (const key of ["leadDays", "graceDays", "escalationAfterDays"]) {
|
|
87
|
+
const days = (0, params_js_1.paramInteger)(params[key]);
|
|
88
|
+
if (days != null)
|
|
89
|
+
schedule.set(key, days);
|
|
90
|
+
}
|
|
91
|
+
// Who the plan belongs to. Without it `syncAssignmentAcl` grants nobody but
|
|
92
|
+
// administrators, so whoever the plan was created for would not see it.
|
|
93
|
+
if (actor)
|
|
94
|
+
schedule.set("user", actor);
|
|
95
|
+
const context = (0, params_js_1.paramBoolean)(params.calendarSync) === false
|
|
96
|
+
? { [exports.NO_CALENDAR_SYNC_CONTEXT_FLAG]: true }
|
|
97
|
+
: {};
|
|
98
|
+
await schedule.save(null, { ...(0, automationCall_js_1.writeOptions)(auth), context });
|
|
99
|
+
// Assignments are Parse Relations and can only be written once the object
|
|
100
|
+
// exists. The ACL is rebuilt from them in `afterSave`, so this second save is
|
|
101
|
+
// what actually makes the plan visible to the assigned roles and users.
|
|
102
|
+
const roles = [
|
|
103
|
+
...(await templateRoles(template)),
|
|
104
|
+
...((0, params_js_1.paramIds)(params.assignedRoleIds) ?? []).map((id) => node_1.default.Role.createWithoutData(id)),
|
|
105
|
+
];
|
|
106
|
+
const users = ((0, params_js_1.paramIds)(params.assignedUserIds) ?? []).map((id) => node_1.default.User.createWithoutData(id));
|
|
107
|
+
if (roles.length > 0 || users.length > 0) {
|
|
108
|
+
if (roles.length > 0) {
|
|
109
|
+
schedule.relation("assignedRoles").add(roles);
|
|
110
|
+
}
|
|
111
|
+
if (users.length > 0) {
|
|
112
|
+
schedule.relation("assignedUsers").add(users);
|
|
113
|
+
}
|
|
114
|
+
await schedule.save(null, { ...(0, automationCall_js_1.writeOptions)(auth), context });
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
scheduleId: schedule.id,
|
|
118
|
+
created: true,
|
|
119
|
+
origin,
|
|
120
|
+
due: cadence.dtstart,
|
|
121
|
+
equipmentId: equipment?.id,
|
|
122
|
+
templateId: template?.id,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* A `COUNT=1` RRULE at `due` in `timezone` — one appointment, no recurrence.
|
|
127
|
+
*
|
|
128
|
+
* Same shape as the frontend's `oneOffCadence()`
|
|
129
|
+
* (`plugins/openservice-schedule/src/features/schedules/utils/cadence.ts`):
|
|
130
|
+
* `isOneOff()` decides by parsing the string, and a plan the editor does not
|
|
131
|
+
* recognise as one-off would be offered a recurrence editor it has no rule for.
|
|
132
|
+
* The zone is written as there, too: a plan without one drifts by an hour at
|
|
133
|
+
* every daylight saving switch. The server has no browser to ask, so it
|
|
134
|
+
* defaults to the frontend's fallback (`Europe/Berlin`, which is also
|
|
135
|
+
* `DEFAULT_SHIFT_TIMEZONE` of `@opendash/schedule`) — the server process's own
|
|
136
|
+
* zone is usually UTC and would be wrong for the plant.
|
|
137
|
+
*/
|
|
138
|
+
function oneOffCadence(due, timezone = schedule_1.DEFAULT_SHIFT_TIMEZONE) {
|
|
139
|
+
return {
|
|
140
|
+
kind: "rrule",
|
|
141
|
+
rrule: "FREQ=DAILY;COUNT=1",
|
|
142
|
+
dtstart: due.toISOString(),
|
|
143
|
+
timezone,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Whether a cadence is a single appointment — answered by `@opendash/schedule`,
|
|
148
|
+
* like in the frontend (`isOneOff` of the schedule plugin) and the scheduler,
|
|
149
|
+
* so the three cannot disagree about whether a plan repeats.
|
|
150
|
+
*/
|
|
151
|
+
function isOneOffCadence(cadence) {
|
|
152
|
+
const rule = cadence;
|
|
153
|
+
return (!!rule && typeof rule.rrule === "string" && (0, schedule_1.isOneOff)({ rrule: rule.rrule }));
|
|
154
|
+
}
|
|
155
|
+
/** The plan's cadence — see {@link ScheduleCreateParams} for the order. */
|
|
156
|
+
function cadenceOf(params, template) {
|
|
157
|
+
const explicit = params.cadence;
|
|
158
|
+
if (explicit && typeof explicit === "object" && explicit.kind) {
|
|
159
|
+
return explicit;
|
|
160
|
+
}
|
|
161
|
+
const timezone = (0, params_js_1.paramString)(params.timezone) ?? schedule_1.DEFAULT_SHIFT_TIMEZONE;
|
|
162
|
+
const due = (0, params_js_1.paramDate)(params.due);
|
|
163
|
+
if (due)
|
|
164
|
+
return oneOffCadence(due, timezone);
|
|
165
|
+
const inDays = (0, params_js_1.paramNumber)(params.dueInDays);
|
|
166
|
+
if (inDays != null) {
|
|
167
|
+
return oneOffCadence(new Date(Date.now() + inDays * 24 * 60 * 60 * 1000), timezone);
|
|
168
|
+
}
|
|
169
|
+
const fromTemplate = template?.get("cadence");
|
|
170
|
+
if (fromTemplate?.kind) {
|
|
171
|
+
const start = (0, params_js_1.paramDate)(params.startAt) ?? new Date();
|
|
172
|
+
return fromTemplate.kind === "rrule"
|
|
173
|
+
? {
|
|
174
|
+
...fromTemplate,
|
|
175
|
+
dtstart: start.toISOString(),
|
|
176
|
+
timezone: fromTemplate.timezone ?? timezone,
|
|
177
|
+
}
|
|
178
|
+
: { ...fromTemplate };
|
|
179
|
+
}
|
|
180
|
+
return oneOffCadence(new Date(), timezone);
|
|
181
|
+
}
|
|
182
|
+
/** Steps a caller passes, each with an id — `undefined` when none are given. */
|
|
183
|
+
function stepsOf(value) {
|
|
184
|
+
if (!Array.isArray(value))
|
|
185
|
+
return undefined;
|
|
186
|
+
return value
|
|
187
|
+
.filter((step) => step && typeof step === "object")
|
|
188
|
+
.map((step, index) => ({
|
|
189
|
+
order: index,
|
|
190
|
+
required: false,
|
|
191
|
+
...step,
|
|
192
|
+
id: (0, params_js_1.paramString)(step.id) ?? (0, crypto_1.randomUUID)(),
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* The plan of this origin that still counts — see `unique`.
|
|
197
|
+
*
|
|
198
|
+
* For a single appointment "still counts" means no execution has been
|
|
199
|
+
* **completed** against it. A running execution counts as unperformed on
|
|
200
|
+
* purpose: the technician is standing at the machine, and a second appointment
|
|
201
|
+
* landing in the calendar mid-job helps nobody.
|
|
202
|
+
*/
|
|
203
|
+
async function findOpenSchedule(tenant, origin) {
|
|
204
|
+
const schedules = await new node_1.default.Query(index_js_1.Service_Schedule)
|
|
205
|
+
.equalTo("tenant", tenant)
|
|
206
|
+
.equalTo("origin", origin)
|
|
207
|
+
.doesNotExist("deletedAt")
|
|
208
|
+
.descending("createdAt")
|
|
209
|
+
.limit(20)
|
|
210
|
+
.find({ useMasterKey: true });
|
|
211
|
+
for (const schedule of schedules) {
|
|
212
|
+
if (!isOneOffCadence(schedule.get("cadence")))
|
|
213
|
+
return schedule;
|
|
214
|
+
const performed = await new node_1.default.Query(index_js_1.Service_Schedule_Execution)
|
|
215
|
+
.equalTo("schedule", schedule)
|
|
216
|
+
.exists("completedAt")
|
|
217
|
+
.count({ useMasterKey: true });
|
|
218
|
+
if (performed === 0)
|
|
219
|
+
return schedule;
|
|
220
|
+
}
|
|
221
|
+
return undefined;
|
|
222
|
+
}
|
|
223
|
+
/** The template, only within the calling tenant. */
|
|
224
|
+
async function resolveTemplate(tenant, templateId, logPrefix) {
|
|
225
|
+
if (!templateId)
|
|
226
|
+
return undefined;
|
|
227
|
+
const template = await new node_1.default.Query(index_js_1.Service_Schedule_Template)
|
|
228
|
+
.equalTo("objectId", templateId)
|
|
229
|
+
.equalTo("tenant", tenant)
|
|
230
|
+
.doesNotExist("deletedAt")
|
|
231
|
+
.first({ useMasterKey: true });
|
|
232
|
+
if (!template) {
|
|
233
|
+
// Not fatal: a plan without steps is still a dated entry somebody can act
|
|
234
|
+
// on, and refusing would lose the request entirely.
|
|
235
|
+
console.warn(`${logPrefix} Template ${templateId} not found for this tenant — creating the plan without it`);
|
|
236
|
+
}
|
|
237
|
+
return template ?? undefined;
|
|
238
|
+
}
|
|
239
|
+
/** Roles the template assigns, so the new plan is visible to the same people. */
|
|
240
|
+
async function templateRoles(template) {
|
|
241
|
+
if (!template)
|
|
242
|
+
return [];
|
|
243
|
+
return template
|
|
244
|
+
.relation("assignedRoles")
|
|
245
|
+
.query()
|
|
246
|
+
.find({ useMasterKey: true });
|
|
247
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Service_Ticket, Tenant, type _User } from "../../../types/index.js";
|
|
2
|
+
import { type WriteAuth } from "./automationCall.js";
|
|
3
|
+
/**
|
|
4
|
+
* What a caller may say about the ticket it wants created.
|
|
5
|
+
*
|
|
6
|
+
* Everything is optional except that a title must come from somewhere — the
|
|
7
|
+
* parameters, the template or the form config. Values are read tolerantly
|
|
8
|
+
* (`params.ts`): ids may arrive as pointers or whole objects, numbers and
|
|
9
|
+
* dates as strings.
|
|
10
|
+
*/
|
|
11
|
+
export interface TicketCreateParams {
|
|
12
|
+
/**
|
|
13
|
+
* Ticket form config (`OD3_Service_FormConfig`) the ticket is created
|
|
14
|
+
* through: its field defaults pre-fill the ticket, its rules run on save.
|
|
15
|
+
* Unset or not the tenant's: the form that applies to the actor.
|
|
16
|
+
*/
|
|
17
|
+
formConfigId?: unknown;
|
|
18
|
+
/**
|
|
19
|
+
* The ticket as filled in with the ticket form in template mode: field
|
|
20
|
+
* values keyed by ticket field, references as ids (see `readTicketDefaults`).
|
|
21
|
+
* Wins over the flat fields below; both win over the form config's defaults.
|
|
22
|
+
*/
|
|
23
|
+
ticket?: unknown;
|
|
24
|
+
title?: unknown;
|
|
25
|
+
description?: unknown;
|
|
26
|
+
equipmentId?: unknown;
|
|
27
|
+
projectId?: unknown;
|
|
28
|
+
/** A status by id. Wins over `statusCategory`. */
|
|
29
|
+
statusId?: unknown;
|
|
30
|
+
/**
|
|
31
|
+
* A status by category (`inbox`, `active`, `pending`, `done`, …) — the
|
|
32
|
+
* tenant's first one. What a caller that does not know the tenant's status
|
|
33
|
+
* ids uses. Default: the inbox.
|
|
34
|
+
*/
|
|
35
|
+
statusCategory?: unknown;
|
|
36
|
+
priority?: unknown;
|
|
37
|
+
assignedUserIds?: unknown;
|
|
38
|
+
assignedRoleIds?: unknown;
|
|
39
|
+
tagIds?: unknown;
|
|
40
|
+
startDate?: unknown;
|
|
41
|
+
dueDate?: unknown;
|
|
42
|
+
/** Start of the machine failure the ticket reports (KPIs: MTBF, MTTR). */
|
|
43
|
+
failureStart?: unknown;
|
|
44
|
+
failureEnd?: unknown;
|
|
45
|
+
/** Minutes. */
|
|
46
|
+
downtime?: unknown;
|
|
47
|
+
/** Minutes. */
|
|
48
|
+
estimatedDuration?: unknown;
|
|
49
|
+
/** Minutes of active repair — what the MTTR counts. */
|
|
50
|
+
actualDuration?: unknown;
|
|
51
|
+
/**
|
|
52
|
+
* Fallback for the machine when `equipmentId` is not given: the first
|
|
53
|
+
* machine whose `sourceTags` hold one of these (see `resolveEquipment`).
|
|
54
|
+
*/
|
|
55
|
+
sourceTags?: unknown;
|
|
56
|
+
/**
|
|
57
|
+
* Where the ticket comes from — a free-form, greppable marker the ticket
|
|
58
|
+
* keeps and the frontend shows ("automatically created"). Convention:
|
|
59
|
+
* `<system>:<key>`, e.g. `alarm:<id>` or `automation:node-red:<key>`.
|
|
60
|
+
*/
|
|
61
|
+
origin?: unknown;
|
|
62
|
+
/**
|
|
63
|
+
* With an `origin`: return the open ticket of that origin instead of
|
|
64
|
+
* creating a second one. "Open" is decided by the status category, so
|
|
65
|
+
* finishing the ticket arms the origin again. Default `false`.
|
|
66
|
+
*/
|
|
67
|
+
unique?: unknown;
|
|
68
|
+
}
|
|
69
|
+
export interface TicketCreateOptions {
|
|
70
|
+
auth: WriteAuth;
|
|
71
|
+
/** Recorded as `createdBy`. With a session the hook sets it anyway. */
|
|
72
|
+
actor?: _User;
|
|
73
|
+
/** Prefix of every log line, e.g. `[openinc-openservice-create-ticket]`. */
|
|
74
|
+
logPrefix: string;
|
|
75
|
+
/**
|
|
76
|
+
* The last word on title and description, after template, parameters and
|
|
77
|
+
* form config were merged — the alarm action renders its placeholders and
|
|
78
|
+
* appends what the alarm reported here.
|
|
79
|
+
*/
|
|
80
|
+
finalizeText?: (merged: {
|
|
81
|
+
title?: string;
|
|
82
|
+
description?: string;
|
|
83
|
+
}) => {
|
|
84
|
+
title: string;
|
|
85
|
+
description?: string;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/** Outcome of one call. */
|
|
89
|
+
export interface TicketCreateResult {
|
|
90
|
+
ticketId: string;
|
|
91
|
+
/** `false` when `unique` found an open ticket of the same origin. */
|
|
92
|
+
created: boolean;
|
|
93
|
+
origin?: string;
|
|
94
|
+
equipmentId?: string;
|
|
95
|
+
/** The per-project number the hook assigned. */
|
|
96
|
+
number?: number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Create a ticket the way the create form does — through a form config, with
|
|
100
|
+
* its defaults and rules — or, with `unique`, return the open one of the same
|
|
101
|
+
* origin.
|
|
102
|
+
*
|
|
103
|
+
* Deliberately thin: number, ACL, closing stamp, assignment notifications and
|
|
104
|
+
* the form config's rules all happen in the `Service_Ticket` hooks, on every
|
|
105
|
+
* write path. This only settles what a form would have settled before saving:
|
|
106
|
+
* which config, which defaults, which status, which machine.
|
|
107
|
+
*/
|
|
108
|
+
export declare function createTicket(tenant: Tenant, params: TicketCreateParams, options: TicketCreateOptions): Promise<TicketCreateResult>;
|
|
109
|
+
/**
|
|
110
|
+
* Dates and durations a caller may set on create and update alike. Only what
|
|
111
|
+
* was passed is touched; `null` clears a field.
|
|
112
|
+
*/
|
|
113
|
+
export declare function applyTicketTimes(ticket: Service_Ticket, params: Record<string, unknown>): void;
|
|
114
|
+
/**
|
|
115
|
+
* The newest unfinished ticket of this origin, if there is one.
|
|
116
|
+
*
|
|
117
|
+
* "Unfinished" is decided by the **status category**, not by `closedAt`: a
|
|
118
|
+
* tenant can name its statuses whatever it likes, the category is the contract
|
|
119
|
+
* (`done` / `cancelled` — the same list core uses).
|
|
120
|
+
*/
|
|
121
|
+
export declare function findOpenTicket(tenant: Tenant, origin: string): Promise<Service_Ticket | undefined>;
|