@vicacha-devs/medusa-plugin-notifications 0.0.0-stage → 0.0.1
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 +340 -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 +133 -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 +589 -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,619 @@
|
|
|
1
|
+
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
|
|
2
|
+
import {
|
|
3
|
+
createStep,
|
|
4
|
+
createWorkflow,
|
|
5
|
+
StepResponse,
|
|
6
|
+
WorkflowResponse,
|
|
7
|
+
} from "@medusajs/framework/workflows-sdk"
|
|
8
|
+
import {
|
|
9
|
+
resolveB2BNotificationContacts,
|
|
10
|
+
resolveOrderDocumentAttachments,
|
|
11
|
+
} from "../modules/notifications/integrations/documents-and-b2b-resolver"
|
|
12
|
+
import type NotificationsModuleService from "../modules/notifications/service"
|
|
13
|
+
import {
|
|
14
|
+
type ContextType,
|
|
15
|
+
type HydrateEntityContextInput,
|
|
16
|
+
NOTIFICATIONS_MODULE,
|
|
17
|
+
type NotificationAttachment,
|
|
18
|
+
type NotificationChannel,
|
|
19
|
+
type RenderedNotificationPayload,
|
|
20
|
+
} from "../types"
|
|
21
|
+
import {
|
|
22
|
+
getMockContextByType,
|
|
23
|
+
transformInventoryLevelData,
|
|
24
|
+
transformOrderData,
|
|
25
|
+
transformProductData,
|
|
26
|
+
transformShipmentData,
|
|
27
|
+
} from "../modules/notifications/utils/transformers"
|
|
28
|
+
|
|
29
|
+
export type { HydrateEntityContextInput } from "../types"
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Resolves and transforms the entity data for an event or preview request using `query.graph`
|
|
33
|
+
* with safe fallback to `event_data` or mock preview data.
|
|
34
|
+
*/
|
|
35
|
+
export async function hydrateEntityContextData(
|
|
36
|
+
container: { resolve: (key: string) => any },
|
|
37
|
+
input: HydrateEntityContextInput
|
|
38
|
+
): Promise<{
|
|
39
|
+
contextType: ContextType
|
|
40
|
+
entityId: string | null
|
|
41
|
+
rawOrder: Record<string, any> | null
|
|
42
|
+
transformedData: Record<string, any>
|
|
43
|
+
}> {
|
|
44
|
+
let mpnService: NotificationsModuleService | null = null
|
|
45
|
+
try {
|
|
46
|
+
mpnService = container.resolve(NOTIFICATIONS_MODULE)
|
|
47
|
+
} catch {
|
|
48
|
+
mpnService = null
|
|
49
|
+
}
|
|
50
|
+
const pluginOptions = mpnService?.getPluginOptions()
|
|
51
|
+
const locale = input.locale || pluginOptions?.defaultLocale || "en"
|
|
52
|
+
const evName = input.event_name || ""
|
|
53
|
+
const evData = input.event_data || {}
|
|
54
|
+
const entityId =
|
|
55
|
+
input.entity_id ||
|
|
56
|
+
evData.id ||
|
|
57
|
+
evData.order_id ||
|
|
58
|
+
evData.document_id ||
|
|
59
|
+
evData.inventory_item_id ||
|
|
60
|
+
null
|
|
61
|
+
|
|
62
|
+
let contextType: ContextType = input.context_type || "custom"
|
|
63
|
+
if (!input.context_type) {
|
|
64
|
+
if (evName.startsWith("order.")) contextType = "order"
|
|
65
|
+
else if (
|
|
66
|
+
evName === "shipment.created" ||
|
|
67
|
+
evName === "delivery.created" ||
|
|
68
|
+
evName.startsWith("fulfillment.")
|
|
69
|
+
) {
|
|
70
|
+
contextType = "shipment"
|
|
71
|
+
} else if (evName.startsWith("inventory")) contextType = "inventory-level"
|
|
72
|
+
else if (evName.startsWith("product")) contextType = "product"
|
|
73
|
+
else if (evName === "document.generated") contextType = "document"
|
|
74
|
+
else if (evName === "cart.abandoned") contextType = "cart"
|
|
75
|
+
else if (evName === "auth.otp_generated") contextType = "otp"
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let query: any = null
|
|
79
|
+
try {
|
|
80
|
+
query = container.resolve(ContainerRegistrationKeys.QUERY)
|
|
81
|
+
} catch {
|
|
82
|
+
query = null
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let rawOrder: Record<string, any> | null = null
|
|
86
|
+
|
|
87
|
+
if (contextType === "order") {
|
|
88
|
+
const orderId = evData.order_id || entityId
|
|
89
|
+
if (orderId && query && typeof query.graph === "function") {
|
|
90
|
+
try {
|
|
91
|
+
const { data } = await query.graph({
|
|
92
|
+
entity: "order",
|
|
93
|
+
fields: [
|
|
94
|
+
"id",
|
|
95
|
+
"display_id",
|
|
96
|
+
"status",
|
|
97
|
+
"payment_status",
|
|
98
|
+
"fulfillment_status",
|
|
99
|
+
"currency_code",
|
|
100
|
+
"email",
|
|
101
|
+
"total",
|
|
102
|
+
"subtotal",
|
|
103
|
+
"item_subtotal",
|
|
104
|
+
"shipping_total",
|
|
105
|
+
"tax_total",
|
|
106
|
+
"discount_total",
|
|
107
|
+
"region_id",
|
|
108
|
+
"sales_channel_id",
|
|
109
|
+
"created_at",
|
|
110
|
+
"metadata",
|
|
111
|
+
"customer.*",
|
|
112
|
+
"items.*",
|
|
113
|
+
"shipping_address.*",
|
|
114
|
+
"billing_address.*",
|
|
115
|
+
],
|
|
116
|
+
filters: { id: orderId },
|
|
117
|
+
})
|
|
118
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
119
|
+
rawOrder = data[0]
|
|
120
|
+
}
|
|
121
|
+
} catch {
|
|
122
|
+
// Fall back to event_data
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const baseOrder = rawOrder || (evData.items ? evData : null)
|
|
127
|
+
const transformedData = baseOrder
|
|
128
|
+
? transformOrderData(baseOrder, pluginOptions, locale)
|
|
129
|
+
: {
|
|
130
|
+
...getMockContextByType("order", pluginOptions, locale),
|
|
131
|
+
...evData,
|
|
132
|
+
}
|
|
133
|
+
return {
|
|
134
|
+
contextType: "order",
|
|
135
|
+
entityId: String(orderId || transformedData.id || ""),
|
|
136
|
+
rawOrder: baseOrder,
|
|
137
|
+
transformedData,
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (contextType === "shipment") {
|
|
142
|
+
let fulfillment: Record<string, any> | null = evData
|
|
143
|
+
const fulId = evData.fulfillment_id || evData.id || entityId
|
|
144
|
+
if (fulId && query && typeof query.graph === "function") {
|
|
145
|
+
try {
|
|
146
|
+
const { data } = await query.graph({
|
|
147
|
+
entity: "fulfillment",
|
|
148
|
+
fields: ["id", "provider_id", "labels.*", "order.*"],
|
|
149
|
+
filters: { id: fulId },
|
|
150
|
+
})
|
|
151
|
+
if (Array.isArray(data) && data.length > 0) {
|
|
152
|
+
fulfillment = data[0]
|
|
153
|
+
rawOrder = data[0].order || null
|
|
154
|
+
}
|
|
155
|
+
} catch {
|
|
156
|
+
// Fallback
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const transformedData = transformShipmentData(
|
|
160
|
+
fulfillment || {},
|
|
161
|
+
rawOrder || evData.order || null,
|
|
162
|
+
pluginOptions,
|
|
163
|
+
locale
|
|
164
|
+
)
|
|
165
|
+
return {
|
|
166
|
+
contextType: "shipment",
|
|
167
|
+
entityId: String(fulId || transformedData.fulfillment_id || ""),
|
|
168
|
+
rawOrder,
|
|
169
|
+
transformedData,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (contextType === "inventory-level") {
|
|
174
|
+
const transformedData = transformInventoryLevelData(evData, pluginOptions)
|
|
175
|
+
return {
|
|
176
|
+
contextType: "inventory-level",
|
|
177
|
+
entityId: String(entityId || transformedData.id || ""),
|
|
178
|
+
rawOrder: null,
|
|
179
|
+
transformedData,
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (contextType === "product") {
|
|
184
|
+
const transformedData = transformProductData(evData, pluginOptions)
|
|
185
|
+
return {
|
|
186
|
+
contextType: "product",
|
|
187
|
+
entityId: String(entityId || transformedData.id || ""),
|
|
188
|
+
rawOrder: null,
|
|
189
|
+
transformedData,
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const fallbackMock = getMockContextByType(contextType, pluginOptions, locale)
|
|
194
|
+
return {
|
|
195
|
+
contextType,
|
|
196
|
+
entityId: entityId ? String(entityId) : null,
|
|
197
|
+
rawOrder: null,
|
|
198
|
+
transformedData: {
|
|
199
|
+
...fallbackMock,
|
|
200
|
+
...evData,
|
|
201
|
+
},
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Core execution helper for dispatching all active DB mappings matching an event.
|
|
207
|
+
* Can be called directly from subscribers, API routes, or inside `dispatchEventNotificationsWorkflow`.
|
|
208
|
+
*/
|
|
209
|
+
export async function executeEventNotificationsDispatch(
|
|
210
|
+
container: { resolve: (key: string) => any },
|
|
211
|
+
input: {
|
|
212
|
+
event_name: string
|
|
213
|
+
event_data: Record<string, any>
|
|
214
|
+
force_send?: boolean
|
|
215
|
+
}
|
|
216
|
+
): Promise<{
|
|
217
|
+
event_name: string
|
|
218
|
+
matched_mappings: number
|
|
219
|
+
sent_count: number
|
|
220
|
+
skipped_count: number
|
|
221
|
+
failed_count: number
|
|
222
|
+
results: Array<Record<string, any>>
|
|
223
|
+
}> {
|
|
224
|
+
const mpnService = container.resolve(
|
|
225
|
+
NOTIFICATIONS_MODULE
|
|
226
|
+
) as NotificationsModuleService
|
|
227
|
+
|
|
228
|
+
const mappings = await mpnService.listMpnEventMappings({
|
|
229
|
+
event_name: input.event_name,
|
|
230
|
+
is_active: true,
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
if (!Array.isArray(mappings) || mappings.length === 0) {
|
|
234
|
+
return {
|
|
235
|
+
event_name: input.event_name,
|
|
236
|
+
matched_mappings: 0,
|
|
237
|
+
sent_count: 0,
|
|
238
|
+
skipped_count: 0,
|
|
239
|
+
failed_count: 0,
|
|
240
|
+
results: [],
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const sortedMappings = [...mappings].toSorted(
|
|
245
|
+
(a: any, b: any) => Number(b.priority ?? 0) - Number(a.priority ?? 0)
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
const { entityId, rawOrder, transformedData } =
|
|
249
|
+
await hydrateEntityContextData(container, {
|
|
250
|
+
event_name: input.event_name,
|
|
251
|
+
event_data: input.event_data,
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
const b2bContacts = await resolveB2BNotificationContacts(
|
|
255
|
+
container,
|
|
256
|
+
rawOrder || transformedData
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
let notificationModuleService: any = null
|
|
260
|
+
try {
|
|
261
|
+
notificationModuleService = container.resolve(Modules.NOTIFICATION)
|
|
262
|
+
} catch {
|
|
263
|
+
notificationModuleService = null
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
let sentCount = 0
|
|
267
|
+
let skippedCount = 0
|
|
268
|
+
let failedCount = 0
|
|
269
|
+
const results: Array<Record<string, any>> = []
|
|
270
|
+
|
|
271
|
+
const concurrency = Math.max(
|
|
272
|
+
1,
|
|
273
|
+
Number(
|
|
274
|
+
mpnService.getPluginOptions().sendConcurrency ||
|
|
275
|
+
process.env.MAILER_SEND_CONCURRENCY ||
|
|
276
|
+
process.env.WHATSAPP_SEND_CONCURRENCY ||
|
|
277
|
+
2
|
|
278
|
+
)
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
for (let i = 0; i < sortedMappings.length; i += concurrency) {
|
|
282
|
+
const batch = sortedMappings.slice(i, i + concurrency)
|
|
283
|
+
const batchOutcomes = await Promise.all(
|
|
284
|
+
batch.map(async (mapping: any) => {
|
|
285
|
+
const channel = (mapping.channel || "email") as NotificationChannel
|
|
286
|
+
|
|
287
|
+
// 1. Check conditions
|
|
288
|
+
if (
|
|
289
|
+
!mpnService.evaluateEventConditions(
|
|
290
|
+
mapping.conditions,
|
|
291
|
+
transformedData
|
|
292
|
+
)
|
|
293
|
+
) {
|
|
294
|
+
skippedCount++
|
|
295
|
+
return {
|
|
296
|
+
mapping_id: mapping.id,
|
|
297
|
+
channel,
|
|
298
|
+
status: "skipped_conditions",
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// 2. Load channel config
|
|
303
|
+
const channelConfig = await mpnService.getOrCreateChannelConfig(channel)
|
|
304
|
+
if (channelConfig && channelConfig.is_enabled === false) {
|
|
305
|
+
skippedCount++
|
|
306
|
+
return {
|
|
307
|
+
mapping_id: mapping.id,
|
|
308
|
+
channel,
|
|
309
|
+
status: "skipped_channel_disabled",
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Telegram verification gate for merchant channel notifications
|
|
314
|
+
if (
|
|
315
|
+
channel === "telegram" &&
|
|
316
|
+
mapping.recipient_rule === "merchant_channel" &&
|
|
317
|
+
!channelConfig.is_verified &&
|
|
318
|
+
!input.force_send
|
|
319
|
+
) {
|
|
320
|
+
skippedCount++
|
|
321
|
+
return {
|
|
322
|
+
mapping_id: mapping.id,
|
|
323
|
+
channel,
|
|
324
|
+
status: "skipped_telegram_unverified",
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// 3. Resolve recipient
|
|
329
|
+
const recipient = mpnService.resolveRecipientForRule({
|
|
330
|
+
rule: mapping.recipient_rule || "customer_auto",
|
|
331
|
+
custom_recipient: mapping.custom_recipient,
|
|
332
|
+
channel,
|
|
333
|
+
data: transformedData,
|
|
334
|
+
b2b_contacts: b2bContacts,
|
|
335
|
+
channel_config: channelConfig,
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
if (!recipient) {
|
|
339
|
+
skippedCount++
|
|
340
|
+
return {
|
|
341
|
+
mapping_id: mapping.id,
|
|
342
|
+
channel,
|
|
343
|
+
status: "skipped_no_recipient",
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// 4. Consent ledger check (SMS & WhatsApp)
|
|
348
|
+
const consentCheck = await mpnService.verifyRecipientConsent({
|
|
349
|
+
recipient,
|
|
350
|
+
channel,
|
|
351
|
+
requireExplicitOptIn: mapping.conditions?.require_consent,
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
if (!consentCheck.allowed && !input.force_send) {
|
|
355
|
+
const idempKey = `${input.event_name}:${entityId || "global"}:${channel}:${mapping.template_id}:${recipient}`
|
|
356
|
+
await mpnService.recordDeliveryLog({
|
|
357
|
+
idempotency_key: idempKey,
|
|
358
|
+
event_name: input.event_name,
|
|
359
|
+
entity_id: entityId,
|
|
360
|
+
channel,
|
|
361
|
+
template_id: mapping.template_id,
|
|
362
|
+
mapping_id: mapping.id,
|
|
363
|
+
recipient,
|
|
364
|
+
status: "skipped_no_consent",
|
|
365
|
+
error_message: consentCheck.reason,
|
|
366
|
+
})
|
|
367
|
+
skippedCount++
|
|
368
|
+
return {
|
|
369
|
+
mapping_id: mapping.id,
|
|
370
|
+
channel,
|
|
371
|
+
status: "skipped_no_consent",
|
|
372
|
+
reason: consentCheck.reason,
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// 5. Idempotency check
|
|
377
|
+
const idemp = await mpnService.checkIdempotency({
|
|
378
|
+
event_name: input.event_name,
|
|
379
|
+
entity_id: entityId,
|
|
380
|
+
channel,
|
|
381
|
+
template_id: mapping.template_id,
|
|
382
|
+
recipient,
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
if (idemp.duplicate && !input.force_send) {
|
|
386
|
+
skippedCount++
|
|
387
|
+
return {
|
|
388
|
+
mapping_id: mapping.id,
|
|
389
|
+
channel,
|
|
390
|
+
status: "skipped_idempotent",
|
|
391
|
+
idempotency_key: idemp.idempotencyKey,
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// 6. Resolve optional PDF attachments from medusa-documents-plugin
|
|
396
|
+
let attachments: NotificationAttachment[] = []
|
|
397
|
+
if (
|
|
398
|
+
Array.isArray(mapping.attach_document_types) &&
|
|
399
|
+
mapping.attach_document_types.length > 0
|
|
400
|
+
) {
|
|
401
|
+
const orderIdForDocs =
|
|
402
|
+
transformedData.order_id || transformedData.id || entityId
|
|
403
|
+
attachments = await resolveOrderDocumentAttachments(
|
|
404
|
+
container,
|
|
405
|
+
orderIdForDocs,
|
|
406
|
+
mapping.attach_document_types
|
|
407
|
+
)
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
try {
|
|
411
|
+
// 7. Render notification payload
|
|
412
|
+
const rendered = await mpnService.renderTemplateById({
|
|
413
|
+
template_id: mapping.template_id,
|
|
414
|
+
data: {
|
|
415
|
+
...transformedData,
|
|
416
|
+
RecipientEmail: recipient,
|
|
417
|
+
},
|
|
418
|
+
variable_map_override: mapping.variable_map,
|
|
419
|
+
telegram_thread_id:
|
|
420
|
+
mapping.telegram_thread_id ??
|
|
421
|
+
channelConfig?.config_json?.default_thread_id ??
|
|
422
|
+
null,
|
|
423
|
+
attachments,
|
|
424
|
+
append_sms_consent_footer: consentCheck.appendStopFooter,
|
|
425
|
+
})
|
|
426
|
+
|
|
427
|
+
// 8. Dispatch via Medusa Notification Module or Sandbox
|
|
428
|
+
let externalMessageId = `mpn_${channel}_${Date.now()}`
|
|
429
|
+
if (
|
|
430
|
+
notificationModuleService &&
|
|
431
|
+
typeof notificationModuleService.createNotifications === "function"
|
|
432
|
+
) {
|
|
433
|
+
const notifRes =
|
|
434
|
+
await notificationModuleService.createNotifications({
|
|
435
|
+
to: recipient,
|
|
436
|
+
channel,
|
|
437
|
+
template: rendered.template_name,
|
|
438
|
+
content: {
|
|
439
|
+
subject: rendered.subject,
|
|
440
|
+
html: rendered.html,
|
|
441
|
+
text: rendered.text,
|
|
442
|
+
},
|
|
443
|
+
data: {
|
|
444
|
+
...transformedData,
|
|
445
|
+
subject: rendered.subject,
|
|
446
|
+
html: rendered.html,
|
|
447
|
+
text: rendered.text,
|
|
448
|
+
blocks_json: rendered.blocks_json,
|
|
449
|
+
telegram_options: rendered.telegram_options,
|
|
450
|
+
whatsapp_options: rendered.whatsapp_options,
|
|
451
|
+
sms_options: rendered.sms_options,
|
|
452
|
+
sender_profile_index: mapping.sender_profile_index || 1,
|
|
453
|
+
},
|
|
454
|
+
attachments: rendered.attachments,
|
|
455
|
+
})
|
|
456
|
+
if (notifRes?.id) {
|
|
457
|
+
externalMessageId = String(notifRes.id)
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
// Capture in sandbox when core Notification module has no provider bound
|
|
461
|
+
const sbx = await mpnService.captureSandboxMessage({
|
|
462
|
+
channel,
|
|
463
|
+
recipient,
|
|
464
|
+
subject: rendered.subject,
|
|
465
|
+
body: rendered.text,
|
|
466
|
+
html: rendered.html,
|
|
467
|
+
attachments_meta: rendered.attachments?.map((a) => ({
|
|
468
|
+
filename: a.filename,
|
|
469
|
+
content_type: a.content_type,
|
|
470
|
+
})),
|
|
471
|
+
})
|
|
472
|
+
externalMessageId = sbx.id
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
// Mark `first_message_sent = true` on consent record if applicable
|
|
476
|
+
if (consentCheck.consentRecordId) {
|
|
477
|
+
await mpnService.updateMpnConsentRecords({
|
|
478
|
+
id: consentCheck.consentRecordId,
|
|
479
|
+
first_message_sent: true,
|
|
480
|
+
})
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
await mpnService.recordDeliveryLog({
|
|
484
|
+
idempotency_key: idemp.idempotencyKey,
|
|
485
|
+
event_name: input.event_name,
|
|
486
|
+
entity_id: entityId,
|
|
487
|
+
channel,
|
|
488
|
+
driver: channelConfig?.driver || "default",
|
|
489
|
+
template_id: mapping.template_id,
|
|
490
|
+
mapping_id: mapping.id,
|
|
491
|
+
recipient,
|
|
492
|
+
status: "sent",
|
|
493
|
+
external_message_id: externalMessageId,
|
|
494
|
+
payload: rendered,
|
|
495
|
+
})
|
|
496
|
+
|
|
497
|
+
sentCount++
|
|
498
|
+
return {
|
|
499
|
+
mapping_id: mapping.id,
|
|
500
|
+
channel,
|
|
501
|
+
status: "sent",
|
|
502
|
+
external_message_id: externalMessageId,
|
|
503
|
+
}
|
|
504
|
+
} catch (err: any) {
|
|
505
|
+
await mpnService.recordDeliveryLog({
|
|
506
|
+
idempotency_key: idemp.idempotencyKey,
|
|
507
|
+
event_name: input.event_name,
|
|
508
|
+
entity_id: entityId,
|
|
509
|
+
channel,
|
|
510
|
+
driver: channelConfig?.driver || "default",
|
|
511
|
+
template_id: mapping.template_id,
|
|
512
|
+
mapping_id: mapping.id,
|
|
513
|
+
recipient,
|
|
514
|
+
status: "failed",
|
|
515
|
+
error_code: String(err?.code || "SEND_ERROR"),
|
|
516
|
+
error_message: err?.message || "Notification dispatch failed",
|
|
517
|
+
})
|
|
518
|
+
failedCount++
|
|
519
|
+
return {
|
|
520
|
+
mapping_id: mapping.id,
|
|
521
|
+
channel,
|
|
522
|
+
status: "failed",
|
|
523
|
+
error: err?.message || "Notification dispatch failed",
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
})
|
|
527
|
+
)
|
|
528
|
+
results.push(...batchOutcomes)
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return {
|
|
532
|
+
event_name: input.event_name,
|
|
533
|
+
matched_mappings: sortedMappings.length,
|
|
534
|
+
sent_count: sentCount,
|
|
535
|
+
skipped_count: skippedCount,
|
|
536
|
+
failed_count: failedCount,
|
|
537
|
+
results,
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
const renderTemplateStep = createStep(
|
|
542
|
+
"render-template",
|
|
543
|
+
async (
|
|
544
|
+
input: {
|
|
545
|
+
template_id: string
|
|
546
|
+
context_type?: ContextType
|
|
547
|
+
entity_id?: string | null
|
|
548
|
+
data?: Record<string, any>
|
|
549
|
+
locale?: string | null
|
|
550
|
+
subject?: string | null
|
|
551
|
+
},
|
|
552
|
+
{ container }
|
|
553
|
+
) => {
|
|
554
|
+
const mpnService = container.resolve(
|
|
555
|
+
NOTIFICATIONS_MODULE
|
|
556
|
+
) as NotificationsModuleService
|
|
557
|
+
const hydrated = input.data
|
|
558
|
+
? { transformedData: input.data }
|
|
559
|
+
: await hydrateEntityContextData(container, {
|
|
560
|
+
context_type: input.context_type || "order",
|
|
561
|
+
entity_id: input.entity_id,
|
|
562
|
+
locale: input.locale,
|
|
563
|
+
})
|
|
564
|
+
|
|
565
|
+
const rendered = await mpnService.renderTemplateById({
|
|
566
|
+
template_id: input.template_id,
|
|
567
|
+
context_type: input.context_type,
|
|
568
|
+
data: hydrated.transformedData,
|
|
569
|
+
locale: input.locale,
|
|
570
|
+
subject: input.subject,
|
|
571
|
+
})
|
|
572
|
+
|
|
573
|
+
return new StepResponse(rendered)
|
|
574
|
+
}
|
|
575
|
+
)
|
|
576
|
+
|
|
577
|
+
export const renderNotificationTemplateWorkflow = createWorkflow(
|
|
578
|
+
"render-notification-template",
|
|
579
|
+
(input: {
|
|
580
|
+
template_id: string
|
|
581
|
+
context_type?: ContextType
|
|
582
|
+
entity_id?: string | null
|
|
583
|
+
data?: Record<string, any>
|
|
584
|
+
locale?: string | null
|
|
585
|
+
subject?: string | null
|
|
586
|
+
}) => {
|
|
587
|
+
const rendered = renderTemplateStep(input)
|
|
588
|
+
return new WorkflowResponse(rendered)
|
|
589
|
+
}
|
|
590
|
+
)
|
|
591
|
+
|
|
592
|
+
const dispatchEventStep = createStep(
|
|
593
|
+
"dispatch-event",
|
|
594
|
+
async (
|
|
595
|
+
input: {
|
|
596
|
+
event_name: string
|
|
597
|
+
event_data: Record<string, any>
|
|
598
|
+
force_send?: boolean
|
|
599
|
+
},
|
|
600
|
+
{ container }
|
|
601
|
+
) => {
|
|
602
|
+
const outcome = await executeEventNotificationsDispatch(container, input)
|
|
603
|
+
return new StepResponse(outcome)
|
|
604
|
+
}
|
|
605
|
+
)
|
|
606
|
+
|
|
607
|
+
export const dispatchEventNotificationsWorkflow = createWorkflow(
|
|
608
|
+
"dispatch-event-notifications",
|
|
609
|
+
(input: {
|
|
610
|
+
event_name: string
|
|
611
|
+
event_data: Record<string, any>
|
|
612
|
+
force_send?: boolean
|
|
613
|
+
}) => {
|
|
614
|
+
const outcome = dispatchEventStep(input)
|
|
615
|
+
return new WorkflowResponse(outcome)
|
|
616
|
+
}
|
|
617
|
+
)
|
|
618
|
+
|
|
619
|
+
export type { RenderedNotificationPayload }
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2023",
|
|
4
|
+
"esModuleInterop": true,
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "Bundler",
|
|
7
|
+
"emitDecoratorMetadata": true,
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"skipDefaultLibCheck": true,
|
|
11
|
+
"declaration": false,
|
|
12
|
+
"sourceMap": false,
|
|
13
|
+
"inlineSourceMap": true,
|
|
14
|
+
"outDir": "./.medusa/server",
|
|
15
|
+
"rootDir": "./",
|
|
16
|
+
"jsx": "react-jsx",
|
|
17
|
+
"forceConsistentCasingInFileNames": true,
|
|
18
|
+
"resolveJsonModule": true,
|
|
19
|
+
"checkJs": false,
|
|
20
|
+
"strictNullChecks": true,
|
|
21
|
+
"baseUrl": ".",
|
|
22
|
+
"ignoreDeprecations": "5.0",
|
|
23
|
+
"paths": {
|
|
24
|
+
"@notifications/*": ["./src/*"]
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"ts-node": {
|
|
28
|
+
"swc": true
|
|
29
|
+
},
|
|
30
|
+
"include": ["**/*", ".medusa/types/*"],
|
|
31
|
+
"exclude": [
|
|
32
|
+
"node_modules",
|
|
33
|
+
".medusa/server",
|
|
34
|
+
".medusa/admin",
|
|
35
|
+
"src/admin",
|
|
36
|
+
"src/__tests__",
|
|
37
|
+
".cache",
|
|
38
|
+
"eslint.config.*"
|
|
39
|
+
]
|
|
40
|
+
}
|