@headless-lms/core 0.2.0 → 0.4.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/assets/events.d.ts +42 -24
- package/dist/assets/events.js +1 -1
- package/dist/assets/events.js.map +1 -1
- package/dist/assets/file-urls.d.ts +20 -0
- package/dist/assets/file-urls.js +25 -0
- package/dist/assets/file-urls.js.map +1 -0
- package/dist/assets/index.d.ts +6 -4
- package/dist/assets/index.js +4 -2
- package/dist/assets/jobs.d.ts +15 -0
- package/dist/assets/jobs.js +30 -0
- package/dist/assets/jobs.js.map +1 -0
- package/dist/assets/model.d.ts +2 -2
- package/dist/assets/ports.d.ts +46 -15
- package/dist/assets/service.d.ts +38 -7
- package/dist/assets/service.js +287 -38
- package/dist/assets/service.js.map +1 -1
- package/dist/automations/actions.d.ts +13 -24
- package/dist/automations/actions.js +106 -42
- package/dist/automations/actions.js.map +1 -1
- package/dist/automations/catalog.js +226 -3
- package/dist/automations/catalog.js.map +1 -1
- package/dist/automations/community-derivations.d.ts +14 -0
- package/dist/automations/community-derivations.js +439 -0
- package/dist/automations/community-derivations.js.map +1 -0
- package/dist/automations/community-mail.d.ts +98 -0
- package/dist/automations/community-mail.js +70 -0
- package/dist/automations/community-mail.js.map +1 -0
- package/dist/automations/events.d.ts +18 -0
- package/dist/automations/index.d.ts +13 -4
- package/dist/automations/index.js +10 -2
- package/dist/automations/model.d.ts +10 -3
- package/dist/automations/model.js +15 -2
- package/dist/automations/model.js.map +1 -1
- package/dist/automations/notifications/debounce.d.ts +25 -0
- package/dist/automations/notifications/debounce.js +93 -0
- package/dist/automations/notifications/debounce.js.map +1 -0
- package/dist/automations/notifications/defaults.d.ts +8 -0
- package/dist/automations/notifications/defaults.js +316 -0
- package/dist/automations/notifications/defaults.js.map +1 -0
- package/dist/automations/notifications/delivery.d.ts +28 -0
- package/dist/automations/notifications/delivery.js +88 -0
- package/dist/automations/notifications/delivery.js.map +1 -0
- package/dist/automations/notifications/processor.d.ts +64 -0
- package/dist/automations/notifications/processor.js +270 -0
- package/dist/automations/notifications/processor.js.map +1 -0
- package/dist/automations/notifications/rules/content.d.ts +6 -0
- package/dist/automations/notifications/rules/content.js +39 -0
- package/dist/automations/notifications/rules/content.js.map +1 -0
- package/dist/automations/notifications/rules/discussion.d.ts +7 -0
- package/dist/automations/notifications/rules/discussion.js +678 -0
- package/dist/automations/notifications/rules/discussion.js.map +1 -0
- package/dist/automations/notifications/rules/entitlements.d.ts +6 -0
- package/dist/automations/notifications/rules/entitlements.js +249 -0
- package/dist/automations/notifications/rules/entitlements.js.map +1 -0
- package/dist/automations/notifications/rules/index.d.ts +6 -0
- package/dist/automations/notifications/rules/index.js +15 -0
- package/dist/automations/notifications/rules/index.js.map +1 -0
- package/dist/automations/notifications/rules/organizations.d.ts +6 -0
- package/dist/automations/notifications/rules/organizations.js +28 -0
- package/dist/automations/notifications/rules/organizations.js.map +1 -0
- package/dist/automations/notifications/rules/recipients.d.ts +32 -0
- package/dist/automations/notifications/rules/recipients.js +99 -0
- package/dist/automations/notifications/rules/recipients.js.map +1 -0
- package/dist/automations/notifications/service.d.ts +27 -0
- package/dist/automations/notifications/service.js +45 -0
- package/dist/automations/notifications/service.js.map +1 -0
- package/dist/automations/notifications/types.d.ts +7 -0
- package/dist/automations/notifications/types.js +473 -0
- package/dist/automations/notifications/types.js.map +1 -0
- package/dist/automations/ports.d.ts +121 -3
- package/dist/automations/schedule.d.ts +6 -0
- package/dist/automations/schedule.js +41 -0
- package/dist/automations/schedule.js.map +1 -0
- package/dist/automations/service.d.ts +20 -4
- package/dist/automations/service.js +246 -45
- package/dist/automations/service.js.map +1 -1
- package/dist/content/events.d.ts +712 -50
- package/dist/content/events.js +55 -1
- package/dist/content/events.js.map +1 -1
- package/dist/content/index.d.ts +6 -4
- package/dist/content/index.js +3 -1
- package/dist/content/model.d.ts +2 -2
- package/dist/content/notification-categories.d.ts +36 -0
- package/dist/content/notification-categories.js +64 -0
- package/dist/content/notification-categories.js.map +1 -0
- package/dist/content/ports.d.ts +90 -18
- package/dist/content/service.d.ts +57 -18
- package/dist/content/service.js +520 -25
- package/dist/content/service.js.map +1 -1
- package/dist/content/titles.d.ts +22 -0
- package/dist/content/titles.js +69 -0
- package/dist/content/titles.js.map +1 -0
- package/dist/content/types.d.ts +2 -2
- package/dist/discussion/authors.d.ts +10 -0
- package/dist/discussion/authors.js +23 -0
- package/dist/discussion/authors.js.map +1 -0
- package/dist/discussion/chats.d.ts +109 -0
- package/dist/discussion/chats.js +831 -0
- package/dist/discussion/chats.js.map +1 -0
- package/dist/discussion/digests.d.ts +24 -0
- package/dist/discussion/digests.js +104 -0
- package/dist/discussion/digests.js.map +1 -0
- package/dist/discussion/events.d.ts +785 -56
- package/dist/discussion/events.js +165 -5
- package/dist/discussion/events.js.map +1 -1
- package/dist/discussion/index.d.ts +12 -3
- package/dist/discussion/index.js +9 -1
- package/dist/discussion/jobs.d.ts +16 -0
- package/dist/discussion/jobs.js +40 -0
- package/dist/discussion/jobs.js.map +1 -0
- package/dist/discussion/mentions.d.ts +11 -0
- package/dist/discussion/mentions.js +35 -0
- package/dist/discussion/mentions.js.map +1 -0
- package/dist/discussion/ports.d.ts +372 -28
- package/dist/discussion/ports.js +6 -1
- package/dist/discussion/ports.js.map +1 -0
- package/dist/discussion/posts.d.ts +113 -0
- package/dist/discussion/posts.js +775 -0
- package/dist/discussion/posts.js.map +1 -0
- package/dist/discussion/service.d.ts +42 -11
- package/dist/discussion/service.js +364 -85
- package/dist/discussion/service.js.map +1 -1
- package/dist/discussion/subscriptions.d.ts +37 -0
- package/dist/discussion/subscriptions.js +99 -0
- package/dist/discussion/subscriptions.js.map +1 -0
- package/dist/discussion/titles.d.ts +26 -0
- package/dist/discussion/titles.js +84 -0
- package/dist/discussion/titles.js.map +1 -0
- package/dist/entitlements/events.d.ts +178 -11
- package/dist/entitlements/events.js +28 -1
- package/dist/entitlements/events.js.map +1 -1
- package/dist/entitlements/index.d.ts +6 -4
- package/dist/entitlements/index.js +3 -1
- package/dist/entitlements/jobs.d.ts +12 -0
- package/dist/entitlements/jobs.js +18 -0
- package/dist/entitlements/jobs.js.map +1 -0
- package/dist/entitlements/model.d.ts +3 -2
- package/dist/entitlements/ports.d.ts +128 -11
- package/dist/entitlements/rules.d.ts +12 -0
- package/dist/entitlements/rules.js +17 -0
- package/dist/entitlements/rules.js.map +1 -0
- package/dist/entitlements/service.d.ts +57 -3
- package/dist/entitlements/service.js +492 -2
- package/dist/entitlements/service.js.map +1 -1
- package/dist/identity/access.d.ts +6 -0
- package/dist/identity/access.js +7 -0
- package/dist/identity/access.js.map +1 -0
- package/dist/identity/devices.d.ts +30 -0
- package/dist/identity/devices.js +107 -0
- package/dist/identity/devices.js.map +1 -0
- package/dist/identity/events.d.ts +2 -2
- package/dist/identity/index.d.ts +7 -4
- package/dist/identity/index.js +4 -1
- package/dist/identity/model.d.ts +2 -1
- package/dist/identity/notification-settings.d.ts +28 -0
- package/dist/identity/notification-settings.js +113 -0
- package/dist/identity/notification-settings.js.map +1 -0
- package/dist/identity/ports.d.ts +97 -3
- package/dist/identity/service.d.ts +14 -3
- package/dist/identity/service.js +62 -5
- package/dist/identity/service.js.map +1 -1
- package/dist/integrations/events.d.ts +3 -3
- package/dist/integrations/model.js +1 -1
- package/dist/integrations/model.js.map +1 -1
- package/dist/integrations/service.js +2 -2
- package/dist/integrations/service.js.map +1 -1
- package/dist/organizations/access-scopes.d.ts +19 -0
- package/dist/organizations/access-scopes.js +89 -0
- package/dist/organizations/access-scopes.js.map +1 -0
- package/dist/organizations/events.d.ts +75 -33
- package/dist/organizations/events.js +6 -0
- package/dist/organizations/events.js.map +1 -1
- package/dist/organizations/index.d.ts +8 -6
- package/dist/organizations/index.js +5 -3
- package/dist/organizations/members.d.ts +8 -5
- package/dist/organizations/members.js.map +1 -1
- package/dist/organizations/notification-categories.d.ts +32 -0
- package/dist/organizations/notification-categories.js +51 -0
- package/dist/organizations/notification-categories.js.map +1 -0
- package/dist/organizations/ports.d.ts +17 -11
- package/dist/organizations/roles.d.ts +6 -10
- package/dist/organizations/roles.js +19 -7
- package/dist/organizations/roles.js.map +1 -1
- package/dist/organizations/service.d.ts +22 -11
- package/dist/organizations/service.js +164 -47
- package/dist/organizations/service.js.map +1 -1
- package/dist/organizations/types.d.ts +1 -10
- package/dist/progress/events.d.ts +14 -14
- package/dist/progress/index.d.ts +2 -2
- package/dist/progress/ports.d.ts +6 -1
- package/dist/progress/service.d.ts +3 -1
- package/dist/progress/service.js +6 -2
- package/dist/progress/service.js.map +1 -1
- package/dist/reporting/admin/index.d.ts +4 -0
- package/dist/reporting/admin/index.js +2 -0
- package/dist/reporting/admin/model.d.ts +108 -0
- package/dist/reporting/admin/ports.d.ts +27 -0
- package/dist/reporting/admin/service.d.ts +22 -0
- package/dist/reporting/admin/service.js +38 -0
- package/dist/reporting/admin/service.js.map +1 -0
- package/dist/reporting/member/index.d.ts +5 -0
- package/dist/reporting/member/index.js +2 -0
- package/dist/reporting/member/model.d.ts +157 -0
- package/dist/reporting/member/ports.d.ts +48 -0
- package/dist/reporting/member/service.d.ts +92 -0
- package/dist/reporting/member/service.js +364 -0
- package/dist/reporting/member/service.js.map +1 -0
- package/dist/shared/actor.d.ts +9 -0
- package/dist/shared/errors.d.ts +19 -1
- package/dist/shared/errors.js +27 -1
- package/dist/shared/errors.js.map +1 -1
- package/dist/shared/id.d.ts +14 -0
- package/dist/shared/id.js +15 -1
- package/dist/shared/id.js.map +1 -1
- package/dist/shared/mailer.d.ts +15 -2
- package/dist/shared/mailer.js +32 -5
- package/dist/shared/mailer.js.map +1 -1
- package/dist/shared/network.d.ts +10 -0
- package/dist/shared/network.js +211 -0
- package/dist/shared/network.js.map +1 -0
- package/dist/shared/notification-categories.d.ts +16 -0
- package/dist/shared/notification-categories.js +94 -0
- package/dist/shared/notification-categories.js.map +1 -0
- package/dist/shared/notification-settings.d.ts +14 -0
- package/dist/shared/notification-settings.js +61 -0
- package/dist/shared/notification-settings.js.map +1 -0
- package/dist/shared/ports.d.ts +48 -2
- package/dist/shared/settings.d.ts +2 -1
- package/dist/shared/settings.js +1 -0
- package/dist/shared/settings.js.map +1 -1
- package/dist/types/assets.d.ts +2 -2
- package/dist/types/automations.d.ts +15 -1
- package/dist/types/content.d.ts +2 -2
- package/dist/types/discussion.d.ts +11 -3
- package/dist/types/email-templates.d.ts +216 -1
- package/dist/types/entitlements.d.ts +2 -2
- package/dist/types/identity.d.ts +48 -2
- package/dist/types/index.d.ts +9 -8
- package/dist/types/notifications.d.ts +157 -0
- package/dist/types/ports.d.ts +6 -0
- package/dist/types/schemas/assets.d.ts +65 -37
- package/dist/types/schemas/assets.js +17 -12
- package/dist/types/schemas/assets.js.map +1 -1
- package/dist/types/schemas/automations.d.ts +9 -1
- package/dist/types/schemas/automations.js +13 -6
- package/dist/types/schemas/automations.js.map +1 -1
- package/dist/types/schemas/content.d.ts +396 -3
- package/dist/types/schemas/content.js +161 -3
- package/dist/types/schemas/content.js.map +1 -1
- package/dist/types/schemas/discussion.d.ts +443 -3
- package/dist/types/schemas/discussion.js +233 -4
- package/dist/types/schemas/discussion.js.map +1 -1
- package/dist/types/schemas/entitlements.d.ts +36 -2
- package/dist/types/schemas/entitlements.js +23 -1
- package/dist/types/schemas/entitlements.js.map +1 -1
- package/dist/types/schemas/identity.d.ts +27 -1
- package/dist/types/schemas/identity.js +14 -2
- package/dist/types/schemas/identity.js.map +1 -1
- package/dist/types/schemas/index.d.ts +9 -8
- package/dist/types/schemas/index.js +9 -8
- package/dist/types/schemas/notifications.d.ts +95 -0
- package/dist/types/schemas/notifications.js +56 -0
- package/dist/types/schemas/notifications.js.map +1 -0
- package/dist/types/schemas/organizations.d.ts +8 -0
- package/dist/types/schemas/organizations.js +2 -0
- package/dist/types/schemas/organizations.js.map +1 -1
- package/dist/types/schemas/progress.d.ts +3 -3
- package/dist/types/schemas/shared.d.ts +3 -1
- package/dist/types/schemas/shared.js +5 -1
- package/dist/types/schemas/shared.js.map +1 -1
- package/package.json +2 -2
- package/dist/reporting/courses/index.d.ts +0 -4
- package/dist/reporting/courses/index.js +0 -2
- package/dist/reporting/courses/model.d.ts +0 -24
- package/dist/reporting/courses/ports.d.ts +0 -13
- package/dist/reporting/courses/service.d.ts +0 -19
- package/dist/reporting/courses/service.js +0 -20
- package/dist/reporting/courses/service.js.map +0 -1
- package/dist/reporting/dashboard/index.d.ts +0 -4
- package/dist/reporting/dashboard/index.js +0 -2
- package/dist/reporting/dashboard/model.d.ts +0 -14
- package/dist/reporting/dashboard/ports.d.ts +0 -13
- package/dist/reporting/dashboard/service.d.ts +0 -19
- package/dist/reporting/dashboard/service.js +0 -20
- package/dist/reporting/dashboard/service.js.map +0 -1
- package/dist/reporting/learn/index.d.ts +0 -5
- package/dist/reporting/learn/index.js +0 -2
- package/dist/reporting/learn/model.d.ts +0 -23
- package/dist/reporting/learn/ports.d.ts +0 -43
- package/dist/reporting/learn/service.d.ts +0 -49
- package/dist/reporting/learn/service.js +0 -116
- package/dist/reporting/learn/service.js.map +0 -1
- package/dist/reporting/students/index.d.ts +0 -4
- package/dist/reporting/students/index.js +0 -2
- package/dist/reporting/students/model.d.ts +0 -51
- package/dist/reporting/students/model.js +0 -1
- package/dist/reporting/students/ports.d.ts +0 -15
- package/dist/reporting/students/ports.js +0 -1
- package/dist/reporting/students/service.d.ts +0 -20
- package/dist/reporting/students/service.js +0 -23
- package/dist/reporting/students/service.js.map +0 -1
- /package/dist/reporting/{courses → admin}/model.js +0 -0
- /package/dist/reporting/{courses → admin}/ports.js +0 -0
- /package/dist/reporting/{dashboard → member}/model.js +0 -0
- /package/dist/reporting/{dashboard → member}/ports.js +0 -0
- /package/dist/{reporting/learn/model.js → shared/actor.js} +0 -0
- /package/dist/{reporting/learn/ports.js → types/notifications.js} +0 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { deliverWebhookInputSchema } from "../types/schemas/automations.js";
|
|
2
|
+
import { ConflictError, InvalidInputError, NotFoundError } from "../shared/errors.js";
|
|
2
3
|
import { noopLogger } from "../shared/logger.js";
|
|
3
|
-
import {
|
|
4
|
-
import { DELIVER_WEBHOOK_ACTION, catalogActions, catalogTriggers } from "./catalog.js";
|
|
4
|
+
import { ActionFailedError, AutomationKindMismatchError, InvalidTriggerError, ReservedActionError } from "./model.js";
|
|
5
|
+
import { DELIVER_WEBHOOK_ACTION, catalogActions, catalogTriggers, publicDataSchema } from "./catalog.js";
|
|
5
6
|
import { generateWebhookSecret } from "./webhook-signing.js";
|
|
6
7
|
import { executeAction } from "./actions.js";
|
|
7
8
|
import { automationEvents } from "./events.js";
|
|
8
|
-
import {
|
|
9
|
+
import { cronSupported } from "./schedule.js";
|
|
9
10
|
//#region src/automations/service.ts
|
|
10
11
|
/** True only when `input` sets `enabled` and nothing else — emits automation.enabled|disabled instead of automation.updated. */
|
|
11
12
|
function isEnabledOnlyUpdate(input) {
|
|
@@ -13,14 +14,83 @@ function isEnabledOnlyUpdate(input) {
|
|
|
13
14
|
const { enabled: _enabled, ...rest } = input;
|
|
14
15
|
return Object.values(rest).every((v) => v === void 0);
|
|
15
16
|
}
|
|
17
|
+
function floorMinute(at) {
|
|
18
|
+
const copy = new Date(at);
|
|
19
|
+
copy.setUTCSeconds(0, 0);
|
|
20
|
+
return copy.getTime();
|
|
21
|
+
}
|
|
16
22
|
function unique(values) {
|
|
17
23
|
return [...new Set(values)];
|
|
18
24
|
}
|
|
25
|
+
function filterMatches(filter, data) {
|
|
26
|
+
if (filter == null) return true;
|
|
27
|
+
if (typeof data !== "object" || data === null || Array.isArray(data)) return false;
|
|
28
|
+
const record = data;
|
|
29
|
+
return Object.entries(filter).every(([key, expected]) => {
|
|
30
|
+
if (!Object.prototype.hasOwnProperty.call(record, key)) return false;
|
|
31
|
+
return JSON.stringify(record[key]) === JSON.stringify(expected);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
19
34
|
function assertAuthorableTriggers(triggers) {
|
|
20
|
-
for (const trigger of triggers)
|
|
35
|
+
for (const trigger of triggers) {
|
|
36
|
+
if (trigger.startsWith("automation.")) throw new InvalidTriggerError(trigger);
|
|
37
|
+
if (trigger.startsWith("schedule:") && !cronSupported(trigger.slice(9))) throw new InvalidTriggerError(trigger, "schedule");
|
|
38
|
+
}
|
|
21
39
|
}
|
|
40
|
+
function assertCatalogTriggers(triggers, schedules) {
|
|
41
|
+
assertAuthorableTriggers(triggers);
|
|
42
|
+
const known = new Set(catalogTriggers().map((t) => t.type));
|
|
43
|
+
for (const trigger of triggers) if (!(schedules && trigger.startsWith("schedule:")) && !known.has(trigger)) throw new InvalidTriggerError(trigger, "unknown");
|
|
44
|
+
}
|
|
45
|
+
const REMOVED_ACTIONS = /* @__PURE__ */ new Set([
|
|
46
|
+
"notify",
|
|
47
|
+
"publishDuePosts",
|
|
48
|
+
"expireDueGrants",
|
|
49
|
+
"sendDigest",
|
|
50
|
+
"removeUnattachedUploads"
|
|
51
|
+
]);
|
|
22
52
|
function assertAuthorableActions(actions) {
|
|
23
|
-
for (const action of actions)
|
|
53
|
+
for (const action of actions) {
|
|
54
|
+
if (action.type === "deliverWebhook") throw new ReservedActionError(action.type);
|
|
55
|
+
if (REMOVED_ACTIONS.has(action.type)) throw new InvalidInputError(`The action "${action.type}" is no longer available`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const SCHEDULE_FIRED = "automation.schedule.fired";
|
|
59
|
+
function publicEventData(event) {
|
|
60
|
+
const parsed = event.type === SCHEDULE_FIRED ? {
|
|
61
|
+
success: true,
|
|
62
|
+
data: event.data
|
|
63
|
+
} : publicDataSchema(event.type)?.safeParse(event.data);
|
|
64
|
+
return parsed?.success ? JSON.parse(JSON.stringify(parsed.data)) : null;
|
|
65
|
+
}
|
|
66
|
+
/** A run as callers see it: its event carries only what a webhook of that event would. */
|
|
67
|
+
function toRunView(run) {
|
|
68
|
+
const { type, version, id, orgId, occurredAt } = run.event;
|
|
69
|
+
return {
|
|
70
|
+
...run,
|
|
71
|
+
event: {
|
|
72
|
+
type,
|
|
73
|
+
version,
|
|
74
|
+
id,
|
|
75
|
+
orgId,
|
|
76
|
+
occurredAt,
|
|
77
|
+
data: publicEventData(run.event)
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** An automation as callers see it: a webhook's credential reference stays internal. */
|
|
82
|
+
function toAutomationView(automation) {
|
|
83
|
+
return {
|
|
84
|
+
...automation,
|
|
85
|
+
actions: automation.actions.map((action) => {
|
|
86
|
+
if (action.type !== "deliverWebhook") return action;
|
|
87
|
+
const { secretRef: _secretRef, ...input } = action.input;
|
|
88
|
+
return {
|
|
89
|
+
...action,
|
|
90
|
+
input
|
|
91
|
+
};
|
|
92
|
+
})
|
|
93
|
+
};
|
|
24
94
|
}
|
|
25
95
|
function webhookTarget(automation) {
|
|
26
96
|
return deliverWebhookInputSchema.parse(automation.actions[0]?.input);
|
|
@@ -60,6 +130,10 @@ var AutomationsServiceImpl = class {
|
|
|
60
130
|
integrations;
|
|
61
131
|
credentials;
|
|
62
132
|
webhooks;
|
|
133
|
+
mail;
|
|
134
|
+
grants;
|
|
135
|
+
delivery;
|
|
136
|
+
processor;
|
|
63
137
|
logger;
|
|
64
138
|
constructor(params) {
|
|
65
139
|
this.repo = params.repo;
|
|
@@ -71,12 +145,41 @@ var AutomationsServiceImpl = class {
|
|
|
71
145
|
this.integrations = params.integrations;
|
|
72
146
|
this.credentials = params.credentials;
|
|
73
147
|
this.webhooks = params.webhooks;
|
|
148
|
+
this.mail = {
|
|
149
|
+
...params.mail,
|
|
150
|
+
recentMessageRecipients: (orgId, automationId, chatId, since) => this.runsRepo.recentRecipients(orgId, automationId, chatId, since)
|
|
151
|
+
};
|
|
152
|
+
this.grants = params.grants;
|
|
153
|
+
this.delivery = params.delivery;
|
|
154
|
+
this.processor = params.processor;
|
|
74
155
|
this.logger = params.logger ?? noopLogger;
|
|
75
156
|
}
|
|
76
157
|
async handle(event) {
|
|
77
158
|
if (event.type.startsWith("automation.")) return;
|
|
78
159
|
const automations = await this.safeListByTrigger(event);
|
|
79
|
-
for (const automation of automations
|
|
160
|
+
for (const automation of automations) {
|
|
161
|
+
const matches = filterMatches(automation.filter, event.data);
|
|
162
|
+
if (!automation.enabled || !matches) {
|
|
163
|
+
if (automation.enabled && !matches) this.logger.debug("automation filter skipped", {
|
|
164
|
+
orgId: event.orgId,
|
|
165
|
+
automationId: automation.id,
|
|
166
|
+
trigger: event.type
|
|
167
|
+
});
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
await this.dispatchOne(automation, event);
|
|
171
|
+
}
|
|
172
|
+
await this.processor.process(event);
|
|
173
|
+
}
|
|
174
|
+
async reconcileSchedules(orgId) {
|
|
175
|
+
try {
|
|
176
|
+
await this.engine.syncSchedules?.();
|
|
177
|
+
} catch (err) {
|
|
178
|
+
this.logger.error("automation schedules not reconciled", {
|
|
179
|
+
orgId,
|
|
180
|
+
err
|
|
181
|
+
});
|
|
182
|
+
}
|
|
80
183
|
}
|
|
81
184
|
async safeListByTrigger(event) {
|
|
82
185
|
try {
|
|
@@ -85,7 +188,7 @@ var AutomationsServiceImpl = class {
|
|
|
85
188
|
this.logger.error("automations: failed to load trigger matches", {
|
|
86
189
|
orgId: event.orgId,
|
|
87
190
|
trigger: event.type,
|
|
88
|
-
|
|
191
|
+
err
|
|
89
192
|
});
|
|
90
193
|
return [];
|
|
91
194
|
}
|
|
@@ -97,14 +200,15 @@ var AutomationsServiceImpl = class {
|
|
|
97
200
|
if (!run) return;
|
|
98
201
|
await this.engine.dispatch(this.dispatchFor(automation, run, event));
|
|
99
202
|
} catch (err) {
|
|
100
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
101
203
|
this.logger.error("automation run failed", {
|
|
102
204
|
orgId: event.orgId,
|
|
103
205
|
automationId: automation.id,
|
|
104
206
|
runId: run?.id,
|
|
105
|
-
|
|
207
|
+
trigger: event.type,
|
|
208
|
+
eventId: event.id,
|
|
209
|
+
err
|
|
106
210
|
});
|
|
107
|
-
if (run) await this.recordFailure(run, message);
|
|
211
|
+
if (run) await this.recordFailure(run, err instanceof Error ? err.message : String(err));
|
|
108
212
|
}
|
|
109
213
|
}
|
|
110
214
|
openRun(automation, event) {
|
|
@@ -139,7 +243,7 @@ var AutomationsServiceImpl = class {
|
|
|
139
243
|
event
|
|
140
244
|
};
|
|
141
245
|
}
|
|
142
|
-
/** Best-effort: logs and never rethrows
|
|
246
|
+
/** Best-effort: logs and never rethrows, so an org automation never fails `handle`. */
|
|
143
247
|
async recordFailure(run, error) {
|
|
144
248
|
try {
|
|
145
249
|
return await this.uow.run(async ({ runs, outbox }) => {
|
|
@@ -157,8 +261,9 @@ var AutomationsServiceImpl = class {
|
|
|
157
261
|
} catch (err) {
|
|
158
262
|
this.logger.error("failed to record automation run failure", {
|
|
159
263
|
orgId: run.orgId,
|
|
264
|
+
automationId: run.automationId,
|
|
160
265
|
runId: run.id,
|
|
161
|
-
|
|
266
|
+
err,
|
|
162
267
|
originalError: error
|
|
163
268
|
});
|
|
164
269
|
return null;
|
|
@@ -167,18 +272,77 @@ var AutomationsServiceImpl = class {
|
|
|
167
272
|
async runAction(d, index) {
|
|
168
273
|
const action = d.actions[index];
|
|
169
274
|
if (!action) throw new Error(`automation ${d.automationId} run ${d.runId}: no action at index ${index}`);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
275
|
+
let outcome;
|
|
276
|
+
try {
|
|
277
|
+
outcome = await executeAction(action, d.event, {
|
|
278
|
+
mailer: this.mailer,
|
|
279
|
+
lookups: this.lookups,
|
|
280
|
+
credentials: this.credentials,
|
|
281
|
+
webhooks: this.webhooks,
|
|
282
|
+
mail: this.mail,
|
|
283
|
+
grants: this.grants,
|
|
284
|
+
automationId: d.automationId,
|
|
285
|
+
actionInput: action.input,
|
|
286
|
+
logger: this.logger
|
|
287
|
+
});
|
|
288
|
+
} catch (err) {
|
|
289
|
+
this.logger.error("automation action failed", {
|
|
290
|
+
orgId: d.orgId,
|
|
291
|
+
automationId: d.automationId,
|
|
292
|
+
runId: d.runId,
|
|
293
|
+
trigger: d.event.type,
|
|
294
|
+
index,
|
|
295
|
+
type: action.type,
|
|
296
|
+
err
|
|
297
|
+
});
|
|
298
|
+
throw err instanceof ActionFailedError ? err : new ActionFailedError();
|
|
299
|
+
}
|
|
300
|
+
const { recipients, failed } = outcome;
|
|
301
|
+
this.logger.info("automation action completed", {
|
|
302
|
+
orgId: d.orgId,
|
|
303
|
+
automationId: d.automationId,
|
|
304
|
+
runId: d.runId,
|
|
305
|
+
type: action.type,
|
|
306
|
+
recipients: recipients.length,
|
|
307
|
+
failed: failed.length
|
|
175
308
|
});
|
|
176
309
|
return {
|
|
177
310
|
index,
|
|
178
311
|
type: action.type,
|
|
179
|
-
status: "completed"
|
|
312
|
+
status: "completed",
|
|
313
|
+
...recipients.length > 0 ? { recipients } : {},
|
|
314
|
+
...failed.length > 0 ? { error: `delivery failed for ${failed.length} recipients: ${failed.join(", ")}` } : {}
|
|
180
315
|
};
|
|
181
316
|
}
|
|
317
|
+
runDelivery(d, lastAttempt) {
|
|
318
|
+
return this.delivery.run(d, lastAttempt);
|
|
319
|
+
}
|
|
320
|
+
async listSchedules() {
|
|
321
|
+
return this.repo.listSchedules();
|
|
322
|
+
}
|
|
323
|
+
async fireSchedule(orgId, automationId, at) {
|
|
324
|
+
const firedAt = new Date(floorMinute(at));
|
|
325
|
+
const automation = await this.repo.findById(orgId, automationId);
|
|
326
|
+
if (!automation?.enabled || firedAt.getTime() < floorMinute(automation.createdAt)) {
|
|
327
|
+
this.logger.debug("schedule skipped", {
|
|
328
|
+
orgId,
|
|
329
|
+
automationId
|
|
330
|
+
});
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const event = {
|
|
334
|
+
type: SCHEDULE_FIRED,
|
|
335
|
+
id: `schedule_${automationId}_${firedAt.toISOString()}`,
|
|
336
|
+
orgId,
|
|
337
|
+
version: 1,
|
|
338
|
+
occurredAt: firedAt.toISOString(),
|
|
339
|
+
data: {
|
|
340
|
+
automationId,
|
|
341
|
+
firedAt: firedAt.toISOString()
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
await this.dispatchOne(automation, event);
|
|
345
|
+
}
|
|
182
346
|
async finalize(d, results) {
|
|
183
347
|
const status = results.length === d.actions.length && results.every((r) => r.status === "completed") ? "completed" : "failed";
|
|
184
348
|
await this.uow.run(async ({ runs, outbox }) => {
|
|
@@ -201,9 +365,39 @@ var AutomationsServiceImpl = class {
|
|
|
201
365
|
}) }));
|
|
202
366
|
await outbox.append([runEvent, ...actionFailedEvents]);
|
|
203
367
|
});
|
|
368
|
+
this.logOutcome(d, status, results);
|
|
369
|
+
}
|
|
370
|
+
logOutcome(d, status, results) {
|
|
371
|
+
const run = {
|
|
372
|
+
orgId: d.orgId,
|
|
373
|
+
automationId: d.automationId,
|
|
374
|
+
runId: d.runId,
|
|
375
|
+
trigger: d.event.type,
|
|
376
|
+
eventId: d.event.id
|
|
377
|
+
};
|
|
378
|
+
if (status === "failed") {
|
|
379
|
+
const failed = results.find((r) => r.status === "failed");
|
|
380
|
+
const index = failed?.index ?? results.length;
|
|
381
|
+
this.logger.error("automation run failed", {
|
|
382
|
+
...run,
|
|
383
|
+
index,
|
|
384
|
+
action: failed?.type ?? d.actions[index]?.type,
|
|
385
|
+
error: failed?.error ?? "not every action ran"
|
|
386
|
+
});
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const partial = results.filter((r) => r.error !== void 0);
|
|
390
|
+
if (partial.length > 0) this.logger.warn("automation run completed with failed deliveries", {
|
|
391
|
+
...run,
|
|
392
|
+
actions: partial.map(({ index, type, error }) => ({
|
|
393
|
+
index,
|
|
394
|
+
type,
|
|
395
|
+
error
|
|
396
|
+
}))
|
|
397
|
+
});
|
|
204
398
|
}
|
|
205
399
|
async create(orgId, input) {
|
|
206
|
-
|
|
400
|
+
assertCatalogTriggers(input.triggers, true);
|
|
207
401
|
assertAuthorableActions(input.actions);
|
|
208
402
|
const automation = await this.uow.run(async ({ automations, outbox }) => {
|
|
209
403
|
const created = await automations.insert(orgId, {
|
|
@@ -221,10 +415,11 @@ var AutomationsServiceImpl = class {
|
|
|
221
415
|
orgId,
|
|
222
416
|
automationId: automation.id
|
|
223
417
|
});
|
|
418
|
+
await this.reconcileSchedules(orgId);
|
|
224
419
|
return automation;
|
|
225
420
|
}
|
|
226
421
|
async update(orgId, id, input) {
|
|
227
|
-
if (input.triggers !== void 0)
|
|
422
|
+
if (input.triggers !== void 0) assertCatalogTriggers(input.triggers, true);
|
|
228
423
|
if (input.actions !== void 0) assertAuthorableActions(input.actions);
|
|
229
424
|
const existing = await this.repo.findById(orgId, id);
|
|
230
425
|
if (!existing) return null;
|
|
@@ -239,10 +434,13 @@ var AutomationsServiceImpl = class {
|
|
|
239
434
|
await outbox.append([updateEvent(orgId, input, updated)]);
|
|
240
435
|
return updated;
|
|
241
436
|
});
|
|
242
|
-
if (automation)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
437
|
+
if (automation) {
|
|
438
|
+
this.logger.info("automation updated", {
|
|
439
|
+
orgId,
|
|
440
|
+
automationId: id
|
|
441
|
+
});
|
|
442
|
+
await this.reconcileSchedules(orgId);
|
|
443
|
+
}
|
|
246
444
|
return automation;
|
|
247
445
|
}
|
|
248
446
|
async delete(orgId, id) {
|
|
@@ -258,20 +456,28 @@ var AutomationsServiceImpl = class {
|
|
|
258
456
|
})]);
|
|
259
457
|
return removed;
|
|
260
458
|
});
|
|
261
|
-
if (deleted)
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
459
|
+
if (deleted) {
|
|
460
|
+
this.logger.info("automation deleted", {
|
|
461
|
+
orgId,
|
|
462
|
+
automationId: id
|
|
463
|
+
});
|
|
464
|
+
await this.reconcileSchedules(orgId);
|
|
465
|
+
}
|
|
265
466
|
return deleted !== null;
|
|
266
467
|
}
|
|
267
|
-
list(orgId, query) {
|
|
268
|
-
return this.repo.listByOrg(orgId, query?.kind);
|
|
468
|
+
async list(orgId, query) {
|
|
469
|
+
return (await this.repo.listByOrg(orgId, query?.kind)).map(toAutomationView);
|
|
269
470
|
}
|
|
270
|
-
get(orgId, id) {
|
|
271
|
-
|
|
471
|
+
async get(orgId, id) {
|
|
472
|
+
const automation = await this.repo.findById(orgId, id);
|
|
473
|
+
return automation ? toAutomationView(automation) : null;
|
|
272
474
|
}
|
|
273
|
-
listRuns(orgId, automationId, query) {
|
|
274
|
-
|
|
475
|
+
async listRuns(orgId, automationId, query) {
|
|
476
|
+
const page = await this.runsRepo.list(orgId, automationId, query);
|
|
477
|
+
return {
|
|
478
|
+
...page,
|
|
479
|
+
rows: page.rows.map(toRunView)
|
|
480
|
+
};
|
|
275
481
|
}
|
|
276
482
|
async rerun(orgId, automationId, runId) {
|
|
277
483
|
const existing = await this.runsRepo.findById(orgId, runId);
|
|
@@ -291,21 +497,21 @@ var AutomationsServiceImpl = class {
|
|
|
291
497
|
try {
|
|
292
498
|
await this.engine.dispatch(this.dispatchFor(automation, run, run.event));
|
|
293
499
|
} catch (err) {
|
|
294
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
295
500
|
this.logger.error("automation rerun failed", {
|
|
296
501
|
orgId,
|
|
297
502
|
automationId,
|
|
298
503
|
runId: run.id,
|
|
299
|
-
|
|
504
|
+
trigger: run.trigger,
|
|
505
|
+
err
|
|
300
506
|
});
|
|
301
|
-
return await this.recordFailure(run, message) ?? run;
|
|
507
|
+
return toRunView(await this.recordFailure(run, err instanceof Error ? err.message : String(err)) ?? run);
|
|
302
508
|
}
|
|
303
509
|
this.logger.info("automation rerun started", {
|
|
304
510
|
orgId,
|
|
305
511
|
automationId,
|
|
306
512
|
runId: run.id
|
|
307
513
|
});
|
|
308
|
-
return run;
|
|
514
|
+
return toRunView(run);
|
|
309
515
|
}
|
|
310
516
|
async listWebhooks(orgId) {
|
|
311
517
|
return (await this.repo.listByOrg(orgId, "webhook")).map(toWebhook);
|
|
@@ -316,7 +522,7 @@ var AutomationsServiceImpl = class {
|
|
|
316
522
|
}
|
|
317
523
|
async createWebhook(orgId, input) {
|
|
318
524
|
const events = unique(input.events);
|
|
319
|
-
|
|
525
|
+
assertCatalogTriggers(events, false);
|
|
320
526
|
const secret = generateWebhookSecret();
|
|
321
527
|
const automation = await this.uow.run(async ({ automations, credentials, outbox }) => {
|
|
322
528
|
const secretRef = await credentials.store(orgId, { secret });
|
|
@@ -352,7 +558,7 @@ var AutomationsServiceImpl = class {
|
|
|
352
558
|
const existing = await this.findWebhook(orgId, id);
|
|
353
559
|
if (!existing) return null;
|
|
354
560
|
const events = input.events === void 0 ? void 0 : unique(input.events);
|
|
355
|
-
if (events)
|
|
561
|
+
if (events) assertCatalogTriggers(events, false);
|
|
356
562
|
const { secretRef } = webhookTarget(existing);
|
|
357
563
|
const patch = {
|
|
358
564
|
name: input.url,
|
|
@@ -422,11 +628,6 @@ var AutomationsServiceImpl = class {
|
|
|
422
628
|
const automation = await this.repo.findById(orgId, id);
|
|
423
629
|
return automation?.kind === "webhook" ? automation : null;
|
|
424
630
|
}
|
|
425
|
-
assertSubscribableEvents(events) {
|
|
426
|
-
assertAuthorableTriggers(events);
|
|
427
|
-
const known = new Set(catalogTriggers().map((t) => t.type));
|
|
428
|
-
for (const event of events) if (!known.has(event)) throw new InvalidTriggerError(event, "unknown");
|
|
429
|
-
}
|
|
430
631
|
availableActions() {
|
|
431
632
|
return [...catalogActions(), ...this.integrations.available().flatMap((integration) => integration.actions.map((action) => ({
|
|
432
633
|
type: `${integration.id}.${action.id}`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.js","names":[],"sources":["../../src/automations/service.ts"],"sourcesContent":["// automations context — service implementation (inbound port) and the\n// AutomationExecutor the engine drives.\n//\n// `handle` is subscribed to all events on the EventBus: it matches an event\n// against enabled automations for its trigger, opens one run per match, and\n// hands it to the injected AutomationEngine, which calls back into\n// `runAction`/`finalize`.\nimport { catalogActions, catalogTriggers, DELIVER_WEBHOOK_ACTION } from './catalog.js';\nimport { executeAction } from './actions.js';\nimport { generateWebhookSecret } from './webhook-signing.js';\nimport {\n AutomationKindMismatchError,\n InvalidTriggerError,\n ReservedActionError,\n type Automation,\n type AutomationAction,\n type AutomationActionResult,\n type AutomationRun,\n type CreatedWebhook,\n type DeliverWebhookInput,\n type Page,\n type Webhook,\n type WebhookSecret,\n} from './model.js';\nimport type {\n AutomationRunsQuery,\n AutomationsQuery,\n AvailableActions,\n AvailableTriggers,\n CreateAutomationInput,\n CreateWebhookInput,\n UpdateAutomationInput,\n UpdateWebhookInput,\n} from './types.js';\nimport type {\n AutomationDispatch,\n AutomationEngine,\n AutomationExecutor,\n AutomationRunsRepository,\n AutomationsRepository,\n AutomationsService,\n AutomationsUnitOfWork,\n WebhookSender,\n} from './ports.js';\nimport type { CredentialStore, DomainEvent, Logger } from '../shared/ports.js';\nimport type { JsonValue } from '../types/index.js';\nimport { deliverWebhookInputSchema } from '../types/schemas/index.js';\nimport { ConflictError, NotFoundError } from '../shared/errors.js';\nimport { noopLogger } from '../shared/logger.js';\nimport type { Mailer, MailerLookups } from '../shared/mailer.js';\nimport type { IntegrationsService } from '../integrations/index.js';\nimport { automationEvents } from './events.js';\n\n/** True only when `input` sets `enabled` and nothing else — emits automation.enabled|disabled instead of automation.updated. */\nfunction isEnabledOnlyUpdate<T extends { enabled?: boolean }>(\n input: T,\n): input is T & { enabled: boolean } {\n if (input.enabled === undefined) {\n return false;\n }\n const { enabled: _enabled, ...rest } = input;\n return Object.values(rest).every((v) => v === undefined);\n}\n\nfunction unique(values: string[]): string[] {\n return [...new Set(values)];\n}\n\nfunction assertAuthorableTriggers(triggers: string[]): void {\n for (const trigger of triggers) {\n if (trigger.startsWith('automation.')) {\n throw new InvalidTriggerError(trigger);\n }\n }\n}\n\nfunction assertAuthorableActions(actions: AutomationAction[]): void {\n for (const action of actions) {\n if (action.type === DELIVER_WEBHOOK_ACTION) {\n throw new ReservedActionError(action.type);\n }\n }\n}\n\nfunction webhookTarget(automation: Automation): DeliverWebhookInput {\n return deliverWebhookInputSchema.parse(automation.actions[0]?.input);\n}\n\nfunction toWebhook(automation: Automation): Webhook {\n return {\n orgId: automation.orgId,\n id: automation.id,\n url: webhookTarget(automation).url,\n events: automation.triggers,\n description: automation.description,\n enabled: automation.enabled,\n createdAt: automation.createdAt,\n updatedAt: automation.updatedAt,\n };\n}\n\nfunction updateEvent(orgId: string, input: { enabled?: boolean }, updated: Automation) {\n if (isEnabledOnlyUpdate(input)) {\n return input.enabled\n ? automationEvents.automationEnabled.make({ orgId, data: updated })\n : automationEvents.automationDisabled.make({ orgId, data: updated });\n }\n return automationEvents.automationUpdated.make({ orgId, data: updated });\n}\n\nexport type AutomationsServiceParams = {\n /** Read-only access (list/get/available) — runs outside any transaction. */\n repo: AutomationsRepository;\n runsRepo: AutomationRunsRepository;\n /** Atomic write scope: tx-bound repos + credential store + outbox appender. */\n uow: AutomationsUnitOfWork;\n engine: AutomationEngine;\n mailer: Pick<Mailer, 'send'>;\n lookups: MailerLookups;\n integrations: Pick<IntegrationsService, 'available'>;\n credentials: CredentialStore;\n webhooks: WebhookSender;\n logger?: Logger;\n};\n\nexport class AutomationsServiceImpl implements AutomationsService, AutomationExecutor {\n private readonly repo: AutomationsRepository;\n private readonly runsRepo: AutomationRunsRepository;\n private readonly uow: AutomationsUnitOfWork;\n private readonly engine: AutomationEngine;\n private readonly mailer: Pick<Mailer, 'send'>;\n private readonly lookups: MailerLookups;\n private readonly integrations: Pick<IntegrationsService, 'available'>;\n private readonly credentials: CredentialStore;\n private readonly webhooks: WebhookSender;\n private readonly logger: Logger;\n\n constructor(params: AutomationsServiceParams) {\n this.repo = params.repo;\n this.runsRepo = params.runsRepo;\n this.uow = params.uow;\n this.engine = params.engine;\n this.mailer = params.mailer;\n this.lookups = params.lookups;\n this.integrations = params.integrations;\n this.credentials = params.credentials;\n this.webhooks = params.webhooks;\n this.logger = params.logger ?? noopLogger;\n }\n\n async handle(event: DomainEvent): Promise<void> {\n // automation.* events are this service's own output — matching one as a trigger would self-loop.\n if (event.type.startsWith('automation.')) {\n return;\n }\n const automations = await this.safeListByTrigger(event);\n for (const automation of automations.filter((a) => a.enabled)) {\n await this.dispatchOne(automation, event);\n }\n }\n\n private async safeListByTrigger(event: DomainEvent): Promise<Automation[]> {\n try {\n return await this.repo.listByTrigger(event.orgId, event.type);\n } catch (err) {\n this.logger.error('automations: failed to load trigger matches', {\n orgId: event.orgId,\n trigger: event.type,\n error: err instanceof Error ? err.message : String(err),\n });\n return [];\n }\n }\n\n private async dispatchOne(automation: Automation, event: DomainEvent): Promise<void> {\n let run: AutomationRun | null = null;\n try {\n run = await this.openRun(automation, event);\n if (!run) {\n // At-least-once redelivery of a trigger event already run for this automation — no-op.\n return;\n }\n await this.engine.dispatch(this.dispatchFor(automation, run, event));\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n this.logger.error('automation run failed', {\n orgId: event.orgId,\n automationId: automation.id,\n runId: run?.id,\n error: message,\n });\n if (run) {\n await this.recordFailure(run, message);\n }\n }\n }\n\n private openRun(automation: Automation, event: DomainEvent): Promise<AutomationRun | null> {\n const now = new Date();\n return this.uow.run(async ({ runs, outbox }) => {\n const inserted = await runs.insert(event.orgId, {\n orgId: event.orgId,\n automationId: automation.id,\n trigger: event.type,\n event,\n status: 'running',\n actionResults: [],\n startedAt: now,\n finishedAt: null,\n createdAt: now,\n updatedAt: now,\n });\n if (!inserted) {\n return null;\n }\n await outbox.append([\n automationEvents.runStarted.make({\n orgId: event.orgId,\n data: inserted,\n }),\n ]);\n return inserted;\n });\n }\n\n private dispatchFor(\n automation: Automation,\n run: AutomationRun,\n event: DomainEvent,\n ): AutomationDispatch {\n return {\n runId: run.id,\n orgId: event.orgId,\n automationId: automation.id,\n actions: automation.actions,\n event,\n };\n }\n\n /** Best-effort: logs and never rethrows — `handle` must never throw. */\n private async recordFailure(run: AutomationRun, error: string): Promise<AutomationRun | null> {\n try {\n return await this.uow.run(async ({ runs, outbox }) => {\n const failed = await runs.recordOutcome(run.orgId, run.id, {\n status: 'failed',\n actionResults: run.actionResults,\n finishedAt: new Date(),\n });\n if (failed) {\n await outbox.append([\n automationEvents.runFailed.make({\n orgId: run.orgId,\n data: failed,\n }),\n ]);\n }\n return failed;\n });\n } catch (err) {\n this.logger.error('failed to record automation run failure', {\n orgId: run.orgId,\n runId: run.id,\n error: err instanceof Error ? err.message : String(err),\n originalError: error,\n });\n return null;\n }\n }\n\n async runAction(d: AutomationDispatch, index: number): Promise<AutomationActionResult> {\n const action = d.actions[index];\n if (!action) {\n throw new Error(`automation ${d.automationId} run ${d.runId}: no action at index ${index}`);\n }\n await executeAction(action, d.event, {\n mailer: this.mailer,\n lookups: this.lookups,\n credentials: this.credentials,\n webhooks: this.webhooks,\n });\n return { index, type: action.type, status: 'completed' };\n }\n\n async finalize(d: AutomationDispatch, results: AutomationActionResult[]): Promise<void> {\n const status: 'completed' | 'failed' =\n results.length === d.actions.length && results.every((r) => r.status === 'completed')\n ? 'completed'\n : 'failed';\n await this.uow.run(async ({ runs, outbox }) => {\n const run = await runs.recordOutcome(d.orgId, d.runId, {\n status,\n actionResults: results,\n finishedAt: new Date(),\n });\n if (!run) {\n return;\n }\n const runEvent =\n status === 'completed'\n ? automationEvents.runCompleted.make({ orgId: d.orgId, data: run })\n : automationEvents.runFailed.make({ orgId: d.orgId, data: run });\n const actionFailedEvents = results\n .filter((r) => r.status === 'failed')\n .map((result) => ({\n ...automationEvents.actionFailed.make({\n orgId: d.orgId,\n data: result,\n }),\n }));\n await outbox.append([runEvent, ...actionFailedEvents]);\n });\n }\n\n async create(orgId: string, input: CreateAutomationInput): Promise<Automation> {\n assertAuthorableTriggers(input.triggers);\n assertAuthorableActions(input.actions);\n const automation = await this.uow.run(async ({ automations, outbox }) => {\n const created = await automations.insert(orgId, {\n ...input,\n triggers: unique(input.triggers),\n kind: 'workflow',\n });\n await outbox.append([automationEvents.automationCreated.make({ orgId, data: created })]);\n return created;\n });\n this.logger.info('automation created', { orgId, automationId: automation.id });\n return automation;\n }\n\n async update(\n orgId: string,\n id: string,\n input: UpdateAutomationInput,\n ): Promise<Automation | null> {\n if (input.triggers !== undefined) {\n assertAuthorableTriggers(input.triggers);\n }\n if (input.actions !== undefined) {\n assertAuthorableActions(input.actions);\n }\n const existing = await this.repo.findById(orgId, id);\n if (!existing) {\n return null;\n }\n if (existing.kind !== 'workflow') {\n throw new AutomationKindMismatchError(id, existing.kind);\n }\n const patch =\n input.triggers === undefined ? input : { ...input, triggers: unique(input.triggers) };\n const automation = await this.uow.run(async ({ automations, outbox }) => {\n const updated = await automations.update(orgId, id, patch);\n if (!updated) {\n return null;\n }\n await outbox.append([updateEvent(orgId, input, updated)]);\n return updated;\n });\n if (automation) {\n this.logger.info('automation updated', { orgId, automationId: id });\n }\n return automation;\n }\n\n async delete(orgId: string, id: string): Promise<boolean> {\n const existing = await this.repo.findById(orgId, id);\n if (!existing) {\n return false;\n }\n if (existing.kind !== 'workflow') {\n throw new AutomationKindMismatchError(id, existing.kind);\n }\n const deleted = await this.uow.run(async ({ automations, outbox }) => {\n const removed = await automations.delete(orgId, id);\n if (!removed) {\n return null;\n }\n await outbox.append([automationEvents.automationDeleted.make({ orgId, data: removed })]);\n return removed;\n });\n if (deleted) {\n this.logger.info('automation deleted', { orgId, automationId: id });\n }\n return deleted !== null;\n }\n\n list(orgId: string, query?: AutomationsQuery): Promise<Automation[]> {\n return this.repo.listByOrg(orgId, query?.kind);\n }\n\n get(orgId: string, id: string): Promise<Automation | null> {\n return this.repo.findById(orgId, id);\n }\n\n listRuns(\n orgId: string,\n automationId: string,\n query: AutomationRunsQuery,\n ): Promise<Page<AutomationRun>> {\n return this.runsRepo.list(orgId, automationId, query);\n }\n\n async rerun(orgId: string, automationId: string, runId: string): Promise<AutomationRun> {\n const existing = await this.runsRepo.findById(orgId, runId);\n if (!existing || existing.automationId !== automationId) {\n throw new NotFoundError('Automation run', runId);\n }\n const automation = await this.repo.findById(orgId, automationId);\n if (!automation) {\n throw new NotFoundError('Automation', automationId);\n }\n const run = await this.uow.run(async ({ runs, outbox }) => {\n const restarted = await runs.restart(orgId, runId, new Date());\n if (!restarted) {\n return null;\n }\n await outbox.append([automationEvents.runStarted.make({ orgId, data: restarted })]);\n return restarted;\n });\n if (!run) {\n throw new ConflictError(`automation run ${runId} is still running`);\n }\n try {\n await this.engine.dispatch(this.dispatchFor(automation, run, run.event));\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err);\n this.logger.error('automation rerun failed', {\n orgId,\n automationId,\n runId: run.id,\n error: message,\n });\n return (await this.recordFailure(run, message)) ?? run;\n }\n this.logger.info('automation rerun started', { orgId, automationId, runId: run.id });\n return run;\n }\n\n async listWebhooks(orgId: string): Promise<Webhook[]> {\n const automations = await this.repo.listByOrg(orgId, 'webhook');\n return automations.map(toWebhook);\n }\n\n async getWebhook(orgId: string, id: string): Promise<Webhook | null> {\n const automation = await this.findWebhook(orgId, id);\n return automation ? toWebhook(automation) : null;\n }\n\n async createWebhook(orgId: string, input: CreateWebhookInput): Promise<CreatedWebhook> {\n const events = unique(input.events);\n this.assertSubscribableEvents(events);\n const secret = generateWebhookSecret();\n const automation = await this.uow.run(async ({ automations, credentials, outbox }) => {\n const secretRef = await credentials.store(orgId, { secret });\n const created = await automations.insert(orgId, {\n kind: 'webhook',\n name: input.url,\n description: input.description,\n triggers: events,\n actions: [{ type: DELIVER_WEBHOOK_ACTION, input: { url: input.url, secretRef } }],\n });\n await outbox.append([automationEvents.automationCreated.make({ orgId, data: created })]);\n return created;\n });\n this.logger.info('webhook created', { orgId, automationId: automation.id });\n return { ...toWebhook(automation), secret };\n }\n\n async updateWebhook(\n orgId: string,\n id: string,\n input: UpdateWebhookInput,\n ): Promise<Webhook | null> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return null;\n }\n const events = input.events === undefined ? undefined : unique(input.events);\n if (events) {\n this.assertSubscribableEvents(events);\n }\n const { secretRef } = webhookTarget(existing);\n const patch: UpdateAutomationInput = {\n name: input.url,\n description: input.description,\n triggers: events,\n actions:\n input.url === undefined\n ? undefined\n : [{ type: DELIVER_WEBHOOK_ACTION, input: { url: input.url, secretRef } }],\n enabled: input.enabled,\n };\n const updated = await this.uow.run(async ({ automations, outbox }) => {\n const row = await automations.update(orgId, id, patch);\n if (!row) {\n return null;\n }\n await outbox.append([updateEvent(orgId, input, row)]);\n return row;\n });\n if (!updated) {\n return null;\n }\n this.logger.info('webhook updated', { orgId, automationId: id });\n return toWebhook(updated);\n }\n\n async deleteWebhook(orgId: string, id: string): Promise<boolean> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return false;\n }\n const { secretRef } = webhookTarget(existing);\n const deleted = await this.uow.run(async ({ automations, credentials, outbox }) => {\n const removed = await automations.delete(orgId, id);\n if (!removed) {\n return null;\n }\n await credentials.destroy(orgId, secretRef);\n await outbox.append([automationEvents.automationDeleted.make({ orgId, data: removed })]);\n return removed;\n });\n if (deleted) {\n this.logger.info('webhook deleted', { orgId, automationId: id });\n }\n return deleted !== null;\n }\n\n async revealWebhookSecret(orgId: string, id: string): Promise<WebhookSecret | null> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return null;\n }\n const secrets = await this.credentials.reveal(orgId, webhookTarget(existing).secretRef);\n const secret = secrets?.['secret'];\n if (typeof secret !== 'string') {\n throw new Error(`webhook ${id}: signing secret is missing`);\n }\n return { secret };\n }\n\n async rotateWebhookSecret(orgId: string, id: string): Promise<WebhookSecret | null> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return null;\n }\n const secret = generateWebhookSecret();\n await this.credentials.update(orgId, webhookTarget(existing).secretRef, { secret });\n this.logger.info('webhook secret rotated', { orgId, automationId: id });\n return { secret };\n }\n\n private async findWebhook(orgId: string, id: string): Promise<Automation | null> {\n const automation = await this.repo.findById(orgId, id);\n return automation?.kind === 'webhook' ? automation : null;\n }\n\n private assertSubscribableEvents(events: string[]): void {\n assertAuthorableTriggers(events);\n const known = new Set(catalogTriggers().map((t) => t.type));\n for (const event of events) {\n if (!known.has(event)) {\n throw new InvalidTriggerError(event, 'unknown');\n }\n }\n }\n\n availableActions(): AvailableActions {\n return [\n ...catalogActions(),\n ...this.integrations.available().flatMap((integration) =>\n integration.actions.map((action) => ({\n type: `${integration.id}.${action.id}`,\n description: action.description,\n inputSchema: action.inputSchema as Record<string, JsonValue>,\n source: integration.id,\n })),\n ),\n ];\n }\n\n availableTriggers(): AvailableTriggers {\n return { triggers: catalogTriggers() };\n }\n}\n"],"mappings":";;;;;;;;;;AAsDA,SAAS,oBACP,OACmC;CACnC,IAAI,MAAM,YAAY,KAAA,GACpB,OAAO;CAET,MAAM,EAAE,SAAS,UAAU,GAAG,SAAS;CACvC,OAAO,OAAO,OAAO,IAAI,CAAC,CAAC,OAAO,MAAM,MAAM,KAAA,CAAS;AACzD;AAEA,SAAS,OAAO,QAA4B;CAC1C,OAAO,CAAC,GAAG,IAAI,IAAI,MAAM,CAAC;AAC5B;AAEA,SAAS,yBAAyB,UAA0B;CAC1D,KAAK,MAAM,WAAW,UACpB,IAAI,QAAQ,WAAW,aAAa,GAClC,MAAM,IAAI,oBAAoB,OAAO;AAG3C;AAEA,SAAS,wBAAwB,SAAmC;CAClE,KAAK,MAAM,UAAU,SACnB,IAAI,OAAO,SAAA,kBACT,MAAM,IAAI,oBAAoB,OAAO,IAAI;AAG/C;AAEA,SAAS,cAAc,YAA6C;CAClE,OAAO,0BAA0B,MAAM,WAAW,QAAQ,EAAE,EAAE,KAAK;AACrE;AAEA,SAAS,UAAU,YAAiC;CAClD,OAAO;EACL,OAAO,WAAW;EAClB,IAAI,WAAW;EACf,KAAK,cAAc,UAAU,CAAC,CAAC;EAC/B,QAAQ,WAAW;EACnB,aAAa,WAAW;EACxB,SAAS,WAAW;EACpB,WAAW,WAAW;EACtB,WAAW,WAAW;CACxB;AACF;AAEA,SAAS,YAAY,OAAe,OAA8B,SAAqB;CACrF,IAAI,oBAAoB,KAAK,GAC3B,OAAO,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AC,MAAM,IAAI,KAAK,GAClB,MAAM,IAAI,oBAAoB,OAAO,SAAS;CAGpD;CAEA,mBAAqC;EACnC,OAAO,CACL,GAAG,eAAe,GAClB,GAAG,KAAK,aAAa,UAAU,CAAC,CAAC,SAAS,gBACxC,YAAY,QAAQ,KAAK,YAAY;GACnC,MAAM,GAAG,YAAY,GAAG,GAAG,OAAO;GAClC,aAAa,OAAO;GACpB,aAAa,OAAO;GACpB,QAAQ,YAAY;EACtB,EAAE,CACJ,CACF;CACF;CAEA,oBAAuC;EACrC,OAAO,EAAE,UAAU,gBAAgB,EAAE;CACvC;AACF"}
|
|
1
|
+
{"version":3,"file":"service.js","names":[],"sources":["../../src/automations/service.ts"],"sourcesContent":["// automations context — service implementation (inbound port) and the\n// AutomationExecutor the engine drives.\n//\n// `handle` is subscribed to all events on the EventBus: it matches an event\n// against enabled automations for its trigger, opens one run per match, and\n// hands it to the injected AutomationEngine, which calls back into\n// `runAction`/`finalize`.\nimport {\n catalogActions,\n catalogTriggers,\n DELIVER_WEBHOOK_ACTION,\n publicDataSchema,\n} from './catalog.js';\nimport { executeAction, type ActionOutcome } from './actions.js';\nimport { generateWebhookSecret } from './webhook-signing.js';\nimport type { CommunityMailReads, GrantWriter } from './community-mail.js';\nimport type { NotificationDelivery } from './notifications/delivery.js';\nimport type { NotificationProcessor } from './notifications/processor.js';\nimport { cronSupported } from './schedule.js';\nimport {\n ActionFailedError,\n AutomationKindMismatchError,\n InvalidTriggerError,\n ReservedActionError,\n type Automation,\n type AutomationAction,\n type AutomationActionResult,\n type AutomationRun,\n type CreatedWebhook,\n type DeliverWebhookInput,\n type Page,\n type Webhook,\n type WebhookSecret,\n} from './model.js';\nimport type {\n AutomationRunsQuery,\n AutomationsQuery,\n AvailableActions,\n AvailableTriggers,\n CreateAutomationInput,\n CreateWebhookInput,\n UpdateAutomationInput,\n UpdateWebhookInput,\n} from './types.js';\nimport type {\n AutomationDispatch,\n AutomationEngine,\n AutomationExecutor,\n AutomationRunsRepository,\n AutomationsRepository,\n AutomationsService,\n AutomationsUnitOfWork,\n DeliveryDispatch,\n WebhookSender,\n} from './ports.js';\nimport type { CredentialStore, DomainEvent, Logger } from '../shared/ports.js';\nimport type { JsonValue, ScheduledAutomation } from '../types/index.js';\nimport { deliverWebhookInputSchema } from '../types/schemas/index.js';\nimport { ConflictError, InvalidInputError, NotFoundError } from '../shared/errors.js';\nimport { noopLogger } from '../shared/logger.js';\nimport type { Mailer, MailerLookups } from '../shared/mailer.js';\nimport type { IntegrationsService } from '../integrations/index.js';\nimport { automationEvents } from './events.js';\n\n/** True only when `input` sets `enabled` and nothing else — emits automation.enabled|disabled instead of automation.updated. */\nfunction isEnabledOnlyUpdate<T extends { enabled?: boolean }>(\n input: T,\n): input is T & { enabled: boolean } {\n if (input.enabled === undefined) {\n return false;\n }\n const { enabled: _enabled, ...rest } = input;\n return Object.values(rest).every((v) => v === undefined);\n}\n\nfunction floorMinute(at: Date): number {\n const copy = new Date(at);\n copy.setUTCSeconds(0, 0);\n return copy.getTime();\n}\n\nfunction unique(values: string[]): string[] {\n return [...new Set(values)];\n}\n\nfunction filterMatches(filter: Record<string, JsonValue> | null, data: unknown): boolean {\n if (filter == null) {\n return true;\n }\n if (typeof data !== 'object' || data === null || Array.isArray(data)) {\n return false;\n }\n const record = data as Record<string, unknown>;\n return Object.entries(filter).every(([key, expected]) => {\n if (!Object.prototype.hasOwnProperty.call(record, key)) {\n return false;\n }\n return JSON.stringify(record[key]) === JSON.stringify(expected);\n });\n}\n\nfunction assertAuthorableTriggers(triggers: string[]): void {\n for (const trigger of triggers) {\n if (trigger.startsWith('automation.')) {\n throw new InvalidTriggerError(trigger);\n }\n if (trigger.startsWith('schedule:') && !cronSupported(trigger.slice('schedule:'.length))) {\n throw new InvalidTriggerError(trigger, 'schedule');\n }\n }\n}\n\nfunction assertCatalogTriggers(triggers: string[], schedules: boolean): void {\n assertAuthorableTriggers(triggers);\n const known = new Set(catalogTriggers().map((t) => t.type));\n for (const trigger of triggers) {\n if (!(schedules && trigger.startsWith('schedule:')) && !known.has(trigger)) {\n throw new InvalidTriggerError(trigger, 'unknown');\n }\n }\n}\n\nconst REMOVED_ACTIONS = new Set([\n 'notify',\n 'publishDuePosts',\n 'expireDueGrants',\n 'sendDigest',\n 'removeUnattachedUploads',\n]);\n\nfunction assertAuthorableActions(actions: AutomationAction[]): void {\n for (const action of actions) {\n if (action.type === DELIVER_WEBHOOK_ACTION) {\n throw new ReservedActionError(action.type);\n }\n if (REMOVED_ACTIONS.has(action.type)) {\n throw new InvalidInputError(`The action \"${action.type}\" is no longer available`);\n }\n }\n}\n\nconst SCHEDULE_FIRED = 'automation.schedule.fired';\n\nfunction publicEventData(event: DomainEvent): JsonValue {\n const parsed =\n event.type === SCHEDULE_FIRED\n ? { success: true as const, data: event.data }\n : publicDataSchema(event.type)?.safeParse(event.data);\n return parsed?.success ? (JSON.parse(JSON.stringify(parsed.data)) as JsonValue) : null;\n}\n\n/** A run as callers see it: its event carries only what a webhook of that event would. */\nfunction toRunView(run: AutomationRun): AutomationRun {\n const { type, version, id, orgId, occurredAt } = run.event;\n return {\n ...run,\n event: { type, version, id, orgId, occurredAt, data: publicEventData(run.event) },\n };\n}\n\n/** An automation as callers see it: a webhook's credential reference stays internal. */\nfunction toAutomationView(automation: Automation): Automation {\n return {\n ...automation,\n actions: automation.actions.map((action) => {\n if (action.type !== DELIVER_WEBHOOK_ACTION) {\n return action;\n }\n const { secretRef: _secretRef, ...input } = action.input;\n return { ...action, input };\n }),\n };\n}\n\nfunction webhookTarget(automation: Automation): DeliverWebhookInput {\n return deliverWebhookInputSchema.parse(automation.actions[0]?.input);\n}\n\nfunction toWebhook(automation: Automation): Webhook {\n return {\n orgId: automation.orgId,\n id: automation.id,\n url: webhookTarget(automation).url,\n events: automation.triggers,\n description: automation.description,\n enabled: automation.enabled,\n createdAt: automation.createdAt,\n updatedAt: automation.updatedAt,\n };\n}\n\nfunction updateEvent(orgId: string, input: { enabled?: boolean }, updated: Automation) {\n if (isEnabledOnlyUpdate(input)) {\n return input.enabled\n ? automationEvents.automationEnabled.make({ orgId, data: updated })\n : automationEvents.automationDisabled.make({ orgId, data: updated });\n }\n return automationEvents.automationUpdated.make({ orgId, data: updated });\n}\n\nexport type AutomationsServiceParams = {\n /** Read-only access (list/get/available) — runs outside any transaction. */\n repo: AutomationsRepository;\n runsRepo: AutomationRunsRepository;\n /** Atomic write scope: tx-bound repos + credential store + outbox appender. */\n uow: AutomationsUnitOfWork;\n engine: AutomationEngine;\n mailer: Pick<Mailer, 'send'>;\n lookups: MailerLookups;\n integrations: Pick<IntegrationsService, 'available'>;\n credentials: CredentialStore;\n webhooks: WebhookSender;\n mail: Omit<CommunityMailReads, 'recentMessageRecipients'>;\n grants: GrantWriter;\n delivery: Pick<NotificationDelivery, 'run'>;\n processor: Pick<NotificationProcessor, 'process'>;\n logger?: Logger;\n};\n\nexport class AutomationsServiceImpl implements AutomationsService, AutomationExecutor {\n private readonly repo: AutomationsRepository;\n private readonly runsRepo: AutomationRunsRepository;\n private readonly uow: AutomationsUnitOfWork;\n private readonly engine: AutomationEngine;\n private readonly mailer: Pick<Mailer, 'send'>;\n private readonly lookups: MailerLookups;\n private readonly integrations: Pick<IntegrationsService, 'available'>;\n private readonly credentials: CredentialStore;\n private readonly webhooks: WebhookSender;\n private readonly mail: CommunityMailReads;\n private readonly grants: GrantWriter;\n private readonly delivery: Pick<NotificationDelivery, 'run'>;\n private readonly processor: Pick<NotificationProcessor, 'process'>;\n private readonly logger: Logger;\n\n constructor(params: AutomationsServiceParams) {\n this.repo = params.repo;\n this.runsRepo = params.runsRepo;\n this.uow = params.uow;\n this.engine = params.engine;\n this.mailer = params.mailer;\n this.lookups = params.lookups;\n this.integrations = params.integrations;\n this.credentials = params.credentials;\n this.webhooks = params.webhooks;\n this.mail = {\n ...params.mail,\n recentMessageRecipients: (orgId, automationId, chatId, since) =>\n this.runsRepo.recentRecipients(orgId, automationId, chatId, since),\n };\n this.grants = params.grants;\n this.delivery = params.delivery;\n this.processor = params.processor;\n this.logger = params.logger ?? noopLogger;\n }\n\n async handle(event: DomainEvent): Promise<void> {\n // automation.* events are this service's own output — matching one as a trigger would self-loop.\n if (event.type.startsWith('automation.')) {\n return;\n }\n const automations = await this.safeListByTrigger(event);\n for (const automation of automations) {\n const matches = filterMatches(automation.filter, event.data);\n if (!automation.enabled || !matches) {\n if (automation.enabled && !matches) {\n this.logger.debug('automation filter skipped', {\n orgId: event.orgId,\n automationId: automation.id,\n trigger: event.type,\n });\n }\n continue;\n }\n await this.dispatchOne(automation, event);\n }\n await this.processor.process(event);\n }\n\n private async reconcileSchedules(orgId: string): Promise<void> {\n try {\n await this.engine.syncSchedules?.();\n } catch (err) {\n this.logger.error('automation schedules not reconciled', { orgId, err });\n }\n }\n\n private async safeListByTrigger(event: DomainEvent): Promise<Automation[]> {\n try {\n return await this.repo.listByTrigger(event.orgId, event.type);\n } catch (err) {\n this.logger.error('automations: failed to load trigger matches', {\n orgId: event.orgId,\n trigger: event.type,\n err,\n });\n return [];\n }\n }\n\n private async dispatchOne(automation: Automation, event: DomainEvent): Promise<void> {\n let run: AutomationRun | null = null;\n try {\n run = await this.openRun(automation, event);\n if (!run) {\n // At-least-once redelivery of a trigger event already run for this automation — no-op.\n return;\n }\n await this.engine.dispatch(this.dispatchFor(automation, run, event));\n } catch (err) {\n this.logger.error('automation run failed', {\n orgId: event.orgId,\n automationId: automation.id,\n runId: run?.id,\n trigger: event.type,\n eventId: event.id,\n err,\n });\n if (run) {\n await this.recordFailure(run, err instanceof Error ? err.message : String(err));\n }\n }\n }\n\n private openRun(automation: Automation, event: DomainEvent): Promise<AutomationRun | null> {\n const now = new Date();\n return this.uow.run(async ({ runs, outbox }) => {\n const inserted = await runs.insert(event.orgId, {\n orgId: event.orgId,\n automationId: automation.id,\n trigger: event.type,\n event,\n status: 'running',\n actionResults: [],\n startedAt: now,\n finishedAt: null,\n createdAt: now,\n updatedAt: now,\n });\n if (!inserted) {\n return null;\n }\n await outbox.append([\n automationEvents.runStarted.make({\n orgId: event.orgId,\n data: inserted,\n }),\n ]);\n return inserted;\n });\n }\n\n private dispatchFor(\n automation: Automation,\n run: AutomationRun,\n event: DomainEvent,\n ): AutomationDispatch {\n return {\n runId: run.id,\n orgId: event.orgId,\n automationId: automation.id,\n actions: automation.actions,\n event,\n };\n }\n\n /** Best-effort: logs and never rethrows, so an org automation never fails `handle`. */\n private async recordFailure(run: AutomationRun, error: string): Promise<AutomationRun | null> {\n try {\n return await this.uow.run(async ({ runs, outbox }) => {\n const failed = await runs.recordOutcome(run.orgId, run.id, {\n status: 'failed',\n actionResults: run.actionResults,\n finishedAt: new Date(),\n });\n if (failed) {\n await outbox.append([\n automationEvents.runFailed.make({\n orgId: run.orgId,\n data: failed,\n }),\n ]);\n }\n return failed;\n });\n } catch (err) {\n this.logger.error('failed to record automation run failure', {\n orgId: run.orgId,\n automationId: run.automationId,\n runId: run.id,\n err,\n originalError: error,\n });\n return null;\n }\n }\n\n async runAction(d: AutomationDispatch, index: number): Promise<AutomationActionResult> {\n const action = d.actions[index];\n if (!action) {\n throw new Error(`automation ${d.automationId} run ${d.runId}: no action at index ${index}`);\n }\n let outcome: ActionOutcome;\n try {\n outcome = await executeAction(action, d.event, {\n mailer: this.mailer,\n lookups: this.lookups,\n credentials: this.credentials,\n webhooks: this.webhooks,\n mail: this.mail,\n grants: this.grants,\n automationId: d.automationId,\n actionInput: action.input,\n logger: this.logger,\n });\n } catch (err) {\n this.logger.error('automation action failed', {\n orgId: d.orgId,\n automationId: d.automationId,\n runId: d.runId,\n trigger: d.event.type,\n index,\n type: action.type,\n err,\n });\n throw err instanceof ActionFailedError ? err : new ActionFailedError();\n }\n const { recipients, failed } = outcome;\n this.logger.info('automation action completed', {\n orgId: d.orgId,\n automationId: d.automationId,\n runId: d.runId,\n type: action.type,\n recipients: recipients.length,\n failed: failed.length,\n });\n return {\n index,\n type: action.type,\n status: 'completed',\n ...(recipients.length > 0 ? { recipients } : {}),\n ...(failed.length > 0\n ? { error: `delivery failed for ${failed.length} recipients: ${failed.join(', ')}` }\n : {}),\n };\n }\n\n runDelivery(d: DeliveryDispatch, lastAttempt: boolean): Promise<void> {\n return this.delivery.run(d, lastAttempt);\n }\n\n async listSchedules(): Promise<ScheduledAutomation[]> {\n return this.repo.listSchedules();\n }\n\n async fireSchedule(orgId: string, automationId: string, at: Date): Promise<void> {\n const firedAt = new Date(floorMinute(at));\n const automation = await this.repo.findById(orgId, automationId);\n if (!automation?.enabled || firedAt.getTime() < floorMinute(automation.createdAt)) {\n this.logger.debug('schedule skipped', { orgId, automationId });\n return;\n }\n const event: DomainEvent = {\n type: SCHEDULE_FIRED,\n id: `schedule_${automationId}_${firedAt.toISOString()}`,\n orgId,\n version: 1,\n occurredAt: firedAt.toISOString(),\n data: { automationId, firedAt: firedAt.toISOString() },\n };\n await this.dispatchOne(automation, event);\n }\n\n async finalize(d: AutomationDispatch, results: AutomationActionResult[]): Promise<void> {\n const status: 'completed' | 'failed' =\n results.length === d.actions.length && results.every((r) => r.status === 'completed')\n ? 'completed'\n : 'failed';\n await this.uow.run(async ({ runs, outbox }) => {\n const run = await runs.recordOutcome(d.orgId, d.runId, {\n status,\n actionResults: results,\n finishedAt: new Date(),\n });\n if (!run) {\n return;\n }\n const runEvent =\n status === 'completed'\n ? automationEvents.runCompleted.make({ orgId: d.orgId, data: run })\n : automationEvents.runFailed.make({ orgId: d.orgId, data: run });\n const actionFailedEvents = results\n .filter((r) => r.status === 'failed')\n .map((result) => ({\n ...automationEvents.actionFailed.make({\n orgId: d.orgId,\n data: result,\n }),\n }));\n await outbox.append([runEvent, ...actionFailedEvents]);\n });\n this.logOutcome(d, status, results);\n }\n\n private logOutcome(\n d: AutomationDispatch,\n status: 'completed' | 'failed',\n results: AutomationActionResult[],\n ): void {\n const run = {\n orgId: d.orgId,\n automationId: d.automationId,\n runId: d.runId,\n trigger: d.event.type,\n eventId: d.event.id,\n };\n if (status === 'failed') {\n const failed = results.find((r) => r.status === 'failed');\n const index = failed?.index ?? results.length;\n this.logger.error('automation run failed', {\n ...run,\n index,\n action: failed?.type ?? d.actions[index]?.type,\n error: failed?.error ?? 'not every action ran',\n });\n return;\n }\n const partial = results.filter((r) => r.error !== undefined);\n if (partial.length > 0) {\n this.logger.warn('automation run completed with failed deliveries', {\n ...run,\n actions: partial.map(({ index, type, error }) => ({ index, type, error })),\n });\n }\n }\n\n async create(orgId: string, input: CreateAutomationInput): Promise<Automation> {\n assertCatalogTriggers(input.triggers, true);\n assertAuthorableActions(input.actions);\n const automation = await this.uow.run(async ({ automations, outbox }) => {\n const created = await automations.insert(orgId, {\n ...input,\n triggers: unique(input.triggers),\n kind: 'workflow',\n });\n await outbox.append([automationEvents.automationCreated.make({ orgId, data: created })]);\n return created;\n });\n this.logger.info('automation created', { orgId, automationId: automation.id });\n await this.reconcileSchedules(orgId);\n return automation;\n }\n\n async update(\n orgId: string,\n id: string,\n input: UpdateAutomationInput,\n ): Promise<Automation | null> {\n if (input.triggers !== undefined) {\n assertCatalogTriggers(input.triggers, true);\n }\n if (input.actions !== undefined) {\n assertAuthorableActions(input.actions);\n }\n const existing = await this.repo.findById(orgId, id);\n if (!existing) {\n return null;\n }\n if (existing.kind !== 'workflow') {\n throw new AutomationKindMismatchError(id, existing.kind);\n }\n const patch =\n input.triggers === undefined ? input : { ...input, triggers: unique(input.triggers) };\n const automation = await this.uow.run(async ({ automations, outbox }) => {\n const updated = await automations.update(orgId, id, patch);\n if (!updated) {\n return null;\n }\n await outbox.append([updateEvent(orgId, input, updated)]);\n return updated;\n });\n if (automation) {\n this.logger.info('automation updated', { orgId, automationId: id });\n await this.reconcileSchedules(orgId);\n }\n return automation;\n }\n\n async delete(orgId: string, id: string): Promise<boolean> {\n const existing = await this.repo.findById(orgId, id);\n if (!existing) {\n return false;\n }\n if (existing.kind !== 'workflow') {\n throw new AutomationKindMismatchError(id, existing.kind);\n }\n const deleted = await this.uow.run(async ({ automations, outbox }) => {\n const removed = await automations.delete(orgId, id);\n if (!removed) {\n return null;\n }\n await outbox.append([automationEvents.automationDeleted.make({ orgId, data: removed })]);\n return removed;\n });\n if (deleted) {\n this.logger.info('automation deleted', { orgId, automationId: id });\n await this.reconcileSchedules(orgId);\n }\n return deleted !== null;\n }\n\n async list(orgId: string, query?: AutomationsQuery): Promise<Automation[]> {\n const automations = await this.repo.listByOrg(orgId, query?.kind);\n return automations.map(toAutomationView);\n }\n\n async get(orgId: string, id: string): Promise<Automation | null> {\n const automation = await this.repo.findById(orgId, id);\n return automation ? toAutomationView(automation) : null;\n }\n\n async listRuns(\n orgId: string,\n automationId: string,\n query: AutomationRunsQuery,\n ): Promise<Page<AutomationRun>> {\n const page = await this.runsRepo.list(orgId, automationId, query);\n return { ...page, rows: page.rows.map(toRunView) };\n }\n\n async rerun(orgId: string, automationId: string, runId: string): Promise<AutomationRun> {\n const existing = await this.runsRepo.findById(orgId, runId);\n if (!existing || existing.automationId !== automationId) {\n throw new NotFoundError('Automation run', runId);\n }\n const automation = await this.repo.findById(orgId, automationId);\n if (!automation) {\n throw new NotFoundError('Automation', automationId);\n }\n const run = await this.uow.run(async ({ runs, outbox }) => {\n const restarted = await runs.restart(orgId, runId, new Date());\n if (!restarted) {\n return null;\n }\n await outbox.append([automationEvents.runStarted.make({ orgId, data: restarted })]);\n return restarted;\n });\n if (!run) {\n throw new ConflictError(`automation run ${runId} is still running`);\n }\n try {\n await this.engine.dispatch(this.dispatchFor(automation, run, run.event));\n } catch (err) {\n this.logger.error('automation rerun failed', {\n orgId,\n automationId,\n runId: run.id,\n trigger: run.trigger,\n err,\n });\n const failed = await this.recordFailure(\n run,\n err instanceof Error ? err.message : String(err),\n );\n return toRunView(failed ?? run);\n }\n this.logger.info('automation rerun started', { orgId, automationId, runId: run.id });\n return toRunView(run);\n }\n\n async listWebhooks(orgId: string): Promise<Webhook[]> {\n const automations = await this.repo.listByOrg(orgId, 'webhook');\n return automations.map(toWebhook);\n }\n\n async getWebhook(orgId: string, id: string): Promise<Webhook | null> {\n const automation = await this.findWebhook(orgId, id);\n return automation ? toWebhook(automation) : null;\n }\n\n async createWebhook(orgId: string, input: CreateWebhookInput): Promise<CreatedWebhook> {\n const events = unique(input.events);\n assertCatalogTriggers(events, false);\n const secret = generateWebhookSecret();\n const automation = await this.uow.run(async ({ automations, credentials, outbox }) => {\n const secretRef = await credentials.store(orgId, { secret });\n const created = await automations.insert(orgId, {\n kind: 'webhook',\n name: input.url,\n description: input.description,\n triggers: events,\n actions: [{ type: DELIVER_WEBHOOK_ACTION, input: { url: input.url, secretRef } }],\n });\n await outbox.append([automationEvents.automationCreated.make({ orgId, data: created })]);\n return created;\n });\n this.logger.info('webhook created', { orgId, automationId: automation.id });\n return { ...toWebhook(automation), secret };\n }\n\n async updateWebhook(\n orgId: string,\n id: string,\n input: UpdateWebhookInput,\n ): Promise<Webhook | null> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return null;\n }\n const events = input.events === undefined ? undefined : unique(input.events);\n if (events) {\n assertCatalogTriggers(events, false);\n }\n const { secretRef } = webhookTarget(existing);\n const patch: UpdateAutomationInput = {\n name: input.url,\n description: input.description,\n triggers: events,\n actions:\n input.url === undefined\n ? undefined\n : [{ type: DELIVER_WEBHOOK_ACTION, input: { url: input.url, secretRef } }],\n enabled: input.enabled,\n };\n const updated = await this.uow.run(async ({ automations, outbox }) => {\n const row = await automations.update(orgId, id, patch);\n if (!row) {\n return null;\n }\n await outbox.append([updateEvent(orgId, input, row)]);\n return row;\n });\n if (!updated) {\n return null;\n }\n this.logger.info('webhook updated', { orgId, automationId: id });\n return toWebhook(updated);\n }\n\n async deleteWebhook(orgId: string, id: string): Promise<boolean> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return false;\n }\n const { secretRef } = webhookTarget(existing);\n const deleted = await this.uow.run(async ({ automations, credentials, outbox }) => {\n const removed = await automations.delete(orgId, id);\n if (!removed) {\n return null;\n }\n await credentials.destroy(orgId, secretRef);\n await outbox.append([automationEvents.automationDeleted.make({ orgId, data: removed })]);\n return removed;\n });\n if (deleted) {\n this.logger.info('webhook deleted', { orgId, automationId: id });\n }\n return deleted !== null;\n }\n\n async revealWebhookSecret(orgId: string, id: string): Promise<WebhookSecret | null> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return null;\n }\n const secrets = await this.credentials.reveal(orgId, webhookTarget(existing).secretRef);\n const secret = secrets?.['secret'];\n if (typeof secret !== 'string') {\n throw new Error(`webhook ${id}: signing secret is missing`);\n }\n return { secret };\n }\n\n async rotateWebhookSecret(orgId: string, id: string): Promise<WebhookSecret | null> {\n const existing = await this.findWebhook(orgId, id);\n if (!existing) {\n return null;\n }\n const secret = generateWebhookSecret();\n await this.credentials.update(orgId, webhookTarget(existing).secretRef, { secret });\n this.logger.info('webhook secret rotated', { orgId, automationId: id });\n return { secret };\n }\n\n private async findWebhook(orgId: string, id: string): Promise<Automation | null> {\n const automation = await this.repo.findById(orgId, id);\n return automation?.kind === 'webhook' ? automation : null;\n }\n\n availableActions(): AvailableActions {\n return [\n ...catalogActions(),\n ...this.integrations.available().flatMap((integration) =>\n integration.actions.map((action) => ({\n type: `${integration.id}.${action.id}`,\n description: action.description,\n inputSchema: action.inputSchema as Record<string, JsonValue>,\n source: integration.id,\n })),\n ),\n ];\n }\n\n availableTriggers(): AvailableTriggers {\n return { triggers: catalogTriggers() };\n }\n}\n"],"mappings":";;;;;;;;;;;AAiEA,SAAS,oBACP,OACmC;CACnC,IAAI,MAAM,YAAY,KAAA,GACpB,OAAO;CAET,MAAM,EAAE,SAAS,UAAU,GAAG,SAAS;CACvC,OAAO,OAAO,OAAO,IAAI,CAAC,CAAC,OAAO,MAAM,MAAM,KAAA,CAAS;AACzD;AAEA,SAAS,YAAY,IAAkB;CACrC,MAAM,OAAO,IAAI,KAAK,EAAE;CACxB,KAAK,cAAc,GAAG,CAAC;CACvB,OAAO,KAAK,QAAQ;AACtB;AAEA,SAAS,OAAO,QAA4B;CAC1C,OAAO,CAAC,GAAG,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