@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.
Files changed (241) hide show
  1. package/.medusa/server/src/admin/index.cjs +4215 -0
  2. package/.medusa/server/src/admin/index.js +4193 -0
  3. package/.medusa/server/src/api/admin/mpn/broadcasts/route.js +99 -0
  4. package/.medusa/server/src/api/admin/mpn/channels/route.js +109 -0
  5. package/.medusa/server/src/api/admin/mpn/logs/route.js +33 -0
  6. package/.medusa/server/src/api/admin/mpn/mappings/[id]/route.js +33 -0
  7. package/.medusa/server/src/api/admin/mpn/mappings/route.js +51 -0
  8. package/.medusa/server/src/api/admin/mpn/render-template/route.js +37 -0
  9. package/.medusa/server/src/api/admin/mpn/sandbox/route.js +36 -0
  10. package/.medusa/server/src/api/admin/mpn/send/route.js +135 -0
  11. package/.medusa/server/src/api/admin/mpn/templates/[id]/route.js +77 -0
  12. package/.medusa/server/src/api/admin/mpn/templates/route.js +71 -0
  13. package/.medusa/server/src/api/admin/plugin/route.js +4 -0
  14. package/.medusa/server/src/api/middlewares.js +28 -0
  15. package/.medusa/server/src/api/store/mpn/consent/route.js +37 -0
  16. package/.medusa/server/src/api/store/mpn/webhooks/[provider]/route.js +97 -0
  17. package/.medusa/server/src/api/store/plugin/route.js +4 -0
  18. package/.medusa/server/src/api/validators.js +118 -0
  19. package/.medusa/server/src/hooks/index.js +7 -0
  20. package/.medusa/server/src/hooks/use-mpn-channels.js +13 -0
  21. package/.medusa/server/src/hooks/use-mpn-dispatch.js +45 -0
  22. package/.medusa/server/src/hooks/use-mpn-mappings.js +21 -0
  23. package/.medusa/server/src/hooks/use-mpn-templates.js +34 -0
  24. package/.medusa/server/src/hooks/use-order-notifications-widget-state.js +79 -0
  25. package/.medusa/server/src/hooks/use-sdk-primitives.js +113 -0
  26. package/.medusa/server/src/index.js +14 -0
  27. package/.medusa/server/src/integrations/automations-action-handler.js +112 -0
  28. package/.medusa/server/src/jobs/abandoned-cart-recovery.js +123 -0
  29. package/.medusa/server/src/jobs/log-retention-and-broadcasts.js +15 -0
  30. package/.medusa/server/src/modules/notifications/i18n/index.js +65 -0
  31. package/.medusa/server/src/modules/notifications/i18n/locales/ar.js +76 -0
  32. package/.medusa/server/src/modules/notifications/i18n/locales/de.js +76 -0
  33. package/.medusa/server/src/modules/notifications/i18n/locales/en.js +76 -0
  34. package/.medusa/server/src/modules/notifications/i18n/locales/es.js +76 -0
  35. package/.medusa/server/src/modules/notifications/i18n/locales/fr.js +76 -0
  36. package/.medusa/server/src/modules/notifications/i18n/locales/it.js +76 -0
  37. package/.medusa/server/src/modules/notifications/i18n/locales/nl.js +76 -0
  38. package/.medusa/server/src/modules/notifications/i18n/locales/pl.js +76 -0
  39. package/.medusa/server/src/modules/notifications/i18n/locales/pt.js +76 -0
  40. package/.medusa/server/src/modules/notifications/i18n/locales/tr.js +76 -0
  41. package/.medusa/server/src/modules/notifications/i18n/types.js +2 -0
  42. package/.medusa/server/src/modules/notifications/index.js +9 -0
  43. package/.medusa/server/src/modules/notifications/integrations/documents-and-b2b-resolver.js +129 -0
  44. package/.medusa/server/src/modules/notifications/migrations/Migration20261010000000.js +184 -0
  45. package/.medusa/server/src/modules/notifications/models/index.js +118 -0
  46. package/.medusa/server/src/modules/notifications/renderer/block-helpers.js +36 -0
  47. package/.medusa/server/src/modules/notifications/renderer/email.js +161 -0
  48. package/.medusa/server/src/modules/notifications/renderer/index.js +240 -0
  49. package/.medusa/server/src/modules/notifications/renderer/slack.js +114 -0
  50. package/.medusa/server/src/modules/notifications/renderer/telegram.js +99 -0
  51. package/.medusa/server/src/modules/notifications/renderer/whatsapp.js +91 -0
  52. package/.medusa/server/src/modules/notifications/service-helpers/channel-config.js +45 -0
  53. package/.medusa/server/src/modules/notifications/service-helpers/consent.js +65 -0
  54. package/.medusa/server/src/modules/notifications/service-helpers/delivery-log.js +29 -0
  55. package/.medusa/server/src/modules/notifications/service-helpers/event-conditions.js +46 -0
  56. package/.medusa/server/src/modules/notifications/service-helpers/external-templates.js +80 -0
  57. package/.medusa/server/src/modules/notifications/service-helpers/recipient-resolution.js +85 -0
  58. package/.medusa/server/src/modules/notifications/service-helpers/sender-profiles.js +29 -0
  59. package/.medusa/server/src/modules/notifications/service-helpers/template-blocks.js +81 -0
  60. package/.medusa/server/src/modules/notifications/service-helpers/template-catalog.js +129 -0
  61. package/.medusa/server/src/modules/notifications/service-helpers/template-render.js +65 -0
  62. package/.medusa/server/src/modules/notifications/service.js +339 -0
  63. package/.medusa/server/src/modules/notifications/types.js +2 -0
  64. package/.medusa/server/src/modules/notifications/utils/auth-presets.js +140 -0
  65. package/.medusa/server/src/modules/notifications/utils/interpolation.js +178 -0
  66. package/.medusa/server/src/modules/notifications/utils/security.js +312 -0
  67. package/.medusa/server/src/modules/notifications/utils/system-presets.js +651 -0
  68. package/.medusa/server/src/modules/notifications/utils/transformers.js +358 -0
  69. package/.medusa/server/src/providers/dummy-sandbox/index.js +96 -0
  70. package/.medusa/server/src/providers/email-smtp/index.js +187 -0
  71. package/.medusa/server/src/providers/index.js +7 -0
  72. package/.medusa/server/src/providers/slack/index.js +54 -0
  73. package/.medusa/server/src/providers/sms/index.js +254 -0
  74. package/.medusa/server/src/providers/telegram/index.js +119 -0
  75. package/.medusa/server/src/providers/whatsapp/index.js +371 -0
  76. package/.medusa/server/src/rbac.js +109 -0
  77. package/.medusa/server/src/sdk/client.js +186 -0
  78. package/.medusa/server/src/sdk/index.js +3 -0
  79. package/.medusa/server/src/sdk/query-keys.js +61 -0
  80. package/.medusa/server/src/subscribers/customer-and-document-events.js +33 -0
  81. package/.medusa/server/src/subscribers/fulfillment-and-inventory-events.js +30 -0
  82. package/.medusa/server/src/subscribers/order-events.js +28 -0
  83. package/.medusa/server/src/types/constants.js +129 -0
  84. package/.medusa/server/src/types/core.js +2 -0
  85. package/.medusa/server/src/types/domain.js +2 -0
  86. package/.medusa/server/src/types/enums.js +53 -0
  87. package/.medusa/server/src/types/errors.js +28 -0
  88. package/.medusa/server/src/types/hooks.js +2 -0
  89. package/.medusa/server/src/types/index.js +12 -0
  90. package/.medusa/server/src/types/providers.js +2 -0
  91. package/.medusa/server/src/types/records.js +2 -0
  92. package/.medusa/server/src/types/render.js +2 -0
  93. package/.medusa/server/src/types/sdk-api.js +2 -0
  94. package/.medusa/server/src/types/translations.js +2 -0
  95. package/.medusa/server/src/workflows/index.js +461 -0
  96. package/LICENSE +21 -0
  97. package/README.md +389 -2
  98. package/package.json +138 -3
  99. package/src/admin/README.md +123 -0
  100. package/src/admin/components/mpn/broadcast-consent-section.tsx +62 -0
  101. package/src/admin/components/mpn/broadcasts-tab.tsx +208 -0
  102. package/src/admin/components/mpn/channels-main-section.tsx +205 -0
  103. package/src/admin/components/mpn/channels-side-section.tsx +88 -0
  104. package/src/admin/components/mpn/channels-tab.tsx +130 -0
  105. package/src/admin/components/mpn/constants.ts +8 -0
  106. package/src/admin/components/mpn/delivery-logs-tab.tsx +70 -0
  107. package/src/admin/components/mpn/event-mapping-form.tsx +191 -0
  108. package/src/admin/components/mpn/event-mappings-tab.tsx +165 -0
  109. package/src/admin/components/mpn/header-bar.tsx +35 -0
  110. package/src/admin/components/mpn/index.ts +17 -0
  111. package/src/admin/components/mpn/overview-cards.tsx +103 -0
  112. package/src/admin/components/mpn/sandbox-tab.tsx +93 -0
  113. package/src/admin/components/mpn/template-editor-section.tsx +203 -0
  114. package/src/admin/components/mpn/template-preview-section.tsx +122 -0
  115. package/src/admin/components/mpn/templates-list-section.tsx +115 -0
  116. package/src/admin/components/mpn/templates-tab.tsx +188 -0
  117. package/src/admin/components/mpn/use-notification-studio.ts +84 -0
  118. package/src/admin/components/rbac-guard.tsx +153 -0
  119. package/src/admin/i18n/README.md +134 -0
  120. package/src/admin/i18n/index.ts +23 -0
  121. package/src/admin/i18n/json/ar.json +157 -0
  122. package/src/admin/i18n/json/de.json +157 -0
  123. package/src/admin/i18n/json/en.json +157 -0
  124. package/src/admin/i18n/json/es.json +157 -0
  125. package/src/admin/i18n/json/fr.json +157 -0
  126. package/src/admin/i18n/json/it.json +157 -0
  127. package/src/admin/i18n/json/nl.json +157 -0
  128. package/src/admin/i18n/json/pl.json +157 -0
  129. package/src/admin/i18n/json/pt.json +157 -0
  130. package/src/admin/i18n/json/tr.json +157 -0
  131. package/src/admin/routes/mpn/broadcasts/page.tsx +75 -0
  132. package/src/admin/routes/mpn/channels/page.tsx +66 -0
  133. package/src/admin/routes/mpn/logs/page.tsx +50 -0
  134. package/src/admin/routes/mpn/mappings/page.tsx +62 -0
  135. package/src/admin/routes/mpn/page.tsx +200 -0
  136. package/src/admin/routes/mpn/sandbox/page.tsx +53 -0
  137. package/src/admin/routes/mpn/templates/page.tsx +55 -0
  138. package/src/admin/tsconfig.json +23 -0
  139. package/src/admin/vite-env.d.ts +1 -0
  140. package/src/admin/widgets/order-notifications-widget.tsx +178 -0
  141. package/src/api/README.md +98 -0
  142. package/src/api/admin/mpn/broadcasts/route.ts +130 -0
  143. package/src/api/admin/mpn/channels/route.ts +143 -0
  144. package/src/api/admin/mpn/logs/route.ts +41 -0
  145. package/src/api/admin/mpn/mappings/[id]/route.ts +45 -0
  146. package/src/api/admin/mpn/mappings/route.ts +68 -0
  147. package/src/api/admin/mpn/render-template/route.ts +46 -0
  148. package/src/api/admin/mpn/sandbox/route.ts +51 -0
  149. package/src/api/admin/mpn/send/route.ts +163 -0
  150. package/src/api/admin/mpn/templates/[id]/route.ts +101 -0
  151. package/src/api/admin/mpn/templates/route.ts +99 -0
  152. package/src/api/admin/plugin/route.ts +5 -0
  153. package/src/api/middlewares.ts +42 -0
  154. package/src/api/store/mpn/consent/route.ts +45 -0
  155. package/src/api/store/mpn/webhooks/[provider]/route.ts +119 -0
  156. package/src/api/store/plugin/route.ts +5 -0
  157. package/src/api/validators.ts +149 -0
  158. package/src/hooks/index.ts +14 -0
  159. package/src/hooks/use-mpn-channels.ts +43 -0
  160. package/src/hooks/use-mpn-dispatch.ts +149 -0
  161. package/src/hooks/use-mpn-mappings.ts +75 -0
  162. package/src/hooks/use-mpn-templates.ts +120 -0
  163. package/src/hooks/use-order-notifications-widget-state.ts +103 -0
  164. package/src/hooks/use-sdk-primitives.ts +157 -0
  165. package/src/index.ts +16 -0
  166. package/src/integrations/automations-action-handler.ts +156 -0
  167. package/src/jobs/README.md +41 -0
  168. package/src/jobs/abandoned-cart-recovery.ts +152 -0
  169. package/src/jobs/log-retention-and-broadcasts.ts +23 -0
  170. package/src/links/README.md +52 -0
  171. package/src/modules/README.md +99 -0
  172. package/src/modules/notifications/i18n/index.ts +83 -0
  173. package/src/modules/notifications/i18n/locales/ar.ts +84 -0
  174. package/src/modules/notifications/i18n/locales/de.ts +83 -0
  175. package/src/modules/notifications/i18n/locales/en.ts +84 -0
  176. package/src/modules/notifications/i18n/locales/es.ts +84 -0
  177. package/src/modules/notifications/i18n/locales/fr.ts +82 -0
  178. package/src/modules/notifications/i18n/locales/it.ts +82 -0
  179. package/src/modules/notifications/i18n/locales/nl.ts +83 -0
  180. package/src/modules/notifications/i18n/locales/pl.ts +84 -0
  181. package/src/modules/notifications/i18n/locales/pt.ts +82 -0
  182. package/src/modules/notifications/i18n/locales/tr.ts +86 -0
  183. package/src/modules/notifications/i18n/types.ts +1 -0
  184. package/src/modules/notifications/index.ts +10 -0
  185. package/src/modules/notifications/integrations/documents-and-b2b-resolver.ts +168 -0
  186. package/src/modules/notifications/migrations/Migration20261010000000.ts +185 -0
  187. package/src/modules/notifications/models/index.ts +125 -0
  188. package/src/modules/notifications/renderer/block-helpers.ts +51 -0
  189. package/src/modules/notifications/renderer/email.ts +281 -0
  190. package/src/modules/notifications/renderer/index.ts +301 -0
  191. package/src/modules/notifications/renderer/slack.ts +168 -0
  192. package/src/modules/notifications/renderer/telegram.ts +161 -0
  193. package/src/modules/notifications/renderer/whatsapp.ts +156 -0
  194. package/src/modules/notifications/service-helpers/channel-config.ts +65 -0
  195. package/src/modules/notifications/service-helpers/consent.ts +88 -0
  196. package/src/modules/notifications/service-helpers/delivery-log.ts +56 -0
  197. package/src/modules/notifications/service-helpers/event-conditions.ts +66 -0
  198. package/src/modules/notifications/service-helpers/external-templates.ts +127 -0
  199. package/src/modules/notifications/service-helpers/recipient-resolution.ts +124 -0
  200. package/src/modules/notifications/service-helpers/sender-profiles.ts +34 -0
  201. package/src/modules/notifications/service-helpers/template-blocks.ts +116 -0
  202. package/src/modules/notifications/service-helpers/template-catalog.ts +170 -0
  203. package/src/modules/notifications/service-helpers/template-render.ts +113 -0
  204. package/src/modules/notifications/service.ts +588 -0
  205. package/src/modules/notifications/types.ts +1 -0
  206. package/src/modules/notifications/utils/auth-presets.ts +145 -0
  207. package/src/modules/notifications/utils/interpolation.ts +231 -0
  208. package/src/modules/notifications/utils/security.ts +378 -0
  209. package/src/modules/notifications/utils/system-presets.ts +695 -0
  210. package/src/modules/notifications/utils/transformers.ts +456 -0
  211. package/src/providers/README.md +92 -0
  212. package/src/providers/dummy-sandbox/index.ts +143 -0
  213. package/src/providers/email-smtp/index.ts +257 -0
  214. package/src/providers/index.ts +38 -0
  215. package/src/providers/slack/index.ts +82 -0
  216. package/src/providers/sms/index.ts +342 -0
  217. package/src/providers/telegram/index.ts +181 -0
  218. package/src/providers/whatsapp/index.ts +505 -0
  219. package/src/rbac.ts +123 -0
  220. package/src/sdk/client.ts +319 -0
  221. package/src/sdk/index.ts +48 -0
  222. package/src/sdk/query-keys.ts +89 -0
  223. package/src/subscribers/README.md +50 -0
  224. package/src/subscribers/customer-and-document-events.ts +36 -0
  225. package/src/subscribers/fulfillment-and-inventory-events.ts +35 -0
  226. package/src/subscribers/order-events.ts +31 -0
  227. package/src/types/constants.ts +143 -0
  228. package/src/types/core.ts +96 -0
  229. package/src/types/domain.ts +84 -0
  230. package/src/types/enums.ts +73 -0
  231. package/src/types/errors.ts +32 -0
  232. package/src/types/hooks.ts +48 -0
  233. package/src/types/index.ts +11 -0
  234. package/src/types/providers.ts +96 -0
  235. package/src/types/records.ts +100 -0
  236. package/src/types/render.ts +74 -0
  237. package/src/types/sdk-api.ts +228 -0
  238. package/src/types/translations.ts +75 -0
  239. package/src/workflows/README.md +67 -0
  240. package/src/workflows/index.ts +619 -0
  241. package/tsconfig.json +40 -0
