@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.
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 +340 -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 +133 -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 +589 -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,101 @@
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+
3
+ import type NotificationsModuleService from "../../../../../modules/notifications/service"
4
+ import { NOTIFICATIONS_MODULE } from "../../../../../modules/notifications/types"
5
+ import { UpdateTemplateSchema } from "../../../../validators"
6
+
7
+ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
8
+ const mpnService = req.scope.resolve(
9
+ NOTIFICATIONS_MODULE
10
+ ) as NotificationsModuleService
11
+ const id = req.params.id
12
+
13
+ const result = await mpnService.getTemplateWithBlocks(id)
14
+ if (!result) {
15
+ return res.status(404).json({
16
+ success: false,
17
+ code: "NOT_FOUND",
18
+ message: `Template '${id}' not found`,
19
+ })
20
+ }
21
+
22
+ return res.json({
23
+ success: true,
24
+ template: result.template,
25
+ blocks: result.blocks,
26
+ flat_blocks: result.flatBlocks,
27
+ })
28
+ }
29
+
30
+ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
31
+ const id = req.params.id
32
+ if (id.startsWith("system_")) {
33
+ return res.status(400).json({
34
+ success: false,
35
+ code: "SYSTEM_TEMPLATE_READONLY",
36
+ message:
37
+ "System templates are read-only. Clone the system preset to DB first.",
38
+ })
39
+ }
40
+
41
+ const parsed = UpdateTemplateSchema.safeParse(req.body || {})
42
+ if (!parsed.success) {
43
+ return res.status(400).json({
44
+ success: false,
45
+ code: "VALIDATION_ERROR",
46
+ message: "Invalid template update payload",
47
+ details: parsed.error.flatten(),
48
+ })
49
+ }
50
+
51
+ const mpnService = req.scope.resolve(
52
+ NOTIFICATIONS_MODULE
53
+ ) as NotificationsModuleService
54
+ const { blocks, ...templateFields } = parsed.data
55
+
56
+ const updated = await mpnService.updateMpnTemplates({
57
+ id,
58
+ ...templateFields,
59
+ })
60
+
61
+ if (Array.isArray(blocks)) {
62
+ await mpnService.replaceTemplateBlocks(id, blocks)
63
+ }
64
+
65
+ const full = await mpnService.getTemplateWithBlocks(id)
66
+ return res.json({
67
+ success: true,
68
+ template: full?.template || updated,
69
+ blocks: full?.blocks || [],
70
+ })
71
+ }
72
+
73
+ export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
74
+ const id = req.params.id
75
+ if (id.startsWith("system_")) {
76
+ return res.status(400).json({
77
+ success: false,
78
+ code: "SYSTEM_TEMPLATE_READONLY",
79
+ message: "System templates cannot be deleted",
80
+ })
81
+ }
82
+
83
+ const mpnService = req.scope.resolve(
84
+ NOTIFICATIONS_MODULE
85
+ ) as NotificationsModuleService
86
+
87
+ const existingBlocks = await mpnService.listMpnTemplateBlocks({
88
+ template_id: id,
89
+ })
90
+ if (existingBlocks.length > 0) {
91
+ await mpnService.deleteMpnTemplateBlocks(
92
+ existingBlocks.map((b: any) => b.id)
93
+ )
94
+ }
95
+ await mpnService.deleteMpnTemplates([id])
96
+
97
+ return res.json({
98
+ success: true,
99
+ deleted: id,
100
+ })
101
+ }
@@ -0,0 +1,99 @@
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+
3
+ import type NotificationsModuleService from "../../../../modules/notifications/service"
4
+ import {
5
+ NOTIFICATIONS_MODULE,
6
+ type NotificationChannel,
7
+ } from "../../../../modules/notifications/types"
8
+ import { CreateOrCloneTemplateSchema } from "../../../validators"
9
+
10
+ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
11
+ const mpnService = req.scope.resolve(
12
+ NOTIFICATIONS_MODULE
13
+ ) as NotificationsModuleService
14
+
15
+ const channel =
16
+ typeof req.query.channel === "string"
17
+ ? (req.query.channel as NotificationChannel)
18
+ : undefined
19
+ const source =
20
+ typeof req.query.source === "string"
21
+ ? (req.query.source as "system" | "db" | "external")
22
+ : undefined
23
+ const locale =
24
+ typeof req.query.locale === "string" ? req.query.locale : undefined
25
+
26
+ const templates = await mpnService.listAllTemplates({
27
+ channel,
28
+ source,
29
+ locale,
30
+ })
31
+
32
+ return res.json({
33
+ templates,
34
+ count: templates.length,
35
+ })
36
+ }
37
+
38
+ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
39
+ const parsed = CreateOrCloneTemplateSchema.safeParse(req.body || {})
40
+ if (!parsed.success) {
41
+ return res.status(400).json({
42
+ success: false,
43
+ code: "VALIDATION_ERROR",
44
+ message: "Invalid template payload",
45
+ details: parsed.error.flatten(),
46
+ })
47
+ }
48
+
49
+ const mpnService = req.scope.resolve(
50
+ NOTIFICATIONS_MODULE
51
+ ) as NotificationsModuleService
52
+ const input = parsed.data
53
+
54
+ if (input.clone_from_preset_id) {
55
+ const cloned = await mpnService.cloneSystemPresetToDb(
56
+ input.clone_from_preset_id,
57
+ {
58
+ name: input.name,
59
+ label: input.label,
60
+ locale: input.locale,
61
+ }
62
+ )
63
+ return res.status(201).json({
64
+ success: true,
65
+ template: cloned.template,
66
+ blocks: cloned.blocks,
67
+ })
68
+ }
69
+
70
+ const created = await mpnService.createMpnTemplates({
71
+ name: input.name || `template-${Date.now()}`,
72
+ label: input.label || input.name || "Untitled Template",
73
+ description: input.description ?? null,
74
+ channel: input.channel,
75
+ mode: input.mode,
76
+ locale: input.locale,
77
+ subject: input.subject ?? null,
78
+ raw_content: input.raw_content ?? null,
79
+ variable_map: input.variable_map ?? null,
80
+ meta_template_name: input.meta_template_name ?? null,
81
+ meta_language_code: input.meta_language_code ?? null,
82
+ styles: input.styles ?? null,
83
+ is_active: input.is_active ?? true,
84
+ })
85
+
86
+ let blocksTree: any[] = []
87
+ if (Array.isArray(input.blocks) && input.blocks.length > 0) {
88
+ blocksTree = await mpnService.replaceTemplateBlocks(
89
+ created.id,
90
+ input.blocks
91
+ )
92
+ }
93
+
94
+ return res.status(201).json({
95
+ success: true,
96
+ template: { ...created, source: "db" },
97
+ blocks: blocksTree,
98
+ })
99
+ }
@@ -0,0 +1,5 @@
1
+ import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+
3
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
4
+ res.sendStatus(200)
5
+ }
@@ -0,0 +1,42 @@
1
+ import {
2
+ defineMiddlewares,
3
+ type MedusaNextFunction,
4
+ type MedusaRequest,
5
+ type MedusaResponse,
6
+ } from "@medusajs/framework/http"
7
+
8
+ export * from "./validators"
9
+
10
+ /**
11
+ * Storefront Anti-IDOR verification: ensures an authenticated customer can only manage
12
+ * their own consent/notification preferences when `customer_id` is present.
13
+ */
14
+ export function verifyStoreCustomerSession(req: MedusaRequest): {
15
+ authenticated: boolean
16
+ customerId: string | null
17
+ } {
18
+ const actorId = (req as any).auth_context?.actor_id
19
+ const actorType = (req as any).auth_context?.actor_type
20
+ if (actorId && (!actorType || actorType === "customer")) {
21
+ return { authenticated: true, customerId: String(actorId) }
22
+ }
23
+ return { authenticated: false, customerId: null }
24
+ }
25
+
26
+ export default defineMiddlewares({
27
+ routes: [
28
+ {
29
+ matcher: "/store/mpn/webhooks/*",
30
+ bodyParser: { preserveRawBody: true },
31
+ middlewares: [
32
+ (
33
+ _req: MedusaRequest,
34
+ _res: MedusaResponse,
35
+ next: MedusaNextFunction
36
+ ) => {
37
+ next()
38
+ },
39
+ ],
40
+ },
41
+ ],
42
+ })
@@ -0,0 +1,45 @@
1
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+
3
+ import type NotificationsModuleService from "../../../../modules/notifications/service"
4
+ import { NOTIFICATIONS_MODULE } from "../../../../modules/notifications/types"
5
+ import { maskRecipientPii } from "../../../../modules/notifications/utils/security"
6
+ import { verifyStoreCustomerSession } from "../../../middlewares"
7
+ import { StoreConsentSchema } from "../../../validators"
8
+
9
+ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
10
+ const parsed = StoreConsentSchema.safeParse(req.body || {})
11
+ if (!parsed.success) {
12
+ return res.status(400).json({
13
+ success: false,
14
+ code: "VALIDATION_ERROR",
15
+ message: "Invalid consent request",
16
+ details: parsed.error.flatten(),
17
+ })
18
+ }
19
+
20
+ const session = verifyStoreCustomerSession(req)
21
+ const mpnService = req.scope.resolve(
22
+ NOTIFICATIONS_MODULE
23
+ ) as NotificationsModuleService
24
+
25
+ const record = await mpnService.upsertConsent({
26
+ identifier: parsed.data.identifier,
27
+ channel: parsed.data.channel,
28
+ customer_id: session.customerId,
29
+ action: parsed.data.action,
30
+ source: "checkout",
31
+ metadata: parsed.data.cart_id
32
+ ? { cart_id: parsed.data.cart_id }
33
+ : undefined,
34
+ })
35
+
36
+ return res.json({
37
+ success: true,
38
+ consent: {
39
+ id: record.id,
40
+ channel: record.channel,
41
+ identifier_masked: maskRecipientPii(record.identifier),
42
+ status: record.status,
43
+ },
44
+ })
45
+ }
@@ -0,0 +1,119 @@
1
+ import { createHash, timingSafeEqual } from "node:crypto"
2
+ import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
3
+ import type NotificationsModuleService from "../../../../../modules/notifications/service"
4
+ import { NOTIFICATIONS_MODULE } from "../../../../../modules/notifications/types"
5
+ import { verifyWebhookHmacSignature } from "../../../../../modules/notifications/utils/security"
6
+
7
+ function timingSafeStringEqual(a: string, b: string): boolean {
8
+ const hashA = new Uint8Array(createHash("sha256").update(a).digest())
9
+ const hashB = new Uint8Array(createHash("sha256").update(b).digest())
10
+ return timingSafeEqual(hashA, hashB)
11
+ }
12
+
13
+ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
14
+ const provider = (req.params.provider || "").toLowerCase()
15
+ const mpnService = req.scope.resolve(
16
+ NOTIFICATIONS_MODULE
17
+ ) as NotificationsModuleService
18
+
19
+ // Verify webhook signature when signing secret is configured
20
+ const signingSecret =
21
+ provider === "signalhouse"
22
+ ? process.env.SIGNALHOUSE_WEBHOOK_SIGNING_SECRET
23
+ : provider === "telegram"
24
+ ? process.env.TELEGRAM_WEBHOOK_SECRET
25
+ : process.env.MPN_WEBHOOK_SIGNING_SECRET
26
+
27
+ if (!signingSecret && process.env.NODE_ENV === "production") {
28
+ return res.status(500).json({
29
+ success: false,
30
+ code: "WEBHOOK_SECRET_NOT_CONFIGURED",
31
+ message: "Webhook signing secret is not configured",
32
+ })
33
+ }
34
+
35
+ if (signingSecret) {
36
+ if (provider === "telegram") {
37
+ const tgSecretHeader = String(
38
+ req.headers["x-telegram-bot-api-secret-token"] ?? ""
39
+ )
40
+ if (
41
+ !tgSecretHeader ||
42
+ !timingSafeStringEqual(tgSecretHeader, signingSecret)
43
+ ) {
44
+ return res.status(401).json({
45
+ success: false,
46
+ code: "INVALID_WEBHOOK_SIGNATURE",
47
+ message: "Telegram webhook secret token mismatch",
48
+ })
49
+ }
50
+ } else {
51
+ const rawBody = (req as any).rawBody || JSON.stringify(req.body || {})
52
+ const sigHeader =
53
+ (req.headers["x-signalhouse-signature"] as string) ||
54
+ (req.headers["x-webhook-signature"] as string) ||
55
+ (req.headers["x-hub-signature-256"] as string)
56
+ if (!verifyWebhookHmacSignature(rawBody, sigHeader, signingSecret)) {
57
+ return res.status(401).json({
58
+ success: false,
59
+ code: "INVALID_WEBHOOK_SIGNATURE",
60
+ message: "Webhook HMAC signature verification failed",
61
+ })
62
+ }
63
+ }
64
+ }
65
+
66
+ const body = (req.body || {}) as Record<string, any>
67
+
68
+ // 1. Telegram Bot `/start <customer_id>` deep-link opt-in & chat_id linking
69
+ if (provider === "telegram" && body.message?.text) {
70
+ const chatId = String(body.message.chat?.id || "")
71
+ const text = String(body.message.text || "").trim()
72
+ if (chatId && text.startsWith("/start")) {
73
+ const tokenArg = text.split(/\s+/)[1] || null
74
+ const record = await mpnService.upsertConsent({
75
+ identifier: chatId,
76
+ channel: "telegram",
77
+ customer_id: tokenArg,
78
+ action: "opt_in",
79
+ source: "telegram_bot_start",
80
+ metadata: {
81
+ username: body.message.from?.username || null,
82
+ },
83
+ })
84
+ return res.json({
85
+ success: true,
86
+ action: "telegram_linked",
87
+ consent_id: record.id,
88
+ })
89
+ }
90
+ }
91
+
92
+ // 2. Inbound SMS / WhatsApp JOIN / STOP keyword handling (Signal House, Twilio, OpenWA/WAHA)
93
+ const fromPhone =
94
+ body.from || body.From || body.data?.from || body.sender || ""
95
+ const messageText =
96
+ body.text || body.Body || body.message || body.data?.body || ""
97
+
98
+ if (fromPhone && messageText) {
99
+ const channel =
100
+ provider === "whatsapp" || provider === "openwa" || provider === "waha"
101
+ ? "whatsapp"
102
+ : "sms"
103
+ const result = await mpnService.processInboundConsentKeyword({
104
+ from: String(fromPhone),
105
+ body: String(messageText),
106
+ channel,
107
+ })
108
+
109
+ return res.json({
110
+ success: true,
111
+ ...result,
112
+ })
113
+ }
114
+
115
+ return res.json({
116
+ success: true,
117
+ handled: false,
118
+ })
119
+ }
@@ -0,0 +1,5 @@
1
+ import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
2
+
3
+ export async function GET(req: MedusaRequest, res: MedusaResponse) {
4
+ res.sendStatus(200)
5
+ }
@@ -0,0 +1,149 @@
1
+ import { z } from "zod"
2
+ import {
3
+ BLOCK_TYPES,
4
+ CONTEXT_TYPES,
5
+ NOTIFICATION_CHANNELS,
6
+ RECIPIENT_RULES,
7
+ SMS_DRIVERS,
8
+ SUPPORTED_LOCALES,
9
+ TEMPLATE_MODES,
10
+ WHATSAPP_DRIVERS,
11
+ } from "../modules/notifications/types"
12
+
13
+ export const BlockInputSchema = z.object({
14
+ id: z.string().max(120).optional(),
15
+ type: z.enum(BLOCK_TYPES),
16
+ position: z.number().int().min(0).default(0),
17
+ parent_id: z.string().max(120).nullable().optional(),
18
+ metadata: z.record(z.string(), z.unknown()).optional().default({}),
19
+ })
20
+
21
+ export const CreateOrCloneTemplateSchema = z.object({
22
+ clone_from_preset_id: z.string().max(120).optional(),
23
+ name: z.string().min(1).max(150).optional(),
24
+ label: z.string().min(1).max(200).optional(),
25
+ description: z.string().max(500).nullable().optional(),
26
+ channel: z.enum(NOTIFICATION_CHANNELS).optional().default("email"),
27
+ mode: z.enum(TEMPLATE_MODES).optional().default("blocks"),
28
+ locale: z.enum(SUPPORTED_LOCALES).optional().default("en"),
29
+ subject: z.string().max(300).nullable().optional(),
30
+ raw_content: z.string().max(250000).nullable().optional(),
31
+ variable_map: z.record(z.string(), z.string()).nullable().optional(),
32
+ meta_template_name: z.string().max(120).nullable().optional(),
33
+ meta_language_code: z.string().max(16).nullable().optional(),
34
+ styles: z.record(z.string(), z.unknown()).nullable().optional(),
35
+ is_active: z.boolean().optional().default(true),
36
+ blocks: z.array(BlockInputSchema).optional(),
37
+ })
38
+
39
+ export const UpdateTemplateSchema = z.object({
40
+ name: z.string().min(1).max(150).optional(),
41
+ label: z.string().min(1).max(200).optional(),
42
+ description: z.string().max(500).nullable().optional(),
43
+ channel: z.enum(NOTIFICATION_CHANNELS).optional(),
44
+ mode: z.enum(TEMPLATE_MODES).optional(),
45
+ locale: z.enum(SUPPORTED_LOCALES).optional(),
46
+ subject: z.string().max(300).nullable().optional(),
47
+ raw_content: z.string().max(250000).nullable().optional(),
48
+ variable_map: z.record(z.string(), z.string()).nullable().optional(),
49
+ meta_template_name: z.string().max(120).nullable().optional(),
50
+ meta_language_code: z.string().max(16).nullable().optional(),
51
+ styles: z.record(z.string(), z.unknown()).nullable().optional(),
52
+ is_active: z.boolean().optional(),
53
+ blocks: z.array(BlockInputSchema).optional(),
54
+ })
55
+
56
+ export const RenderTemplatePreviewSchema = z.object({
57
+ template_id: z.string().min(1).max(120),
58
+ context_type: z.enum(CONTEXT_TYPES).optional().default("order"),
59
+ entity_id: z.string().max(120).nullable().optional(),
60
+ locale: z.string().max(16).nullable().optional(),
61
+ subject: z.string().max(300).nullable().optional(),
62
+ data: z.record(z.string(), z.unknown()).optional(),
63
+ append_sms_consent_footer: z.boolean().optional(),
64
+ })
65
+
66
+ export const UpsertEventMappingSchema = z.object({
67
+ event_name: z.string().min(1).max(120),
68
+ channel: z.enum(NOTIFICATION_CHANNELS).default("email"),
69
+ template_id: z.string().min(1).max(120),
70
+ recipient_rule: z.enum(RECIPIENT_RULES).optional().default("customer_auto"),
71
+ custom_recipient: z.string().max(255).nullable().optional(),
72
+ sender_profile_index: z.number().int().min(1).max(5).optional().default(1),
73
+ telegram_thread_id: z.number().int().positive().nullable().optional(),
74
+ attach_document_types: z.array(z.string().max(40)).nullable().optional(),
75
+ variable_map: z.record(z.string(), z.string()).nullable().optional(),
76
+ conditions: z
77
+ .object({
78
+ region_ids: z.array(z.string()).optional(),
79
+ sales_channel_ids: z.array(z.string()).optional(),
80
+ currency_codes: z.array(z.string()).optional(),
81
+ min_order_total: z.number().min(0).optional(),
82
+ document_types: z.array(z.string()).optional(),
83
+ require_consent: z.boolean().optional(),
84
+ })
85
+ .nullable()
86
+ .optional(),
87
+ is_active: z.boolean().optional().default(true),
88
+ priority: z.number().int().min(-100).max(1000).optional().default(0),
89
+ })
90
+
91
+ export const UpdateChannelConfigSchema = z.object({
92
+ channel: z.enum(NOTIFICATION_CHANNELS),
93
+ driver: z
94
+ .union([z.enum(WHATSAPP_DRIVERS), z.enum(SMS_DRIVERS), z.string().max(64)])
95
+ .optional(),
96
+ is_enabled: z.boolean().optional(),
97
+ rate_limit_per_sec: z.number().int().min(1).max(500).optional(),
98
+ config_json: z.record(z.string(), z.unknown()).optional(),
99
+ action: z.enum(["save", "verify"]).optional().default("save"),
100
+ test_recipient: z.string().max(255).optional(),
101
+ })
102
+
103
+ export const ManualSendSchema = z.object({
104
+ channel: z.enum(NOTIFICATION_CHANNELS).optional(),
105
+ template_id: z.string().min(1).max(120).optional(),
106
+ event_name: z.string().max(120).optional(),
107
+ recipient: z.string().max(255).optional(),
108
+ order_id: z.string().max(120).nullable().optional(),
109
+ entity_id: z.string().max(120).nullable().optional(),
110
+ locale: z.string().max(16).optional(),
111
+ attach_document_types: z.array(z.string().max(40)).optional(),
112
+ telegram_thread_id: z.number().int().positive().nullable().optional(),
113
+ custom_data: z.record(z.string(), z.unknown()).optional(),
114
+ })
115
+
116
+ export const CreateBroadcastSchema = z.object({
117
+ name: z.string().min(1).max(150),
118
+ channel: z.enum(["sms", "whatsapp", "telegram", "email"]).default("sms"),
119
+ template_id: z.string().min(1).max(120),
120
+ customer_group_id: z.string().max(120).nullable().optional(),
121
+ custom_recipients: z
122
+ .array(z.string().max(255))
123
+ .max(500)
124
+ .nullable()
125
+ .optional(),
126
+ send_now: z.boolean().optional().default(false),
127
+ scheduled_at: z.string().nullable().optional(),
128
+ })
129
+
130
+ export const StoreConsentSchema = z.object({
131
+ identifier: z.string().min(3).max(255),
132
+ channel: z.enum(["sms", "whatsapp", "telegram"]).default("sms"),
133
+ action: z.enum(["opt_in", "opt_out"]).default("opt_in"),
134
+ cart_id: z.string().max(120).nullable().optional(),
135
+ })
136
+
137
+ export type BlockInput = z.infer<typeof BlockInputSchema>
138
+ export type CreateOrCloneTemplateInput = z.infer<
139
+ typeof CreateOrCloneTemplateSchema
140
+ >
141
+ export type UpdateTemplateInput = z.infer<typeof UpdateTemplateSchema>
142
+ export type RenderTemplatePreviewInput = z.infer<
143
+ typeof RenderTemplatePreviewSchema
144
+ >
145
+ export type UpsertEventMappingInput = z.infer<typeof UpsertEventMappingSchema>
146
+ export type UpdateChannelConfigInput = z.infer<typeof UpdateChannelConfigSchema>
147
+ export type ManualSendInput = z.infer<typeof ManualSendSchema>
148
+ export type CreateBroadcastInput = z.infer<typeof CreateBroadcastSchema>
149
+ export type StoreConsentInput = z.infer<typeof StoreConsentSchema>
@@ -0,0 +1,14 @@
1
+ export * from "./use-sdk-primitives"
2
+ export * from "./use-mpn-templates"
3
+ export * from "./use-mpn-mappings"
4
+ export * from "./use-mpn-channels"
5
+ export * from "./use-mpn-dispatch"
6
+ export * from "./use-order-notifications-widget-state"
7
+ export type {
8
+ NotificationsMutationHookOptions,
9
+ NotificationsQueryHookOptions,
10
+ StatusBannerState,
11
+ UseNotificationsMutationResult,
12
+ UseNotificationsQueryResult,
13
+ UseOrderNotificationsWidgetStateProps,
14
+ } from "../types"
@@ -0,0 +1,43 @@
1
+ import type {
2
+ ListMpnChannelsResponse,
3
+ NotificationsMutationHookOptions,
4
+ NotificationsQueryHookOptions,
5
+ SaveOrVerifyMpnChannelPayload,
6
+ SaveOrVerifyMpnChannelResponse,
7
+ } from "../types"
8
+ import { notificationsAdminSdk } from "../sdk/client"
9
+ import { notificationsQueryKeys } from "../sdk/query-keys"
10
+ import {
11
+ useNotificationsMutation,
12
+ useNotificationsQuery,
13
+ } from "./use-sdk-primitives"
14
+
15
+ export function useMpnChannels(
16
+ options?: NotificationsQueryHookOptions<ListMpnChannelsResponse>
17
+ ) {
18
+ const client = options?.client ?? notificationsAdminSdk
19
+ const queryKey = options?.queryKey ?? notificationsQueryKeys.channels()
20
+
21
+ return useNotificationsQuery<ListMpnChannelsResponse>(
22
+ queryKey,
23
+ () => client.listChannels(),
24
+ options
25
+ )
26
+ }
27
+
28
+ export function useSaveOrVerifyMpnChannel(
29
+ options?: NotificationsMutationHookOptions<
30
+ SaveOrVerifyMpnChannelResponse,
31
+ SaveOrVerifyMpnChannelPayload
32
+ >
33
+ ) {
34
+ const client = options?.client ?? notificationsAdminSdk
35
+ return useNotificationsMutation<
36
+ SaveOrVerifyMpnChannelResponse,
37
+ SaveOrVerifyMpnChannelPayload
38
+ >(
39
+ (payload) => client.saveOrVerifyChannel(payload),
40
+ options,
41
+ notificationsQueryKeys.channels()
42
+ )
43
+ }