@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,456 @@
|
|
|
1
|
+
import type { ContextType, NotificationsPluginOptions } from "../types"
|
|
2
|
+
|
|
3
|
+
export function formatCurrencyAmount(
|
|
4
|
+
amount: number | string | null | undefined,
|
|
5
|
+
currencyCode = "USD",
|
|
6
|
+
locale = "en-US"
|
|
7
|
+
): string {
|
|
8
|
+
const num = Number(amount ?? 0)
|
|
9
|
+
if (!Number.isFinite(num)) {
|
|
10
|
+
return `0.00 ${(currencyCode || "USD").toUpperCase()}`
|
|
11
|
+
}
|
|
12
|
+
const code = (currencyCode || "USD").toUpperCase()
|
|
13
|
+
try {
|
|
14
|
+
return new Intl.NumberFormat(locale, {
|
|
15
|
+
style: "currency",
|
|
16
|
+
currency: code,
|
|
17
|
+
minimumFractionDigits: 2,
|
|
18
|
+
maximumFractionDigits: 2,
|
|
19
|
+
}).format(num)
|
|
20
|
+
} catch {
|
|
21
|
+
return `${num.toFixed(2)} ${code}`
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function formatDateTime(
|
|
26
|
+
dateInput: string | Date | null | undefined,
|
|
27
|
+
locale = "en-US"
|
|
28
|
+
): string {
|
|
29
|
+
if (!dateInput) return ""
|
|
30
|
+
const date = dateInput instanceof Date ? dateInput : new Date(dateInput)
|
|
31
|
+
if (Number.isNaN(date.getTime())) return String(dateInput)
|
|
32
|
+
try {
|
|
33
|
+
return new Intl.DateTimeFormat(locale, {
|
|
34
|
+
year: "numeric",
|
|
35
|
+
month: "short",
|
|
36
|
+
day: "2-digit",
|
|
37
|
+
}).format(date)
|
|
38
|
+
} catch {
|
|
39
|
+
return date.toISOString().slice(0, 10)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function formatAddressInline(addr?: Record<string, any> | null): string {
|
|
44
|
+
if (!addr || typeof addr !== "object") return ""
|
|
45
|
+
const name = [addr.first_name, addr.last_name]
|
|
46
|
+
.filter(Boolean)
|
|
47
|
+
.join(" ")
|
|
48
|
+
.trim()
|
|
49
|
+
const parts = [
|
|
50
|
+
name,
|
|
51
|
+
addr.company,
|
|
52
|
+
addr.address_1,
|
|
53
|
+
addr.address_2,
|
|
54
|
+
[addr.postal_code, addr.city].filter(Boolean).join(" "),
|
|
55
|
+
addr.province,
|
|
56
|
+
addr.country_code ? String(addr.country_code).toUpperCase() : "",
|
|
57
|
+
]
|
|
58
|
+
.map((p) => (p ? String(p).trim() : ""))
|
|
59
|
+
.filter(Boolean)
|
|
60
|
+
return parts.join(", ")
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function buildGlobalStoreContext(
|
|
64
|
+
pluginOptions?: NotificationsPluginOptions
|
|
65
|
+
): Record<string, any> {
|
|
66
|
+
const storeName =
|
|
67
|
+
pluginOptions?.storeName || process.env.STORE_NAME || "Medusa Store"
|
|
68
|
+
const storeUrl =
|
|
69
|
+
pluginOptions?.storeUrl ||
|
|
70
|
+
process.env.STORE_URL ||
|
|
71
|
+
process.env.STOREFRONT_URL ||
|
|
72
|
+
"https://store.example.com"
|
|
73
|
+
const logoUrl = pluginOptions?.logoUrl || process.env.MAILER_LOGO_URL || ""
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
store_name: storeName,
|
|
77
|
+
store_url: storeUrl,
|
|
78
|
+
logo_url: logoUrl,
|
|
79
|
+
// @sam-ael Go-style compatibility aliases
|
|
80
|
+
StoreName: storeName,
|
|
81
|
+
StoreURL: storeUrl,
|
|
82
|
+
SiteURL: storeUrl,
|
|
83
|
+
LogoURL: logoUrl,
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function transformOrderData(
|
|
88
|
+
order: Record<string, any>,
|
|
89
|
+
pluginOptions?: NotificationsPluginOptions,
|
|
90
|
+
locale = "en"
|
|
91
|
+
): Record<string, any> {
|
|
92
|
+
const currency = (order?.currency_code || "usd").toUpperCase()
|
|
93
|
+
const globalCtx = buildGlobalStoreContext(pluginOptions)
|
|
94
|
+
const items = Array.isArray(order?.items) ? order.items : []
|
|
95
|
+
|
|
96
|
+
const transformedItems = items.map(
|
|
97
|
+
(item: Record<string, any>, idx: number) => {
|
|
98
|
+
const qty = Number(item.quantity ?? 1)
|
|
99
|
+
const unitPrice = Number(item.unit_price ?? 0)
|
|
100
|
+
const lineTotal = Number(item.total ?? unitPrice * qty)
|
|
101
|
+
return {
|
|
102
|
+
index: idx + 1,
|
|
103
|
+
id: item.id || `item_${idx + 1}`,
|
|
104
|
+
title: item.title || item.product_title || "Item",
|
|
105
|
+
subtitle: item.subtitle || item.variant_title || "",
|
|
106
|
+
variant_title: item.variant_title || item.variant?.title || "",
|
|
107
|
+
sku: item.variant_sku || item.variant?.sku || item.sku || "N/A",
|
|
108
|
+
thumbnail: item.thumbnail || item.product?.thumbnail || "",
|
|
109
|
+
quantity: qty,
|
|
110
|
+
unit_price: unitPrice,
|
|
111
|
+
unit_price_formatted: formatCurrencyAmount(unitPrice, currency, locale),
|
|
112
|
+
total: lineTotal,
|
|
113
|
+
total_formatted: formatCurrencyAmount(lineTotal, currency, locale),
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
const customerFirstName =
|
|
119
|
+
order?.customer?.first_name ||
|
|
120
|
+
order?.shipping_address?.first_name ||
|
|
121
|
+
order?.billing_address?.first_name ||
|
|
122
|
+
"Customer"
|
|
123
|
+
const customerLastName =
|
|
124
|
+
order?.customer?.last_name ||
|
|
125
|
+
order?.shipping_address?.last_name ||
|
|
126
|
+
order?.billing_address?.last_name ||
|
|
127
|
+
""
|
|
128
|
+
const customerFullName = [customerFirstName, customerLastName]
|
|
129
|
+
.filter(Boolean)
|
|
130
|
+
.join(" ")
|
|
131
|
+
.trim()
|
|
132
|
+
|
|
133
|
+
const subtotal = Number(order?.item_subtotal ?? order?.subtotal ?? 0)
|
|
134
|
+
const shippingTotal = Number(order?.shipping_total ?? 0)
|
|
135
|
+
const taxTotal = Number(order?.tax_total ?? 0)
|
|
136
|
+
const discountTotal = Number(order?.discount_total ?? 0)
|
|
137
|
+
const total = Number(
|
|
138
|
+
order?.total ?? subtotal + shippingTotal + taxTotal - discountTotal
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
const displayId = order?.display_id ?? order?.id ?? "1001"
|
|
142
|
+
const orderUrl = `${globalCtx.store_url.replace(/\/$/, "")}/orders/${order?.id || displayId}`
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
...globalCtx,
|
|
146
|
+
id: order?.id || "order_01",
|
|
147
|
+
display_id: String(displayId),
|
|
148
|
+
order_number: String(displayId),
|
|
149
|
+
created_at: order?.created_at || new Date().toISOString(),
|
|
150
|
+
created_at_formatted: formatDateTime(
|
|
151
|
+
order?.created_at || new Date(),
|
|
152
|
+
locale
|
|
153
|
+
),
|
|
154
|
+
status: order?.status || "pending",
|
|
155
|
+
payment_status: order?.payment_status || "captured",
|
|
156
|
+
fulfillment_status: order?.fulfillment_status || "not_fulfilled",
|
|
157
|
+
currency_code: currency,
|
|
158
|
+
email: order?.email || order?.customer?.email || "",
|
|
159
|
+
phone:
|
|
160
|
+
order?.shipping_address?.phone ||
|
|
161
|
+
order?.billing_address?.phone ||
|
|
162
|
+
order?.customer?.phone ||
|
|
163
|
+
"",
|
|
164
|
+
first_name: customerFirstName,
|
|
165
|
+
last_name: customerLastName,
|
|
166
|
+
customer_name: customerFullName,
|
|
167
|
+
customer: {
|
|
168
|
+
id: order?.customer?.id || order?.customer_id || "cus_01",
|
|
169
|
+
first_name: customerFirstName,
|
|
170
|
+
last_name: customerLastName,
|
|
171
|
+
full_name: customerFullName,
|
|
172
|
+
email: order?.email || order?.customer?.email || "",
|
|
173
|
+
phone: order?.customer?.phone || order?.shipping_address?.phone || "",
|
|
174
|
+
telegram_chat_id:
|
|
175
|
+
order?.customer?.metadata?.telegram_chat_id ||
|
|
176
|
+
order?.metadata?.telegram_chat_id ||
|
|
177
|
+
"",
|
|
178
|
+
},
|
|
179
|
+
shipping_address: order?.shipping_address || {},
|
|
180
|
+
shipping_address_formatted: formatAddressInline(order?.shipping_address),
|
|
181
|
+
billing_address: order?.billing_address || {},
|
|
182
|
+
billing_address_formatted: formatAddressInline(order?.billing_address),
|
|
183
|
+
items: transformedItems,
|
|
184
|
+
items_count: transformedItems.reduce(
|
|
185
|
+
(acc: number, item: any) => acc + item.quantity,
|
|
186
|
+
0
|
|
187
|
+
),
|
|
188
|
+
subtotal,
|
|
189
|
+
subtotal_formatted: formatCurrencyAmount(subtotal, currency, locale),
|
|
190
|
+
shipping_total: shippingTotal,
|
|
191
|
+
shipping_total_formatted: formatCurrencyAmount(
|
|
192
|
+
shippingTotal,
|
|
193
|
+
currency,
|
|
194
|
+
locale
|
|
195
|
+
),
|
|
196
|
+
tax_total: taxTotal,
|
|
197
|
+
tax_total_formatted: formatCurrencyAmount(taxTotal, currency, locale),
|
|
198
|
+
discount_total: discountTotal,
|
|
199
|
+
discount_total_formatted: formatCurrencyAmount(
|
|
200
|
+
discountTotal,
|
|
201
|
+
currency,
|
|
202
|
+
locale
|
|
203
|
+
),
|
|
204
|
+
total,
|
|
205
|
+
total_formatted: formatCurrencyAmount(total, currency, locale),
|
|
206
|
+
order_url: orderUrl,
|
|
207
|
+
region_id: order?.region_id || order?.region?.id || null,
|
|
208
|
+
sales_channel_id: order?.sales_channel_id || null,
|
|
209
|
+
metadata: order?.metadata || {},
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function transformShipmentData(
|
|
214
|
+
fulfillment: Record<string, any>,
|
|
215
|
+
order?: Record<string, any> | null,
|
|
216
|
+
pluginOptions?: NotificationsPluginOptions,
|
|
217
|
+
locale = "en"
|
|
218
|
+
): Record<string, any> {
|
|
219
|
+
const orderCtx = order
|
|
220
|
+
? transformOrderData(order, pluginOptions, locale)
|
|
221
|
+
: buildGlobalStoreContext(pluginOptions)
|
|
222
|
+
|
|
223
|
+
const labels = Array.isArray(fulfillment?.labels) ? fulfillment.labels : []
|
|
224
|
+
const firstLabel = labels[0] || {}
|
|
225
|
+
const trackingNumber =
|
|
226
|
+
firstLabel.tracking_number ||
|
|
227
|
+
fulfillment?.tracking_numbers?.[0] ||
|
|
228
|
+
fulfillment?.data?.tracking_number ||
|
|
229
|
+
"TRK-99887766"
|
|
230
|
+
const trackingUrl =
|
|
231
|
+
firstLabel.tracking_url ||
|
|
232
|
+
fulfillment?.tracking_links?.[0]?.url ||
|
|
233
|
+
fulfillment?.data?.tracking_url ||
|
|
234
|
+
`${orderCtx.store_url}/track/${trackingNumber}`
|
|
235
|
+
const carrier =
|
|
236
|
+
fulfillment?.provider_id ||
|
|
237
|
+
fulfillment?.shipping_option?.name ||
|
|
238
|
+
"Standard Carrier"
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
...orderCtx,
|
|
242
|
+
fulfillment_id: fulfillment?.id || "ful_01",
|
|
243
|
+
order_display_id: orderCtx.display_id || "1001",
|
|
244
|
+
carrier,
|
|
245
|
+
provider_id: carrier,
|
|
246
|
+
tracking_number: trackingNumber,
|
|
247
|
+
tracking_url: trackingUrl,
|
|
248
|
+
labels,
|
|
249
|
+
shipped_at: formatDateTime(
|
|
250
|
+
fulfillment?.shipped_at || fulfillment?.created_at || new Date(),
|
|
251
|
+
locale
|
|
252
|
+
),
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function transformInventoryLevelData(
|
|
257
|
+
inventory: Record<string, any>,
|
|
258
|
+
pluginOptions?: NotificationsPluginOptions
|
|
259
|
+
): Record<string, any> {
|
|
260
|
+
const globalCtx = buildGlobalStoreContext(pluginOptions)
|
|
261
|
+
const stocked = Number(inventory?.stocked_quantity ?? 0)
|
|
262
|
+
const reserved = Number(inventory?.reserved_quantity ?? 0)
|
|
263
|
+
const available = Number(
|
|
264
|
+
inventory?.available_quantity ?? Math.max(0, stocked - reserved)
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
...globalCtx,
|
|
269
|
+
id: inventory?.id || "ilev_01",
|
|
270
|
+
inventory_item_id: inventory?.inventory_item_id || "iitem_01",
|
|
271
|
+
sku: inventory?.sku || inventory?.inventory_item?.sku || "SKU-DEFAULT-01",
|
|
272
|
+
title:
|
|
273
|
+
inventory?.title || inventory?.inventory_item?.title || "Inventory Item",
|
|
274
|
+
location_id: inventory?.location_id || "sloc_01",
|
|
275
|
+
location_name:
|
|
276
|
+
inventory?.location_name ||
|
|
277
|
+
inventory?.stock_location?.name ||
|
|
278
|
+
"Main Warehouse",
|
|
279
|
+
stocked_quantity: stocked,
|
|
280
|
+
reserved_quantity: reserved,
|
|
281
|
+
available_quantity: available,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function transformProductData(
|
|
286
|
+
product: Record<string, any>,
|
|
287
|
+
pluginOptions?: NotificationsPluginOptions
|
|
288
|
+
): Record<string, any> {
|
|
289
|
+
const globalCtx = buildGlobalStoreContext(pluginOptions)
|
|
290
|
+
return {
|
|
291
|
+
...globalCtx,
|
|
292
|
+
id: product?.id || "prod_01",
|
|
293
|
+
title: product?.title || "Sample Product",
|
|
294
|
+
handle: product?.handle || "sample-product",
|
|
295
|
+
status: product?.status || "published",
|
|
296
|
+
description: product?.description || "",
|
|
297
|
+
thumbnail: product?.thumbnail || "",
|
|
298
|
+
product_url: `${globalCtx.store_url.replace(/\/$/, "")}/products/${product?.handle || product?.id || "sample-product"}`,
|
|
299
|
+
variants: Array.isArray(product?.variants) ? product.variants : [],
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function getMockContextByType(
|
|
304
|
+
contextType: ContextType,
|
|
305
|
+
pluginOptions?: NotificationsPluginOptions,
|
|
306
|
+
locale = "en"
|
|
307
|
+
): Record<string, any> {
|
|
308
|
+
const globalCtx = buildGlobalStoreContext(pluginOptions)
|
|
309
|
+
|
|
310
|
+
switch (contextType) {
|
|
311
|
+
case "order":
|
|
312
|
+
return transformOrderData(
|
|
313
|
+
{
|
|
314
|
+
id: "order_01J9MOCK8899",
|
|
315
|
+
display_id: 1042,
|
|
316
|
+
created_at: "2026-10-10T10:30:00.000Z",
|
|
317
|
+
status: "pending",
|
|
318
|
+
payment_status: "captured",
|
|
319
|
+
fulfillment_status: "not_fulfilled",
|
|
320
|
+
currency_code: "EUR",
|
|
321
|
+
email: "alex.rivera@example.com",
|
|
322
|
+
customer: {
|
|
323
|
+
id: "cus_01J9MOCK",
|
|
324
|
+
first_name: "Alex",
|
|
325
|
+
last_name: "Rivera",
|
|
326
|
+
email: "alex.rivera@example.com",
|
|
327
|
+
phone: "+14155552671",
|
|
328
|
+
metadata: { telegram_chat_id: "987654321" },
|
|
329
|
+
},
|
|
330
|
+
shipping_address: {
|
|
331
|
+
first_name: "Alex",
|
|
332
|
+
last_name: "Rivera",
|
|
333
|
+
company: "Rivera Studio GmbH",
|
|
334
|
+
address_1: "Friedrichstraße 123",
|
|
335
|
+
city: "Berlin",
|
|
336
|
+
postal_code: "10117",
|
|
337
|
+
country_code: "de",
|
|
338
|
+
phone: "+14155552671",
|
|
339
|
+
},
|
|
340
|
+
billing_address: {
|
|
341
|
+
first_name: "Alex",
|
|
342
|
+
last_name: "Rivera",
|
|
343
|
+
company: "Rivera Studio GmbH",
|
|
344
|
+
address_1: "Friedrichstraße 123",
|
|
345
|
+
city: "Berlin",
|
|
346
|
+
postal_code: "10117",
|
|
347
|
+
country_code: "de",
|
|
348
|
+
},
|
|
349
|
+
items: [
|
|
350
|
+
{
|
|
351
|
+
id: "item_1",
|
|
352
|
+
title: "Mechanical Wireless Keyboard Pro",
|
|
353
|
+
variant_title: "Tactile / Space Gray",
|
|
354
|
+
variant_sku: "KB-PRO-GRY",
|
|
355
|
+
quantity: 1,
|
|
356
|
+
unit_price: 189,
|
|
357
|
+
total: 189,
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
id: "item_2",
|
|
361
|
+
title: "Coiled Aviator USB-C Cable",
|
|
362
|
+
variant_title: "Obsidian Black",
|
|
363
|
+
variant_sku: "CBL-USB-BLK",
|
|
364
|
+
quantity: 2,
|
|
365
|
+
unit_price: 29,
|
|
366
|
+
total: 58,
|
|
367
|
+
},
|
|
368
|
+
],
|
|
369
|
+
subtotal: 247,
|
|
370
|
+
shipping_total: 12,
|
|
371
|
+
tax_total: 49.21,
|
|
372
|
+
discount_total: 0,
|
|
373
|
+
total: 308.21,
|
|
374
|
+
},
|
|
375
|
+
pluginOptions,
|
|
376
|
+
locale
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
case "shipment":
|
|
380
|
+
return transformShipmentData(
|
|
381
|
+
{
|
|
382
|
+
id: "ful_01J9MOCK",
|
|
383
|
+
provider_id: "DHL Express",
|
|
384
|
+
labels: [
|
|
385
|
+
{
|
|
386
|
+
tracking_number: "JJD009988776655",
|
|
387
|
+
tracking_url:
|
|
388
|
+
"https://www.dhl.com/en/express/tracking.html?AWB=JJD009988776655",
|
|
389
|
+
},
|
|
390
|
+
],
|
|
391
|
+
},
|
|
392
|
+
getMockContextByType("order", pluginOptions, locale),
|
|
393
|
+
pluginOptions,
|
|
394
|
+
locale
|
|
395
|
+
)
|
|
396
|
+
|
|
397
|
+
case "inventory-level":
|
|
398
|
+
return transformInventoryLevelData(
|
|
399
|
+
{
|
|
400
|
+
id: "ilev_01J9MOCK",
|
|
401
|
+
sku: "KB-PRO-GRY",
|
|
402
|
+
title: "Mechanical Wireless Keyboard Pro",
|
|
403
|
+
location_name: "Berlin Central Fulfillment Hub",
|
|
404
|
+
stocked_quantity: 4,
|
|
405
|
+
reserved_quantity: 2,
|
|
406
|
+
available_quantity: 2,
|
|
407
|
+
},
|
|
408
|
+
pluginOptions
|
|
409
|
+
)
|
|
410
|
+
|
|
411
|
+
case "product":
|
|
412
|
+
return transformProductData(
|
|
413
|
+
{
|
|
414
|
+
id: "prod_01J9MOCK",
|
|
415
|
+
title: "Mechanical Wireless Keyboard Pro",
|
|
416
|
+
handle: "mechanical-wireless-keyboard-pro",
|
|
417
|
+
status: "published",
|
|
418
|
+
description: "Hot-swappable aluminum wireless mechanical keyboard.",
|
|
419
|
+
},
|
|
420
|
+
pluginOptions
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
case "cart":
|
|
424
|
+
return {
|
|
425
|
+
...globalCtx,
|
|
426
|
+
id: "cart_01J9MOCK",
|
|
427
|
+
first_name: "Alex",
|
|
428
|
+
email: "alex.rivera@example.com",
|
|
429
|
+
phone: "+14155552671",
|
|
430
|
+
total_formatted: formatCurrencyAmount(247, "EUR", locale),
|
|
431
|
+
recovery_url: `${globalCtx.store_url.replace(/\/$/, "")}/cart/recover?cart_id=cart_01J9MOCK`,
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
case "document":
|
|
435
|
+
return {
|
|
436
|
+
...globalCtx,
|
|
437
|
+
document_id: "doc_01J9MOCK",
|
|
438
|
+
document_number: "INV-2026-00042",
|
|
439
|
+
document_type: "invoice",
|
|
440
|
+
document_type_label: "Invoice",
|
|
441
|
+
order_id: "order_01J9MOCK8899",
|
|
442
|
+
order_display_id: "1042",
|
|
443
|
+
first_name: "Alex",
|
|
444
|
+
total_formatted: formatCurrencyAmount(308.21, "EUR", locale),
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
case "otp":
|
|
448
|
+
default:
|
|
449
|
+
return {
|
|
450
|
+
...globalCtx,
|
|
451
|
+
first_name: "Alex",
|
|
452
|
+
otp_code: "482910",
|
|
453
|
+
expires_in_minutes: 10,
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# Notification Providers
|
|
2
|
+
|
|
3
|
+
Six Medusa v2 `Modules.NOTIFICATION` transport providers (`AbstractNotificationProviderService`) located under `src/providers/`.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
providers/
|
|
7
|
+
├── index.ts Barrel re-exporting all 6 provider services, IDs, and option types
|
|
8
|
+
├── email-smtp/
|
|
9
|
+
│ └── index.ts SmtpEmailNotificationProviderService (id: "mpn-email")
|
|
10
|
+
├── whatsapp/
|
|
11
|
+
│ └── index.ts WhatsAppNotificationProviderService (id: "mpn-whatsapp")
|
|
12
|
+
├── telegram/
|
|
13
|
+
│ └── index.ts TelegramNotificationProviderService (id: "mpn-telegram")
|
|
14
|
+
├── sms/
|
|
15
|
+
│ └── index.ts SmsNotificationProviderService (id: "mpn-sms")
|
|
16
|
+
├── slack/
|
|
17
|
+
│ └── index.ts SlackNotificationProviderService (id: "mpn-slack")
|
|
18
|
+
└── dummy-sandbox/
|
|
19
|
+
└── index.ts DummySandboxNotificationProviderService (id: "mpn-dummy-sandbox")
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## Provider Overview
|
|
25
|
+
|
|
26
|
+
| Provider ID | Resolve Path | Channels | Supported Drivers | Key Features |
|
|
27
|
+
| ------------------- | ------------------------------------------------------------------- | -------------- | ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
28
|
+
| `mpn-email` | `@vicacha-devs/medusa-plugin-notifications/providers/email-smtp` | `["email"]` | `nodemailer` SMTP, HTTP Relay | 1..5 sender profiles (`MAILER_FROM_NAME_1..5` / `MAILER_FROM_ADDRESS_1..5`), HTML + plain-text fallback, base64 PDF attachments (`medusa-plugin-documents`). |
|
|
29
|
+
| `mpn-whatsapp` | `@vicacha-devs/medusa-plugin-notifications/providers/whatsapp` | `["whatsapp"]` | `meta-cloud`, `openwa`, `waha`, `evolution` | Official Meta Cloud API (approved WABA templates, interactive buttons, documents, session text with `toWhatsAppDigitsOnly`) AND self-hosted **`rmyndharis/OpenWA`** (`:2785`), WAHA, and Evolution API v2 gateways. |
|
|
30
|
+
| `mpn-telegram` | `@vicacha-devs/medusa-plugin-notifications/providers/telegram` | `["telegram"]` | Telegram Bot API | Merchant Supergroup / Forum Topic Alerts (`-100...` or `@channel` with `message_thread_id`) + Customer DMs (`chat_id`, `InlineKeyboardMarkup`, multipart `sendDocument` PDF uploads), 30 msg/s global & 1 msg/s per-chat rate limiting. |
|
|
31
|
+
| `mpn-sms` | `@vicacha-devs/medusa-plugin-notifications/providers/sms` | `["sms"]` | `twilio`, `signalhouse`, `msg91`, `smsapi`, `webhook` | Twilio SMS/MMS + Messaging Service SID + `20xxx` permanent vs. retryable error classification, Signal House 10DLC, MSG91 Flow, SMSAPI, Generic Webhook, and GSM-7 / UCS-2 segment calculation. |
|
|
32
|
+
| `mpn-slack` | `@vicacha-devs/medusa-plugin-notifications/providers/slack` | `["slack"]` | Slack Incoming Webhook | Slack Block Kit JSON (`blocks` + `text`) with SSRF URL validation (`validateSafeExternalUrl`). |
|
|
33
|
+
| `mpn-dummy-sandbox` | `@vicacha-devs/medusa-plugin-notifications/providers/dummy-sandbox` | `["dummy"]` | In-Memory + DB (`mpn_sandbox_message`) | Captures notifications across any channel and extracts 4–8 digit OTP codes for local/staging inspection in `/app/mpn/sandbox` at zero carrier cost. |
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## 1. `mpn-email` (`email-smtp`)
|
|
38
|
+
|
|
39
|
+
- **Class**: `SmtpEmailNotificationProviderService`
|
|
40
|
+
- **Options (`SmtpEmailProviderOptions`) / Env Vars**:
|
|
41
|
+
- `host` (`MAILER_SMTP_HOST`, e.g. `smtp.resend.com`), `port` (`MAILER_SMTP_PORT`, default `587` or `465`), `secure` (`MAILER_SMTP_SECURE`), `user` (`MAILER_SMTP_USER`), `pass` (`MAILER_SMTP_PASS`)
|
|
42
|
+
- `from_name` (`MAILER_FROM_NAME_1..5`), `from_address` (`MAILER_FROM_ADDRESS_1..5` / `SMTP_FROM`)
|
|
43
|
+
- `webhook_url` (`MAILER_HTTP_WEBHOOK_URL`, e.g. `https://api.resend.com/emails`), `api_key` (`MAILER_HTTP_API_KEY`) for direct HTTP Email API relay
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 2. `mpn-whatsapp` (`whatsapp`)
|
|
48
|
+
|
|
49
|
+
- **Class**: `WhatsAppNotificationProviderService`
|
|
50
|
+
- **4 Selectable Drivers (`WhatsAppDriver`)**:
|
|
51
|
+
1. **`meta-cloud`**: Official Meta Graph API (`v21.0` / `v25.0`). Formats recipients via `toWhatsAppDigitsOnly()` (no leading `+`) and supports `type: "template"` (approved WABA templates outside the 24h window), `type: "interactive"` (reply buttons), `type: "document"`, and `type: "text"`.
|
|
52
|
+
2. **`openwa`**: Self-hosted **`rmyndharis/OpenWA`** (`ENGINE_TYPE=baileys`, default port `2785`) REST API (`POST /api/sessions/{sessionId}/messages/send-text` and `POST /api/sessions/{sessionId}/messages/send-document` with `X-API-Key` + `Authorization: Bearer` headers, `@c.us` chat IDs, and validated session IDs).
|
|
53
|
+
3. **`waha`**: Self-hosted WAHA (WhatsApp HTTP API) (`/api/sendText`, `/api/sendFile` with `X-Api-Key`).
|
|
54
|
+
4. **`evolution`**: Self-hosted Evolution API v2 (`/message/sendText/{instance}`, `/message/sendMedia/{instance}` with `apikey` header).
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 3. `mpn-telegram` (`telegram`)
|
|
59
|
+
|
|
60
|
+
- **Class**: `TelegramNotificationProviderService`
|
|
61
|
+
- **Features**:
|
|
62
|
+
- Sanitizes HTML via `sanitizeTelegramHtml` (`<b>`, `<i>`, `<u>`, `<s>`, `<code>`, `<pre>`, `<a href="...">`).
|
|
63
|
+
- Supports `message_thread_id` for routing different store events (e.g., Orders vs. Low Inventory) into dedicated Telegram Supergroup Forum Topics.
|
|
64
|
+
- Supports `InlineKeyboardMarkup` action buttons (`url` or `callback_data`).
|
|
65
|
+
- Uploads attached PDFs (`medusa-plugin-documents`) via Telegram's `sendDocument` endpoint.
|
|
66
|
+
- Enforces non-dropping rate limits (`ChannelRateLimiter`: 30 msg/s global, 1 msg/s per chat).
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## 4. `mpn-sms` (`sms`)
|
|
71
|
+
|
|
72
|
+
- **Class**: `SmsNotificationProviderService`
|
|
73
|
+
- **5 Selectable Drivers (`SmsDriver`)**:
|
|
74
|
+
1. **`twilio`**: Form-encoded `POST /2010-04-01/Accounts/{SID}/Messages.json` supporting `From` or `MessagingServiceSid`, MMS `MediaUrl`, and `isRetryableSmsError` (`20000`–`21999` validation error classification).
|
|
75
|
+
2. **`signalhouse`**: JSON `POST /message/sendSMS` (`x-api-key`) for 10DLC SMS/MMS and abandoned cart recovery.
|
|
76
|
+
3. **`msg91`**: JSON `POST https://control.msg91.com/api/v5/flow/` (`authkey` + `template_id`).
|
|
77
|
+
4. **`smsapi`**: Form-encoded `POST https://api.smsapi.com/sms.do` (Bearer token).
|
|
78
|
+
5. **`webhook`**: Generic SSRF-validated HTTP JSON webhook.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## 5. `mpn-slack` (`slack`)
|
|
83
|
+
|
|
84
|
+
- **Class**: `SlackNotificationProviderService`
|
|
85
|
+
- **Features**: Sends rendered `blocks` (Slack Block Kit JSON array) and fallback `text` to `webhook_url` (`SLACK_WEBHOOK_URL`) after passing `validateSafeExternalUrl`.
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## 6. `mpn-dummy-sandbox` (`dummy-sandbox`)
|
|
90
|
+
|
|
91
|
+
- **Class**: `DummySandboxNotificationProviderService`
|
|
92
|
+
- **Features**: Captures messages in both `inMemorySandboxStore` (`getInMemorySandboxMessages`, `clearInMemorySandboxMessages`) and `mpn_sandbox_message` via `notificationsModuleService.captureSandboxMessage`, automatically extracting OTP codes (`extractOtpCode`) so developers and QA teams can inspect verification codes in `/app/mpn/sandbox`.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { Logger, NotificationTypes } from "@medusajs/framework/types"
|
|
2
|
+
import {
|
|
3
|
+
AbstractNotificationProviderService,
|
|
4
|
+
ModuleProvider,
|
|
5
|
+
Modules,
|
|
6
|
+
} from "@medusajs/framework/utils"
|
|
7
|
+
import {
|
|
8
|
+
type CapturedSandboxEntry,
|
|
9
|
+
type DummySandboxProviderOptions,
|
|
10
|
+
NOTIFICATIONS_MODULE,
|
|
11
|
+
type NotificationChannel,
|
|
12
|
+
} from "../../types"
|
|
13
|
+
import { extractOtpCode } from "../../modules/notifications/utils/security"
|
|
14
|
+
|
|
15
|
+
export const MPN_DUMMY_SANDBOX_PROVIDER_ID = "mpn-dummy-sandbox"
|
|
16
|
+
export type {
|
|
17
|
+
CapturedSandboxEntry,
|
|
18
|
+
DummySandboxProviderOptions,
|
|
19
|
+
} from "../../types"
|
|
20
|
+
|
|
21
|
+
const inMemorySandboxStore: CapturedSandboxEntry[] = []
|
|
22
|
+
|
|
23
|
+
export function getInMemorySandboxMessages(
|
|
24
|
+
limit = 50,
|
|
25
|
+
offset = 0
|
|
26
|
+
): {
|
|
27
|
+
messages: CapturedSandboxEntry[]
|
|
28
|
+
count: number
|
|
29
|
+
} {
|
|
30
|
+
return {
|
|
31
|
+
messages: inMemorySandboxStore.slice(offset, offset + limit),
|
|
32
|
+
count: inMemorySandboxStore.length,
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function clearInMemorySandboxMessages(): void {
|
|
37
|
+
inMemorySandboxStore.length = 0
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type InjectedDependencies = {
|
|
41
|
+
logger?: Logger
|
|
42
|
+
[key: string]: unknown
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Dummy Sandbox Notification Provider (inspired by `medusa-dummy-sms`, expanded to all channels).
|
|
47
|
+
* Captures SMS, OTPs, WhatsApp, Telegram, and Email notifications in memory + DB (`mpn_sandbox_message`)
|
|
48
|
+
* so local/staging environments can inspect OTPs and message payloads in Medusa Admin without carrier cost.
|
|
49
|
+
*/
|
|
50
|
+
export class DummySandboxNotificationProviderService extends AbstractNotificationProviderService {
|
|
51
|
+
static identifier = MPN_DUMMY_SANDBOX_PROVIDER_ID
|
|
52
|
+
protected readonly container_: InjectedDependencies
|
|
53
|
+
protected readonly logger_?: Logger
|
|
54
|
+
protected readonly options_: DummySandboxProviderOptions
|
|
55
|
+
|
|
56
|
+
constructor(
|
|
57
|
+
container: InjectedDependencies,
|
|
58
|
+
options?: DummySandboxProviderOptions
|
|
59
|
+
) {
|
|
60
|
+
super()
|
|
61
|
+
this.container_ = container || {}
|
|
62
|
+
this.logger_ = container?.logger
|
|
63
|
+
this.options_ = options ?? {}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async send(
|
|
67
|
+
notification: NotificationTypes.ProviderSendNotificationDTO
|
|
68
|
+
): Promise<NotificationTypes.ProviderSendNotificationResultsDTO> {
|
|
69
|
+
const data = (notification.data || {}) as Record<string, any>
|
|
70
|
+
const content = (notification.content || {}) as Record<string, any>
|
|
71
|
+
const body =
|
|
72
|
+
content.text ||
|
|
73
|
+
(data.text as string) ||
|
|
74
|
+
(data.body as string) ||
|
|
75
|
+
content.html ||
|
|
76
|
+
""
|
|
77
|
+
const html = content.html || (data.html as string) || null
|
|
78
|
+
const subject = content.subject || (data.subject as string) || null
|
|
79
|
+
const otpCode = extractOtpCode(body) || extractOtpCode(subject)
|
|
80
|
+
|
|
81
|
+
const mediaUrls: string[] = Array.isArray(data.mediaUrl)
|
|
82
|
+
? data.mediaUrl
|
|
83
|
+
: []
|
|
84
|
+
const attachmentsMeta = Array.isArray(notification.attachments)
|
|
85
|
+
? notification.attachments.map((a: any) => ({
|
|
86
|
+
filename: String(a.filename || "attachment.pdf"),
|
|
87
|
+
content_type: String(a.content_type || "application/pdf"),
|
|
88
|
+
}))
|
|
89
|
+
: []
|
|
90
|
+
|
|
91
|
+
const id = `sbx_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`
|
|
92
|
+
const entry: CapturedSandboxEntry = {
|
|
93
|
+
id,
|
|
94
|
+
channel: notification.channel || "sms",
|
|
95
|
+
recipient: notification.to,
|
|
96
|
+
sender: (notification as any).from || null,
|
|
97
|
+
subject,
|
|
98
|
+
body,
|
|
99
|
+
html,
|
|
100
|
+
otp_code: otpCode,
|
|
101
|
+
media_urls: mediaUrls,
|
|
102
|
+
attachments_meta: attachmentsMeta,
|
|
103
|
+
created_at: new Date().toISOString(),
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const maxStored = Math.max(
|
|
107
|
+
10,
|
|
108
|
+
Number(this.options_.maxStoredMessages ?? 200)
|
|
109
|
+
)
|
|
110
|
+
inMemorySandboxStore.unshift(entry)
|
|
111
|
+
if (inMemorySandboxStore.length > maxStored) {
|
|
112
|
+
inMemorySandboxStore.length = maxStored
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const mpnService = this.container_?.[NOTIFICATIONS_MODULE] as any
|
|
117
|
+
if (
|
|
118
|
+
mpnService &&
|
|
119
|
+
typeof mpnService.captureSandboxMessage === "function"
|
|
120
|
+
) {
|
|
121
|
+
await mpnService.captureSandboxMessage({
|
|
122
|
+
channel: (notification.channel || "sms") as NotificationChannel,
|
|
123
|
+
recipient: notification.to,
|
|
124
|
+
sender: entry.sender,
|
|
125
|
+
subject,
|
|
126
|
+
body,
|
|
127
|
+
html,
|
|
128
|
+
media_urls: mediaUrls,
|
|
129
|
+
attachments_meta: attachmentsMeta,
|
|
130
|
+
metadata: { provider: MPN_DUMMY_SANDBOX_PROVIDER_ID },
|
|
131
|
+
})
|
|
132
|
+
}
|
|
133
|
+
} catch {
|
|
134
|
+
// In-memory store already captured the message
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return { id }
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export default ModuleProvider(Modules.NOTIFICATION, {
|
|
142
|
+
services: [DummySandboxNotificationProviderService],
|
|
143
|
+
})
|