@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,85 @@
|
|
|
1
|
+
const BLOCKED_SEGMENTS = new Set(["__proto__", "constructor", "prototype"]);
|
|
2
|
+
export function resolveContextPath(source, path) {
|
|
3
|
+
if (!source || typeof source !== "object" || !path) {
|
|
4
|
+
return undefined;
|
|
5
|
+
}
|
|
6
|
+
const segments = path.trim().split(".").filter(Boolean);
|
|
7
|
+
let current = source;
|
|
8
|
+
for (const seg of segments) {
|
|
9
|
+
if (BLOCKED_SEGMENTS.has(seg)) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
if (current == null || typeof current !== "object") {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
if (Array.isArray(current) && /^\d+$/.test(seg)) {
|
|
16
|
+
current = current[Number(seg)];
|
|
17
|
+
}
|
|
18
|
+
else if (Object.prototype.hasOwnProperty.call(current, seg)) {
|
|
19
|
+
current = current[seg];
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return current;
|
|
26
|
+
}
|
|
27
|
+
export function evaluateCondition(condition, context) {
|
|
28
|
+
const actual = resolveContextPath(context, condition.field);
|
|
29
|
+
const expected = condition.value;
|
|
30
|
+
switch (condition.operator) {
|
|
31
|
+
case "exists":
|
|
32
|
+
return actual !== undefined && actual !== null && actual !== "";
|
|
33
|
+
case "not_exists":
|
|
34
|
+
return actual === undefined || actual === null || actual === "";
|
|
35
|
+
case "eq":
|
|
36
|
+
return actual === expected;
|
|
37
|
+
case "neq":
|
|
38
|
+
return actual !== expected;
|
|
39
|
+
case "gt":
|
|
40
|
+
return typeof actual === "number" && typeof expected === "number"
|
|
41
|
+
? actual > expected
|
|
42
|
+
: Number(actual) > Number(expected);
|
|
43
|
+
case "gte":
|
|
44
|
+
return typeof actual === "number" && typeof expected === "number"
|
|
45
|
+
? actual >= expected
|
|
46
|
+
: Number(actual) >= Number(expected);
|
|
47
|
+
case "lt":
|
|
48
|
+
return typeof actual === "number" && typeof expected === "number"
|
|
49
|
+
? actual < expected
|
|
50
|
+
: Number(actual) < Number(expected);
|
|
51
|
+
case "lte":
|
|
52
|
+
return typeof actual === "number" && typeof expected === "number"
|
|
53
|
+
? actual <= expected
|
|
54
|
+
: Number(actual) <= Number(expected);
|
|
55
|
+
case "in":
|
|
56
|
+
return Array.isArray(expected) && expected.includes(actual);
|
|
57
|
+
case "not_in":
|
|
58
|
+
return Array.isArray(expected) && !expected.includes(actual);
|
|
59
|
+
case "contains": {
|
|
60
|
+
if (Array.isArray(actual))
|
|
61
|
+
return actual.includes(expected);
|
|
62
|
+
if (typeof actual === "string" && typeof expected === "string") {
|
|
63
|
+
return actual.toLowerCase().includes(expected.toLowerCase());
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
case "not_contains": {
|
|
68
|
+
if (Array.isArray(actual))
|
|
69
|
+
return !actual.includes(expected);
|
|
70
|
+
if (typeof actual === "string" && typeof expected === "string") {
|
|
71
|
+
return !actual.toLowerCase().includes(expected.toLowerCase());
|
|
72
|
+
}
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
default:
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function evaluateConditions(conditions, context) {
|
|
80
|
+
if (!Array.isArray(conditions) || conditions.length === 0) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return conditions.every((cond) => evaluateCondition(cond, context));
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=rule-evaluator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rule-evaluator.js","sourceRoot":"","sources":["../../../../../../src/modules/automations/utils/rule-evaluator.ts"],"names":[],"mappings":"AAEA,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,WAAW,EAAE,aAAa,EAAE,WAAW,CAAC,CAAC,CAAA;AAE3E,MAAM,UAAU,kBAAkB,CAChC,MAA8C,EAC9C,IAAY;IAEZ,IAAI,CAAC,MAAM,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;QACnD,OAAO,SAAS,CAAA;IAClB,CAAC;IACD,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;IACvD,IAAI,OAAO,GAAQ,MAAM,CAAA;IACzB,KAAK,MAAM,GAAG,IAAI,QAAQ,EAAE,CAAC;QAC3B,IAAI,gBAAgB,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC9B,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,IAAI,OAAO,IAAI,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;YACnD,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;YAChD,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAA;QAChC,CAAC;aAAM,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc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AAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAA;YAC/D,CAAC;YACD,OAAO,IAAI,CAAA;QACb,CAAC;QACD;YACE,OAAO,KAAK,CAAA;IAChB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,kBAAkB,CAChC,UAAoD,EACpD,OAA4B;IAE5B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1D,OAAO,IAAI,CAAA;IACb,CAAC;IACD,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAA;AACrE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/sdk/index.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,SAAS,EAAE,oBAAoB;IAC/B,SAAS,EAAE,oBAAoB;CACvB,CAAA"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { evaluateAndExecuteAutomationsWorkflow } from "../workflows";
|
|
2
|
+
export default async function automationEventsSubscriber({ event: { name, data }, container, }) {
|
|
3
|
+
if (!name)
|
|
4
|
+
return;
|
|
5
|
+
try {
|
|
6
|
+
await evaluateAndExecuteAutomationsWorkflow(container).run({
|
|
7
|
+
input: {
|
|
8
|
+
trigger_type: "event",
|
|
9
|
+
event_name: name,
|
|
10
|
+
context: data || {},
|
|
11
|
+
},
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
// Non-blocking in event subscriber so commerce lifecycle never fails
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export const config = {
|
|
19
|
+
event: [
|
|
20
|
+
"order.placed",
|
|
21
|
+
"order.completed",
|
|
22
|
+
"order.canceled",
|
|
23
|
+
"customer.created",
|
|
24
|
+
"product.created",
|
|
25
|
+
"product.updated",
|
|
26
|
+
"inventory-level.updated",
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=automation-events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"automation-events.js","sourceRoot":"","sources":["../../../../src/subscribers/automation-events.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qCAAqC,EAAE,MAAM,cAAc,CAAA;AAEpE,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,0BAA0B,CAAC,EACvD,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,EACrB,SAAS,GAC2B;IACpC,IAAI,CAAC,IAAI;QAAE,OAAM;IACjB,IAAI,CAAC;QACH,MAAM,qCAAqC,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC;YACzD,KAAK,EAAE;gBACL,YAAY,EAAE,OAAO;gBACrB,UAAU,EAAE,IAAI;gBAChB,OAAO,EAAE,IAAI,IAAI,EAAE;aACpB;SACF,CAAC,CAAA;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,qEAAqE;IACvE,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,MAAM,GAAqB;IACtC,KAAK,EAAE;QACL,cAAc;QACd,iBAAiB;QACjB,gBAAgB;QAChB,kBAAkB;QAClB,iBAAiB;QACjB,iBAAiB;QACjB,yBAAyB;KAC1B;CACF,CAAA"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export type AutomationTriggerType = "event" | "schedule" | "manual";
|
|
2
|
+
export type RuleOperator = "eq" | "neq" | "gt" | "gte" | "lt" | "lte" | "in" | "not_in" | "contains" | "not_contains" | "exists" | "not_exists";
|
|
3
|
+
export interface AutomationCondition {
|
|
4
|
+
field: string;
|
|
5
|
+
operator: RuleOperator;
|
|
6
|
+
value?: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface AutomationActionConfig {
|
|
9
|
+
type: string;
|
|
10
|
+
config?: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
export interface AutomationActionResult {
|
|
13
|
+
success: boolean;
|
|
14
|
+
message?: string;
|
|
15
|
+
data?: unknown;
|
|
16
|
+
}
|
|
17
|
+
export interface AutomationActionField {
|
|
18
|
+
name: string;
|
|
19
|
+
key: string;
|
|
20
|
+
label: string;
|
|
21
|
+
type: "text" | "email" | "textarea" | "select" | "boolean" | "number";
|
|
22
|
+
required?: boolean;
|
|
23
|
+
defaultValue?: unknown;
|
|
24
|
+
options?: Array<{
|
|
25
|
+
value: string;
|
|
26
|
+
name: string;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
export interface AutomationActionHandler {
|
|
30
|
+
id: string;
|
|
31
|
+
label: string;
|
|
32
|
+
description?: string;
|
|
33
|
+
enabled?: boolean;
|
|
34
|
+
fields?: AutomationActionField[];
|
|
35
|
+
executeAction: (params: {
|
|
36
|
+
trigger: {
|
|
37
|
+
type: AutomationTriggerType;
|
|
38
|
+
event_name?: string | null;
|
|
39
|
+
};
|
|
40
|
+
action: AutomationActionConfig;
|
|
41
|
+
context: Record<string, any>;
|
|
42
|
+
container: any;
|
|
43
|
+
eventName: string;
|
|
44
|
+
}) => Promise<AutomationActionResult>;
|
|
45
|
+
}
|
|
46
|
+
export interface CreateAutomationRuleInput {
|
|
47
|
+
name: string;
|
|
48
|
+
description?: string | null;
|
|
49
|
+
trigger_type?: AutomationTriggerType;
|
|
50
|
+
event_name?: string | null;
|
|
51
|
+
cron_expression?: string | null;
|
|
52
|
+
conditions?: AutomationCondition[];
|
|
53
|
+
actions: AutomationActionConfig[];
|
|
54
|
+
is_active?: boolean;
|
|
55
|
+
metadata?: Record<string, unknown> | null;
|
|
56
|
+
}
|
|
57
|
+
export interface UpdateAutomationRuleInput {
|
|
58
|
+
id: string;
|
|
59
|
+
name?: string;
|
|
60
|
+
description?: string | null;
|
|
61
|
+
trigger_type?: AutomationTriggerType;
|
|
62
|
+
event_name?: string | null;
|
|
63
|
+
cron_expression?: string | null;
|
|
64
|
+
conditions?: AutomationCondition[];
|
|
65
|
+
actions?: AutomationActionConfig[];
|
|
66
|
+
is_active?: boolean;
|
|
67
|
+
metadata?: Record<string, unknown> | null;
|
|
68
|
+
}
|
|
69
|
+
export interface CreateAutomationsInput {
|
|
70
|
+
label: string;
|
|
71
|
+
active?: boolean;
|
|
72
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/types/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { AutomationTriggerType } from "../types";
|
|
2
|
+
export interface DispatchAutomationEventInput {
|
|
3
|
+
trigger_type: AutomationTriggerType;
|
|
4
|
+
event_name: string;
|
|
5
|
+
context: Record<string, any>;
|
|
6
|
+
rule_id?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare const evaluateAndExecuteAutomationsStep: import("@medusajs/framework/workflows-sdk").StepFunction<DispatchAutomationEventInput, {
|
|
9
|
+
matched_rules: number;
|
|
10
|
+
executions: {
|
|
11
|
+
status: "success" | "failed" | "skipped";
|
|
12
|
+
results: import("..").AutomationActionResult[];
|
|
13
|
+
rule_id: string;
|
|
14
|
+
}[];
|
|
15
|
+
}>;
|
|
16
|
+
export declare const evaluateAndExecuteAutomationsWorkflow: import("@medusajs/framework/workflows-sdk").ReturnWorkflow<DispatchAutomationEventInput, {
|
|
17
|
+
matched_rules: number;
|
|
18
|
+
executions: {
|
|
19
|
+
status: "success" | "failed" | "skipped";
|
|
20
|
+
results: import("..").AutomationActionResult[];
|
|
21
|
+
rule_id: string;
|
|
22
|
+
}[];
|
|
23
|
+
}, []>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createStep, createWorkflow, StepResponse, WorkflowResponse, } from "@medusajs/framework/workflows-sdk";
|
|
2
|
+
import { AUTOMATIONS_MODULE } from "../modules/automations";
|
|
3
|
+
export const evaluateAndExecuteAutomationsStep = createStep("evaluate-and-execute-automations", async (input, { container }) => {
|
|
4
|
+
const service = container.resolve(AUTOMATIONS_MODULE);
|
|
5
|
+
const filters = input.rule_id
|
|
6
|
+
? { id: input.rule_id }
|
|
7
|
+
: {
|
|
8
|
+
is_active: true,
|
|
9
|
+
trigger_type: input.trigger_type,
|
|
10
|
+
event_name: input.event_name,
|
|
11
|
+
};
|
|
12
|
+
const rules = await service.listAutomationRules(filters);
|
|
13
|
+
const executions = [];
|
|
14
|
+
for (const rule of rules) {
|
|
15
|
+
const outcome = await service.executeRule({
|
|
16
|
+
rule,
|
|
17
|
+
triggerType: input.trigger_type,
|
|
18
|
+
eventName: input.event_name,
|
|
19
|
+
context: input.context || {},
|
|
20
|
+
container,
|
|
21
|
+
});
|
|
22
|
+
executions.push({ rule_id: rule.id, ...outcome });
|
|
23
|
+
}
|
|
24
|
+
return new StepResponse({ matched_rules: rules.length, executions });
|
|
25
|
+
});
|
|
26
|
+
export const evaluateAndExecuteAutomationsWorkflow = createWorkflow("evaluate-and-execute-automations", (input) => {
|
|
27
|
+
const result = evaluateAndExecuteAutomationsStep(input);
|
|
28
|
+
return new WorkflowResponse(result);
|
|
29
|
+
});
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/workflows/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,cAAc,EACd,YAAY,EACZ,gBAAgB,GACjB,MAAM,mCAAmC,CAAA;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAW3D,MAAM,CAAC,MAAM,iCAAiC,GAAG,UAAU,CACzD,kCAAkC,EAClC,KAAK,EAAE,KAAmC,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE;IAC3D,MAAM,OAAO,GAAG,SAAS,CAAC,OAAO,CAC/B,kBAAkB,CACS,CAAA;IAE7B,MAAM,OAAO,GAA4B,KAAK,CAAC,OAAO;QACpD,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,CAAC,OAAO,EAAE;QACvB,CAAC,CAAC;YACE,SAAS,EAAE,IAAI;YACf,YAAY,EAAE,KAAK,CAAC,YAAY;YAChC,UAAU,EAAE,KAAK,CAAC,UAAU;SAC7B,CAAA;IAEL,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAA;IACxD,MAAM,UAAU,GAAG,EAAE,CAAA;IACrB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,WAAW,CAAC;YACxC,IAAI;YACJ,WAAW,EAAE,KAAK,CAAC,YAAY;YAC/B,SAAS,EAAE,KAAK,CAAC,UAAU;YAC3B,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,EAAE;YAC5B,SAAS;SACV,CAAC,CAAA;QACF,UAAU,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA;IACnD,CAAC;IAED,OAAO,IAAI,YAAY,CAAC,EAAE,aAAa,EAAE,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,CAAC,CAAA;AACtE,CAAC,CACF,CAAA;AAED,MAAM,CAAC,MAAM,qCAAqC,GAAG,cAAc,CACjE,kCAAkC,EAClC,CAAC,KAAmC,EAAE,EAAE;IACtC,MAAM,MAAM,GAAG,iCAAiC,CAAC,KAAK,CAAC,CAAA;IACvD,OAAO,IAAI,gBAAgB,CAAC,MAAM,CAAC,CAAA;AACrC,CAAC,CACF,CAAA"}
|
package/README.md
CHANGED
|
@@ -1,3 +1,29 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vicacha-devs/medusa-plugin-automations`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Rule-based **trigger → condition → action** automation engine for **Medusa v2** (`>= 2.23.0`).
|
|
4
|
+
|
|
5
|
+
- **Triggers**: Commerce events (`order.placed`, `order.completed`, `customer.created`, `inventory-level.updated`, etc.), scheduled rules, and manual triggers (`POST /admin/automations/:id/trigger`).
|
|
6
|
+
- **Condition Engine**: Prototype-safe nested path evaluation (`eq`, `neq`, `gt`, `gte`, `lt`, `lte`, `in`, `not_in`, `contains`, `not_contains`, `exists`, `not_exists`).
|
|
7
|
+
- **Action Handlers**:
|
|
8
|
+
- `notification`: Dispatches via Medusa's built-in `Modules.NOTIFICATION`.
|
|
9
|
+
- `mpn-notification-studio`: Optional zero-hard-dependency integration with `@vicacha-devs/medusa-plugin-notifications`.
|
|
10
|
+
- `webhook`: SSRF-guarded outgoing JSON webhook POST.
|
|
11
|
+
- Extensible via `registerActionHandler()` or `options.actionHandlers`.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @vicacha-devs/medusa-plugin-automations
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// medusa-config.ts
|
|
21
|
+
export default defineConfig({
|
|
22
|
+
plugins: [
|
|
23
|
+
{
|
|
24
|
+
resolve: "@vicacha-devs/medusa-plugin-automations",
|
|
25
|
+
options: {},
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
})
|
|
29
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,88 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vicacha-devs/medusa-plugin-automations",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Automations plugin for Medusa v2 by Vicacha Devs.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"automations",
|
|
7
|
+
"medusa",
|
|
8
|
+
"medusa-plugin",
|
|
9
|
+
"medusa-plugin-automations",
|
|
10
|
+
"medusa-plugin-integration",
|
|
11
|
+
"medusa-v2",
|
|
12
|
+
"rules",
|
|
13
|
+
"workflows"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Vicacha Devs",
|
|
17
|
+
"files": [
|
|
18
|
+
".medusa/server",
|
|
19
|
+
"src",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"tsconfig.json"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"exports": {
|
|
26
|
+
"./package.json": "./package.json",
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./.medusa/server/src/index.js",
|
|
29
|
+
"default": "./.medusa/server/src/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./admin": {
|
|
32
|
+
"import": "./.medusa/server/src/admin/index.js",
|
|
33
|
+
"default": "./.medusa/server/src/admin/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./sdk": {
|
|
36
|
+
"import": "./src/sdk/index.ts",
|
|
37
|
+
"default": "./.medusa/server/src/sdk/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./types": {
|
|
40
|
+
"import": "./src/types/index.ts",
|
|
41
|
+
"default": "./.medusa/server/src/types/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./workflows": "./.medusa/server/src/workflows/index.js",
|
|
44
|
+
"./modules/*": "./.medusa/server/src/modules/*/index.js"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"provenance": true
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@medusajs/admin-sdk": "2.23.0",
|
|
52
|
+
"@medusajs/admin-shared": "2.23.0",
|
|
53
|
+
"@medusajs/cli": "2.23.0",
|
|
54
|
+
"@medusajs/framework": "2.23.0",
|
|
55
|
+
"@medusajs/medusa": "2.23.0",
|
|
56
|
+
"@medusajs/types": "2.23.0",
|
|
57
|
+
"@medusajs/ui": "4.3.0",
|
|
58
|
+
"react": "^19.3.1",
|
|
59
|
+
"react-dom": "^19.3.1",
|
|
60
|
+
"tsc-alias": "^1.9.2",
|
|
61
|
+
"typescript": "5.6.2",
|
|
62
|
+
"vite": "^8.3.8",
|
|
63
|
+
"@vicacha-devs/medusa-plugin-testing": "1.0.0",
|
|
64
|
+
"@vicacha-devs/medusa-typescript-config": "1.0.0",
|
|
65
|
+
"@vicacha-devs/medusa-eslint-config": "1.0.0"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"@medusajs/admin-sdk": "2.23.0",
|
|
69
|
+
"@medusajs/admin-shared": "2.23.0",
|
|
70
|
+
"@medusajs/framework": "2.23.0",
|
|
71
|
+
"@medusajs/medusa": "2.23.0",
|
|
72
|
+
"@medusajs/ui": "4.3.0",
|
|
73
|
+
"react": "^19.3.1",
|
|
74
|
+
"react-dom": "^19.3.1",
|
|
75
|
+
"zod": "4.6.7"
|
|
76
|
+
},
|
|
77
|
+
"engines": {
|
|
78
|
+
"node": ">=22.22.0"
|
|
79
|
+
},
|
|
80
|
+
"scripts": {
|
|
81
|
+
"build": "medusa plugin:build && tsc-alias -p tsconfig.json --resolve-full-paths",
|
|
82
|
+
"clean": "rm -rf .medusa dist",
|
|
83
|
+
"dev": "medusa plugin:develop",
|
|
84
|
+
"lint": "medusa lint",
|
|
85
|
+
"test": "node --import ../plugin-testing/register-ts-resolver.mjs --import ../plugin-testing/register-js-extension.mjs --test src/__tests__/*.spec.ts",
|
|
86
|
+
"typecheck": "tsc --noEmit"
|
|
87
|
+
}
|
|
6
88
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import assert from "node:assert/strict"
|
|
2
|
+
import { describe, it } from "node:test"
|
|
3
|
+
import { Modules } from "@medusajs/framework/utils"
|
|
4
|
+
import {
|
|
5
|
+
MedusaNotificationActionHandler,
|
|
6
|
+
WebhookActionHandler,
|
|
7
|
+
} from "../modules/automations/actions/built-in-handlers"
|
|
8
|
+
import {
|
|
9
|
+
evaluateConditions,
|
|
10
|
+
resolveContextPath,
|
|
11
|
+
} from "../modules/automations/utils/rule-evaluator"
|
|
12
|
+
|
|
13
|
+
describe("@vicacha-devs/medusa-plugin-automations", () => {
|
|
14
|
+
it("resolves nested paths safely and blocks prototype traversal", () => {
|
|
15
|
+
const ctx = {
|
|
16
|
+
order: { total: 250, currency_code: "usd", items: [{ sku: "SKU-1" }] },
|
|
17
|
+
}
|
|
18
|
+
assert.equal(resolveContextPath(ctx, "order.total"), 250)
|
|
19
|
+
assert.equal(resolveContextPath(ctx, "order.items.0.sku"), "SKU-1")
|
|
20
|
+
assert.equal(resolveContextPath(ctx, "order.__proto__"), undefined)
|
|
21
|
+
assert.equal(resolveContextPath(ctx, "constructor.prototype"), undefined)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it("evaluates multi-condition rules across numeric, string, and array operators", () => {
|
|
25
|
+
const ctx = {
|
|
26
|
+
order: { total: 150, currency_code: "USD" },
|
|
27
|
+
customer: { email: "vip@example.com", tags: ["vip", "b2b"] },
|
|
28
|
+
}
|
|
29
|
+
assert.equal(
|
|
30
|
+
evaluateConditions(
|
|
31
|
+
[
|
|
32
|
+
{ field: "order.total", operator: "gte", value: 100 },
|
|
33
|
+
{
|
|
34
|
+
field: "order.currency_code",
|
|
35
|
+
operator: "in",
|
|
36
|
+
value: ["USD", "EUR"],
|
|
37
|
+
},
|
|
38
|
+
{ field: "customer.tags", operator: "contains", value: "vip" },
|
|
39
|
+
{ field: "customer.email", operator: "exists" },
|
|
40
|
+
],
|
|
41
|
+
ctx
|
|
42
|
+
),
|
|
43
|
+
true
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
assert.equal(
|
|
47
|
+
evaluateConditions(
|
|
48
|
+
[{ field: "order.total", operator: "lt", value: 100 }],
|
|
49
|
+
ctx
|
|
50
|
+
),
|
|
51
|
+
false
|
|
52
|
+
)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("dispatches notifications via Modules.NOTIFICATION in MedusaNotificationActionHandler", async () => {
|
|
56
|
+
const dispatched: any[] = []
|
|
57
|
+
const container = {
|
|
58
|
+
resolve(key: string) {
|
|
59
|
+
if (key === Modules.NOTIFICATION) {
|
|
60
|
+
return {
|
|
61
|
+
async createNotifications(payload: any) {
|
|
62
|
+
dispatched.push(payload)
|
|
63
|
+
return { id: "notif_auto_1" }
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return null
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const handler = new MedusaNotificationActionHandler()
|
|
72
|
+
const res = await handler.executeAction({
|
|
73
|
+
trigger: { type: "event", event_name: "order.placed" },
|
|
74
|
+
action: {
|
|
75
|
+
type: "notification",
|
|
76
|
+
config: {
|
|
77
|
+
channel: "email",
|
|
78
|
+
template: "order-vip",
|
|
79
|
+
to: "{{customer.email}}",
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
context: { customer: { email: "buyer@example.com" }, total: 500 },
|
|
83
|
+
container,
|
|
84
|
+
eventName: "order.placed",
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
assert.equal(res.success, true)
|
|
88
|
+
assert.equal(dispatched.length, 1)
|
|
89
|
+
assert.equal(dispatched[0].to, "buyer@example.com")
|
|
90
|
+
assert.equal(dispatched[0].template, "order-vip")
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it("blocks unsafe non-HTTPS webhook URLs in production", async () => {
|
|
94
|
+
const prevEnv = process.env.NODE_ENV
|
|
95
|
+
process.env.NODE_ENV = "production"
|
|
96
|
+
try {
|
|
97
|
+
const handler = new WebhookActionHandler()
|
|
98
|
+
const res = await handler.executeAction({
|
|
99
|
+
trigger: { type: "manual" },
|
|
100
|
+
action: {
|
|
101
|
+
type: "webhook",
|
|
102
|
+
config: { url: "http://169.254.169.254/latest/meta-data" },
|
|
103
|
+
},
|
|
104
|
+
context: {},
|
|
105
|
+
container: {},
|
|
106
|
+
eventName: "manual.trigger",
|
|
107
|
+
})
|
|
108
|
+
assert.equal(res.success, false)
|
|
109
|
+
} finally {
|
|
110
|
+
process.env.NODE_ENV = prevEnv
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
})
|