@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,319 @@
1
+ import { MedusaError } from "@medusajs/framework/utils"
2
+ import type {
3
+ ClearMpnSandboxResponse,
4
+ CreateMpnBroadcastPayload,
5
+ CreateMpnBroadcastResponse,
6
+ CreateMpnMappingPayload,
7
+ CreateOrCloneMpnTemplatePayload,
8
+ DeleteMpnMappingResponse,
9
+ DeleteMpnTemplateResponse,
10
+ GetMpnTemplateResponse,
11
+ ListMpnBroadcastsResponse,
12
+ ListMpnChannelsResponse,
13
+ ListMpnLogsParams,
14
+ ListMpnLogsResponse,
15
+ ListMpnMappingsResponse,
16
+ ListMpnSandboxParams,
17
+ ListMpnSandboxResponse,
18
+ ListMpnTemplatesParams,
19
+ ListMpnTemplatesResponse,
20
+ NotificationsApiClientOptions,
21
+ RenderMpnTemplatePayload,
22
+ RenderMpnTemplateResponse,
23
+ SaveMpnMappingResponse,
24
+ SaveMpnTemplateResponse,
25
+ SaveOrVerifyMpnChannelPayload,
26
+ SaveOrVerifyMpnChannelResponse,
27
+ SendMpnNotificationPayload,
28
+ SendMpnNotificationResponse,
29
+ StoreUpdateConsentPayload,
30
+ StoreUpdateConsentResponse,
31
+ UpdateMpnTemplatePayload,
32
+ } from "../types"
33
+
34
+ function buildQueryString(params?: Record<string, unknown>): string {
35
+ if (!params) return ""
36
+ const search = new URLSearchParams()
37
+ for (const [key, value] of Object.entries(params)) {
38
+ if (value === undefined || value === null || value === "") continue
39
+ search.set(key, String(value))
40
+ }
41
+ const str = search.toString()
42
+ return str ? `?${str}` : ""
43
+ }
44
+
45
+ /**
46
+ * Typed SDK client for `medusa-plugin-notifications` (MPN).
47
+ * Supports both `/admin` and `/store` routes with automatic JSON parsing
48
+ * and standard Medusa error extraction.
49
+ */
50
+ export class NotificationsApiClient {
51
+ private readonly baseUrl: string
52
+ private readonly prefix: string
53
+ private readonly options: NotificationsApiClientOptions
54
+
55
+ constructor(
56
+ prefix: "/admin" | "/store" = "/store",
57
+ baseUrl: string = "",
58
+ options: NotificationsApiClientOptions = {}
59
+ ) {
60
+ this.prefix = prefix
61
+ this.baseUrl = baseUrl.replace(/\/$/, "")
62
+ this.options = options
63
+ }
64
+
65
+ public resolveUrl(
66
+ endpoint: string,
67
+ params?: Record<string, unknown>
68
+ ): string {
69
+ const normalizedEndpoint = endpoint.startsWith("/")
70
+ ? endpoint
71
+ : `/${endpoint}`
72
+ return `${this.baseUrl}${this.prefix}${normalizedEndpoint}${buildQueryString(params)}`
73
+ }
74
+
75
+ private buildHeaders(
76
+ customHeaders?: HeadersInit,
77
+ hasJsonBody = true
78
+ ): Record<string, string> {
79
+ const headers: Record<string, string> = {
80
+ ...(hasJsonBody ? { "Content-Type": "application/json" } : {}),
81
+ ...this.options.headers,
82
+ }
83
+
84
+ if (this.options.publishableApiKey) {
85
+ headers["x-publishable-api-key"] = this.options.publishableApiKey
86
+ }
87
+
88
+ if (customHeaders) {
89
+ if (customHeaders instanceof Headers) {
90
+ customHeaders.forEach((v, k) => {
91
+ headers[k] = v
92
+ })
93
+ } else if (Array.isArray(customHeaders)) {
94
+ for (const [k, v] of customHeaders) {
95
+ headers[k] = v
96
+ }
97
+ } else {
98
+ Object.assign(headers, customHeaders)
99
+ }
100
+ }
101
+
102
+ return headers
103
+ }
104
+
105
+ private async fetch<T>(
106
+ endpoint: string,
107
+ options?: RequestInit,
108
+ queryParams?: Record<string, unknown>
109
+ ): Promise<T> {
110
+ const url = this.resolveUrl(endpoint, queryParams)
111
+ const fetchFn = this.options.fetchImpl ?? globalThis.fetch
112
+ const hasJsonBody =
113
+ options?.body !== undefined && !(options.body instanceof FormData)
114
+
115
+ const response = await fetchFn(url, {
116
+ credentials: this.options.credentials ?? "include",
117
+ ...options,
118
+ headers: this.buildHeaders(options?.headers, hasJsonBody),
119
+ })
120
+
121
+ if (response.status === 204) {
122
+ return {} as T
123
+ }
124
+
125
+ const data = await response.json().catch(() => ({}))
126
+
127
+ if (!response.ok) {
128
+ throw new MedusaError(
129
+ MedusaError.Types.UNEXPECTED_STATE,
130
+ (data as { message?: string; detail?: string }).message ||
131
+ (data as { detail?: string }).detail ||
132
+ `Request failed with status ${response.status}`
133
+ )
134
+ }
135
+
136
+ return data as T
137
+ }
138
+
139
+ // ---------------------------------------------------------------------------
140
+ // Admin Templates & Preview Endpoints (/admin/mpn/templates & /render-template)
141
+ // ---------------------------------------------------------------------------
142
+
143
+ async listTemplates(
144
+ params?: ListMpnTemplatesParams
145
+ ): Promise<ListMpnTemplatesResponse> {
146
+ return this.fetch<ListMpnTemplatesResponse>(
147
+ "/mpn/templates",
148
+ { method: "GET" },
149
+ params as Record<string, unknown>
150
+ )
151
+ }
152
+
153
+ async getTemplate(id: string): Promise<GetMpnTemplateResponse> {
154
+ return this.fetch<GetMpnTemplateResponse>(
155
+ `/mpn/templates/${encodeURIComponent(id)}`,
156
+ { method: "GET" }
157
+ )
158
+ }
159
+
160
+ async createOrCloneTemplate(
161
+ payload: CreateOrCloneMpnTemplatePayload
162
+ ): Promise<SaveMpnTemplateResponse> {
163
+ return this.fetch<SaveMpnTemplateResponse>("/mpn/templates", {
164
+ method: "POST",
165
+ body: JSON.stringify(payload),
166
+ })
167
+ }
168
+
169
+ async updateTemplate(
170
+ id: string,
171
+ payload: UpdateMpnTemplatePayload
172
+ ): Promise<SaveMpnTemplateResponse> {
173
+ return this.fetch<SaveMpnTemplateResponse>(
174
+ `/mpn/templates/${encodeURIComponent(id)}`,
175
+ {
176
+ method: "POST",
177
+ body: JSON.stringify(payload),
178
+ }
179
+ )
180
+ }
181
+
182
+ async deleteTemplate(id: string): Promise<DeleteMpnTemplateResponse> {
183
+ return this.fetch<DeleteMpnTemplateResponse>(
184
+ `/mpn/templates/${encodeURIComponent(id)}`,
185
+ { method: "DELETE" }
186
+ )
187
+ }
188
+
189
+ async renderTemplatePreview(
190
+ payload: RenderMpnTemplatePayload
191
+ ): Promise<RenderMpnTemplateResponse> {
192
+ return this.fetch<RenderMpnTemplateResponse>("/mpn/render-template", {
193
+ method: "POST",
194
+ body: JSON.stringify(payload),
195
+ })
196
+ }
197
+
198
+ // ---------------------------------------------------------------------------
199
+ // Admin Event Mappings Endpoints (/admin/mpn/mappings)
200
+ // ---------------------------------------------------------------------------
201
+
202
+ async listMappings(): Promise<ListMpnMappingsResponse> {
203
+ return this.fetch<ListMpnMappingsResponse>("/mpn/mappings", {
204
+ method: "GET",
205
+ })
206
+ }
207
+
208
+ async createMapping(
209
+ payload: CreateMpnMappingPayload
210
+ ): Promise<SaveMpnMappingResponse> {
211
+ return this.fetch<SaveMpnMappingResponse>("/mpn/mappings", {
212
+ method: "POST",
213
+ body: JSON.stringify(payload),
214
+ })
215
+ }
216
+
217
+ async updateMapping(
218
+ id: string,
219
+ payload: Partial<CreateMpnMappingPayload>
220
+ ): Promise<SaveMpnMappingResponse> {
221
+ return this.fetch<SaveMpnMappingResponse>(
222
+ `/mpn/mappings/${encodeURIComponent(id)}`,
223
+ {
224
+ method: "POST",
225
+ body: JSON.stringify(payload),
226
+ }
227
+ )
228
+ }
229
+
230
+ async deleteMapping(id: string): Promise<DeleteMpnMappingResponse> {
231
+ return this.fetch<DeleteMpnMappingResponse>(
232
+ `/mpn/mappings/${encodeURIComponent(id)}`,
233
+ { method: "DELETE" }
234
+ )
235
+ }
236
+
237
+ // ---------------------------------------------------------------------------
238
+ // Admin Channels, Logs, Broadcasts, Sandbox, & Send Endpoints (/admin/mpn/*)
239
+ // ---------------------------------------------------------------------------
240
+
241
+ async listChannels(): Promise<ListMpnChannelsResponse> {
242
+ return this.fetch<ListMpnChannelsResponse>("/mpn/channels", {
243
+ method: "GET",
244
+ })
245
+ }
246
+
247
+ async saveOrVerifyChannel(
248
+ payload: SaveOrVerifyMpnChannelPayload
249
+ ): Promise<SaveOrVerifyMpnChannelResponse> {
250
+ return this.fetch<SaveOrVerifyMpnChannelResponse>("/mpn/channels", {
251
+ method: "POST",
252
+ body: JSON.stringify(payload),
253
+ })
254
+ }
255
+
256
+ async listLogs(params?: ListMpnLogsParams): Promise<ListMpnLogsResponse> {
257
+ return this.fetch<ListMpnLogsResponse>(
258
+ "/mpn/logs",
259
+ { method: "GET" },
260
+ params as Record<string, unknown>
261
+ )
262
+ }
263
+
264
+ async listBroadcasts(): Promise<ListMpnBroadcastsResponse> {
265
+ return this.fetch<ListMpnBroadcastsResponse>("/mpn/broadcasts", {
266
+ method: "GET",
267
+ })
268
+ }
269
+
270
+ async createBroadcast(
271
+ payload: CreateMpnBroadcastPayload
272
+ ): Promise<CreateMpnBroadcastResponse> {
273
+ return this.fetch<CreateMpnBroadcastResponse>("/mpn/broadcasts", {
274
+ method: "POST",
275
+ body: JSON.stringify(payload),
276
+ })
277
+ }
278
+
279
+ async listSandboxMessages(
280
+ params?: ListMpnSandboxParams
281
+ ): Promise<ListMpnSandboxResponse> {
282
+ return this.fetch<ListMpnSandboxResponse>(
283
+ "/mpn/sandbox",
284
+ { method: "GET" },
285
+ params as Record<string, unknown>
286
+ )
287
+ }
288
+
289
+ async clearSandboxMessages(): Promise<ClearMpnSandboxResponse> {
290
+ return this.fetch<ClearMpnSandboxResponse>("/mpn/sandbox", {
291
+ method: "DELETE",
292
+ })
293
+ }
294
+
295
+ async sendNotification(
296
+ payload: SendMpnNotificationPayload
297
+ ): Promise<SendMpnNotificationResponse> {
298
+ return this.fetch<SendMpnNotificationResponse>("/mpn/send", {
299
+ method: "POST",
300
+ body: JSON.stringify(payload),
301
+ })
302
+ }
303
+
304
+ // ---------------------------------------------------------------------------
305
+ // Storefront Consent Endpoint (/store/mpn/consent)
306
+ // ---------------------------------------------------------------------------
307
+
308
+ async updateConsent(
309
+ payload: StoreUpdateConsentPayload
310
+ ): Promise<StoreUpdateConsentResponse> {
311
+ return this.fetch<StoreUpdateConsentResponse>("/mpn/consent", {
312
+ method: "POST",
313
+ body: JSON.stringify(payload),
314
+ })
315
+ }
316
+ }
317
+
318
+ export const notificationsAdminSdk = new NotificationsApiClient("/admin")
319
+ export const notificationsStoreSdk = new NotificationsApiClient("/store")
@@ -0,0 +1,48 @@
1
+ export * from "./client"
2
+ export * from "./query-keys"
3
+ export type {
4
+ ClearMpnSandboxResponse,
5
+ CreateMpnBroadcastPayload,
6
+ CreateMpnBroadcastResponse,
7
+ CreateMpnMappingPayload,
8
+ CreateOrCloneMpnTemplatePayload,
9
+ DeleteMpnMappingResponse,
10
+ DeleteMpnTemplateResponse,
11
+ GetMpnTemplateResponse,
12
+ ListMpnBroadcastsResponse,
13
+ ListMpnChannelsResponse,
14
+ ListMpnLogsParams,
15
+ ListMpnLogsResponse,
16
+ ListMpnMappingsResponse,
17
+ ListMpnSandboxParams,
18
+ ListMpnSandboxResponse,
19
+ ListMpnTemplatesParams,
20
+ ListMpnTemplatesResponse,
21
+ MappedNotificationError,
22
+ MpnBroadcastRecord,
23
+ MpnChannel,
24
+ MpnChannelConfigRecord,
25
+ MpnConsentSummary,
26
+ MpnEventMappingRecord,
27
+ MpnLocale,
28
+ MpnLogRecord,
29
+ MpnRecipientRule,
30
+ MpnSandboxMessageRecord,
31
+ MpnSupportedEvent,
32
+ MpnTemplateBlockRecord,
33
+ MpnTemplateBlockType,
34
+ MpnTemplateMode,
35
+ MpnTemplateRecord,
36
+ NotificationsApiClientOptions,
37
+ RenderMpnTemplatePayload,
38
+ RenderMpnTemplateResponse,
39
+ SaveMpnMappingResponse,
40
+ SaveMpnTemplateResponse,
41
+ SaveOrVerifyMpnChannelPayload,
42
+ SaveOrVerifyMpnChannelResponse,
43
+ SendMpnNotificationPayload,
44
+ SendMpnNotificationResponse,
45
+ StoreUpdateConsentPayload,
46
+ StoreUpdateConsentResponse,
47
+ UpdateMpnTemplatePayload,
48
+ } from "../types"
@@ -0,0 +1,89 @@
1
+ export const NOTIFICATIONS_QUERY_KEY = "notifications" as const
2
+
3
+ type QueryInvalidationListener = (
4
+ invalidatedKeyPrefix: readonly unknown[]
5
+ ) => void
6
+
7
+ const invalidationListeners = new Set<QueryInvalidationListener>()
8
+
9
+ /**
10
+ * Hierarchical query key factory for `medusa-plugin-notifications`.
11
+ * Follows the same factory conventions as `boldQueryKeys` in `vicacha-medusa-plugins`.
12
+ */
13
+ export const notificationsQueryKeys = {
14
+ all: [NOTIFICATIONS_QUERY_KEY] as const,
15
+
16
+ studio: () => [...notificationsQueryKeys.all, "studio"] as const,
17
+
18
+ templatesAll: () => [...notificationsQueryKeys.all, "templates"] as const,
19
+ templates: (params?: Record<string, unknown>) =>
20
+ params
21
+ ? ([...notificationsQueryKeys.templatesAll(), params] as const)
22
+ : notificationsQueryKeys.templatesAll(),
23
+ template: (id: string) =>
24
+ [...notificationsQueryKeys.templatesAll(), "detail", id] as const,
25
+
26
+ mappingsAll: () => [...notificationsQueryKeys.all, "mappings"] as const,
27
+ mappings: (params?: Record<string, unknown>) =>
28
+ params
29
+ ? ([...notificationsQueryKeys.mappingsAll(), params] as const)
30
+ : notificationsQueryKeys.mappingsAll(),
31
+
32
+ channels: () => [...notificationsQueryKeys.all, "channels"] as const,
33
+
34
+ logsAll: () => [...notificationsQueryKeys.all, "logs"] as const,
35
+ logs: (params?: Record<string, unknown>) =>
36
+ params
37
+ ? ([...notificationsQueryKeys.logsAll(), params] as const)
38
+ : notificationsQueryKeys.logsAll(),
39
+ orderLogs: (orderId: string) =>
40
+ [...notificationsQueryKeys.logsAll(), "order", orderId] as const,
41
+
42
+ broadcasts: () => [...notificationsQueryKeys.all, "broadcasts"] as const,
43
+
44
+ sandbox: (params?: Record<string, unknown>) =>
45
+ params
46
+ ? ([...notificationsQueryKeys.all, "sandbox", params] as const)
47
+ : ([...notificationsQueryKeys.all, "sandbox"] as const),
48
+ }
49
+
50
+ export function matchesNotificationsQueryKeyPrefix(
51
+ candidateKey: readonly unknown[],
52
+ prefixKey: readonly unknown[]
53
+ ): boolean {
54
+ if (prefixKey.length > candidateKey.length) {
55
+ return false
56
+ }
57
+ for (let i = 0; i < prefixKey.length; i++) {
58
+ const a = candidateKey[i]
59
+ const b = prefixKey[i]
60
+ if (a === b) continue
61
+ if (
62
+ typeof a === "object" &&
63
+ a !== null &&
64
+ typeof b === "object" &&
65
+ b !== null
66
+ ) {
67
+ if (JSON.stringify(a) === JSON.stringify(b)) continue
68
+ }
69
+ return false
70
+ }
71
+ return true
72
+ }
73
+
74
+ export function invalidateNotificationsQueries(
75
+ keyPrefix: readonly unknown[] = notificationsQueryKeys.all
76
+ ): void {
77
+ for (const listener of invalidationListeners) {
78
+ listener(keyPrefix)
79
+ }
80
+ }
81
+
82
+ export function subscribeNotificationsQueryInvalidation(
83
+ listener: QueryInvalidationListener
84
+ ): () => void {
85
+ invalidationListeners.add(listener)
86
+ return () => {
87
+ invalidationListeners.delete(listener)
88
+ }
89
+ }
@@ -0,0 +1,50 @@
1
+ # Subscribers
2
+
3
+ Event subscribers that listen to 25 Medusa core and custom plugin events and trigger database-driven notification mappings via `executeEventNotificationsDispatch`.
4
+
5
+ ```sh
6
+ subscribers/
7
+ ├── order-events.ts 9 order.* lifecycle events
8
+ ├── fulfillment-and-inventory-events.ts 6 shipment, delivery, fulfillment, and inventory events
9
+ └── customer-and-document-events.ts 10 customer, document, auth OTP, loyalty, wallet, membership & contact events
10
+ ```
11
+
12
+ ---
13
+
14
+ ## Conventions
15
+
16
+ - Every subscriber delegates to `executeEventNotificationsDispatch(container, { event_name, event_data })` from `src/workflows/index.ts`.
17
+ - All subscribers wrap execution in a non-blocking `try / catch` block so store checkout, fulfillment, or customer creation workflows never fail if an external notification gateway is unreachable.
18
+ - `fulfillment-and-inventory-events.ts` normalizes `fulfillment.shipment_created` → `shipment.created` and `fulfillment.delivery_created` → `delivery.created` so merchants only need to configure a single canonical event mapping.
19
+
20
+ ---
21
+
22
+ ## Subscribed Events Reference
23
+
24
+ | Subscriber File | Event Name | Default Hydrated Context | Typical Channels & Presets |
25
+ | ------------------------------------- | ------------------------------ | --------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
26
+ | `order-events.ts` | `order.placed` | `order` | Email (`system_email_order_placed`), WhatsApp (`system_whatsapp_order_placed`), Telegram (`system_telegram_order_placed`), SMS (`system_sms_order_placed`), Slack (`system_slack_order_placed`) |
27
+ | `order-events.ts` | `order.completed` | `order` | Email, WhatsApp, Telegram, SMS |
28
+ | `order-events.ts` | `order.canceled` | `order` | Email, WhatsApp, Telegram, SMS |
29
+ | `order-events.ts` | `order.updated` | `order` | Email, Telegram |
30
+ | `order-events.ts` | `order.fulfillment_created` | `order` | Email, WhatsApp, SMS |
31
+ | `order-events.ts` | `order.fulfillment_canceled` | `order` | Email, Telegram |
32
+ | `order-events.ts` | `order.return_requested` | `order` | Email, Telegram, Slack |
33
+ | `order-events.ts` | `order.return_received` | `order` | Email, Telegram |
34
+ | `order-events.ts` | `order.refund_created` | `order` | Email (with Credit Note PDF), WhatsApp, SMS |
35
+ | `fulfillment-and-inventory-events.ts` | `shipment.created` | `shipment` | Email (`system_email_shipment_created`), WhatsApp (`system_whatsapp_shipment_created`), SMS (`system_sms_shipment_created`) |
36
+ | `fulfillment-and-inventory-events.ts` | `delivery.created` | `shipment` | Email, WhatsApp, SMS |
37
+ | `fulfillment-and-inventory-events.ts` | `fulfillment.created` | `shipment` | Email, Telegram |
38
+ | `fulfillment-and-inventory-events.ts` | `fulfillment.shipment_created` | `shipment` (normalized to `shipment.created`) | Email, WhatsApp, SMS |
39
+ | `fulfillment-and-inventory-events.ts` | `fulfillment.delivery_created` | `shipment` (normalized to `delivery.created`) | Email, WhatsApp, SMS |
40
+ | `fulfillment-and-inventory-events.ts` | `inventory-level.updated` | `inventory-level` | Email (`system_email_inventory_low`), Telegram (`system_telegram_inventory_low`), Slack |
41
+ | `customer-and-document-events.ts` | `customer.created` | `custom` | Email, WhatsApp, Telegram |
42
+ | `customer-and-document-events.ts` | `customer.updated` | `custom` | Email |
43
+ | `customer-and-document-events.ts` | `document.generated` | `document` | Email (`system_email_document_ready`), WhatsApp, Telegram (auto-attaches generated PDF from `medusa-plugin-documents`) |
44
+ | `customer-and-document-events.ts` | `auth.otp_generated` | `otp` | SMS (`system_sms_otp_code`), WhatsApp, Dummy Sandbox (`mpn-dummy-sandbox`) |
45
+ | `customer-and-document-events.ts` | `loyalty.points_credited` | `custom` | Email, SMS, WhatsApp |
46
+ | `customer-and-document-events.ts` | `loyalty.points_debited` | `custom` | Email, SMS |
47
+ | `customer-and-document-events.ts` | `wallet.credited` | `custom` | Email, SMS, Telegram |
48
+ | `customer-and-document-events.ts` | `wallet.debited` | `custom` | Email, SMS |
49
+ | `customer-and-document-events.ts` | `membership.tier_changed` | `custom` | Email, WhatsApp |
50
+ | `customer-and-document-events.ts` | `contact.form_submitted` | `custom` | Email, Telegram, Slack |
@@ -0,0 +1,36 @@
1
+ import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
2
+ import { executeEventNotificationsDispatch } from "../workflows"
3
+
4
+ export default async function customerAndDocumentEventsSubscriber({
5
+ event: { name, data },
6
+ container,
7
+ }: SubscriberArgs<Record<string, any>>) {
8
+ if (!name) return
9
+ try {
10
+ await executeEventNotificationsDispatch(container, {
11
+ event_name: name,
12
+ event_data: data || {},
13
+ })
14
+ } catch {
15
+ // Non-blocking in event subscriber
16
+ }
17
+ }
18
+
19
+ export const config: SubscriberConfig = {
20
+ event: [
21
+ "customer.created",
22
+ "customer.updated",
23
+ "invite.created",
24
+ "invite.resent",
25
+ "auth.password_reset",
26
+ "auth.verification_requested",
27
+ "document.generated",
28
+ "auth.otp_generated",
29
+ "loyalty.points_credited",
30
+ "loyalty.points_debited",
31
+ "wallet.credited",
32
+ "wallet.debited",
33
+ "membership.tier_changed",
34
+ "contact.form_submitted",
35
+ ],
36
+ }
@@ -0,0 +1,35 @@
1
+ import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
2
+ import { executeEventNotificationsDispatch } from "../workflows"
3
+
4
+ export default async function fulfillmentAndInventorySubscriber({
5
+ event: { name, data },
6
+ container,
7
+ }: SubscriberArgs<Record<string, any>>) {
8
+ if (!name) return
9
+ const normalizedEvent =
10
+ name === "fulfillment.shipment_created"
11
+ ? "shipment.created"
12
+ : name === "fulfillment.delivery_created"
13
+ ? "delivery.created"
14
+ : name
15
+
16
+ try {
17
+ await executeEventNotificationsDispatch(container, {
18
+ event_name: normalizedEvent,
19
+ event_data: data || {},
20
+ })
21
+ } catch {
22
+ // Non-blocking in event subscriber
23
+ }
24
+ }
25
+
26
+ export const config: SubscriberConfig = {
27
+ event: [
28
+ "shipment.created",
29
+ "delivery.created",
30
+ "fulfillment.created",
31
+ "fulfillment.shipment_created",
32
+ "fulfillment.delivery_created",
33
+ "inventory-level.updated",
34
+ ],
35
+ }
@@ -0,0 +1,31 @@
1
+ import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
2
+ import { executeEventNotificationsDispatch } from "../workflows"
3
+
4
+ export default async function orderEventsNotificationSubscriber({
5
+ event: { name, data },
6
+ container,
7
+ }: SubscriberArgs<Record<string, any>>) {
8
+ if (!name) return
9
+ try {
10
+ await executeEventNotificationsDispatch(container, {
11
+ event_name: name,
12
+ event_data: data || {},
13
+ })
14
+ } catch {
15
+ // Non-blocking in event subscriber so order lifecycle never fails
16
+ }
17
+ }
18
+
19
+ export const config: SubscriberConfig = {
20
+ event: [
21
+ "order.placed",
22
+ "order.completed",
23
+ "order.canceled",
24
+ "order.updated",
25
+ "order.fulfillment_created",
26
+ "order.fulfillment_canceled",
27
+ "order.return_requested",
28
+ "order.return_received",
29
+ "order.refund_created",
30
+ ],
31
+ }