@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,149 @@
1
+ import type {
2
+ ClearMpnSandboxResponse,
3
+ CreateMpnBroadcastPayload,
4
+ CreateMpnBroadcastResponse,
5
+ ListMpnBroadcastsResponse,
6
+ ListMpnLogsParams,
7
+ ListMpnLogsResponse,
8
+ ListMpnSandboxParams,
9
+ ListMpnSandboxResponse,
10
+ NotificationsMutationHookOptions,
11
+ NotificationsQueryHookOptions,
12
+ SendMpnNotificationPayload,
13
+ SendMpnNotificationResponse,
14
+ StoreUpdateConsentPayload,
15
+ StoreUpdateConsentResponse,
16
+ } from "../types"
17
+ import { notificationsAdminSdk, notificationsStoreSdk } from "../sdk/client"
18
+ import { notificationsQueryKeys } from "../sdk/query-keys"
19
+ import {
20
+ useNotificationsMutation,
21
+ useNotificationsQuery,
22
+ } from "./use-sdk-primitives"
23
+
24
+ export function useMpnLogs(
25
+ params?: ListMpnLogsParams,
26
+ options?: NotificationsQueryHookOptions<ListMpnLogsResponse>
27
+ ) {
28
+ const client = options?.client ?? notificationsAdminSdk
29
+ const queryKey =
30
+ options?.queryKey ??
31
+ notificationsQueryKeys.logs(params as Record<string, unknown> | undefined)
32
+
33
+ return useNotificationsQuery<ListMpnLogsResponse>(
34
+ queryKey,
35
+ () => client.listLogs(params),
36
+ options
37
+ )
38
+ }
39
+
40
+ export function useMpnOrderLogs(
41
+ orderId: string | undefined,
42
+ options?: NotificationsQueryHookOptions<ListMpnLogsResponse>
43
+ ) {
44
+ const client = options?.client ?? notificationsAdminSdk
45
+ const queryKey =
46
+ options?.queryKey ?? notificationsQueryKeys.orderLogs(orderId || "")
47
+
48
+ return useNotificationsQuery<ListMpnLogsResponse>(
49
+ queryKey,
50
+ () => client.listLogs({ entity_id: orderId!, limit: 15 }),
51
+ {
52
+ ...options,
53
+ enabled: Boolean(orderId) && (options?.enabled ?? true),
54
+ }
55
+ )
56
+ }
57
+
58
+ export function useMpnBroadcasts(
59
+ options?: NotificationsQueryHookOptions<ListMpnBroadcastsResponse>
60
+ ) {
61
+ const client = options?.client ?? notificationsAdminSdk
62
+ const queryKey = options?.queryKey ?? notificationsQueryKeys.broadcasts()
63
+
64
+ return useNotificationsQuery<ListMpnBroadcastsResponse>(
65
+ queryKey,
66
+ () => client.listBroadcasts(),
67
+ options
68
+ )
69
+ }
70
+
71
+ export function useCreateMpnBroadcast(
72
+ options?: NotificationsMutationHookOptions<
73
+ CreateMpnBroadcastResponse,
74
+ CreateMpnBroadcastPayload
75
+ >
76
+ ) {
77
+ const client = options?.client ?? notificationsAdminSdk
78
+ return useNotificationsMutation<
79
+ CreateMpnBroadcastResponse,
80
+ CreateMpnBroadcastPayload
81
+ >(
82
+ (payload) => client.createBroadcast(payload),
83
+ options,
84
+ notificationsQueryKeys.broadcasts()
85
+ )
86
+ }
87
+
88
+ export function useMpnSandboxMessages(
89
+ params?: ListMpnSandboxParams,
90
+ options?: NotificationsQueryHookOptions<ListMpnSandboxResponse>
91
+ ) {
92
+ const client = options?.client ?? notificationsAdminSdk
93
+ const queryKey =
94
+ options?.queryKey ??
95
+ notificationsQueryKeys.sandbox(
96
+ params as Record<string, unknown> | undefined
97
+ )
98
+
99
+ return useNotificationsQuery<ListMpnSandboxResponse>(
100
+ queryKey,
101
+ () => client.listSandboxMessages(params),
102
+ options
103
+ )
104
+ }
105
+
106
+ export function useClearMpnSandbox(
107
+ options?: NotificationsMutationHookOptions<ClearMpnSandboxResponse, void>
108
+ ) {
109
+ const client = options?.client ?? notificationsAdminSdk
110
+ return useNotificationsMutation<ClearMpnSandboxResponse, void>(
111
+ () => client.clearSandboxMessages(),
112
+ options,
113
+ notificationsQueryKeys.sandbox()
114
+ )
115
+ }
116
+
117
+ export function useSendMpnNotification(
118
+ options?: NotificationsMutationHookOptions<
119
+ SendMpnNotificationResponse,
120
+ SendMpnNotificationPayload
121
+ >
122
+ ) {
123
+ const client = options?.client ?? notificationsAdminSdk
124
+ return useNotificationsMutation<
125
+ SendMpnNotificationResponse,
126
+ SendMpnNotificationPayload
127
+ >(
128
+ (payload) => client.sendNotification(payload),
129
+ options,
130
+ notificationsQueryKeys.logsAll()
131
+ )
132
+ }
133
+
134
+ export function useUpdateStoreMpnConsent(
135
+ options?: NotificationsMutationHookOptions<
136
+ StoreUpdateConsentResponse,
137
+ StoreUpdateConsentPayload
138
+ >
139
+ ) {
140
+ const client = options?.client ?? notificationsStoreSdk
141
+ return useNotificationsMutation<
142
+ StoreUpdateConsentResponse,
143
+ StoreUpdateConsentPayload
144
+ >(
145
+ (payload) => client.updateConsent(payload),
146
+ options,
147
+ notificationsQueryKeys.broadcasts()
148
+ )
149
+ }
@@ -0,0 +1,75 @@
1
+ import type {
2
+ CreateMpnMappingPayload,
3
+ DeleteMpnMappingResponse,
4
+ ListMpnMappingsResponse,
5
+ NotificationsMutationHookOptions,
6
+ NotificationsQueryHookOptions,
7
+ SaveMpnMappingResponse,
8
+ } from "../types"
9
+ import { notificationsAdminSdk } from "../sdk/client"
10
+ import { notificationsQueryKeys } from "../sdk/query-keys"
11
+ import {
12
+ useNotificationsMutation,
13
+ useNotificationsQuery,
14
+ } from "./use-sdk-primitives"
15
+
16
+ export function useMpnMappings(
17
+ options?: NotificationsQueryHookOptions<ListMpnMappingsResponse>
18
+ ) {
19
+ const client = options?.client ?? notificationsAdminSdk
20
+ const queryKey = options?.queryKey ?? notificationsQueryKeys.mappings()
21
+
22
+ return useNotificationsQuery<ListMpnMappingsResponse>(
23
+ queryKey,
24
+ () => client.listMappings(),
25
+ options
26
+ )
27
+ }
28
+
29
+ export function useCreateMpnMapping(
30
+ options?: NotificationsMutationHookOptions<
31
+ SaveMpnMappingResponse,
32
+ CreateMpnMappingPayload
33
+ >
34
+ ) {
35
+ const client = options?.client ?? notificationsAdminSdk
36
+ return useNotificationsMutation<
37
+ SaveMpnMappingResponse,
38
+ CreateMpnMappingPayload
39
+ >(
40
+ (payload) => client.createMapping(payload),
41
+ options,
42
+ notificationsQueryKeys.mappingsAll()
43
+ )
44
+ }
45
+
46
+ export function useUpdateMpnMapping(
47
+ options?: NotificationsMutationHookOptions<
48
+ SaveMpnMappingResponse,
49
+ { id: string; payload: Partial<CreateMpnMappingPayload> }
50
+ >
51
+ ) {
52
+ const client = options?.client ?? notificationsAdminSdk
53
+ return useNotificationsMutation<
54
+ SaveMpnMappingResponse,
55
+ { id: string; payload: Partial<CreateMpnMappingPayload> }
56
+ >(
57
+ ({ id, payload }) => client.updateMapping(id, payload),
58
+ options,
59
+ notificationsQueryKeys.mappingsAll()
60
+ )
61
+ }
62
+
63
+ export function useDeleteMpnMapping(
64
+ options?: NotificationsMutationHookOptions<
65
+ DeleteMpnMappingResponse,
66
+ { id: string }
67
+ >
68
+ ) {
69
+ const client = options?.client ?? notificationsAdminSdk
70
+ return useNotificationsMutation<DeleteMpnMappingResponse, { id: string }>(
71
+ ({ id }) => client.deleteMapping(id),
72
+ options,
73
+ notificationsQueryKeys.mappingsAll()
74
+ )
75
+ }
@@ -0,0 +1,120 @@
1
+ import type {
2
+ CreateOrCloneMpnTemplatePayload,
3
+ DeleteMpnTemplateResponse,
4
+ GetMpnTemplateResponse,
5
+ ListMpnTemplatesParams,
6
+ ListMpnTemplatesResponse,
7
+ NotificationsMutationHookOptions,
8
+ NotificationsQueryHookOptions,
9
+ RenderMpnTemplatePayload,
10
+ RenderMpnTemplateResponse,
11
+ SaveMpnTemplateResponse,
12
+ UpdateMpnTemplatePayload,
13
+ } from "../types"
14
+ import { notificationsAdminSdk } from "../sdk/client"
15
+ import { notificationsQueryKeys } from "../sdk/query-keys"
16
+ import {
17
+ useNotificationsMutation,
18
+ useNotificationsQuery,
19
+ } from "./use-sdk-primitives"
20
+
21
+ export function useMpnTemplates(
22
+ params?: ListMpnTemplatesParams,
23
+ options?: NotificationsQueryHookOptions<ListMpnTemplatesResponse>
24
+ ) {
25
+ const client = options?.client ?? notificationsAdminSdk
26
+ const queryKey =
27
+ options?.queryKey ??
28
+ notificationsQueryKeys.templates(
29
+ params as Record<string, unknown> | undefined
30
+ )
31
+
32
+ return useNotificationsQuery<ListMpnTemplatesResponse>(
33
+ queryKey,
34
+ () => client.listTemplates(params),
35
+ options
36
+ )
37
+ }
38
+
39
+ export function useMpnTemplateDetail(
40
+ id: string | undefined,
41
+ options?: NotificationsQueryHookOptions<GetMpnTemplateResponse>
42
+ ) {
43
+ const client = options?.client ?? notificationsAdminSdk
44
+ const queryKey =
45
+ options?.queryKey ?? notificationsQueryKeys.template(id || "")
46
+
47
+ return useNotificationsQuery<GetMpnTemplateResponse>(
48
+ queryKey,
49
+ () => client.getTemplate(id!),
50
+ {
51
+ ...options,
52
+ enabled: Boolean(id) && (options?.enabled ?? true),
53
+ }
54
+ )
55
+ }
56
+
57
+ export function useCreateOrCloneMpnTemplate(
58
+ options?: NotificationsMutationHookOptions<
59
+ SaveMpnTemplateResponse,
60
+ CreateOrCloneMpnTemplatePayload
61
+ >
62
+ ) {
63
+ const client = options?.client ?? notificationsAdminSdk
64
+ return useNotificationsMutation<
65
+ SaveMpnTemplateResponse,
66
+ CreateOrCloneMpnTemplatePayload
67
+ >(
68
+ (payload) => client.createOrCloneTemplate(payload),
69
+ options,
70
+ notificationsQueryKeys.templatesAll()
71
+ )
72
+ }
73
+
74
+ export function useUpdateMpnTemplate(
75
+ options?: NotificationsMutationHookOptions<
76
+ SaveMpnTemplateResponse,
77
+ { id: string; payload: UpdateMpnTemplatePayload }
78
+ >
79
+ ) {
80
+ const client = options?.client ?? notificationsAdminSdk
81
+ return useNotificationsMutation<
82
+ SaveMpnTemplateResponse,
83
+ { id: string; payload: UpdateMpnTemplatePayload }
84
+ >(
85
+ ({ id, payload }) => client.updateTemplate(id, payload),
86
+ options,
87
+ notificationsQueryKeys.templatesAll()
88
+ )
89
+ }
90
+
91
+ export function useDeleteMpnTemplate(
92
+ options?: NotificationsMutationHookOptions<
93
+ DeleteMpnTemplateResponse,
94
+ { id: string }
95
+ >
96
+ ) {
97
+ const client = options?.client ?? notificationsAdminSdk
98
+ return useNotificationsMutation<DeleteMpnTemplateResponse, { id: string }>(
99
+ ({ id }) => client.deleteTemplate(id),
100
+ options,
101
+ notificationsQueryKeys.templatesAll()
102
+ )
103
+ }
104
+
105
+ export function useRenderMpnTemplatePreview(
106
+ options?: NotificationsMutationHookOptions<
107
+ RenderMpnTemplateResponse,
108
+ RenderMpnTemplatePayload
109
+ >
110
+ ) {
111
+ const client = options?.client ?? notificationsAdminSdk
112
+ return useNotificationsMutation<
113
+ RenderMpnTemplateResponse,
114
+ RenderMpnTemplatePayload
115
+ >(
116
+ (payload) => client.renderTemplatePreview(payload),
117
+ options,
118
+ notificationsQueryKeys.templatesAll()
119
+ )
120
+ }
@@ -0,0 +1,103 @@
1
+ import { useCallback, useState } from "react"
2
+ import type {
3
+ MpnLogRecord,
4
+ MpnTemplateRecord,
5
+ UseOrderNotificationsWidgetStateProps,
6
+ } from "../types"
7
+ import { notificationsAdminSdk } from "../sdk/client"
8
+ import { notificationsQueryKeys } from "../sdk/query-keys"
9
+ import { useMpnOrderLogs, useSendMpnNotification } from "./use-mpn-dispatch"
10
+ import { useMpnTemplates } from "./use-mpn-templates"
11
+
12
+ export function useOrderNotificationsWidgetState({
13
+ order,
14
+ client = notificationsAdminSdk,
15
+ t = (key: string) => key,
16
+ }: UseOrderNotificationsWidgetStateProps) {
17
+ const orderId: string | undefined = order?.id
18
+ const [selectedChannel, setSelectedChannel] = useState<string>("email")
19
+ const [selectedTemplateId, setSelectedTemplateId] = useState<string>(
20
+ "system_email_order_placed"
21
+ )
22
+ const [attachInvoicePdf, setAttachInvoicePdf] = useState<boolean>(true)
23
+ const [statusMessage, setStatusMessage] = useState<string | null>(null)
24
+
25
+ const orderLogsQueryKey = notificationsQueryKeys.orderLogs(orderId || "")
26
+
27
+ const { data: logsData, refetch: refetchOrderLogs } = useMpnOrderLogs(
28
+ orderId,
29
+ {
30
+ client,
31
+ queryKey: orderLogsQueryKey,
32
+ }
33
+ )
34
+
35
+ const { data: templatesData } = useMpnTemplates(undefined, {
36
+ client,
37
+ enabled: Boolean(orderId),
38
+ })
39
+
40
+ const logs: MpnLogRecord[] = logsData?.logs || []
41
+ const templates: MpnTemplateRecord[] = templatesData?.templates || []
42
+
43
+ const sendMutation = useSendMpnNotification({
44
+ client,
45
+ queryKey: orderLogsQueryKey,
46
+ })
47
+
48
+ const handleSendOrderNotification = useCallback(async () => {
49
+ if (!orderId) return
50
+ setStatusMessage(null)
51
+ try {
52
+ const recipient =
53
+ selectedChannel === "email"
54
+ ? order?.email || "customer@example.com"
55
+ : selectedChannel === "whatsapp" || selectedChannel === "sms"
56
+ ? order?.shipping_address?.phone ||
57
+ order?.billing_address?.phone ||
58
+ "+14155552671"
59
+ : order?.metadata?.telegram_chat_id || "-1001234567890"
60
+
61
+ await sendMutation.mutateAsync({
62
+ channel: selectedChannel,
63
+ recipient,
64
+ template_id: selectedTemplateId,
65
+ order_id: orderId,
66
+ context_type: "order",
67
+ data: order,
68
+ attach_invoice_pdf: attachInvoicePdf,
69
+ })
70
+
71
+ setStatusMessage(
72
+ t("widget.dispatchedSuccess", { channel: selectedChannel })
73
+ )
74
+ } catch (err: any) {
75
+ setStatusMessage(err?.message || t("widget.dispatchFailed"))
76
+ }
77
+ }, [
78
+ orderId,
79
+ order,
80
+ selectedChannel,
81
+ selectedTemplateId,
82
+ attachInvoicePdf,
83
+ sendMutation,
84
+ t,
85
+ ])
86
+
87
+ return {
88
+ orderId,
89
+ logs,
90
+ templates,
91
+ selectedChannel,
92
+ setSelectedChannel,
93
+ selectedTemplateId,
94
+ setSelectedTemplateId,
95
+ attachInvoicePdf,
96
+ setAttachInvoicePdf,
97
+ sending: sendMutation.isPending,
98
+ statusMessage,
99
+ setStatusMessage,
100
+ loadOrderActivity: refetchOrderLogs,
101
+ handleSendOrderNotification,
102
+ }
103
+ }
@@ -0,0 +1,157 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react"
2
+ import type {
3
+ NotificationsMutationHookOptions,
4
+ NotificationsQueryHookOptions,
5
+ UseNotificationsMutationResult,
6
+ UseNotificationsQueryResult,
7
+ } from "../types"
8
+ import {
9
+ invalidateNotificationsQueries,
10
+ matchesNotificationsQueryKeyPrefix,
11
+ notificationsQueryKeys,
12
+ subscribeNotificationsQueryInvalidation,
13
+ } from "../sdk/query-keys"
14
+
15
+ export function useNotificationsQuery<TData>(
16
+ queryKey: readonly unknown[],
17
+ queryFn: () => Promise<TData>,
18
+ options?: NotificationsQueryHookOptions<TData>
19
+ ): UseNotificationsQueryResult<TData> {
20
+ const enabled = options?.enabled ?? true
21
+ const [data, setData] = useState<TData | undefined>(undefined)
22
+ const [isLoading, setIsLoading] = useState<boolean>(enabled)
23
+ const [isFetching, setIsFetching] = useState<boolean>(false)
24
+ const [error, setError] = useState<Error | null>(null)
25
+
26
+ const queryFnRef = useRef(queryFn)
27
+ queryFnRef.current = queryFn
28
+
29
+ const optionsRef = useRef(options)
30
+ optionsRef.current = options
31
+
32
+ const serializedKey = JSON.stringify(queryKey)
33
+ const keyRef = useRef(queryKey)
34
+ keyRef.current = queryKey
35
+
36
+ const execute = useCallback(async (): Promise<TData | undefined> => {
37
+ setIsFetching(true)
38
+ setError(null)
39
+ try {
40
+ const result = await queryFnRef.current()
41
+ setData(result)
42
+ optionsRef.current?.onSuccess?.(result)
43
+ return result
44
+ } catch (err) {
45
+ const normalized =
46
+ err instanceof Error ? err : new Error(String(err || "Unknown error"))
47
+ setError(normalized)
48
+ optionsRef.current?.onError?.(normalized)
49
+ return undefined
50
+ } finally {
51
+ setIsLoading(false)
52
+ setIsFetching(false)
53
+ }
54
+ }, [])
55
+
56
+ useEffect(() => {
57
+ if (!enabled) {
58
+ setIsLoading(false)
59
+ return
60
+ }
61
+ setIsLoading(true)
62
+ void execute()
63
+ }, [enabled, serializedKey, execute])
64
+
65
+ useEffect(() => {
66
+ if (!enabled) return
67
+ return subscribeNotificationsQueryInvalidation((invalidatedPrefix) => {
68
+ if (
69
+ matchesNotificationsQueryKeyPrefix(keyRef.current, invalidatedPrefix)
70
+ ) {
71
+ void execute()
72
+ }
73
+ })
74
+ }, [enabled, execute])
75
+
76
+ return {
77
+ data,
78
+ isLoading,
79
+ isFetching,
80
+ error,
81
+ refetch: execute,
82
+ }
83
+ }
84
+
85
+ export function useNotificationsMutation<TData, TVariables>(
86
+ mutationFn: (variables: TVariables) => Promise<TData>,
87
+ options?: NotificationsMutationHookOptions<TData, TVariables>,
88
+ defaultInvalidateKey: readonly unknown[] = notificationsQueryKeys.all
89
+ ): UseNotificationsMutationResult<TData, TVariables> {
90
+ const [isPending, setIsPending] = useState(false)
91
+ const [error, setError] = useState<Error | null>(null)
92
+ const [data, setData] = useState<TData | undefined>(undefined)
93
+
94
+ const mutationFnRef = useRef(mutationFn)
95
+ mutationFnRef.current = mutationFn
96
+
97
+ const optionsRef = useRef(options)
98
+ optionsRef.current = options
99
+
100
+ const mutateAsync = useCallback(
101
+ async (variables: TVariables): Promise<TData> => {
102
+ setIsPending(true)
103
+ setError(null)
104
+ try {
105
+ const result = await mutationFnRef.current(variables)
106
+ setData(result)
107
+ const targetKey = optionsRef.current?.queryKey ?? defaultInvalidateKey
108
+ invalidateNotificationsQueries(targetKey)
109
+ optionsRef.current?.onSuccess?.(result, variables)
110
+ return result
111
+ } catch (err) {
112
+ const normalized =
113
+ err instanceof Error ? err : new Error(String(err || "Unknown error"))
114
+ setError(normalized)
115
+ optionsRef.current?.onError?.(normalized, variables)
116
+ throw normalized
117
+ } finally {
118
+ setIsPending(false)
119
+ }
120
+ },
121
+ [defaultInvalidateKey]
122
+ )
123
+
124
+ const mutate = useCallback(
125
+ (
126
+ variables: TVariables,
127
+ callOptions?: {
128
+ onSuccess?: (data: TData, variables: TVariables) => void
129
+ onError?: (error: Error, variables: TVariables) => void
130
+ }
131
+ ) => {
132
+ mutateAsync(variables)
133
+ .then((res) => {
134
+ callOptions?.onSuccess?.(res, variables)
135
+ })
136
+ .catch((err: Error) => {
137
+ callOptions?.onError?.(err, variables)
138
+ })
139
+ },
140
+ [mutateAsync]
141
+ )
142
+
143
+ const reset = useCallback(() => {
144
+ setError(null)
145
+ setData(undefined)
146
+ setIsPending(false)
147
+ }, [])
148
+
149
+ return {
150
+ mutate,
151
+ mutateAsync,
152
+ isPending,
153
+ error,
154
+ data,
155
+ reset,
156
+ }
157
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ export * from "./types"
2
+ export {
3
+ NotificationsModuleService,
4
+ default as NotificationsModule,
5
+ } from "./modules/notifications"
6
+ export * from "./modules/notifications/i18n"
7
+ export * from "./modules/notifications/utils/security"
8
+ export * from "./modules/notifications/utils/interpolation"
9
+ export * from "./modules/notifications/utils/transformers"
10
+ export * from "./modules/notifications/utils/system-presets"
11
+ export * from "./modules/notifications/renderer"
12
+ export * from "./modules/notifications/integrations/documents-and-b2b-resolver"
13
+ export * from "./providers"
14
+ export * from "./workflows"
15
+ export * from "./sdk"
16
+ export * from "./integrations/automations-action-handler"