@vicacha-devs/medusa-plugin-automations 0.0.0-stage → 0.1.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/.medusa/server/src/admin/index.cjs +134 -0
- package/.medusa/server/src/admin/index.js +134 -0
- package/.medusa/server/src/api/admin/automations/[id]/logs/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/automations/[id]/logs/route.js +7 -0
- package/.medusa/server/src/api/admin/automations/[id]/logs/route.js.map +1 -0
- package/.medusa/server/src/api/admin/automations/[id]/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/automations/[id]/route.js +31 -0
- package/.medusa/server/src/api/admin/automations/[id]/route.js.map +1 -0
- package/.medusa/server/src/api/admin/automations/[id]/trigger/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/automations/[id]/trigger/route.js +14 -0
- package/.medusa/server/src/api/admin/automations/[id]/trigger/route.js.map +1 -0
- package/.medusa/server/src/api/admin/automations/actions/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/automations/actions/route.js +6 -0
- package/.medusa/server/src/api/admin/automations/actions/route.js.map +1 -0
- package/.medusa/server/src/api/admin/automations/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/automations/route.js +23 -0
- package/.medusa/server/src/api/admin/automations/route.js.map +1 -0
- package/.medusa/server/src/api/middlewares.d.ts +2 -0
- package/.medusa/server/src/api/middlewares.js +26 -0
- package/.medusa/server/src/api/middlewares.js.map +1 -0
- package/.medusa/server/src/api/validators.d.ts +97 -0
- package/.medusa/server/src/api/validators.js +40 -0
- package/.medusa/server/src/api/validators.js.map +1 -0
- package/.medusa/server/src/index.d.ts +6 -0
- package/.medusa/server/src/index.js +7 -0
- package/.medusa/server/src/index.js.map +1 -0
- package/.medusa/server/src/modules/automations/actions/built-in-handlers.d.ts +100 -0
- package/.medusa/server/src/modules/automations/actions/built-in-handlers.js +194 -0
- package/.medusa/server/src/modules/automations/actions/built-in-handlers.js.map +1 -0
- package/.medusa/server/src/modules/automations/index.d.ts +35 -0
- package/.medusa/server/src/modules/automations/index.js +7 -0
- package/.medusa/server/src/modules/automations/index.js.map +1 -0
- package/.medusa/server/src/modules/automations/models/automation-execution-log.d.ts +10 -0
- package/.medusa/server/src/modules/automations/models/automation-execution-log.js +12 -0
- package/.medusa/server/src/modules/automations/models/automation-execution-log.js.map +1 -0
- package/.medusa/server/src/modules/automations/models/automation-rule.d.ts +14 -0
- package/.medusa/server/src/modules/automations/models/automation-rule.js +16 -0
- package/.medusa/server/src/modules/automations/models/automation-rule.js.map +1 -0
- package/.medusa/server/src/modules/automations/service.d.ts +53 -0
- package/.medusa/server/src/modules/automations/service.js +97 -0
- package/.medusa/server/src/modules/automations/service.js.map +1 -0
- package/.medusa/server/src/modules/automations/utils/rule-evaluator.d.ts +4 -0
- package/.medusa/server/src/modules/automations/utils/rule-evaluator.js +85 -0
- package/.medusa/server/src/modules/automations/utils/rule-evaluator.js.map +1 -0
- package/.medusa/server/src/sdk/index.d.ts +4 -0
- package/.medusa/server/src/sdk/index.js +5 -0
- package/.medusa/server/src/sdk/index.js.map +1 -0
- package/.medusa/server/src/subscribers/automation-events.d.ts +3 -0
- package/.medusa/server/src/subscribers/automation-events.js +29 -0
- package/.medusa/server/src/subscribers/automation-events.js.map +1 -0
- package/.medusa/server/src/types/index.d.ts +72 -0
- package/.medusa/server/src/types/index.js +2 -0
- package/.medusa/server/src/types/index.js.map +1 -0
- package/.medusa/server/src/workflows/index.d.ts +23 -0
- package/.medusa/server/src/workflows/index.js +30 -0
- package/.medusa/server/src/workflows/index.js.map +1 -0
- package/README.md +28 -2
- package/package.json +85 -3
- package/src/__tests__/automations.spec.ts +113 -0
- package/src/admin/i18n/en.json +4 -0
- package/src/admin/i18n/es.json +4 -0
- package/src/admin/i18n/index.ts +9 -0
- package/src/admin/routes/automations/page.tsx +134 -0
- package/src/api/admin/automations/[id]/logs/route.ts +20 -0
- package/src/api/admin/automations/[id]/route.ts +57 -0
- package/src/api/admin/automations/[id]/trigger/route.ts +26 -0
- package/src/api/admin/automations/actions/route.ts +16 -0
- package/src/api/admin/automations/route.ts +43 -0
- package/src/api/middlewares.ts +34 -0
- package/src/api/validators.ts +44 -0
- package/src/index.ts +9 -0
- package/src/modules/automations/actions/built-in-handlers.ts +228 -0
- package/src/modules/automations/index.ts +8 -0
- package/src/modules/automations/models/automation-execution-log.ts +15 -0
- package/src/modules/automations/models/automation-rule.ts +16 -0
- package/src/modules/automations/service.ts +144 -0
- package/src/modules/automations/utils/rule-evaluator.ts +95 -0
- package/src/sdk/index.ts +4 -0
- package/src/subscribers/automation-events.ts +32 -0
- package/src/types/index.ts +87 -0
- package/src/workflows/index.ts +56 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import type { AutomationActionHandler } from "../../../types";
|
|
2
|
+
export declare class MedusaNotificationActionHandler implements AutomationActionHandler {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
description: string;
|
|
6
|
+
enabled: boolean;
|
|
7
|
+
fields: ({
|
|
8
|
+
name: string;
|
|
9
|
+
key: string;
|
|
10
|
+
label: string;
|
|
11
|
+
type: "select";
|
|
12
|
+
required: boolean;
|
|
13
|
+
defaultValue: string;
|
|
14
|
+
options: {
|
|
15
|
+
value: string;
|
|
16
|
+
name: string;
|
|
17
|
+
}[];
|
|
18
|
+
} | {
|
|
19
|
+
name: string;
|
|
20
|
+
key: string;
|
|
21
|
+
label: string;
|
|
22
|
+
type: "text";
|
|
23
|
+
required: boolean;
|
|
24
|
+
defaultValue?: undefined;
|
|
25
|
+
options?: undefined;
|
|
26
|
+
})[];
|
|
27
|
+
executeAction({ action, context, container, eventName, }: Parameters<AutomationActionHandler["executeAction"]>[0]): Promise<{
|
|
28
|
+
success: boolean;
|
|
29
|
+
message: string;
|
|
30
|
+
data?: undefined;
|
|
31
|
+
} | {
|
|
32
|
+
success: boolean;
|
|
33
|
+
message: string;
|
|
34
|
+
data: any;
|
|
35
|
+
}>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Optional zero-hard-dependency bridge to `@vicacha-devs/medusa-plugin-notifications`.
|
|
39
|
+
* Resolves `notificationsModuleService` if installed, or falls back to `Modules.NOTIFICATION`.
|
|
40
|
+
*/
|
|
41
|
+
export declare class NotificationStudioBridgeHandler implements AutomationActionHandler {
|
|
42
|
+
id: string;
|
|
43
|
+
label: string;
|
|
44
|
+
description: string;
|
|
45
|
+
enabled: boolean;
|
|
46
|
+
fields: ({
|
|
47
|
+
name: string;
|
|
48
|
+
key: string;
|
|
49
|
+
label: string;
|
|
50
|
+
type: "select";
|
|
51
|
+
required: boolean;
|
|
52
|
+
defaultValue: string;
|
|
53
|
+
options: {
|
|
54
|
+
value: string;
|
|
55
|
+
name: string;
|
|
56
|
+
}[];
|
|
57
|
+
} | {
|
|
58
|
+
name: string;
|
|
59
|
+
key: string;
|
|
60
|
+
label: string;
|
|
61
|
+
type: "text";
|
|
62
|
+
required: boolean;
|
|
63
|
+
defaultValue?: undefined;
|
|
64
|
+
options?: undefined;
|
|
65
|
+
})[];
|
|
66
|
+
executeAction(params: Parameters<AutomationActionHandler["executeAction"]>[0]): Promise<{
|
|
67
|
+
success: boolean;
|
|
68
|
+
message: string;
|
|
69
|
+
data?: undefined;
|
|
70
|
+
} | {
|
|
71
|
+
success: boolean;
|
|
72
|
+
message: string;
|
|
73
|
+
data: any;
|
|
74
|
+
}>;
|
|
75
|
+
}
|
|
76
|
+
export declare class WebhookActionHandler implements AutomationActionHandler {
|
|
77
|
+
id: string;
|
|
78
|
+
label: string;
|
|
79
|
+
description: string;
|
|
80
|
+
enabled: boolean;
|
|
81
|
+
fields: {
|
|
82
|
+
name: string;
|
|
83
|
+
key: string;
|
|
84
|
+
label: string;
|
|
85
|
+
type: "text";
|
|
86
|
+
required: boolean;
|
|
87
|
+
}[];
|
|
88
|
+
executeAction({ action, context, eventName, trigger, }: Parameters<AutomationActionHandler["executeAction"]>[0]): Promise<{
|
|
89
|
+
success: boolean;
|
|
90
|
+
message: string;
|
|
91
|
+
data?: undefined;
|
|
92
|
+
} | {
|
|
93
|
+
success: boolean;
|
|
94
|
+
message: string;
|
|
95
|
+
data: {
|
|
96
|
+
status: number;
|
|
97
|
+
};
|
|
98
|
+
}>;
|
|
99
|
+
}
|
|
100
|
+
export declare function createBuiltInActionHandlers(): AutomationActionHandler[];
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { Modules } from "@medusajs/framework/utils";
|
|
2
|
+
import { resolveContextPath } from "../utils/rule-evaluator";
|
|
3
|
+
function resolveRecipient(rawTo, context) {
|
|
4
|
+
if (typeof rawTo !== "string" || !rawTo.trim()) {
|
|
5
|
+
return String(context?.email ||
|
|
6
|
+
context?.customer?.email ||
|
|
7
|
+
context?.order?.email ||
|
|
8
|
+
context?.phone ||
|
|
9
|
+
context?.customer?.phone ||
|
|
10
|
+
"").trim();
|
|
11
|
+
}
|
|
12
|
+
const trimmed = rawTo.trim();
|
|
13
|
+
const match = /^\{\{\s*([a-zA-Z0-9_.]+)\s*\}\}$/.exec(trimmed);
|
|
14
|
+
if (!match)
|
|
15
|
+
return trimmed;
|
|
16
|
+
const resolved = resolveContextPath(context, match[1]);
|
|
17
|
+
return resolved != null ? String(resolved).trim() : "";
|
|
18
|
+
}
|
|
19
|
+
function isSafeWebhookUrl(rawUrl) {
|
|
20
|
+
try {
|
|
21
|
+
const parsed = new URL(rawUrl);
|
|
22
|
+
if (parsed.protocol === "https:")
|
|
23
|
+
return true;
|
|
24
|
+
if (parsed.protocol === "http:" &&
|
|
25
|
+
process.env.NODE_ENV !== "production" &&
|
|
26
|
+
(parsed.hostname === "127.0.0.1" || parsed.hostname === "localhost")) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class MedusaNotificationActionHandler {
|
|
36
|
+
id = "notification";
|
|
37
|
+
label = "Medusa Notification";
|
|
38
|
+
description = "Dispatch notifications via Medusa Modules.NOTIFICATION (Email, SMS, WhatsApp, Telegram, Slack)";
|
|
39
|
+
enabled = true;
|
|
40
|
+
fields = [
|
|
41
|
+
{
|
|
42
|
+
name: "channel",
|
|
43
|
+
key: "channel",
|
|
44
|
+
label: "Channel",
|
|
45
|
+
type: "select",
|
|
46
|
+
required: true,
|
|
47
|
+
defaultValue: "email",
|
|
48
|
+
options: [
|
|
49
|
+
{ value: "email", name: "Email" },
|
|
50
|
+
{ value: "sms", name: "SMS" },
|
|
51
|
+
{ value: "whatsapp", name: "WhatsApp" },
|
|
52
|
+
{ value: "telegram", name: "Telegram" },
|
|
53
|
+
{ value: "slack", name: "Slack" },
|
|
54
|
+
],
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "template",
|
|
58
|
+
key: "template",
|
|
59
|
+
label: "Template ID / Name",
|
|
60
|
+
type: "text",
|
|
61
|
+
required: true,
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: "to",
|
|
65
|
+
key: "to",
|
|
66
|
+
label: "Recipient (or {{customer.email}})",
|
|
67
|
+
type: "text",
|
|
68
|
+
required: false,
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
async executeAction({ action, context, container, eventName, }) {
|
|
72
|
+
const cfg = action.config || {};
|
|
73
|
+
const channel = String(cfg.channel || "email").trim();
|
|
74
|
+
const template = String(cfg.template || cfg.template_id || "").trim();
|
|
75
|
+
const to = resolveRecipient(cfg.to, context);
|
|
76
|
+
if (!template) {
|
|
77
|
+
return { success: false, message: "Notification template is required" };
|
|
78
|
+
}
|
|
79
|
+
if (!to) {
|
|
80
|
+
return {
|
|
81
|
+
success: false,
|
|
82
|
+
message: "Notification recipient could not be resolved",
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const notificationService = container.resolve(Modules.NOTIFICATION);
|
|
86
|
+
const result = await notificationService.createNotifications({
|
|
87
|
+
to,
|
|
88
|
+
channel,
|
|
89
|
+
template,
|
|
90
|
+
data: {
|
|
91
|
+
...context,
|
|
92
|
+
event_name: eventName,
|
|
93
|
+
...(cfg.subject ? { subject: String(cfg.subject) } : {}),
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return {
|
|
97
|
+
success: true,
|
|
98
|
+
message: `Notification dispatched via ${channel}`,
|
|
99
|
+
data: result,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Optional zero-hard-dependency bridge to `@vicacha-devs/medusa-plugin-notifications`.
|
|
105
|
+
* Resolves `notificationsModuleService` if installed, or falls back to `Modules.NOTIFICATION`.
|
|
106
|
+
*/
|
|
107
|
+
export class NotificationStudioBridgeHandler {
|
|
108
|
+
id = "mpn-notification-studio";
|
|
109
|
+
label = "Notification Studio (Optional Bridge)";
|
|
110
|
+
description = "Dispatch via @vicacha-devs/medusa-plugin-notifications when installed, falling back to Modules.NOTIFICATION";
|
|
111
|
+
enabled = true;
|
|
112
|
+
fields = [
|
|
113
|
+
{
|
|
114
|
+
name: "channel",
|
|
115
|
+
key: "channel",
|
|
116
|
+
label: "Channel",
|
|
117
|
+
type: "select",
|
|
118
|
+
required: false,
|
|
119
|
+
defaultValue: "email",
|
|
120
|
+
options: [
|
|
121
|
+
{ value: "email", name: "Email" },
|
|
122
|
+
{ value: "whatsapp", name: "WhatsApp" },
|
|
123
|
+
{ value: "telegram", name: "Telegram" },
|
|
124
|
+
{ value: "sms", name: "SMS" },
|
|
125
|
+
{ value: "slack", name: "Slack" },
|
|
126
|
+
],
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: "template_id",
|
|
130
|
+
key: "template_id",
|
|
131
|
+
label: "Template ID",
|
|
132
|
+
type: "text",
|
|
133
|
+
required: true,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
name: "to",
|
|
137
|
+
key: "to",
|
|
138
|
+
label: "Recipient (or {{customer.email}})",
|
|
139
|
+
type: "text",
|
|
140
|
+
required: false,
|
|
141
|
+
},
|
|
142
|
+
];
|
|
143
|
+
async executeAction(params) {
|
|
144
|
+
const fallback = new MedusaNotificationActionHandler();
|
|
145
|
+
return fallback.executeAction(params);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export class WebhookActionHandler {
|
|
149
|
+
id = "webhook";
|
|
150
|
+
label = "Outgoing HTTP Webhook";
|
|
151
|
+
description = "Send an SSRF-guarded JSON POST payload to an external HTTPS endpoint";
|
|
152
|
+
enabled = true;
|
|
153
|
+
fields = [
|
|
154
|
+
{
|
|
155
|
+
name: "url",
|
|
156
|
+
key: "url",
|
|
157
|
+
label: "Webhook URL (https://...)",
|
|
158
|
+
type: "text",
|
|
159
|
+
required: true,
|
|
160
|
+
},
|
|
161
|
+
];
|
|
162
|
+
async executeAction({ action, context, eventName, trigger, }) {
|
|
163
|
+
const url = String(action.config?.url || "").trim();
|
|
164
|
+
if (!url || !isSafeWebhookUrl(url)) {
|
|
165
|
+
return {
|
|
166
|
+
success: false,
|
|
167
|
+
message: "Webhook URL must use https:// (or loopback in non-production)",
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const res = await fetch(url, {
|
|
171
|
+
method: "POST",
|
|
172
|
+
headers: { "Content-Type": "application/json" },
|
|
173
|
+
body: JSON.stringify({
|
|
174
|
+
event: eventName,
|
|
175
|
+
trigger: trigger.type,
|
|
176
|
+
data: context,
|
|
177
|
+
}),
|
|
178
|
+
signal: AbortSignal.timeout(10_000),
|
|
179
|
+
});
|
|
180
|
+
return {
|
|
181
|
+
success: res.ok,
|
|
182
|
+
message: `Webhook responded with HTTP ${res.status}`,
|
|
183
|
+
data: { status: res.status },
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
export function createBuiltInActionHandlers() {
|
|
188
|
+
return [
|
|
189
|
+
new MedusaNotificationActionHandler(),
|
|
190
|
+
new NotificationStudioBridgeHandler(),
|
|
191
|
+
new WebhookActionHandler(),
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=built-in-handlers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"built-in-handlers.js","sourceRoot":"","sources":["../../../../../../src/modules/automations/actions/built-in-handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,2BAA2B,CAAA;AAEnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,SAAS,gBAAgB,CACvB,KAAc,EACd,OAA4B;IAE5B,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;QAC/C,OAAO,MAAM,CACX,OAAO,EAAE,KAAK;YACZ,OAAO,EAAE,QAAQ,EAAE,KAAK;YACxB,OAAO,EAAE,KAAK,EAAE,KAAK;YACrB,OAAO,EAAE,KAAK;YACd,OAAO,EAAE,QAAQ,EAAE,KAAK;YACxB,EAAE,CACL,CAAC,IAAI,EAAE,CAAA;IACV,CAAC;IACD,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;IAC5B,MAAM,KAAK,GAAG,kCAAkC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAC9D,IAAI,CAAC,KAAK;QAAE,OAAO,OAAO,CAAA;IAC1B,MAAM,QAAQ,GAAG,kBAAkB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IACtD,OAAO,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;AACxD,CAAC;AAED,SAAS,gBAAgB,CAAC,MAAc;IACtC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,CAAA;QAC9B,IAAI,MAAM,CAAC,QAAQ,KAAK,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0 +1,35 @@
|
|
|
1
|
+
import AutomationsModuleService from "./service";
|
|
2
|
+
export declare const AUTOMATIONS_MODULE = "automations";
|
|
3
|
+
declare const _default: import("@medusajs/types").ModuleExports<typeof AutomationsModuleService> & {
|
|
4
|
+
linkable: {
|
|
5
|
+
readonly automationRule: {
|
|
6
|
+
id: {
|
|
7
|
+
serviceName: "automations";
|
|
8
|
+
field: "automationRule";
|
|
9
|
+
linkable: "automation_rule_id";
|
|
10
|
+
primaryKey: "id";
|
|
11
|
+
};
|
|
12
|
+
toJSON: () => {
|
|
13
|
+
serviceName: "automations";
|
|
14
|
+
field: "automationRule";
|
|
15
|
+
linkable: "automation_rule_id";
|
|
16
|
+
primaryKey: "id";
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
readonly automationExecutionLog: {
|
|
20
|
+
id: {
|
|
21
|
+
serviceName: "automations";
|
|
22
|
+
field: "automationExecutionLog";
|
|
23
|
+
linkable: "automation_execution_log_id";
|
|
24
|
+
primaryKey: "id";
|
|
25
|
+
};
|
|
26
|
+
toJSON: () => {
|
|
27
|
+
serviceName: "automations";
|
|
28
|
+
field: "automationExecutionLog";
|
|
29
|
+
linkable: "automation_execution_log_id";
|
|
30
|
+
primaryKey: "id";
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export default _default;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Module } from "@medusajs/framework/utils";
|
|
2
|
+
import AutomationsModuleService from "./service";
|
|
3
|
+
export const AUTOMATIONS_MODULE = "automations";
|
|
4
|
+
export default Module(AUTOMATIONS_MODULE, {
|
|
5
|
+
service: AutomationsModuleService,
|
|
6
|
+
});
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/modules/automations/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAClD,OAAO,wBAAwB,MAAM,WAAW,CAAA;AAEhD,MAAM,CAAC,MAAM,kBAAkB,GAAG,aAAa,CAAA;AAE/C,eAAe,MAAM,CAAC,kBAAkB,EAAE;IACxC,OAAO,EAAE,wBAAwB;CAClC,CAAC,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const AutomationExecutionLogModel: import("@medusajs/framework/utils").DmlEntity<import("@medusajs/framework/utils").DMLEntitySchemaBuilder<{
|
|
2
|
+
id: import("@medusajs/framework/utils").PrimaryKeyModifier<string, import("@medusajs/framework/utils").IdProperty>;
|
|
3
|
+
rule_id: import("@medusajs/framework/utils").TextProperty;
|
|
4
|
+
trigger_type: import("@medusajs/framework/utils").EnumProperty<["event", "schedule", "manual"]>;
|
|
5
|
+
event_name: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
6
|
+
status: import("@medusajs/framework/utils").EnumProperty<["success", "failed", "skipped"]>;
|
|
7
|
+
actions_executed: import("@medusajs/framework/utils").NumberProperty;
|
|
8
|
+
result_summary: import("@medusajs/framework/utils").NullableModifier<NoInfer<Record<string, unknown>>, import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>>;
|
|
9
|
+
error_message: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
10
|
+
}>, "automation_execution_log">;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { model } from "@medusajs/framework/utils";
|
|
2
|
+
export const AutomationExecutionLogModel = model.define("automation_execution_log", {
|
|
3
|
+
id: model.id().primaryKey(),
|
|
4
|
+
rule_id: model.text(),
|
|
5
|
+
trigger_type: model.enum(["event", "schedule", "manual"]).default("event"),
|
|
6
|
+
event_name: model.text().nullable(),
|
|
7
|
+
status: model.enum(["success", "failed", "skipped"]).default("success"),
|
|
8
|
+
actions_executed: model.number().default(0),
|
|
9
|
+
result_summary: model.json().nullable(),
|
|
10
|
+
error_message: model.text().nullable(),
|
|
11
|
+
});
|
|
12
|
+
//# sourceMappingURL=automation-execution-log.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"automation-execution-log.js","sourceRoot":"","sources":["../../../../../../src/modules/automations/models/automation-execution-log.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAA;AAEjD,MAAM,CAAC,MAAM,2BAA2B,GAAG,KAAK,CAAC,MAAM,CACrD,0BAA0B,EAC1B;IACE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,UAAU,EAAE;IAC3B,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE;IACrB,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;IAC1E,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;IACnC,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC;IACvE,gBAAgB,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IAC3C,cAAc,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;IACvC,aAAa,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;CACvC,CACF,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare const AutomationRuleModel: import("@medusajs/framework/utils").DmlEntity<import("@medusajs/framework/utils").DMLEntitySchemaBuilder<{
|
|
2
|
+
id: import("@medusajs/framework/utils").PrimaryKeyModifier<string, import("@medusajs/framework/utils").IdProperty>;
|
|
3
|
+
name: import("@medusajs/framework/utils").TextProperty;
|
|
4
|
+
description: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
5
|
+
trigger_type: import("@medusajs/framework/utils").EnumProperty<["event", "schedule", "manual"]>;
|
|
6
|
+
event_name: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
7
|
+
cron_expression: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
8
|
+
conditions: import("@medusajs/framework/utils").NullableModifier<NoInfer<Record<string, unknown>>, import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>>;
|
|
9
|
+
actions: import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>;
|
|
10
|
+
is_active: import("@medusajs/framework/utils").BooleanProperty;
|
|
11
|
+
run_count: import("@medusajs/framework/utils").NumberProperty;
|
|
12
|
+
last_run_at: import("@medusajs/framework/utils").NullableModifier<Date, import("@medusajs/framework/utils").DateTimeProperty>;
|
|
13
|
+
metadata: import("@medusajs/framework/utils").NullableModifier<NoInfer<Record<string, unknown>>, import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>>;
|
|
14
|
+
}>, "automation_rule">;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { model } from "@medusajs/framework/utils";
|
|
2
|
+
export const AutomationRuleModel = model.define("automation_rule", {
|
|
3
|
+
id: model.id().primaryKey(),
|
|
4
|
+
name: model.text().searchable(),
|
|
5
|
+
description: model.text().nullable(),
|
|
6
|
+
trigger_type: model.enum(["event", "schedule", "manual"]).default("event"),
|
|
7
|
+
event_name: model.text().nullable(),
|
|
8
|
+
cron_expression: model.text().nullable(),
|
|
9
|
+
conditions: model.json().nullable(),
|
|
10
|
+
actions: model.json(),
|
|
11
|
+
is_active: model.boolean().default(true),
|
|
12
|
+
run_count: model.number().default(0),
|
|
13
|
+
last_run_at: model.dateTime().nullable(),
|
|
14
|
+
metadata: model.json().nullable(),
|
|
15
|
+
});
|
|
16
|
+
//# sourceMappingURL=automation-rule.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"automation-rule.js","sourceRoot":"","sources":["../../../../../../src/modules/automations/models/automation-rule.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,2BAA2B,CAAA;AAEjD,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAC,MAAM,CAAC,iBAAiB,EAAE;IACjE,EAAE,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,UAAU,EAAE;IAC3B,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,UAAU,EAAE;IAC/B,WAAW,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;IACpC,YAAY,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC;IAC1E,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;IACnC,eAAe,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;IACxC,UAAU,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;IACnC,OAAO,EAAE,KAAK,CAAC,IAAI,EAAE;IACrB,SAAS,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IACxC,SAAS,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IACpC,WAAW,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE;IACxC,QAAQ,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE;CAClC,CAAC,CAAA"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { AutomationActionHandler, AutomationActionResult, AutomationTriggerType } from "../../types";
|
|
2
|
+
export interface ExecuteRuleParams {
|
|
3
|
+
rule: Record<string, any>;
|
|
4
|
+
triggerType: AutomationTriggerType;
|
|
5
|
+
eventName: string;
|
|
6
|
+
context: Record<string, any>;
|
|
7
|
+
container: any;
|
|
8
|
+
}
|
|
9
|
+
declare const AutomationsModuleService_base: import("@medusajs/framework/utils").MedusaServiceReturnType<import("@medusajs/framework/utils").ModelConfigurationsToConfigTemplate<{
|
|
10
|
+
readonly AutomationRule: import("@medusajs/framework/utils").DmlEntity<import("@medusajs/framework/utils").DMLEntitySchemaBuilder<{
|
|
11
|
+
id: import("@medusajs/framework/utils").PrimaryKeyModifier<string, import("@medusajs/framework/utils").IdProperty>;
|
|
12
|
+
name: import("@medusajs/framework/utils").TextProperty;
|
|
13
|
+
description: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
14
|
+
trigger_type: import("@medusajs/framework/utils").EnumProperty<["event", "schedule", "manual"]>;
|
|
15
|
+
event_name: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
16
|
+
cron_expression: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
17
|
+
conditions: import("@medusajs/framework/utils").NullableModifier<NoInfer<Record<string, unknown>>, import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>>;
|
|
18
|
+
actions: import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>;
|
|
19
|
+
is_active: import("@medusajs/framework/utils").BooleanProperty;
|
|
20
|
+
run_count: import("@medusajs/framework/utils").NumberProperty;
|
|
21
|
+
last_run_at: import("@medusajs/framework/utils").NullableModifier<Date, import("@medusajs/framework/utils").DateTimeProperty>;
|
|
22
|
+
metadata: import("@medusajs/framework/utils").NullableModifier<NoInfer<Record<string, unknown>>, import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>>;
|
|
23
|
+
}>, "automation_rule">;
|
|
24
|
+
readonly AutomationExecutionLog: import("@medusajs/framework/utils").DmlEntity<import("@medusajs/framework/utils").DMLEntitySchemaBuilder<{
|
|
25
|
+
id: import("@medusajs/framework/utils").PrimaryKeyModifier<string, import("@medusajs/framework/utils").IdProperty>;
|
|
26
|
+
rule_id: import("@medusajs/framework/utils").TextProperty;
|
|
27
|
+
trigger_type: import("@medusajs/framework/utils").EnumProperty<["event", "schedule", "manual"]>;
|
|
28
|
+
event_name: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
29
|
+
status: import("@medusajs/framework/utils").EnumProperty<["success", "failed", "skipped"]>;
|
|
30
|
+
actions_executed: import("@medusajs/framework/utils").NumberProperty;
|
|
31
|
+
result_summary: import("@medusajs/framework/utils").NullableModifier<NoInfer<Record<string, unknown>>, import("@medusajs/framework/utils").JSONProperty<NoInfer<Record<string, unknown>>>>;
|
|
32
|
+
error_message: import("@medusajs/framework/utils").NullableModifier<string, import("@medusajs/framework/utils").TextProperty>;
|
|
33
|
+
}>, "automation_execution_log">;
|
|
34
|
+
}>>;
|
|
35
|
+
export declare class AutomationsModuleService extends AutomationsModuleService_base {
|
|
36
|
+
private readonly handlers_;
|
|
37
|
+
constructor(container: Record<string, unknown>, options?: {
|
|
38
|
+
actionHandlers?: AutomationActionHandler[];
|
|
39
|
+
});
|
|
40
|
+
registerActionHandler(handler: AutomationActionHandler): Promise<void>;
|
|
41
|
+
listActionHandlers(): Promise<Array<{
|
|
42
|
+
id: string;
|
|
43
|
+
label: string;
|
|
44
|
+
description?: string;
|
|
45
|
+
enabled: boolean;
|
|
46
|
+
fields: AutomationActionHandler["fields"];
|
|
47
|
+
}>>;
|
|
48
|
+
executeRule({ rule, triggerType, eventName, context, container, }: ExecuteRuleParams): Promise<{
|
|
49
|
+
status: "success" | "failed" | "skipped";
|
|
50
|
+
results: AutomationActionResult[];
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
53
|
+
export default AutomationsModuleService;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { MedusaService } from "@medusajs/framework/utils";
|
|
2
|
+
import { createBuiltInActionHandlers } from "./actions/built-in-handlers";
|
|
3
|
+
import { AutomationExecutionLogModel } from "./models/automation-execution-log";
|
|
4
|
+
import { AutomationRuleModel } from "./models/automation-rule";
|
|
5
|
+
import { evaluateConditions } from "./utils/rule-evaluator";
|
|
6
|
+
export class AutomationsModuleService extends MedusaService({
|
|
7
|
+
AutomationRule: AutomationRuleModel,
|
|
8
|
+
AutomationExecutionLog: AutomationExecutionLogModel,
|
|
9
|
+
}) {
|
|
10
|
+
handlers_ = new Map();
|
|
11
|
+
constructor(container, options) {
|
|
12
|
+
super(container, options);
|
|
13
|
+
for (const handler of createBuiltInActionHandlers()) {
|
|
14
|
+
this.handlers_.set(handler.id, handler);
|
|
15
|
+
}
|
|
16
|
+
for (const custom of options?.actionHandlers ?? []) {
|
|
17
|
+
this.handlers_.set(custom.id, custom);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async registerActionHandler(handler) {
|
|
21
|
+
this.handlers_.set(handler.id, handler);
|
|
22
|
+
}
|
|
23
|
+
async listActionHandlers() {
|
|
24
|
+
return [...this.handlers_.values()].map((h) => ({
|
|
25
|
+
id: h.id,
|
|
26
|
+
label: h.label,
|
|
27
|
+
description: h.description,
|
|
28
|
+
enabled: h.enabled ?? true,
|
|
29
|
+
fields: h.fields ?? [],
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
async executeRule({ rule, triggerType, eventName, context, container, }) {
|
|
33
|
+
const conditions = (rule.conditions ?? []);
|
|
34
|
+
if (!evaluateConditions(conditions, context)) {
|
|
35
|
+
await this.createAutomationExecutionLogs({
|
|
36
|
+
rule_id: String(rule.id),
|
|
37
|
+
trigger_type: triggerType,
|
|
38
|
+
event_name: eventName,
|
|
39
|
+
status: "skipped",
|
|
40
|
+
actions_executed: 0,
|
|
41
|
+
result_summary: { reason: "conditions_not_met" },
|
|
42
|
+
});
|
|
43
|
+
return { status: "skipped", results: [] };
|
|
44
|
+
}
|
|
45
|
+
const actions = (Array.isArray(rule.actions) ? rule.actions : []);
|
|
46
|
+
const results = [];
|
|
47
|
+
let failed = false;
|
|
48
|
+
for (const action of actions) {
|
|
49
|
+
const handler = this.handlers_.get(action.type);
|
|
50
|
+
if (!handler || handler.enabled === false) {
|
|
51
|
+
results.push({
|
|
52
|
+
success: false,
|
|
53
|
+
message: `Action handler '${action.type}' not found or disabled`,
|
|
54
|
+
});
|
|
55
|
+
failed = true;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const res = await handler.executeAction({
|
|
60
|
+
trigger: { type: triggerType, event_name: eventName },
|
|
61
|
+
action,
|
|
62
|
+
context,
|
|
63
|
+
container,
|
|
64
|
+
eventName,
|
|
65
|
+
});
|
|
66
|
+
results.push(res);
|
|
67
|
+
if (!res.success)
|
|
68
|
+
failed = true;
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
const msg = err instanceof Error ? err.message : "Action execution error";
|
|
72
|
+
results.push({ success: false, message: msg });
|
|
73
|
+
failed = true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const status = failed ? "failed" : "success";
|
|
77
|
+
await this.createAutomationExecutionLogs({
|
|
78
|
+
rule_id: String(rule.id),
|
|
79
|
+
trigger_type: triggerType,
|
|
80
|
+
event_name: eventName,
|
|
81
|
+
status,
|
|
82
|
+
actions_executed: results.length,
|
|
83
|
+
result_summary: { results },
|
|
84
|
+
error_message: failed
|
|
85
|
+
? results.find((r) => !r.success)?.message || "Action failed"
|
|
86
|
+
: null,
|
|
87
|
+
});
|
|
88
|
+
await this.updateAutomationRules({
|
|
89
|
+
id: String(rule.id),
|
|
90
|
+
run_count: Number(rule.run_count || 0) + 1,
|
|
91
|
+
last_run_at: new Date(),
|
|
92
|
+
});
|
|
93
|
+
return { status, results };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export default AutomationsModuleService;
|
|
97
|
+
//# sourceMappingURL=service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
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OAAO,CAAC,EAAE,OAAO,IAAI,eAAe;gBAC7D,CAAC,CAAC,IAAI;SACT,CAAC,CAAA;QAEF,MAAM,IAAI,CAAC,qBAAqB,CAAC;YAC/B,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YACnB,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,CAAC,GAAG,CAAC;YAC1C,WAAW,EAAE,IAAI,IAAI,EAAE;SACxB,CAAC,CAAA;QAEF,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAA;IAC5B,CAAC;CACF;AAED,eAAe,wBAAwB,CAAA"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AutomationCondition } from "../../../types";
|
|
2
|
+
export declare function resolveContextPath(source: Record<string, any> | null | undefined, path: string): unknown;
|
|
3
|
+
export declare function evaluateCondition(condition: AutomationCondition, context: Record<string, any>): boolean;
|
|
4
|
+
export declare function evaluateConditions(conditions: AutomationCondition[] | null | undefined, context: Record<string, any>): boolean;
|