@vicacha-devs/medusa-plugin-notifications 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 +4215 -0
- package/.medusa/server/src/admin/index.js +4193 -0
- package/.medusa/server/src/api/admin/mpn/broadcasts/route.js +99 -0
- package/.medusa/server/src/api/admin/mpn/channels/route.js +109 -0
- package/.medusa/server/src/api/admin/mpn/logs/route.js +33 -0
- package/.medusa/server/src/api/admin/mpn/mappings/[id]/route.js +33 -0
- package/.medusa/server/src/api/admin/mpn/mappings/route.js +51 -0
- package/.medusa/server/src/api/admin/mpn/render-template/route.js +37 -0
- package/.medusa/server/src/api/admin/mpn/sandbox/route.js +36 -0
- package/.medusa/server/src/api/admin/mpn/send/route.js +135 -0
- package/.medusa/server/src/api/admin/mpn/templates/[id]/route.js +77 -0
- package/.medusa/server/src/api/admin/mpn/templates/route.js +71 -0
- package/.medusa/server/src/api/admin/plugin/route.js +4 -0
- package/.medusa/server/src/api/middlewares.js +28 -0
- package/.medusa/server/src/api/store/mpn/consent/route.js +37 -0
- package/.medusa/server/src/api/store/mpn/webhooks/[provider]/route.js +97 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/api/validators.js +118 -0
- package/.medusa/server/src/hooks/index.js +7 -0
- package/.medusa/server/src/hooks/use-mpn-channels.js +13 -0
- package/.medusa/server/src/hooks/use-mpn-dispatch.js +45 -0
- package/.medusa/server/src/hooks/use-mpn-mappings.js +21 -0
- package/.medusa/server/src/hooks/use-mpn-templates.js +34 -0
- package/.medusa/server/src/hooks/use-order-notifications-widget-state.js +79 -0
- package/.medusa/server/src/hooks/use-sdk-primitives.js +113 -0
- package/.medusa/server/src/index.js +14 -0
- package/.medusa/server/src/integrations/automations-action-handler.js +112 -0
- package/.medusa/server/src/jobs/abandoned-cart-recovery.js +123 -0
- package/.medusa/server/src/jobs/log-retention-and-broadcasts.js +15 -0
- package/.medusa/server/src/modules/notifications/i18n/index.js +65 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/ar.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/de.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/en.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/es.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/fr.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/it.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/nl.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/pl.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/pt.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/tr.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/types.js +2 -0
- package/.medusa/server/src/modules/notifications/index.js +9 -0
- package/.medusa/server/src/modules/notifications/integrations/documents-and-b2b-resolver.js +129 -0
- package/.medusa/server/src/modules/notifications/migrations/Migration20261010000000.js +184 -0
- package/.medusa/server/src/modules/notifications/models/index.js +118 -0
- package/.medusa/server/src/modules/notifications/renderer/block-helpers.js +36 -0
- package/.medusa/server/src/modules/notifications/renderer/email.js +161 -0
- package/.medusa/server/src/modules/notifications/renderer/index.js +240 -0
- package/.medusa/server/src/modules/notifications/renderer/slack.js +114 -0
- package/.medusa/server/src/modules/notifications/renderer/telegram.js +99 -0
- package/.medusa/server/src/modules/notifications/renderer/whatsapp.js +91 -0
- package/.medusa/server/src/modules/notifications/service-helpers/channel-config.js +45 -0
- package/.medusa/server/src/modules/notifications/service-helpers/consent.js +65 -0
- package/.medusa/server/src/modules/notifications/service-helpers/delivery-log.js +29 -0
- package/.medusa/server/src/modules/notifications/service-helpers/event-conditions.js +46 -0
- package/.medusa/server/src/modules/notifications/service-helpers/external-templates.js +80 -0
- package/.medusa/server/src/modules/notifications/service-helpers/recipient-resolution.js +85 -0
- package/.medusa/server/src/modules/notifications/service-helpers/sender-profiles.js +29 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-blocks.js +81 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-catalog.js +129 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-render.js +65 -0
- package/.medusa/server/src/modules/notifications/service.js +339 -0
- package/.medusa/server/src/modules/notifications/types.js +2 -0
- package/.medusa/server/src/modules/notifications/utils/auth-presets.js +140 -0
- package/.medusa/server/src/modules/notifications/utils/interpolation.js +178 -0
- package/.medusa/server/src/modules/notifications/utils/security.js +312 -0
- package/.medusa/server/src/modules/notifications/utils/system-presets.js +651 -0
- package/.medusa/server/src/modules/notifications/utils/transformers.js +358 -0
- package/.medusa/server/src/providers/dummy-sandbox/index.js +96 -0
- package/.medusa/server/src/providers/email-smtp/index.js +187 -0
- package/.medusa/server/src/providers/index.js +7 -0
- package/.medusa/server/src/providers/slack/index.js +54 -0
- package/.medusa/server/src/providers/sms/index.js +254 -0
- package/.medusa/server/src/providers/telegram/index.js +119 -0
- package/.medusa/server/src/providers/whatsapp/index.js +371 -0
- package/.medusa/server/src/rbac.js +109 -0
- package/.medusa/server/src/sdk/client.js +186 -0
- package/.medusa/server/src/sdk/index.js +3 -0
- package/.medusa/server/src/sdk/query-keys.js +61 -0
- package/.medusa/server/src/subscribers/customer-and-document-events.js +33 -0
- package/.medusa/server/src/subscribers/fulfillment-and-inventory-events.js +30 -0
- package/.medusa/server/src/subscribers/order-events.js +28 -0
- package/.medusa/server/src/types/constants.js +129 -0
- package/.medusa/server/src/types/core.js +2 -0
- package/.medusa/server/src/types/domain.js +2 -0
- package/.medusa/server/src/types/enums.js +53 -0
- package/.medusa/server/src/types/errors.js +28 -0
- package/.medusa/server/src/types/hooks.js +2 -0
- package/.medusa/server/src/types/index.js +12 -0
- package/.medusa/server/src/types/providers.js +2 -0
- package/.medusa/server/src/types/records.js +2 -0
- package/.medusa/server/src/types/render.js +2 -0
- package/.medusa/server/src/types/sdk-api.js +2 -0
- package/.medusa/server/src/types/translations.js +2 -0
- package/.medusa/server/src/workflows/index.js +461 -0
- package/LICENSE +21 -0
- package/README.md +389 -2
- package/package.json +138 -3
- package/src/admin/README.md +123 -0
- package/src/admin/components/mpn/broadcast-consent-section.tsx +62 -0
- package/src/admin/components/mpn/broadcasts-tab.tsx +208 -0
- package/src/admin/components/mpn/channels-main-section.tsx +205 -0
- package/src/admin/components/mpn/channels-side-section.tsx +88 -0
- package/src/admin/components/mpn/channels-tab.tsx +130 -0
- package/src/admin/components/mpn/constants.ts +8 -0
- package/src/admin/components/mpn/delivery-logs-tab.tsx +70 -0
- package/src/admin/components/mpn/event-mapping-form.tsx +191 -0
- package/src/admin/components/mpn/event-mappings-tab.tsx +165 -0
- package/src/admin/components/mpn/header-bar.tsx +35 -0
- package/src/admin/components/mpn/index.ts +17 -0
- package/src/admin/components/mpn/overview-cards.tsx +103 -0
- package/src/admin/components/mpn/sandbox-tab.tsx +93 -0
- package/src/admin/components/mpn/template-editor-section.tsx +203 -0
- package/src/admin/components/mpn/template-preview-section.tsx +122 -0
- package/src/admin/components/mpn/templates-list-section.tsx +115 -0
- package/src/admin/components/mpn/templates-tab.tsx +188 -0
- package/src/admin/components/mpn/use-notification-studio.ts +84 -0
- package/src/admin/components/rbac-guard.tsx +153 -0
- package/src/admin/i18n/README.md +134 -0
- package/src/admin/i18n/index.ts +23 -0
- package/src/admin/i18n/json/ar.json +157 -0
- package/src/admin/i18n/json/de.json +157 -0
- package/src/admin/i18n/json/en.json +157 -0
- package/src/admin/i18n/json/es.json +157 -0
- package/src/admin/i18n/json/fr.json +157 -0
- package/src/admin/i18n/json/it.json +157 -0
- package/src/admin/i18n/json/nl.json +157 -0
- package/src/admin/i18n/json/pl.json +157 -0
- package/src/admin/i18n/json/pt.json +157 -0
- package/src/admin/i18n/json/tr.json +157 -0
- package/src/admin/routes/mpn/broadcasts/page.tsx +75 -0
- package/src/admin/routes/mpn/channels/page.tsx +66 -0
- package/src/admin/routes/mpn/logs/page.tsx +50 -0
- package/src/admin/routes/mpn/mappings/page.tsx +62 -0
- package/src/admin/routes/mpn/page.tsx +200 -0
- package/src/admin/routes/mpn/sandbox/page.tsx +53 -0
- package/src/admin/routes/mpn/templates/page.tsx +55 -0
- package/src/admin/tsconfig.json +23 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/order-notifications-widget.tsx +178 -0
- package/src/api/README.md +98 -0
- package/src/api/admin/mpn/broadcasts/route.ts +130 -0
- package/src/api/admin/mpn/channels/route.ts +143 -0
- package/src/api/admin/mpn/logs/route.ts +41 -0
- package/src/api/admin/mpn/mappings/[id]/route.ts +45 -0
- package/src/api/admin/mpn/mappings/route.ts +68 -0
- package/src/api/admin/mpn/render-template/route.ts +46 -0
- package/src/api/admin/mpn/sandbox/route.ts +51 -0
- package/src/api/admin/mpn/send/route.ts +163 -0
- package/src/api/admin/mpn/templates/[id]/route.ts +101 -0
- package/src/api/admin/mpn/templates/route.ts +99 -0
- package/src/api/admin/plugin/route.ts +5 -0
- package/src/api/middlewares.ts +42 -0
- package/src/api/store/mpn/consent/route.ts +45 -0
- package/src/api/store/mpn/webhooks/[provider]/route.ts +119 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/api/validators.ts +149 -0
- package/src/hooks/index.ts +14 -0
- package/src/hooks/use-mpn-channels.ts +43 -0
- package/src/hooks/use-mpn-dispatch.ts +149 -0
- package/src/hooks/use-mpn-mappings.ts +75 -0
- package/src/hooks/use-mpn-templates.ts +120 -0
- package/src/hooks/use-order-notifications-widget-state.ts +103 -0
- package/src/hooks/use-sdk-primitives.ts +157 -0
- package/src/index.ts +16 -0
- package/src/integrations/automations-action-handler.ts +156 -0
- package/src/jobs/README.md +41 -0
- package/src/jobs/abandoned-cart-recovery.ts +152 -0
- package/src/jobs/log-retention-and-broadcasts.ts +23 -0
- package/src/links/README.md +52 -0
- package/src/modules/README.md +99 -0
- package/src/modules/notifications/i18n/index.ts +83 -0
- package/src/modules/notifications/i18n/locales/ar.ts +84 -0
- package/src/modules/notifications/i18n/locales/de.ts +83 -0
- package/src/modules/notifications/i18n/locales/en.ts +84 -0
- package/src/modules/notifications/i18n/locales/es.ts +84 -0
- package/src/modules/notifications/i18n/locales/fr.ts +82 -0
- package/src/modules/notifications/i18n/locales/it.ts +82 -0
- package/src/modules/notifications/i18n/locales/nl.ts +83 -0
- package/src/modules/notifications/i18n/locales/pl.ts +84 -0
- package/src/modules/notifications/i18n/locales/pt.ts +82 -0
- package/src/modules/notifications/i18n/locales/tr.ts +86 -0
- package/src/modules/notifications/i18n/types.ts +1 -0
- package/src/modules/notifications/index.ts +10 -0
- package/src/modules/notifications/integrations/documents-and-b2b-resolver.ts +168 -0
- package/src/modules/notifications/migrations/Migration20261010000000.ts +185 -0
- package/src/modules/notifications/models/index.ts +125 -0
- package/src/modules/notifications/renderer/block-helpers.ts +51 -0
- package/src/modules/notifications/renderer/email.ts +281 -0
- package/src/modules/notifications/renderer/index.ts +301 -0
- package/src/modules/notifications/renderer/slack.ts +168 -0
- package/src/modules/notifications/renderer/telegram.ts +161 -0
- package/src/modules/notifications/renderer/whatsapp.ts +156 -0
- package/src/modules/notifications/service-helpers/channel-config.ts +65 -0
- package/src/modules/notifications/service-helpers/consent.ts +88 -0
- package/src/modules/notifications/service-helpers/delivery-log.ts +56 -0
- package/src/modules/notifications/service-helpers/event-conditions.ts +66 -0
- package/src/modules/notifications/service-helpers/external-templates.ts +127 -0
- package/src/modules/notifications/service-helpers/recipient-resolution.ts +124 -0
- package/src/modules/notifications/service-helpers/sender-profiles.ts +34 -0
- package/src/modules/notifications/service-helpers/template-blocks.ts +116 -0
- package/src/modules/notifications/service-helpers/template-catalog.ts +170 -0
- package/src/modules/notifications/service-helpers/template-render.ts +113 -0
- package/src/modules/notifications/service.ts +588 -0
- package/src/modules/notifications/types.ts +1 -0
- package/src/modules/notifications/utils/auth-presets.ts +145 -0
- package/src/modules/notifications/utils/interpolation.ts +231 -0
- package/src/modules/notifications/utils/security.ts +378 -0
- package/src/modules/notifications/utils/system-presets.ts +695 -0
- package/src/modules/notifications/utils/transformers.ts +456 -0
- package/src/providers/README.md +92 -0
- package/src/providers/dummy-sandbox/index.ts +143 -0
- package/src/providers/email-smtp/index.ts +257 -0
- package/src/providers/index.ts +38 -0
- package/src/providers/slack/index.ts +82 -0
- package/src/providers/sms/index.ts +342 -0
- package/src/providers/telegram/index.ts +181 -0
- package/src/providers/whatsapp/index.ts +505 -0
- package/src/rbac.ts +123 -0
- package/src/sdk/client.ts +319 -0
- package/src/sdk/index.ts +48 -0
- package/src/sdk/query-keys.ts +89 -0
- package/src/subscribers/README.md +50 -0
- package/src/subscribers/customer-and-document-events.ts +36 -0
- package/src/subscribers/fulfillment-and-inventory-events.ts +35 -0
- package/src/subscribers/order-events.ts +31 -0
- package/src/types/constants.ts +143 -0
- package/src/types/core.ts +96 -0
- package/src/types/domain.ts +84 -0
- package/src/types/enums.ts +73 -0
- package/src/types/errors.ts +32 -0
- package/src/types/hooks.ts +48 -0
- package/src/types/index.ts +11 -0
- package/src/types/providers.ts +96 -0
- package/src/types/records.ts +100 -0
- package/src/types/render.ts +74 -0
- package/src/types/sdk-api.ts +228 -0
- package/src/types/translations.ts +75 -0
- package/src/workflows/README.md +67 -0
- package/src/workflows/index.ts +619 -0
- package/tsconfig.json +40 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { EventMappingCondition } from "../types"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Evaluates whether an Event Mapping's conditional filters match the current entity context.
|
|
5
|
+
*/
|
|
6
|
+
export function evaluateEventConditions(
|
|
7
|
+
conditions: EventMappingCondition | null | undefined,
|
|
8
|
+
data: Record<string, any>
|
|
9
|
+
): boolean {
|
|
10
|
+
if (!conditions || typeof conditions !== "object") {
|
|
11
|
+
return true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (
|
|
15
|
+
Array.isArray(conditions.region_ids) &&
|
|
16
|
+
conditions.region_ids.length > 0
|
|
17
|
+
) {
|
|
18
|
+
const regionId = data.region_id || data.region?.id
|
|
19
|
+
if (!regionId || !conditions.region_ids.includes(regionId)) {
|
|
20
|
+
return false
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (
|
|
25
|
+
Array.isArray(conditions.sales_channel_ids) &&
|
|
26
|
+
conditions.sales_channel_ids.length > 0
|
|
27
|
+
) {
|
|
28
|
+
const scId = data.sales_channel_id
|
|
29
|
+
if (!scId || !conditions.sales_channel_ids.includes(scId)) {
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (
|
|
35
|
+
Array.isArray(conditions.currency_codes) &&
|
|
36
|
+
conditions.currency_codes.length > 0
|
|
37
|
+
) {
|
|
38
|
+
const code = String(data.currency_code || "").toUpperCase()
|
|
39
|
+
const allowed = conditions.currency_codes.map((c) => c.toUpperCase())
|
|
40
|
+
if (!code || !allowed.includes(code)) {
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (
|
|
46
|
+
typeof conditions.min_order_total === "number" &&
|
|
47
|
+
conditions.min_order_total > 0
|
|
48
|
+
) {
|
|
49
|
+
const total = Number(data.total ?? 0)
|
|
50
|
+
if (total < conditions.min_order_total) {
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (
|
|
56
|
+
Array.isArray(conditions.document_types) &&
|
|
57
|
+
conditions.document_types.length > 0
|
|
58
|
+
) {
|
|
59
|
+
const docType = data.document_type
|
|
60
|
+
if (!docType || !conditions.document_types.includes(docType)) {
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true
|
|
66
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { createRequire } from "node:module"
|
|
2
|
+
import type {
|
|
3
|
+
BlockType,
|
|
4
|
+
NotificationChannel,
|
|
5
|
+
NotificationsPluginOptions,
|
|
6
|
+
TemplateBlockNode,
|
|
7
|
+
} from "../types"
|
|
8
|
+
|
|
9
|
+
export type ModuleLogger = {
|
|
10
|
+
info?: (...args: any[]) => void
|
|
11
|
+
warn?: (...args: any[]) => void
|
|
12
|
+
error?: (...args: any[]) => void
|
|
13
|
+
debug?: (...args: any[]) => void
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface LoadedExternalTemplate {
|
|
17
|
+
id: string
|
|
18
|
+
name: string
|
|
19
|
+
label: string
|
|
20
|
+
channel: NotificationChannel
|
|
21
|
+
blocks: TemplateBlockNode[]
|
|
22
|
+
translations?: Record<string, Record<string, any>>
|
|
23
|
+
subject?: string | null
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const nodeRequire = createRequire(import.meta.url)
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Loads declarative external templates configured under `options.extend.services[].templates`
|
|
30
|
+
* via either `{ module }` or `{ path }` (`require` semantics supporting directory or extensionless paths).
|
|
31
|
+
* Logs an error naming the template if loading or export validation fails (`codee-sh` v1.1.0 contract).
|
|
32
|
+
*/
|
|
33
|
+
export function loadExternalTemplates(
|
|
34
|
+
options: NotificationsPluginOptions,
|
|
35
|
+
logger?: ModuleLogger
|
|
36
|
+
): Map<string, LoadedExternalTemplate> {
|
|
37
|
+
const registry = new Map<string, LoadedExternalTemplate>()
|
|
38
|
+
const extServices = options.extend?.services || []
|
|
39
|
+
for (const svc of extServices) {
|
|
40
|
+
if (!Array.isArray(svc.templates)) continue
|
|
41
|
+
const channel = (svc.channel ||
|
|
42
|
+
(svc.id === "slack"
|
|
43
|
+
? "slack"
|
|
44
|
+
: svc.id === "whatsapp"
|
|
45
|
+
? "whatsapp"
|
|
46
|
+
: svc.id === "telegram"
|
|
47
|
+
? "telegram"
|
|
48
|
+
: svc.id === "sms"
|
|
49
|
+
? "sms"
|
|
50
|
+
: "email")) as NotificationChannel
|
|
51
|
+
|
|
52
|
+
for (const tplConfig of svc.templates) {
|
|
53
|
+
if (!tplConfig?.name) continue
|
|
54
|
+
const templateId = tplConfig.name.startsWith("external_")
|
|
55
|
+
? tplConfig.name
|
|
56
|
+
: `external_${tplConfig.name}`
|
|
57
|
+
const sourceDesc = tplConfig.module
|
|
58
|
+
? "module option"
|
|
59
|
+
: `"${tplConfig.path}"`
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const rawModule = tplConfig.module
|
|
63
|
+
? tplConfig.module
|
|
64
|
+
: tplConfig.path
|
|
65
|
+
? nodeRequire(tplConfig.path)
|
|
66
|
+
: null
|
|
67
|
+
|
|
68
|
+
const exported = pickExternalTemplateExports(rawModule)
|
|
69
|
+
if (!exported) {
|
|
70
|
+
logger?.error?.(
|
|
71
|
+
`External template "${templateId}" from ${sourceDesc} exports neither templateBlocks nor translations`
|
|
72
|
+
)
|
|
73
|
+
continue
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const rawBlocks = Array.isArray(exported.templateBlocks)
|
|
77
|
+
? exported.templateBlocks
|
|
78
|
+
: []
|
|
79
|
+
const normalizedBlocks: TemplateBlockNode[] = rawBlocks.map(
|
|
80
|
+
(blk: any, idx: number) => ({
|
|
81
|
+
id: String(blk.id || `blk_${idx + 1}`),
|
|
82
|
+
type: (blk.type || "text") as BlockType,
|
|
83
|
+
position: Number(blk.position ?? idx),
|
|
84
|
+
parent_id: blk.parent_id ?? null,
|
|
85
|
+
metadata: blk.metadata || blk.props || {},
|
|
86
|
+
children: Array.isArray(blk.children) ? blk.children : undefined,
|
|
87
|
+
})
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
registry.set(templateId, {
|
|
91
|
+
id: templateId,
|
|
92
|
+
name: templateId,
|
|
93
|
+
label: exported.label || templateId,
|
|
94
|
+
channel,
|
|
95
|
+
blocks: normalizedBlocks,
|
|
96
|
+
translations: exported.translations,
|
|
97
|
+
subject: exported.subject ?? null,
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
logger?.info?.(
|
|
101
|
+
`Custom template "${templateId}" registered for handler "${svc.id}"`
|
|
102
|
+
)
|
|
103
|
+
} catch (err: any) {
|
|
104
|
+
logger?.error?.(
|
|
105
|
+
`Failed to load external template "${templateId}" from ${sourceDesc}: ${err?.message || "Unknown error"}`
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return registry
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function pickExternalTemplateExports(
|
|
115
|
+
rawModule: any
|
|
116
|
+
): Record<string, any> | null {
|
|
117
|
+
const candidates = [rawModule, rawModule?.default]
|
|
118
|
+
return (
|
|
119
|
+
candidates.find(
|
|
120
|
+
(c) =>
|
|
121
|
+
c &&
|
|
122
|
+
typeof c === "object" &&
|
|
123
|
+
(Array.isArray(c.templateBlocks) ||
|
|
124
|
+
(c.translations && typeof c.translations === "object"))
|
|
125
|
+
) || null
|
|
126
|
+
)
|
|
127
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type { B2BNotificationContacts } from "../integrations/documents-and-b2b-resolver"
|
|
2
|
+
import type { NotificationChannel, RecipientRule } from "../types"
|
|
3
|
+
import { normalizeE164Phone } from "../utils/security"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Resolves the recipient address/phone/chat_id for a given `RecipientRule` and `NotificationChannel`.
|
|
7
|
+
*/
|
|
8
|
+
export function resolveRecipientForRule(params: {
|
|
9
|
+
rule: RecipientRule | string
|
|
10
|
+
custom_recipient?: string | null
|
|
11
|
+
channel: NotificationChannel
|
|
12
|
+
data: Record<string, any>
|
|
13
|
+
b2b_contacts?: B2BNotificationContacts
|
|
14
|
+
channel_config?: Record<string, any> | null
|
|
15
|
+
}): string | null {
|
|
16
|
+
const {
|
|
17
|
+
rule,
|
|
18
|
+
custom_recipient,
|
|
19
|
+
channel,
|
|
20
|
+
data,
|
|
21
|
+
b2b_contacts,
|
|
22
|
+
channel_config,
|
|
23
|
+
} = params
|
|
24
|
+
|
|
25
|
+
if (rule === "custom_recipient" && custom_recipient) {
|
|
26
|
+
return custom_recipient.trim()
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (rule === "merchant_channel") {
|
|
30
|
+
const cfgJson = channel_config?.config_json || {}
|
|
31
|
+
return (
|
|
32
|
+
custom_recipient?.trim() ||
|
|
33
|
+
cfgJson.merchant_channel_id ||
|
|
34
|
+
cfgJson.default_recipient ||
|
|
35
|
+
process.env.TELEGRAM_MERCHANT_CHANNEL_ID ||
|
|
36
|
+
process.env.SLACK_WEBHOOK_CHANNEL ||
|
|
37
|
+
null
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (rule === "b2b_accounting_contact") {
|
|
42
|
+
if (channel === "email") {
|
|
43
|
+
return (
|
|
44
|
+
b2b_contacts?.accounting_email ||
|
|
45
|
+
b2b_contacts?.buyer_contact_email ||
|
|
46
|
+
data.email ||
|
|
47
|
+
null
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
if (channel === "sms" || channel === "whatsapp") {
|
|
51
|
+
return normalizeE164Phone(b2b_contacts?.buyer_phone || data.phone)
|
|
52
|
+
}
|
|
53
|
+
if (channel === "telegram") {
|
|
54
|
+
return (
|
|
55
|
+
b2b_contacts?.telegram_chat_id ||
|
|
56
|
+
data.customer?.telegram_chat_id ||
|
|
57
|
+
null
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (rule === "order_email") {
|
|
63
|
+
return data.email || data.customer?.email || null
|
|
64
|
+
}
|
|
65
|
+
if (rule === "customer_email") {
|
|
66
|
+
return data.customer?.email || data.email || null
|
|
67
|
+
}
|
|
68
|
+
if (rule === "order_phone") {
|
|
69
|
+
return normalizeE164Phone(
|
|
70
|
+
data.phone ||
|
|
71
|
+
data.shipping_address?.phone ||
|
|
72
|
+
data.billing_address?.phone ||
|
|
73
|
+
data.customer?.phone
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
if (rule === "customer_phone") {
|
|
77
|
+
return normalizeE164Phone(data.customer?.phone || data.phone)
|
|
78
|
+
}
|
|
79
|
+
if (rule === "customer_telegram") {
|
|
80
|
+
return (
|
|
81
|
+
data.customer?.telegram_chat_id ||
|
|
82
|
+
data.telegram_chat_id ||
|
|
83
|
+
b2b_contacts?.telegram_chat_id ||
|
|
84
|
+
null
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Default `customer_auto` resolution by channel
|
|
89
|
+
switch (channel) {
|
|
90
|
+
case "email":
|
|
91
|
+
return data.email || data.customer?.email || null
|
|
92
|
+
case "sms":
|
|
93
|
+
case "whatsapp":
|
|
94
|
+
return normalizeE164Phone(
|
|
95
|
+
data.phone ||
|
|
96
|
+
data.customer?.phone ||
|
|
97
|
+
data.shipping_address?.phone ||
|
|
98
|
+
data.billing_address?.phone
|
|
99
|
+
)
|
|
100
|
+
case "telegram":
|
|
101
|
+
return (
|
|
102
|
+
data.customer?.telegram_chat_id ||
|
|
103
|
+
data.telegram_chat_id ||
|
|
104
|
+
channel_config?.config_json?.merchant_channel_id ||
|
|
105
|
+
process.env.TELEGRAM_MERCHANT_CHANNEL_ID ||
|
|
106
|
+
null
|
|
107
|
+
)
|
|
108
|
+
case "slack":
|
|
109
|
+
return (
|
|
110
|
+
custom_recipient ||
|
|
111
|
+
channel_config?.config_json?.webhook_url ||
|
|
112
|
+
process.env.SLACK_WEBHOOK_URL ||
|
|
113
|
+
"default-slack-channel"
|
|
114
|
+
)
|
|
115
|
+
case "dummy":
|
|
116
|
+
return (
|
|
117
|
+
normalizeE164Phone(data.phone || data.customer?.phone) ||
|
|
118
|
+
data.email ||
|
|
119
|
+
"sandbox-recipient"
|
|
120
|
+
)
|
|
121
|
+
default:
|
|
122
|
+
return null
|
|
123
|
+
}
|
|
124
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { NotificationsPluginOptions, SenderProfileConfig } from "../types"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves configured SMTP sender profiles (1..5) from environment variables (`@sam-ael/medusa-plugin-mailer` pattern).
|
|
5
|
+
*/
|
|
6
|
+
export function resolveSenderProfiles(
|
|
7
|
+
options: NotificationsPluginOptions
|
|
8
|
+
): SenderProfileConfig[] {
|
|
9
|
+
const profiles: SenderProfileConfig[] = []
|
|
10
|
+
for (let i = 1; i <= 5; i++) {
|
|
11
|
+
const name = process.env[`MAILER_FROM_NAME_${i}`]
|
|
12
|
+
const addr = process.env[`MAILER_FROM_ADDRESS_${i}`]
|
|
13
|
+
if (addr) {
|
|
14
|
+
profiles.push({
|
|
15
|
+
index: i,
|
|
16
|
+
name: name || options.storeName || "Medusa Store",
|
|
17
|
+
from_address: addr,
|
|
18
|
+
reply_to: process.env[`MAILER_REPLY_TO_${i}`] || undefined,
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (profiles.length === 0) {
|
|
23
|
+
profiles.push({
|
|
24
|
+
index: 1,
|
|
25
|
+
name:
|
|
26
|
+
process.env.MAILER_FROM_NAME_1 || options.storeName || "Medusa Store",
|
|
27
|
+
from_address:
|
|
28
|
+
process.env.MAILER_FROM_ADDRESS_1 ||
|
|
29
|
+
process.env.SMTP_FROM ||
|
|
30
|
+
"no-reply@example.com",
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
return profiles
|
|
34
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { normalizeLocale } from "../i18n"
|
|
2
|
+
import type { BlockType } from "../types"
|
|
3
|
+
import type { getSystemPresetById } from "../utils/system-presets"
|
|
4
|
+
|
|
5
|
+
type SystemPreset = NonNullable<ReturnType<typeof getSystemPresetById>>
|
|
6
|
+
|
|
7
|
+
export type CreateBlockRow = (
|
|
8
|
+
data: Record<string, any>
|
|
9
|
+
) => Promise<Record<string, any>>
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Builds the editable `mpn_template` row for a cloned System Preset.
|
|
13
|
+
*/
|
|
14
|
+
export function buildClonedTemplateRow(
|
|
15
|
+
preset: SystemPreset,
|
|
16
|
+
overrides?: { name?: string; label?: string; locale?: string }
|
|
17
|
+
) {
|
|
18
|
+
return {
|
|
19
|
+
name: overrides?.name || `${preset.name}-custom`,
|
|
20
|
+
label: overrides?.label || `${preset.label} (Custom)`,
|
|
21
|
+
description: preset.description,
|
|
22
|
+
channel: preset.channel,
|
|
23
|
+
mode: preset.mode,
|
|
24
|
+
locale: normalizeLocale(overrides?.locale || "en"),
|
|
25
|
+
subject: preset.subject ?? null,
|
|
26
|
+
raw_content: preset.raw_content ?? null,
|
|
27
|
+
variable_map: preset.variable_map ?? null,
|
|
28
|
+
meta_template_name: preset.meta_template_name ?? null,
|
|
29
|
+
meta_language_code: preset.meta_language_code ?? null,
|
|
30
|
+
source_preset_id: preset.id,
|
|
31
|
+
is_active: true,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Persists a preset's block tree, mapping preset block IDs to new DB IDs
|
|
37
|
+
* while preserving `parent_id` relationships (roots first, then children).
|
|
38
|
+
*/
|
|
39
|
+
export async function createPresetBlocks(
|
|
40
|
+
createBlock: CreateBlockRow,
|
|
41
|
+
templateId: string,
|
|
42
|
+
presetBlocks: SystemPreset["blocks"]
|
|
43
|
+
): Promise<Array<Record<string, any>>> {
|
|
44
|
+
const oldToNewId = new Map<string, string>()
|
|
45
|
+
const created: Array<Record<string, any>> = []
|
|
46
|
+
|
|
47
|
+
const sortedBlocks = [...presetBlocks].toSorted(
|
|
48
|
+
(a, b) => a.position - b.position
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
for (const pass of [
|
|
52
|
+
sortedBlocks.filter((b) => !b.parent_id),
|
|
53
|
+
sortedBlocks.filter((b) => Boolean(b.parent_id)),
|
|
54
|
+
]) {
|
|
55
|
+
for (const blk of pass) {
|
|
56
|
+
const mappedParentId = blk.parent_id
|
|
57
|
+
? oldToNewId.get(blk.parent_id) || null
|
|
58
|
+
: null
|
|
59
|
+
const row = await createBlock({
|
|
60
|
+
template_id: templateId,
|
|
61
|
+
type: blk.type,
|
|
62
|
+
position: blk.position,
|
|
63
|
+
parent_id: mappedParentId,
|
|
64
|
+
metadata: blk.metadata,
|
|
65
|
+
})
|
|
66
|
+
oldToNewId.set(blk.id, row.id)
|
|
67
|
+
created.push(row)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return created
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type BlockInput = {
|
|
75
|
+
id?: string
|
|
76
|
+
type?: BlockType
|
|
77
|
+
position?: number
|
|
78
|
+
parent_id?: string | null
|
|
79
|
+
metadata?: Record<string, any>
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Persists client-submitted blocks (roots first, then children), remapping
|
|
84
|
+
* client-side parent IDs to the newly created DB IDs.
|
|
85
|
+
*/
|
|
86
|
+
export async function createBlocksFromInput(
|
|
87
|
+
createBlock: CreateBlockRow,
|
|
88
|
+
templateId: string,
|
|
89
|
+
blocks: BlockInput[]
|
|
90
|
+
): Promise<Array<Record<string, any>>> {
|
|
91
|
+
const clientIdToDbId = new Map<string, string>()
|
|
92
|
+
const created: Array<Record<string, any>> = []
|
|
93
|
+
|
|
94
|
+
for (const pass of [
|
|
95
|
+
blocks.filter((b) => !b.parent_id),
|
|
96
|
+
blocks.filter((b) => Boolean(b.parent_id)),
|
|
97
|
+
]) {
|
|
98
|
+
for (let i = 0; i < pass.length; i++) {
|
|
99
|
+
const blk = pass[i]
|
|
100
|
+
const parentId = blk.parent_id
|
|
101
|
+
? clientIdToDbId.get(blk.parent_id) || blk.parent_id
|
|
102
|
+
: null
|
|
103
|
+
const row = await createBlock({
|
|
104
|
+
template_id: templateId,
|
|
105
|
+
type: blk.type || "text",
|
|
106
|
+
position: blk.position ?? i,
|
|
107
|
+
parent_id: parentId,
|
|
108
|
+
metadata: blk.metadata ?? {},
|
|
109
|
+
})
|
|
110
|
+
if (blk.id) clientIdToDbId.set(blk.id, row.id)
|
|
111
|
+
created.push(row)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return created
|
|
116
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NotificationChannel,
|
|
3
|
+
NotificationsPluginOptions,
|
|
4
|
+
TemplateBlockNode,
|
|
5
|
+
} from "../types"
|
|
6
|
+
import {
|
|
7
|
+
buildBlockTreeFromFlatBlocks,
|
|
8
|
+
SYSTEM_TEMPLATE_PRESETS,
|
|
9
|
+
} from "../utils/system-presets"
|
|
10
|
+
import type { LoadedExternalTemplate } from "./external-templates"
|
|
11
|
+
|
|
12
|
+
export type TemplateListFilters = {
|
|
13
|
+
channel?: NotificationChannel
|
|
14
|
+
source?: "system" | "db" | "external"
|
|
15
|
+
locale?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type SystemPreset = (typeof SYSTEM_TEMPLATE_PRESETS)[number]
|
|
19
|
+
|
|
20
|
+
export type ResolvedTemplate = {
|
|
21
|
+
template: Record<string, any>
|
|
22
|
+
blocks: TemplateBlockNode[]
|
|
23
|
+
flatBlocks: Array<Record<string, any>>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Lists read-only System Presets (`system_*`), optionally filtered by channel.
|
|
28
|
+
*/
|
|
29
|
+
export function listSystemTemplates(
|
|
30
|
+
filters?: TemplateListFilters
|
|
31
|
+
): Array<Record<string, any>> {
|
|
32
|
+
const results: Array<Record<string, any>> = []
|
|
33
|
+
for (const preset of SYSTEM_TEMPLATE_PRESETS) {
|
|
34
|
+
if (filters?.channel && preset.channel !== filters.channel) {
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
results.push({
|
|
38
|
+
id: preset.id,
|
|
39
|
+
name: preset.name,
|
|
40
|
+
label: preset.label,
|
|
41
|
+
description: preset.description,
|
|
42
|
+
channel: preset.channel,
|
|
43
|
+
mode: preset.mode,
|
|
44
|
+
locale: filters?.locale || "en",
|
|
45
|
+
subject: preset.subject ?? null,
|
|
46
|
+
raw_content: preset.raw_content ?? null,
|
|
47
|
+
variable_map: preset.variable_map ?? null,
|
|
48
|
+
meta_template_name: preset.meta_template_name ?? null,
|
|
49
|
+
meta_language_code: preset.meta_language_code ?? null,
|
|
50
|
+
is_active: true,
|
|
51
|
+
source: "system",
|
|
52
|
+
default_event: preset.default_event,
|
|
53
|
+
blocks_count: preset.blocks.length,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
return results
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Lists External Service templates (`external_*`): loaded block templates plus
|
|
61
|
+
* raw templates exposed by `svc.getSystemTemplates()`.
|
|
62
|
+
*/
|
|
63
|
+
export function listExternalTemplates(
|
|
64
|
+
externalTemplates: Map<string, LoadedExternalTemplate>,
|
|
65
|
+
options: NotificationsPluginOptions,
|
|
66
|
+
filters?: TemplateListFilters
|
|
67
|
+
): Array<Record<string, any>> {
|
|
68
|
+
const results: Array<Record<string, any>> = []
|
|
69
|
+
|
|
70
|
+
for (const extTpl of externalTemplates.values()) {
|
|
71
|
+
if (filters?.channel && extTpl.channel !== filters.channel) continue
|
|
72
|
+
results.push({
|
|
73
|
+
id: extTpl.id,
|
|
74
|
+
name: extTpl.name,
|
|
75
|
+
label: extTpl.label,
|
|
76
|
+
channel: extTpl.channel,
|
|
77
|
+
mode: "blocks",
|
|
78
|
+
locale: filters?.locale || "en",
|
|
79
|
+
subject: extTpl.subject ?? null,
|
|
80
|
+
source: "external",
|
|
81
|
+
is_active: true,
|
|
82
|
+
blocks_count: extTpl.blocks.length,
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const extServices = options.extend?.services || []
|
|
87
|
+
for (const svc of extServices) {
|
|
88
|
+
if (filters?.channel && svc.channel && svc.channel !== filters.channel)
|
|
89
|
+
continue
|
|
90
|
+
const templates = svc.getSystemTemplates ? svc.getSystemTemplates() : []
|
|
91
|
+
for (const tpl of templates) {
|
|
92
|
+
results.push({
|
|
93
|
+
...tpl,
|
|
94
|
+
id: tpl.id.startsWith("external_") ? tpl.id : `external_${tpl.id}`,
|
|
95
|
+
source: "external",
|
|
96
|
+
mode: "raw",
|
|
97
|
+
is_active: true,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return results
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function resolveExternalTemplate(
|
|
106
|
+
extTpl: LoadedExternalTemplate
|
|
107
|
+
): ResolvedTemplate {
|
|
108
|
+
return {
|
|
109
|
+
template: {
|
|
110
|
+
id: extTpl.id,
|
|
111
|
+
name: extTpl.name,
|
|
112
|
+
label: extTpl.label,
|
|
113
|
+
channel: extTpl.channel,
|
|
114
|
+
mode: "blocks",
|
|
115
|
+
locale: "en",
|
|
116
|
+
subject: extTpl.subject ?? null,
|
|
117
|
+
translations: extTpl.translations,
|
|
118
|
+
is_active: true,
|
|
119
|
+
source: "external",
|
|
120
|
+
},
|
|
121
|
+
blocks: extTpl.blocks,
|
|
122
|
+
flatBlocks: extTpl.blocks,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function resolveSystemPreset(preset: SystemPreset): ResolvedTemplate {
|
|
127
|
+
return {
|
|
128
|
+
template: {
|
|
129
|
+
id: preset.id,
|
|
130
|
+
name: preset.name,
|
|
131
|
+
label: preset.label,
|
|
132
|
+
description: preset.description,
|
|
133
|
+
channel: preset.channel,
|
|
134
|
+
mode: preset.mode,
|
|
135
|
+
locale: "en",
|
|
136
|
+
subject: preset.subject ?? null,
|
|
137
|
+
raw_content: preset.raw_content ?? null,
|
|
138
|
+
variable_map: preset.variable_map ?? null,
|
|
139
|
+
meta_template_name: preset.meta_template_name ?? null,
|
|
140
|
+
meta_language_code: preset.meta_language_code ?? null,
|
|
141
|
+
is_active: true,
|
|
142
|
+
source: "system",
|
|
143
|
+
},
|
|
144
|
+
blocks: buildBlockTreeFromFlatBlocks(preset.blocks),
|
|
145
|
+
flatBlocks: preset.blocks,
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Merges a template's own translations (falling back to `en`) over the base dictionary.
|
|
151
|
+
*/
|
|
152
|
+
export function mergeTemplateTranslations(
|
|
153
|
+
baseTranslations: Record<string, any>,
|
|
154
|
+
templateTranslations: unknown,
|
|
155
|
+
locale: string
|
|
156
|
+
): Record<string, any> {
|
|
157
|
+
if (!templateTranslations || typeof templateTranslations !== "object") {
|
|
158
|
+
return baseTranslations
|
|
159
|
+
}
|
|
160
|
+
const dict = templateTranslations as Record<string, any>
|
|
161
|
+
const tplLocaleDict = dict[locale] || dict.en || {}
|
|
162
|
+
return {
|
|
163
|
+
...baseTranslations,
|
|
164
|
+
...tplLocaleDict,
|
|
165
|
+
general: {
|
|
166
|
+
...baseTranslations.general,
|
|
167
|
+
...tplLocaleDict.general,
|
|
168
|
+
},
|
|
169
|
+
}
|
|
170
|
+
}
|