@@ -0,0 +1,200 @@
1
+ import { defineRouteConfig } from "@medusajs/admin-sdk"
2
+ import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
3
+ import { LayoutComposer } from "@medusajs/dashboard/components"
4
+ import {
5
+ ArrowPath,
6
+ ChatBubbleLeftRight,
7
+ CheckCircle,
8
+ CogSixTooth,
9
+ DocumentText,
10
+ EnvelopeSolid,
11
+ Megaphone,
12
+ Tag,
13
+ } from "@medusajs/icons"
14
+ import { Button, Container, Heading, Text, Toaster, toast } from "@medusajs/ui"
15
+ import React from "react"
16
+ import { useTranslation } from "react-i18next"
17
+ import { useNavigate } from "react-router-dom"
18
+ import {
19
+ NavigationCard,
20
+ StatCard,
21
+ useNotificationStudio,
22
+ } from "../../components/mpn"
23
+ import { RbacGuard } from "../../components/rbac-guard"
24
+
25
+ const NotificationStudioOverviewPage: React.FC = () => {
26
+ const { t, i18n } = useTranslation()
27
+ const navigate = useNavigate()
28
+ const {
29
+ isLoading,
30
+ templates,
31
+ mappings,
32
+ logs,
33
+ sandboxMessages,
34
+ loadAllStudioData,
35
+ } = useNotificationStudio()
36
+
37
+ const isRtl = (i18n.resolvedLanguage || i18n.language || "").startsWith("ar")
38
+
39
+ const handleRefresh = async () => {
40
+ try {
41
+ await loadAllStudioData()
42
+ toast.success(t("header.refresh"))
43
+ } catch {
44
+ toast.error(t("messages.loadStudioError"))
45
+ }
46
+ }
47
+
48
+ return (
49
+ <LayoutComposer
50
+ widgetsZonePrefix="mpn.overview"
51
+ preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_COLUMN}
52
+ sections={{
53
+ main: (
54
+ <LayoutComposer.Entry id="MpnOverviewDashboard">
55
+ <div className="flex flex-col gap-y-4" dir={isRtl ? "rtl" : "ltr"}>
56
+ <Toaster />
57
+ <div className="flex flex-wrap items-center justify-between gap-4">
58
+ <div>
59
+ <Heading className="font-sans font-medium h1-core">
60
+ {t("header.title")}
61
+ </Heading>
62
+
63
+ <Text className="text-ui-fg-subtle mt-1">
64
+ {t("header.subtitle")}
65
+ </Text>
66
+ </div>
67
+
68
+ <Button
69
+ size="small"
70
+ variant="secondary"
71
+ onClick={() => void handleRefresh()}
72
+ >
73
+ <ArrowPath />
74
+ {t("header.refresh")}
75
+ </Button>
76
+ </div>
77
+
78
+ <div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-4">
79
+ <StatCard
80
+ title={t("templates.title")}
81
+ value={templates.length}
82
+ icon={<DocumentText />}
83
+ loading={isLoading}
84
+ onClick={() => navigate("/mpn/templates")}
85
+ />
86
+
87
+ <StatCard
88
+ title={t("mappings.title")}
89
+ value={mappings.length}
90
+ icon={<Tag />}
91
+ loading={isLoading}
92
+ onClick={() => navigate("/mpn/mappings")}
93
+ />
94
+
95
+ <StatCard
96
+ title={t("logs.title")}
97
+ value={logs.length}
98
+ icon={<CheckCircle />}
99
+ loading={isLoading}
100
+ onClick={() => navigate("/mpn/logs")}
101
+ />
102
+
103
+ <StatCard
104
+ title={t("sandbox.title")}
105
+ value={sandboxMessages.length}
106
+ icon={<EnvelopeSolid />}
107
+ loading={isLoading}
108
+ onClick={() => navigate("/mpn/sandbox")}
109
+ />
110
+ </div>
111
+
112
+ <Container className="p-0 overflow-hidden">
113
+ <div className="px-6 py-5 border-b border-ui-border-base">
114
+ <Heading level="h2">{t("nav.label")}</Heading>
115
+
116
+ <Text className="text-ui-fg-subtle mt-1">
117
+ {t("header.subtitle")}
118
+ </Text>
119
+ </div>
120
+
121
+ <div className="grid grid-cols-1 md:grid-cols-2">
122
+ <NavigationCard
123
+ icon={<DocumentText />}
124
+ title={t("tabs.templates", { count: templates.length })}
125
+ description={t("templates.emptySelection")}
126
+ onClick={() => navigate("/mpn/templates")}
127
+ position="top-left"
128
+ />
129
+
130
+ <NavigationCard
131
+ icon={<Tag />}
132
+ title={t("tabs.mappings", { count: mappings.length })}
133
+ description={t("mappings.title")}
134
+ onClick={() => navigate("/mpn/mappings")}
135
+ position="top-right"
136
+ />
137
+
138
+ <NavigationCard
139
+ icon={<CogSixTooth />}
140
+ title={t("tabs.channels")}
141
+ description={t("channels.whatsappSubtitle")}
142
+ onClick={() => navigate("/mpn/channels")}
143
+ position="middle-left"
144
+ />
145
+
146
+ <NavigationCard
147
+ icon={<CheckCircle />}
148
+ title={t("tabs.logs", { count: logs.length })}
149
+ description={t("logs.title")}
150
+ onClick={() => navigate("/mpn/logs")}
151
+ position="middle-right"
152
+ />
153
+
154
+ <NavigationCard
155
+ icon={<Megaphone />}
156
+ title={t("tabs.broadcasts")}
157
+ description={t("broadcasts.title")}
158
+ onClick={() => navigate("/mpn/broadcasts")}
159
+ position="bottom-left"
160
+ />
161
+
162
+ <NavigationCard
163
+ icon={<EnvelopeSolid />}
164
+ title={t("tabs.sandbox", { count: sandboxMessages.length })}
165
+ description={t("sandbox.subtitle")}
166
+ onClick={() => navigate("/mpn/sandbox")}
167
+ position="bottom-right"
168
+ />
169
+ </div>
170
+ </Container>
171
+ </div>
172
+ </LayoutComposer.Entry>
173
+ ),
174
+ }}
175
+ />
176
+ )
177
+ }
178
+
179
+ const Breadcrumb = () => {
180
+ const { t } = useTranslation()
181
+ return t("nav.label")
182
+ }
183
+
184
+ export const config = defineRouteConfig({
185
+ label: "Notifications",
186
+ translationNs: "mpn",
187
+ icon: ChatBubbleLeftRight,
188
+ })
189
+
190
+ export const handle = {
191
+ breadcrumb: () => <Breadcrumb />,
192
+ }
193
+
194
+ const GuardedNotificationStudioOverviewPage = () => (
195
+ <RbacGuard permission="notifications.read">
196
+ <NotificationStudioOverviewPage />
197
+ </RbacGuard>
198
+ )
199
+
200
+ export default GuardedNotificationStudioOverviewPage
@@ -0,0 +1,53 @@
1
+ import { defineRouteConfig } from "@medusajs/admin-sdk"
2
+ import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
3
+ import { LayoutComposer } from "@medusajs/dashboard/components"
4
+ import { EnvelopeSolid } from "@medusajs/icons"
5
+ import React from "react"
6
+ import { useTranslation } from "react-i18next"
7
+ import { DummySandboxTab } from "../../../components/mpn/sandbox-tab"
8
+ import { useNotificationStudio } from "../../../components/mpn/use-notification-studio"
9
+ import { RbacGuard } from "../../../components/rbac-guard"
10
+
11
+ const MpnSandboxPage: React.FC = () => {
12
+ const { sandboxMessages, loadAllStudioData } = useNotificationStudio()
13
+
14
+ return (
15
+ <LayoutComposer
16
+ widgetsZonePrefix="mpn.sandbox"
17
+ preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_COLUMN}
18
+ sections={{
19
+ main: (
20
+ <LayoutComposer.Entry id="MpnSandboxSection">
21
+ <DummySandboxTab
22
+ sandboxMessages={sandboxMessages}
23
+ onRefresh={loadAllStudioData}
24
+ />
25
+ </LayoutComposer.Entry>
26
+ ),
27
+ }}
28
+ />
29
+ )
30
+ }
31
+
32
+ const Breadcrumb = () => {
33
+ const { t } = useTranslation()
34
+ return t("sandbox.title")
35
+ }
36
+
37
+ export const config = defineRouteConfig({
38
+ label: "OTP Sandbox",
39
+ translationNs: "mpn",
40
+ icon: EnvelopeSolid,
41
+ })
42
+
43
+ export const handle = {
44
+ breadcrumb: () => <Breadcrumb />,
45
+ }
46
+
47
+ const GuardedMpnSandboxPage = () => (
48
+ <RbacGuard permission="notifications.read">
49
+ <MpnSandboxPage />
50
+ </RbacGuard>
51
+ )
52
+
53
+ export default GuardedMpnSandboxPage
@@ -0,0 +1,55 @@
1
+ import { defineRouteConfig } from "@medusajs/admin-sdk"
2
+ import { CORE_LAYOUT_IDS } from "@medusajs/admin-shared"
3
+ import { LayoutComposer } from "@medusajs/dashboard/components"
4
+ import { DocumentText } from "@medusajs/icons"
5
+ import React from "react"
6
+ import { useTranslation } from "react-i18next"
7
+ import { TemplatesBuilderTab } from "../../../components/mpn/templates-tab"
8
+ import { useNotificationStudio } from "../../../components/mpn/use-notification-studio"
9
+ import { RbacGuard } from "../../../components/rbac-guard"
10
+
11
+ const MpnTemplatesPage: React.FC = () => {
12
+ const { templates, loadAllStudioData, setStatusBanner } =
13
+ useNotificationStudio()
14
+
15
+ return (
16
+ <LayoutComposer
17
+ widgetsZonePrefix="mpn.templates"
18
+ preferredLayoutId={CORE_LAYOUT_IDS.SINGLE_COLUMN}
19
+ sections={{
20
+ main: (
21
+ <LayoutComposer.Entry id="MpnTemplatesSection">
22
+ <TemplatesBuilderTab
23
+ templates={templates}
24
+ onRefresh={loadAllStudioData}
25
+ onStatus={setStatusBanner}
26
+ />
27
+ </LayoutComposer.Entry>
28
+ ),
29
+ }}
30
+ />
31
+ )
32
+ }
33
+
34
+ const Breadcrumb = () => {
35
+ const { t } = useTranslation()
36
+ return t("templates.title")
37
+ }
38
+
39
+ export const config = defineRouteConfig({
40
+ label: "Templates",
41
+ translationNs: "mpn",
42
+ icon: DocumentText,
43
+ })
44
+
45
+ export const handle = {
46
+ breadcrumb: () => <Breadcrumb />,
47
+ }
48
+
49
+ const GuardedMpnTemplatesPage = () => (
50
+ <RbacGuard permission="notifications.read">
51
+ <MpnTemplatesPage />
52
+ </RbacGuard>
53
+ )
54
+
55
+ export default GuardedMpnTemplatesPage
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2023",
4
+ "useDefineForClassFields": true,
5
+ "lib": ["ES2023", "DOM", "DOM.Iterable"],
6
+ "module": "ESNext",
7
+ "skipLibCheck": true,
8
+ "moduleResolution": "bundler",
9
+ "allowImportingTsExtensions": true,
10
+ "resolveJsonModule": true,
11
+ "isolatedModules": true,
12
+ "noEmit": true,
13
+ "jsx": "react-jsx",
14
+ "strict": true,
15
+ "noUnusedLocals": true,
16
+ "noUnusedParameters": true,
17
+ "noFallthroughCasesInSwitch": true,
18
+ "paths": {
19
+ "@/*": ["./*"]
20
+ }
21
+ },
22
+ "include": ["."]
23
+ }
@@ -0,0 +1 @@
1
+ /// <reference types="vite/client" />
@@ -0,0 +1,178 @@
1
+ import { defineWidgetConfig } from "@medusajs/admin-sdk"
2
+ import { SectionRow } from "@medusajs/dashboard/components"
3
+ import {
4
+ Badge,
5
+ Button,
6
+ Container,
7
+ Heading,
8
+ Select,
9
+ StatusBadge,
10
+ Switch,
11
+ Table,
12
+ Text,
13
+ Toaster,
14
+ toast,
15
+ } from "@medusajs/ui"
16
+ import React, { useEffect } from "react"
17
+ import { useTranslation } from "react-i18next"
18
+
19
+ import { useOrderNotificationsWidgetState } from "../../hooks"
20
+
21
+ const OrderNotificationsActivityWidget = ({ data }: { data?: any }) => {
22
+ const { t, i18n } = useTranslation()
23
+ const {
24
+ orderId,
25
+ logs,
26
+ templates,
27
+ selectedChannel,
28
+ setSelectedChannel,
29
+ selectedTemplateId,
30
+ setSelectedTemplateId,
31
+ attachInvoicePdf,
32
+ setAttachInvoicePdf,
33
+ sending,
34
+ statusMessage,
35
+ setStatusMessage,
36
+ handleSendOrderNotification,
37
+ } = useOrderNotificationsWidgetState({
38
+ order: data,
39
+ t,
40
+ })
41
+
42
+ useEffect(() => {
43
+ if (!statusMessage) return
44
+ const isError = statusMessage
45
+ .toLowerCase()
46
+ .includes(t("widget.dispatchFailed").toLowerCase())
47
+ if (isError) {
48
+ toast.error(statusMessage)
49
+ } else {
50
+ toast.success(statusMessage)
51
+ }
52
+ setStatusMessage(null)
53
+ }, [statusMessage, setStatusMessage, t])
54
+
55
+ if (!orderId) return null
56
+
57
+ const isRtl = (i18n.resolvedLanguage || i18n.language || "").startsWith("ar")
58
+
59
+ return (
60
+ <Container className="divide-y p-0" dir={isRtl ? "rtl" : "ltr"}>
61
+ <Toaster />
62
+ <div className="flex items-center justify-between px-6 py-4">
63
+ <div>
64
+ <Heading level="h2">
65
+ {t("widget.title", { count: logs.length })}
66
+ </Heading>
67
+ <Text className="text-ui-fg-subtle text-xs mt-1">
68
+ {t("widget.subtitle")}
69
+ </Text>
70
+ </div>
71
+ <StatusBadge color="green">{logs.length}</StatusBadge>
72
+ </div>
73
+
74
+ <SectionRow title={t("widget.colChannel")} value={selectedChannel} />
75
+
76
+ <div className="flex flex-wrap items-center gap-3 px-6 py-4">
77
+ <div className="w-40">
78
+ <Select
79
+ size="small"
80
+ value={selectedChannel}
81
+ onValueChange={setSelectedChannel}
82
+ >
83
+ <Select.Trigger>
84
+ <Select.Value placeholder={t("widget.channelPlaceholder")} />
85
+ </Select.Trigger>
86
+ <Select.Content>
87
+ <Select.Item value="email">EMAIL</Select.Item>
88
+ <Select.Item value="whatsapp">WHATSAPP / OPENWA</Select.Item>
89
+ <Select.Item value="telegram">TELEGRAM</Select.Item>
90
+ <Select.Item value="sms">SMS</Select.Item>
91
+ <Select.Item value="dummy">DUMMY SANDBOX</Select.Item>
92
+ </Select.Content>
93
+ </Select>
94
+ </div>
95
+
96
+ <div className="w-64">
97
+ <Select
98
+ size="small"
99
+ value={selectedTemplateId}
100
+ onValueChange={setSelectedTemplateId}
101
+ >
102
+ <Select.Trigger>
103
+ <Select.Value placeholder={t("widget.templatePlaceholder")} />
104
+ </Select.Trigger>
105
+ <Select.Content>
106
+ {templates.map((tpl) => (
107
+ <Select.Item key={tpl.id} value={tpl.id}>
108
+ [{tpl.channel.toUpperCase()}] {tpl.label}
109
+ </Select.Item>
110
+ ))}
111
+ </Select.Content>
112
+ </Select>
113
+ </div>
114
+
115
+ <div className="flex items-center gap-x-2">
116
+ <Switch
117
+ checked={attachInvoicePdf}
118
+ onCheckedChange={setAttachInvoicePdf}
119
+ />
120
+ <Text className="text-xs">{t("widget.attachInvoiceLabel")}</Text>
121
+ </div>
122
+
123
+ <Button
124
+ size="small"
125
+ variant="primary"
126
+ disabled={sending}
127
+ isLoading={sending}
128
+ onClick={() => void handleSendOrderNotification()}
129
+ >
130
+ {sending ? t("widget.sending") : t("widget.sendResendButton")}
131
+ </Button>
132
+ </div>
133
+
134
+ <Table>
135
+ <Table.Header>
136
+ <Table.Row>
137
+ <Table.HeaderCell>{t("widget.colChannel")}</Table.HeaderCell>
138
+ <Table.HeaderCell>{t("widget.colEventTemplate")}</Table.HeaderCell>
139
+ <Table.HeaderCell>{t("widget.colRecipient")}</Table.HeaderCell>
140
+ <Table.HeaderCell>{t("widget.colStatus")}</Table.HeaderCell>
141
+ </Table.Row>
142
+ </Table.Header>
143
+ <Table.Body>
144
+ {logs.map((row) => (
145
+ <Table.Row key={row.id}>
146
+ <Table.Cell>
147
+ <Badge size="2xsmall" color="blue">
148
+ {row.channel}
149
+ </Badge>
150
+ </Table.Cell>
151
+ <Table.Cell>
152
+ {row.event_name} ({row.template_id || "—"})
153
+ </Table.Cell>
154
+ <Table.Cell>{row.recipient_masked}</Table.Cell>
155
+ <Table.Cell>
156
+ <StatusBadge
157
+ color={
158
+ row.status === "sent" || row.status === "delivered"
159
+ ? "green"
160
+ : "orange"
161
+ }
162
+ >
163
+ {row.status}
164
+ </StatusBadge>
165
+ </Table.Cell>
166
+ </Table.Row>
167
+ ))}
168
+ </Table.Body>
169
+ </Table>
170
+ </Container>
171
+ )
172
+ }
173
+
174
+ export const config = defineWidgetConfig({
175
+ zone: "order.details.after",
176
+ })
177
+
178
+ export default OrderNotificationsActivityWidget
@@ -0,0 +1,98 @@
1
+ # API Routes
2
+
3
+ File-based REST routes under `/admin` and `/store`, following Medusa v2's `src/api/<surface>/<path>/route.ts` convention.
4
+
5
+ ```sh
6
+ api/
7
+ ├── validators.ts Zod validation schemas & inferred request payload types
8
+ ├── middlewares.ts Global middleware registration (rawBody for webhooks + verifyStoreCustomerSession)
9
+ ├── admin/
10
+ │ ├── plugin/
11
+ │ │ └── route.ts GET /admin/plugin — Admin plugin status & metadata
12
+ │ └── mpn/
13
+ │ ├── templates/
14
+ │ │ ├── route.ts GET, POST /admin/mpn/templates (list, create, or 1-click clone preset)
15
+ │ │ └── [id]/
16
+ │ │ └── route.ts GET, POST, DELETE /admin/mpn/templates/:id (detail, update, delete)
17
+ │ ├── render-template/
18
+ │ │ └── route.ts POST /admin/mpn/render-template (live multi-locale entity/mock preview)
19
+ │ ├── mappings/
20
+ │ │ ├── route.ts GET, POST /admin/mpn/mappings (list & create event mappings)
21
+ │ │ └── [id]/
22
+ │ │ └── route.ts POST, DELETE /admin/mpn/mappings/:id (update & delete event mapping)
23
+ │ ├── channels/
24
+ │ │ └── route.ts GET, POST /admin/mpn/channels (list, save driver config, or verify channel)
25
+ │ ├── logs/
26
+ │ │ └── route.ts GET /admin/mpn/logs (list PII-masked delivery logs)
27
+ │ ├── broadcasts/
28
+ │ │ └── route.ts GET, POST /admin/mpn/broadcasts (list & create/dispatch campaigns)
29
+ │ ├── sandbox/
30
+ │ │ └── route.ts GET, DELETE /admin/mpn/sandbox (list & clear captured sandbox messages/OTPs)
31
+ │ └── send/
32
+ │ └── route.ts POST /admin/mpn/send (ad-hoc send or event simulation with optional PDF invoice)
33
+ └── store/
34
+ ├── plugin/
35
+ │ └── route.ts GET /store/plugin — Storefront plugin health check
36
+ └── mpn/
37
+ ├── consent/
38
+ │ └── route.ts POST /store/mpn/consent (opt-in / opt-out consent registration)
39
+ └── webhooks/
40
+ └── [provider]/
41
+ └── route.ts GET, POST /store/mpn/webhooks/:provider (HMAC-verified inbound webhooks)
42
+ ```
43
+
44
+ ---
45
+
46
+ ## Authentication & Security Middlewares
47
+
48
+ ### Admin Routes (`/admin/mpn/*`)
49
+
50
+ Protected by Medusa's standard admin authentication (`user` session, bearer token, or API key). All request bodies are validated against Zod schemas defined in `src/api/validators.ts`.
51
+
52
+ ### Storefront Anti-IDOR Guard (`verifyStoreCustomerSession`)
53
+
54
+ Defined in `src/api/middlewares.ts` and enforced in `POST /store/mpn/consent`:
55
+
56
+ - Inspects `req.auth_context?.actor_id` and `req.auth_context?.actor_type`.
57
+ - When a `customer_id` is supplied in the consent payload and an authenticated customer session is active, ensures `customer_id === actor_id` (returning `403 Forbidden` on mismatch) so a customer can never modify another customer's consent or Telegram linkage.
58
+
59
+ ### Inbound Webhook Raw Body Preservation (`preserveRawBody: true`)
60
+
61
+ `src/api/middlewares.ts` registers `bodyParser: { preserveRawBody: true }` on `/store/mpn/webhooks/*` so `POST /store/mpn/webhooks/:provider` can verify cryptographic HMAC signatures (`X-Hub-Signature-256`, `X-Webhook-Signature`, `X-Signature`) against the exact unparsed byte stream via `verifyWebhookHmacSignature` (`crypto.timingSafeEqual`).
62
+
63
+ ---
64
+
65
+ ## Admin Endpoints
66
+
67
+ | Method | Endpoint | Schema (`validators.ts`) | Description |
68
+ | -------- | ---------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
69
+ | `GET` | `/admin/mpn/templates` | Query: `channel`, `source`, `locale` | Lists all System Presets (`system_*`), External Templates (`external_*`), and DB Templates (`mpntpl_*`). |
70
+ | `POST` | `/admin/mpn/templates` | `AdminCreateTemplateSchema` | Clones a system preset (`clone_from_preset_id`) into DB rows or creates a new custom template with blocks. |
71
+ | `GET` | `/admin/mpn/templates/:id` | Path: `id` | Returns `{ template, blocks, flatBlocks }` for a preset or DB template. |
72
+ | `POST` | `/admin/mpn/templates/:id` | `AdminUpdateTemplateSchema` | Updates template fields (`label`, `mode`, `subject`, `raw_content`, `variable_map`, `meta_template_name`) and replaces its block tree if `blocks` is provided. |
73
+ | `DELETE` | `/admin/mpn/templates/:id` | Path: `id` | Deletes a DB template (`mpntpl_*`) and its associated `mpn_template_block` rows (system presets cannot be deleted). |
74
+ | `POST` | `/admin/mpn/render-template` | `AdminRenderPreviewSchema` | Renders a stored `template_id` or ad-hoc `blocks`/`raw_content` against live entity data (`entity_id`) or mock context (`order`, `shipment`, `inventory-level`, `product`, `cart`, `document`, `otp`). |
75
+ | `GET` | `/admin/mpn/mappings` | — | Returns `{ mappings, supported_events, recipient_rules, sender_profiles }`. |
76
+ | `POST` | `/admin/mpn/mappings` | `AdminUpsertEventMappingSchema` | Creates a new `mpn_event_mapping` rule. |
77
+ | `POST` | `/admin/mpn/mappings/:id` | `AdminUpsertEventMappingSchema.partial()` | Updates an existing `mpn_event_mapping` rule. |
78
+ | `DELETE` | `/admin/mpn/mappings/:id` | Path: `id` | Deletes an event mapping rule. |
79
+ | `GET` | `/admin/mpn/channels` | — | Returns `{ channels, sender_profiles, supported_drivers }`. |
80
+ | `POST` | `/admin/mpn/channels` | `AdminUpdateChannelConfigSchema` | Saves operational channel settings (`driver`, `is_enabled`, `rate_limit_per_sec`, `config_json`) or runs `action: "verify"` to dispatch a test verification message. |
81
+ | `GET` | `/admin/mpn/logs` | Query: `channel`, `event_name`, `entity_id`, `status`, `limit` | Lists PII-masked `mpn_delivery_log` records sorted newest-first. |
82
+ | `GET` | `/admin/mpn/broadcasts` | — | Lists `mpn_broadcast_campaign` rows and consent summary statistics (`opted_in_count`, `opted_out_count`). |
83
+ | `POST` | `/admin/mpn/broadcasts` | `AdminCreateBroadcastSchema` | Creates a broadcast campaign and dispatches immediately when `send_now: true`. |
84
+ | `GET` | `/admin/mpn/sandbox` | Query: `channel`, `recipient`, `limit` | Lists captured `mpn_sandbox_message` entries and extracted OTP codes. |
85
+ | `DELETE` | `/admin/mpn/sandbox` | — | Clears all DB and in-memory sandbox messages. |
86
+ | `POST` | `/admin/mpn/send` | `AdminManualSendSchema` | Dispatches an ad-hoc notification (`channel` + `recipient`) or triggers all active mappings for an `event_name`, with optional `attach_invoice_pdf`. |
87
+ | `GET` | `/admin/plugin` | — | Returns plugin status and module identifier. |
88
+
89
+ ---
90
+
91
+ ## Storefront & Webhook Endpoints
92
+
93
+ | Method | Endpoint | Schema / Auth | Description |
94
+ | ------ | ------------------------------- | ------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
95
+ | `POST` | `/store/mpn/consent` | `StoreConsentSchema` + `verifyStoreCustomerSession` | Records `opted_in` or `opted_out` consent in `mpn_consent_record` for `sms`, `whatsapp`, or `telegram`. |
96
+ | `GET` | `/store/mpn/webhooks/:provider` | Query: `hub.mode`, `hub.verify_token`, `hub.challenge` | Responds to Meta WhatsApp Cloud API webhook verification challenges against `WHATSAPP_WEBHOOK_VERIFY_TOKEN`. |
97
+ | `POST` | `/store/mpn/webhooks/:provider` | `preserveRawBody: true` + HMAC / `X-Telegram-Bot-Api-Secret-Token` | Processes inbound `whatsapp`/`openwa`/`waha`/`evolution` (`JOIN`/`STOP` + status receipts), `telegram` (`/start <customer_id>`, `/stop`), and `sms`/`signalhouse`/`twilio` (`JOIN`/`STOP`) webhooks. |
98
+ | `GET` | `/store/plugin` | Public | Storefront health check endpoint. |