@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,168 @@
1
+ import type { TemplateBlockNode } from "../../../types"
2
+ import { getNestedValue, multiInterpolateString } from "../utils/interpolation"
3
+ import {
4
+ buildRepeaterItemContext,
5
+ getBlockChildren,
6
+ getBlockMeta,
7
+ isEmptyInterpolatedValue,
8
+ } from "./block-helpers"
9
+
10
+ export function renderSlackBlocks(
11
+ nodes: TemplateBlockNode[],
12
+ data: Record<string, any>,
13
+ translations: Record<string, any>,
14
+ arrayPath?: string
15
+ ): { blocks: Array<Record<string, any>>; fallbackText: string } {
16
+ const blocks: Array<Record<string, any>> = []
17
+ const textLines: string[] = []
18
+
19
+ for (const node of nodes) {
20
+ const meta = getBlockMeta(node)
21
+ const children = getBlockChildren(node, meta)
22
+ switch (node.type) {
23
+ case "heading": {
24
+ const text = multiInterpolateString(
25
+ String(meta.text ?? meta.value ?? ""),
26
+ { data, translations },
27
+ { arrayPath }
28
+ )
29
+ if (!isEmptyInterpolatedValue(text)) {
30
+ textLines.push(text)
31
+ blocks.push({
32
+ type: "header",
33
+ text: {
34
+ type: "plain_text",
35
+ text: text.slice(0, 150),
36
+ emoji: true,
37
+ },
38
+ })
39
+ }
40
+ break
41
+ }
42
+ case "text":
43
+ case "section": {
44
+ const text = multiInterpolateString(
45
+ String(meta.text ?? meta.value ?? ""),
46
+ { data, translations },
47
+ { arrayPath }
48
+ )
49
+ if (!isEmptyInterpolatedValue(text)) {
50
+ textLines.push(text)
51
+ blocks.push({
52
+ type: "section",
53
+ text: {
54
+ type: "mrkdwn",
55
+ text,
56
+ },
57
+ })
58
+ }
59
+ if (children.length > 0) {
60
+ const child = renderSlackBlocks(
61
+ children,
62
+ data,
63
+ translations,
64
+ arrayPath
65
+ )
66
+ blocks.push(...child.blocks)
67
+ if (child.fallbackText) textLines.push(child.fallbackText)
68
+ }
69
+ break
70
+ }
71
+ case "row": {
72
+ const label = multiInterpolateString(
73
+ String(meta.label ?? ""),
74
+ { data, translations },
75
+ { arrayPath }
76
+ )
77
+ const value = multiInterpolateString(
78
+ String(meta.value ?? ""),
79
+ { data, translations },
80
+ { arrayPath }
81
+ )
82
+ if (!isEmptyInterpolatedValue(value)) {
83
+ textLines.push(`${label}: ${value}`)
84
+ blocks.push({
85
+ type: "section",
86
+ fields: [
87
+ { type: "mrkdwn", text: `*${label}*` },
88
+ { type: "mrkdwn", text: value },
89
+ ],
90
+ })
91
+ }
92
+ break
93
+ }
94
+ case "repeater": {
95
+ const arrayKey = String(
96
+ meta.arrayKey || meta.arrayPath || "items"
97
+ ).trim()
98
+ const list = getNestedValue(data, arrayKey)
99
+ if (Array.isArray(list) && list.length > 0 && children.length > 0) {
100
+ for (const item of list) {
101
+ const itemContext = buildRepeaterItemContext(data, item, arrayKey)
102
+ const child = renderSlackBlocks(
103
+ children,
104
+ itemContext,
105
+ translations,
106
+ arrayKey
107
+ )
108
+ blocks.push(...child.blocks)
109
+ if (child.fallbackText) textLines.push(child.fallbackText)
110
+ }
111
+ } else if (
112
+ Array.isArray(meta.emptyBlocks) &&
113
+ meta.emptyBlocks.length > 0
114
+ ) {
115
+ const empty = renderSlackBlocks(
116
+ meta.emptyBlocks,
117
+ data,
118
+ translations,
119
+ arrayPath
120
+ )
121
+ blocks.push(...empty.blocks)
122
+ if (empty.fallbackText) textLines.push(empty.fallbackText)
123
+ }
124
+ break
125
+ }
126
+ case "divider":
127
+ case "separator": {
128
+ blocks.push({ type: "divider" })
129
+ break
130
+ }
131
+ case "button":
132
+ case "actions": {
133
+ const rawButtons: Array<Record<string, any>> = Array.isArray(
134
+ meta.buttons
135
+ )
136
+ ? meta.buttons
137
+ : [{ text: meta.text, url: meta.url }]
138
+ const elements = rawButtons.map((b, idx) => ({
139
+ type: "button",
140
+ action_id: String(b.id || `action_${idx + 1}`),
141
+ text: {
142
+ type: "plain_text",
143
+ text: multiInterpolateString(
144
+ String(b.text || b.title || "View"),
145
+ { data, translations },
146
+ { arrayPath }
147
+ ).slice(0, 75),
148
+ emoji: true,
149
+ },
150
+ url: multiInterpolateString(
151
+ String(b.url || data.store_url || ""),
152
+ { data, translations },
153
+ { arrayPath }
154
+ ),
155
+ }))
156
+ if (elements.length > 0) {
157
+ blocks.push({ type: "actions", elements })
158
+ }
159
+ break
160
+ }
161
+ }
162
+ }
163
+
164
+ return {
165
+ blocks,
166
+ fallbackText: textLines.join("\n").trim() || "Medusa Store Notification",
167
+ }
168
+ }
@@ -0,0 +1,161 @@
1
+ import type { TelegramInlineButton, TemplateBlockNode } from "../../../types"
2
+ import { getNestedValue, multiInterpolateString } from "../utils/interpolation"
3
+ import { escapeHtml, sanitizeTelegramHtml } from "../utils/security"
4
+ import {
5
+ buildRepeaterItemContext,
6
+ getBlockChildren,
7
+ getBlockMeta,
8
+ isEmptyInterpolatedValue,
9
+ } from "./block-helpers"
10
+
11
+ export function renderTelegramNodes(
12
+ nodes: TemplateBlockNode[],
13
+ data: Record<string, any>,
14
+ translations: Record<string, any>,
15
+ arrayPath?: string
16
+ ): { lines: string[]; inlineKeyboard: TelegramInlineButton[][] } {
17
+ const lines: string[] = []
18
+ const inlineKeyboard: TelegramInlineButton[][] = []
19
+
20
+ for (const node of nodes) {
21
+ const meta = getBlockMeta(node)
22
+ const children = getBlockChildren(node, meta)
23
+ switch (node.type) {
24
+ case "heading": {
25
+ const raw = multiInterpolateString(
26
+ String(meta.text ?? meta.value ?? ""),
27
+ { data, translations },
28
+ { arrayPath }
29
+ )
30
+ if (!isEmptyInterpolatedValue(raw)) {
31
+ lines.push(`<b>${escapeHtml(raw)}</b>`)
32
+ }
33
+ break
34
+ }
35
+ case "text":
36
+ case "section": {
37
+ const raw = multiInterpolateString(
38
+ String(meta.text ?? meta.value ?? ""),
39
+ { data, translations },
40
+ { arrayPath }
41
+ )
42
+ if (!isEmptyInterpolatedValue(raw)) {
43
+ lines.push(sanitizeTelegramHtml(raw))
44
+ }
45
+ if (children.length > 0) {
46
+ const child = renderTelegramNodes(
47
+ children,
48
+ data,
49
+ translations,
50
+ arrayPath
51
+ )
52
+ lines.push(...child.lines)
53
+ inlineKeyboard.push(...child.inlineKeyboard)
54
+ }
55
+ break
56
+ }
57
+ case "separator":
58
+ case "divider": {
59
+ lines.push("⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯")
60
+ break
61
+ }
62
+ case "row": {
63
+ const rawLabel = multiInterpolateString(
64
+ String(meta.label ?? ""),
65
+ { data, translations },
66
+ { arrayPath }
67
+ )
68
+ const rawValue = multiInterpolateString(
69
+ String(meta.value ?? ""),
70
+ { data, translations },
71
+ { arrayPath }
72
+ )
73
+ if (!isEmptyInterpolatedValue(rawValue)) {
74
+ const label = escapeHtml(rawLabel)
75
+ const value = escapeHtml(rawValue)
76
+ lines.push(
77
+ meta.bold
78
+ ? `<b>${label}: ${value}</b>`
79
+ : `• ${label}: <code>${value}</code>`
80
+ )
81
+ }
82
+ break
83
+ }
84
+ case "repeater": {
85
+ const arrayKey = String(
86
+ meta.arrayKey || meta.arrayPath || "items"
87
+ ).trim()
88
+ const list = getNestedValue(data, arrayKey)
89
+ if (Array.isArray(list) && list.length > 0 && children.length > 0) {
90
+ for (const item of list) {
91
+ const itemContext = buildRepeaterItemContext(data, item, arrayKey)
92
+ const child = renderTelegramNodes(
93
+ children,
94
+ itemContext,
95
+ translations,
96
+ arrayKey
97
+ )
98
+ lines.push(...child.lines)
99
+ }
100
+ } else if (
101
+ Array.isArray(meta.emptyBlocks) &&
102
+ meta.emptyBlocks.length > 0
103
+ ) {
104
+ const empty = renderTelegramNodes(
105
+ meta.emptyBlocks,
106
+ data,
107
+ translations,
108
+ arrayPath
109
+ )
110
+ lines.push(...empty.lines)
111
+ }
112
+ break
113
+ }
114
+ case "product-item": {
115
+ if (meta.variant === "inventory_item") {
116
+ lines.push(
117
+ `• <b>${escapeHtml(data.title || "Item")}</b> (<code>${escapeHtml(data.sku || "N/A")}</code>) — Available: <b>${escapeHtml(data.available_quantity ?? 0)}</b> (${escapeHtml(data.location_name || "Warehouse")})`
118
+ )
119
+ } else {
120
+ lines.push(
121
+ `• ${escapeHtml(data.quantity ?? 1)}x <b>${escapeHtml(data.title || "Item")}</b> — <code>${escapeHtml(data.total_formatted || "")}</code>`
122
+ )
123
+ }
124
+ break
125
+ }
126
+ case "button":
127
+ case "actions": {
128
+ const rawButtons: Array<Record<string, any>> = Array.isArray(
129
+ meta.buttons
130
+ )
131
+ ? meta.buttons
132
+ : [{ text: meta.text, url: meta.url }]
133
+ const rowButtons: TelegramInlineButton[] = []
134
+ for (const b of rawButtons) {
135
+ const text = multiInterpolateString(
136
+ String(b.text || b.title || "Open"),
137
+ { data, translations },
138
+ { arrayPath }
139
+ )
140
+ const url = b.url
141
+ ? multiInterpolateString(
142
+ String(b.url),
143
+ { data, translations },
144
+ { arrayPath }
145
+ )
146
+ : undefined
147
+ rowButtons.push({
148
+ text,
149
+ ...(url ? { url } : { callback_data: String(b.id || "action") }),
150
+ })
151
+ }
152
+ if (rowButtons.length > 0) {
153
+ inlineKeyboard.push(rowButtons)
154
+ }
155
+ break
156
+ }
157
+ }
158
+ }
159
+
160
+ return { lines, inlineKeyboard }
161
+ }
@@ -0,0 +1,156 @@
1
+ import type {
2
+ TemplateBlockNode,
3
+ WhatsAppInteractiveButton,
4
+ } from "../../../types"
5
+ import { getNestedValue, multiInterpolateString } from "../utils/interpolation"
6
+ import {
7
+ buildRepeaterItemContext,
8
+ getBlockChildren,
9
+ getBlockMeta,
10
+ isEmptyInterpolatedValue,
11
+ } from "./block-helpers"
12
+
13
+ export function renderWhatsAppNodes(
14
+ nodes: TemplateBlockNode[],
15
+ data: Record<string, any>,
16
+ translations: Record<string, any>,
17
+ arrayPath?: string
18
+ ): { lines: string[]; buttons: WhatsAppInteractiveButton[] } {
19
+ const lines: string[] = []
20
+ const buttons: WhatsAppInteractiveButton[] = []
21
+
22
+ for (const node of nodes) {
23
+ const meta = getBlockMeta(node)
24
+ const children = getBlockChildren(node, meta)
25
+ switch (node.type) {
26
+ case "heading": {
27
+ const text = multiInterpolateString(
28
+ String(meta.text ?? meta.value ?? ""),
29
+ { data, translations },
30
+ { arrayPath }
31
+ )
32
+ if (!isEmptyInterpolatedValue(text)) {
33
+ lines.push(`*${text}*`)
34
+ }
35
+ break
36
+ }
37
+ case "text":
38
+ case "section": {
39
+ const text = multiInterpolateString(
40
+ String(meta.text ?? meta.value ?? ""),
41
+ { data, translations },
42
+ { arrayPath }
43
+ )
44
+ if (!isEmptyInterpolatedValue(text)) {
45
+ lines.push(text)
46
+ }
47
+ if (children.length > 0) {
48
+ const child = renderWhatsAppNodes(
49
+ children,
50
+ data,
51
+ translations,
52
+ arrayPath
53
+ )
54
+ lines.push(...child.lines)
55
+ buttons.push(...child.buttons)
56
+ }
57
+ break
58
+ }
59
+ case "separator":
60
+ case "divider": {
61
+ lines.push("────────────────────")
62
+ break
63
+ }
64
+ case "row": {
65
+ const label = multiInterpolateString(
66
+ String(meta.label ?? ""),
67
+ { data, translations },
68
+ { arrayPath }
69
+ )
70
+ const value = multiInterpolateString(
71
+ String(meta.value ?? ""),
72
+ { data, translations },
73
+ { arrayPath }
74
+ )
75
+ if (!isEmptyInterpolatedValue(value)) {
76
+ lines.push(
77
+ meta.bold ? `*${label}:* *${value}*` : `• ${label}: ${value}`
78
+ )
79
+ }
80
+ break
81
+ }
82
+ case "repeater": {
83
+ const arrayKey = String(
84
+ meta.arrayKey || meta.arrayPath || "items"
85
+ ).trim()
86
+ const list = getNestedValue(data, arrayKey)
87
+ if (Array.isArray(list) && list.length > 0 && children.length > 0) {
88
+ for (const item of list) {
89
+ const itemContext = buildRepeaterItemContext(data, item, arrayKey)
90
+ const child = renderWhatsAppNodes(
91
+ children,
92
+ itemContext,
93
+ translations,
94
+ arrayKey
95
+ )
96
+ lines.push(...child.lines)
97
+ }
98
+ } else if (
99
+ Array.isArray(meta.emptyBlocks) &&
100
+ meta.emptyBlocks.length > 0
101
+ ) {
102
+ const empty = renderWhatsAppNodes(
103
+ meta.emptyBlocks,
104
+ data,
105
+ translations,
106
+ arrayPath
107
+ )
108
+ lines.push(...empty.lines)
109
+ }
110
+ break
111
+ }
112
+ case "product-item": {
113
+ if (meta.variant === "inventory_item") {
114
+ lines.push(
115
+ `• *${data.title || "Item"}* (\`${data.sku || "N/A"}\`) — Available: *${data.available_quantity ?? 0}* (${data.location_name || "Warehouse"})`
116
+ )
117
+ } else {
118
+ lines.push(
119
+ `• ${data.quantity ?? 1}x *${data.title || "Item"}* — ${data.total_formatted || ""}`
120
+ )
121
+ }
122
+ break
123
+ }
124
+ case "button":
125
+ case "actions": {
126
+ const rawButtons: Array<Record<string, any>> = Array.isArray(
127
+ meta.buttons
128
+ )
129
+ ? meta.buttons
130
+ : [{ id: "btn_1", title: meta.text, url: meta.url }]
131
+ for (const [idx, b] of rawButtons.entries()) {
132
+ const title = multiInterpolateString(
133
+ String(b.title || b.text || "Open"),
134
+ { data, translations },
135
+ { arrayPath }
136
+ )
137
+ const url = b.url
138
+ ? multiInterpolateString(
139
+ String(b.url),
140
+ { data, translations },
141
+ { arrayPath }
142
+ )
143
+ : undefined
144
+ buttons.push({
145
+ id: String(b.id || `btn_${idx + 1}`),
146
+ title,
147
+ url,
148
+ })
149
+ }
150
+ break
151
+ }
152
+ }
153
+ }
154
+
155
+ return { lines, buttons }
156
+ }
@@ -0,0 +1,65 @@
1
+ import type { NotificationChannel } from "../types"
2
+
3
+ /**
4
+ * Default non-secret operational configuration for a channel that has none yet.
5
+ */
6
+ export function buildDefaultChannelConfig(
7
+ channel: NotificationChannel,
8
+ driver: string
9
+ ) {
10
+ const defaultRate =
11
+ channel === "telegram"
12
+ ? 30
13
+ : channel === "whatsapp"
14
+ ? Number(process.env.WHATSAPP_SEND_CONCURRENCY || 5)
15
+ : 10
16
+ return {
17
+ channel,
18
+ driver,
19
+ is_enabled: true,
20
+ is_verified: channel === "email" || channel === "dummy",
21
+ rate_limit_per_sec: defaultRate,
22
+ config_json: {},
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Merges `config_json` updates, strips accidental secret fields, and resets
28
+ * `is_verified` for Telegram if `merchant_channel_id` changes
29
+ * (`medusa-notification-provider` pattern).
30
+ */
31
+ export function mergeChannelConfigUpdate(
32
+ current: Record<string, any>,
33
+ params: {
34
+ channel: NotificationChannel
35
+ is_verified?: boolean
36
+ config_json?: Record<string, any>
37
+ }
38
+ ): { nextConfigJson: Record<string, any>; isVerified: boolean } {
39
+ const prevConfigJson = (current.config_json || {}) as Record<string, any>
40
+ const nextConfigJson = params.config_json
41
+ ? { ...prevConfigJson, ...params.config_json }
42
+ : prevConfigJson
43
+
44
+ // Strip any accidental secret fields before saving to DB
45
+ for (const key of Object.keys(nextConfigJson)) {
46
+ if (/(token|secret|password|api_key|private_key)/i.test(key)) {
47
+ delete nextConfigJson[key]
48
+ }
49
+ }
50
+
51
+ let isVerified =
52
+ params.is_verified !== undefined ? params.is_verified : current.is_verified
53
+
54
+ if (
55
+ params.channel === "telegram" &&
56
+ params.config_json?.merchant_channel_id &&
57
+ params.config_json.merchant_channel_id !==
58
+ prevConfigJson.merchant_channel_id &&
59
+ params.is_verified === undefined
60
+ ) {
61
+ isVerified = false
62
+ }
63
+
64
+ return { nextConfigJson, isVerified }
65
+ }
@@ -0,0 +1,88 @@
1
+ import type { NotificationChannel, NotificationsPluginOptions } from "../types"
2
+ import { normalizeE164Phone } from "../utils/security"
3
+
4
+ const OPT_IN_KEYWORDS = ["JOIN", "START", "OPTIN", "UNSTOP", "YES"]
5
+ const OPT_OUT_KEYWORDS = [
6
+ "STOP",
7
+ "END",
8
+ "QUIT",
9
+ "CANCEL",
10
+ "UNSUBSCRIBE",
11
+ "OPTOUT",
12
+ ]
13
+
14
+ export function normalizeConsentIdentifier(
15
+ channel: NotificationChannel,
16
+ identifier: string
17
+ ): string {
18
+ return channel === "sms" || channel === "whatsapp"
19
+ ? normalizeE164Phone(identifier) || identifier.trim()
20
+ : identifier.trim()
21
+ }
22
+
23
+ /**
24
+ * Maps an inbound SMS/WhatsApp keyword reply to a consent action (or `null` when not a keyword).
25
+ */
26
+ export function parseConsentKeyword(body: string): "opt_in" | "opt_out" | null {
27
+ const keyword = (body || "").trim().toUpperCase()
28
+ if (OPT_IN_KEYWORDS.includes(keyword)) return "opt_in"
29
+ if (OPT_OUT_KEYWORDS.includes(keyword)) return "opt_out"
30
+ return null
31
+ }
32
+
33
+ export type ConsentDecision = {
34
+ allowed: boolean
35
+ reason?: string
36
+ appendStopFooter: boolean
37
+ consentRecordId?: string
38
+ }
39
+
40
+ /**
41
+ * Decides whether sending is permitted given the recipient's existing consent records.
42
+ */
43
+ export function evaluateConsent(
44
+ records: Array<Record<string, any>>,
45
+ params: {
46
+ channel: NotificationChannel
47
+ requireExplicitOptIn?: boolean
48
+ },
49
+ options: NotificationsPluginOptions
50
+ ): ConsentDecision {
51
+ if (records.length > 0) {
52
+ const rec = records[0]
53
+ if (rec.status === "opted_out") {
54
+ return {
55
+ allowed: false,
56
+ reason: `Recipient opted out (${rec.source || "STOP"})`,
57
+ appendStopFooter: false,
58
+ consentRecordId: rec.id,
59
+ }
60
+ }
61
+ if (rec.status === "opted_in") {
62
+ return {
63
+ allowed: true,
64
+ appendStopFooter: !rec.first_message_sent && params.channel === "sms",
65
+ consentRecordId: rec.id,
66
+ }
67
+ }
68
+ }
69
+
70
+ const strictConsent =
71
+ params.requireExplicitOptIn ??
72
+ (params.channel === "sms"
73
+ ? Boolean(options.requireSmsConsent)
74
+ : Boolean(options.requireWhatsAppConsent))
75
+
76
+ if (strictConsent) {
77
+ return {
78
+ allowed: false,
79
+ reason: "No active opt-in consent record found",
80
+ appendStopFooter: false,
81
+ }
82
+ }
83
+
84
+ return {
85
+ allowed: true,
86
+ appendStopFooter: params.channel === "sms",
87
+ }
88
+ }
@@ -0,0 +1,56 @@
1
+ import type { NotificationChannel } from "../types"
2
+ import {
3
+ hashRecipient,
4
+ maskRecipientPii,
5
+ redactSensitivePayload,
6
+ } from "../utils/security"
7
+
8
+ export type DeliveryLogParams = {
9
+ idempotency_key: string
10
+ event_name: string
11
+ entity_id?: string | null
12
+ channel: NotificationChannel
13
+ provider_id?: string | null
14
+ driver?: string | null
15
+ template_id?: string | null
16
+ mapping_id?: string | null
17
+ recipient: string
18
+ status: string
19
+ attempts?: number
20
+ external_message_id?: string | null
21
+ payload?: unknown
22
+ error_code?: string | null
23
+ error_message?: string | null
24
+ }
25
+
26
+ /**
27
+ * Builds a delivery log row with masked PII and redacted payload.
28
+ */
29
+ export function buildDeliveryLogRow(params: DeliveryLogParams) {
30
+ return {
31
+ idempotency_key: params.idempotency_key,
32
+ event_name: params.event_name,
33
+ entity_id: params.entity_id ?? null,
34
+ channel: params.channel,
35
+ provider_id: params.provider_id ?? null,
36
+ driver: params.driver ?? null,
37
+ template_id: params.template_id ?? null,
38
+ mapping_id: params.mapping_id ?? null,
39
+ recipient_masked: maskRecipientPii(params.recipient),
40
+ recipient_hash: hashRecipient(params.recipient),
41
+ status: params.status,
42
+ attempts: params.attempts ?? 1,
43
+ external_message_id: params.external_message_id ?? null,
44
+ payload_redacted: redactSensitivePayload(params.payload) as Record<
45
+ string,
46
+ any
47
+ >,
48
+ error_code: params.error_code ?? null,
49
+ error_message: params.error_message ?? null,
50
+ sent_at:
51
+ params.status === "sent" || params.status === "delivered"
52
+ ? new Date()
53
+ : null,
54
+ delivered_at: params.status === "delivered" ? new Date() : null,
55
+ }
56
+ }