@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.
- package/.medusa/server/src/admin/index.cjs +4215 -0
- package/.medusa/server/src/admin/index.js +4193 -0
- package/.medusa/server/src/api/admin/mpn/broadcasts/route.js +99 -0
- package/.medusa/server/src/api/admin/mpn/channels/route.js +109 -0
- package/.medusa/server/src/api/admin/mpn/logs/route.js +33 -0
- package/.medusa/server/src/api/admin/mpn/mappings/[id]/route.js +33 -0
- package/.medusa/server/src/api/admin/mpn/mappings/route.js +51 -0
- package/.medusa/server/src/api/admin/mpn/render-template/route.js +37 -0
- package/.medusa/server/src/api/admin/mpn/sandbox/route.js +36 -0
- package/.medusa/server/src/api/admin/mpn/send/route.js +135 -0
- package/.medusa/server/src/api/admin/mpn/templates/[id]/route.js +77 -0
- package/.medusa/server/src/api/admin/mpn/templates/route.js +71 -0
- package/.medusa/server/src/api/admin/plugin/route.js +4 -0
- package/.medusa/server/src/api/middlewares.js +28 -0
- package/.medusa/server/src/api/store/mpn/consent/route.js +37 -0
- package/.medusa/server/src/api/store/mpn/webhooks/[provider]/route.js +97 -0
- package/.medusa/server/src/api/store/plugin/route.js +4 -0
- package/.medusa/server/src/api/validators.js +118 -0
- package/.medusa/server/src/hooks/index.js +7 -0
- package/.medusa/server/src/hooks/use-mpn-channels.js +13 -0
- package/.medusa/server/src/hooks/use-mpn-dispatch.js +45 -0
- package/.medusa/server/src/hooks/use-mpn-mappings.js +21 -0
- package/.medusa/server/src/hooks/use-mpn-templates.js +34 -0
- package/.medusa/server/src/hooks/use-order-notifications-widget-state.js +79 -0
- package/.medusa/server/src/hooks/use-sdk-primitives.js +113 -0
- package/.medusa/server/src/index.js +14 -0
- package/.medusa/server/src/integrations/automations-action-handler.js +112 -0
- package/.medusa/server/src/jobs/abandoned-cart-recovery.js +123 -0
- package/.medusa/server/src/jobs/log-retention-and-broadcasts.js +15 -0
- package/.medusa/server/src/modules/notifications/i18n/index.js +65 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/ar.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/de.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/en.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/es.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/fr.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/it.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/nl.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/pl.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/pt.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/locales/tr.js +76 -0
- package/.medusa/server/src/modules/notifications/i18n/types.js +2 -0
- package/.medusa/server/src/modules/notifications/index.js +9 -0
- package/.medusa/server/src/modules/notifications/integrations/documents-and-b2b-resolver.js +129 -0
- package/.medusa/server/src/modules/notifications/migrations/Migration20261010000000.js +184 -0
- package/.medusa/server/src/modules/notifications/models/index.js +118 -0
- package/.medusa/server/src/modules/notifications/renderer/block-helpers.js +36 -0
- package/.medusa/server/src/modules/notifications/renderer/email.js +161 -0
- package/.medusa/server/src/modules/notifications/renderer/index.js +240 -0
- package/.medusa/server/src/modules/notifications/renderer/slack.js +114 -0
- package/.medusa/server/src/modules/notifications/renderer/telegram.js +99 -0
- package/.medusa/server/src/modules/notifications/renderer/whatsapp.js +91 -0
- package/.medusa/server/src/modules/notifications/service-helpers/channel-config.js +45 -0
- package/.medusa/server/src/modules/notifications/service-helpers/consent.js +65 -0
- package/.medusa/server/src/modules/notifications/service-helpers/delivery-log.js +29 -0
- package/.medusa/server/src/modules/notifications/service-helpers/event-conditions.js +46 -0
- package/.medusa/server/src/modules/notifications/service-helpers/external-templates.js +80 -0
- package/.medusa/server/src/modules/notifications/service-helpers/recipient-resolution.js +85 -0
- package/.medusa/server/src/modules/notifications/service-helpers/sender-profiles.js +29 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-blocks.js +81 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-catalog.js +129 -0
- package/.medusa/server/src/modules/notifications/service-helpers/template-render.js +65 -0
- package/.medusa/server/src/modules/notifications/service.js +339 -0
- package/.medusa/server/src/modules/notifications/types.js +2 -0
- package/.medusa/server/src/modules/notifications/utils/auth-presets.js +140 -0
- package/.medusa/server/src/modules/notifications/utils/interpolation.js +178 -0
- package/.medusa/server/src/modules/notifications/utils/security.js +312 -0
- package/.medusa/server/src/modules/notifications/utils/system-presets.js +651 -0
- package/.medusa/server/src/modules/notifications/utils/transformers.js +358 -0
- package/.medusa/server/src/providers/dummy-sandbox/index.js +96 -0
- package/.medusa/server/src/providers/email-smtp/index.js +187 -0
- package/.medusa/server/src/providers/index.js +7 -0
- package/.medusa/server/src/providers/slack/index.js +54 -0
- package/.medusa/server/src/providers/sms/index.js +254 -0
- package/.medusa/server/src/providers/telegram/index.js +119 -0
- package/.medusa/server/src/providers/whatsapp/index.js +371 -0
- package/.medusa/server/src/rbac.js +109 -0
- package/.medusa/server/src/sdk/client.js +186 -0
- package/.medusa/server/src/sdk/index.js +3 -0
- package/.medusa/server/src/sdk/query-keys.js +61 -0
- package/.medusa/server/src/subscribers/customer-and-document-events.js +33 -0
- package/.medusa/server/src/subscribers/fulfillment-and-inventory-events.js +30 -0
- package/.medusa/server/src/subscribers/order-events.js +28 -0
- package/.medusa/server/src/types/constants.js +129 -0
- package/.medusa/server/src/types/core.js +2 -0
- package/.medusa/server/src/types/domain.js +2 -0
- package/.medusa/server/src/types/enums.js +53 -0
- package/.medusa/server/src/types/errors.js +28 -0
- package/.medusa/server/src/types/hooks.js +2 -0
- package/.medusa/server/src/types/index.js +12 -0
- package/.medusa/server/src/types/providers.js +2 -0
- package/.medusa/server/src/types/records.js +2 -0
- package/.medusa/server/src/types/render.js +2 -0
- package/.medusa/server/src/types/sdk-api.js +2 -0
- package/.medusa/server/src/types/translations.js +2 -0
- package/.medusa/server/src/workflows/index.js +461 -0
- package/LICENSE +21 -0
- package/README.md +389 -2
- package/package.json +138 -3
- package/src/admin/README.md +123 -0
- package/src/admin/components/mpn/broadcast-consent-section.tsx +62 -0
- package/src/admin/components/mpn/broadcasts-tab.tsx +208 -0
- package/src/admin/components/mpn/channels-main-section.tsx +205 -0
- package/src/admin/components/mpn/channels-side-section.tsx +88 -0
- package/src/admin/components/mpn/channels-tab.tsx +130 -0
- package/src/admin/components/mpn/constants.ts +8 -0
- package/src/admin/components/mpn/delivery-logs-tab.tsx +70 -0
- package/src/admin/components/mpn/event-mapping-form.tsx +191 -0
- package/src/admin/components/mpn/event-mappings-tab.tsx +165 -0
- package/src/admin/components/mpn/header-bar.tsx +35 -0
- package/src/admin/components/mpn/index.ts +17 -0
- package/src/admin/components/mpn/overview-cards.tsx +103 -0
- package/src/admin/components/mpn/sandbox-tab.tsx +93 -0
- package/src/admin/components/mpn/template-editor-section.tsx +203 -0
- package/src/admin/components/mpn/template-preview-section.tsx +122 -0
- package/src/admin/components/mpn/templates-list-section.tsx +115 -0
- package/src/admin/components/mpn/templates-tab.tsx +188 -0
- package/src/admin/components/mpn/use-notification-studio.ts +84 -0
- package/src/admin/components/rbac-guard.tsx +153 -0
- package/src/admin/i18n/README.md +134 -0
- package/src/admin/i18n/index.ts +23 -0
- package/src/admin/i18n/json/ar.json +157 -0
- package/src/admin/i18n/json/de.json +157 -0
- package/src/admin/i18n/json/en.json +157 -0
- package/src/admin/i18n/json/es.json +157 -0
- package/src/admin/i18n/json/fr.json +157 -0
- package/src/admin/i18n/json/it.json +157 -0
- package/src/admin/i18n/json/nl.json +157 -0
- package/src/admin/i18n/json/pl.json +157 -0
- package/src/admin/i18n/json/pt.json +157 -0
- package/src/admin/i18n/json/tr.json +157 -0
- package/src/admin/routes/mpn/broadcasts/page.tsx +75 -0
- package/src/admin/routes/mpn/channels/page.tsx +66 -0
- package/src/admin/routes/mpn/logs/page.tsx +50 -0
- package/src/admin/routes/mpn/mappings/page.tsx +62 -0
- package/src/admin/routes/mpn/page.tsx +200 -0
- package/src/admin/routes/mpn/sandbox/page.tsx +53 -0
- package/src/admin/routes/mpn/templates/page.tsx +55 -0
- package/src/admin/tsconfig.json +23 -0
- package/src/admin/vite-env.d.ts +1 -0
- package/src/admin/widgets/order-notifications-widget.tsx +178 -0
- package/src/api/README.md +98 -0
- package/src/api/admin/mpn/broadcasts/route.ts +130 -0
- package/src/api/admin/mpn/channels/route.ts +143 -0
- package/src/api/admin/mpn/logs/route.ts +41 -0
- package/src/api/admin/mpn/mappings/[id]/route.ts +45 -0
- package/src/api/admin/mpn/mappings/route.ts +68 -0
- package/src/api/admin/mpn/render-template/route.ts +46 -0
- package/src/api/admin/mpn/sandbox/route.ts +51 -0
- package/src/api/admin/mpn/send/route.ts +163 -0
- package/src/api/admin/mpn/templates/[id]/route.ts +101 -0
- package/src/api/admin/mpn/templates/route.ts +99 -0
- package/src/api/admin/plugin/route.ts +5 -0
- package/src/api/middlewares.ts +42 -0
- package/src/api/store/mpn/consent/route.ts +45 -0
- package/src/api/store/mpn/webhooks/[provider]/route.ts +119 -0
- package/src/api/store/plugin/route.ts +5 -0
- package/src/api/validators.ts +149 -0
- package/src/hooks/index.ts +14 -0
- package/src/hooks/use-mpn-channels.ts +43 -0
- package/src/hooks/use-mpn-dispatch.ts +149 -0
- package/src/hooks/use-mpn-mappings.ts +75 -0
- package/src/hooks/use-mpn-templates.ts +120 -0
- package/src/hooks/use-order-notifications-widget-state.ts +103 -0
- package/src/hooks/use-sdk-primitives.ts +157 -0
- package/src/index.ts +16 -0
- package/src/integrations/automations-action-handler.ts +156 -0
- package/src/jobs/README.md +41 -0
- package/src/jobs/abandoned-cart-recovery.ts +152 -0
- package/src/jobs/log-retention-and-broadcasts.ts +23 -0
- package/src/links/README.md +52 -0
- package/src/modules/README.md +99 -0
- package/src/modules/notifications/i18n/index.ts +83 -0
- package/src/modules/notifications/i18n/locales/ar.ts +84 -0
- package/src/modules/notifications/i18n/locales/de.ts +83 -0
- package/src/modules/notifications/i18n/locales/en.ts +84 -0
- package/src/modules/notifications/i18n/locales/es.ts +84 -0
- package/src/modules/notifications/i18n/locales/fr.ts +82 -0
- package/src/modules/notifications/i18n/locales/it.ts +82 -0
- package/src/modules/notifications/i18n/locales/nl.ts +83 -0
- package/src/modules/notifications/i18n/locales/pl.ts +84 -0
- package/src/modules/notifications/i18n/locales/pt.ts +82 -0
- package/src/modules/notifications/i18n/locales/tr.ts +86 -0
- package/src/modules/notifications/i18n/types.ts +1 -0
- package/src/modules/notifications/index.ts +10 -0
- package/src/modules/notifications/integrations/documents-and-b2b-resolver.ts +168 -0
- package/src/modules/notifications/migrations/Migration20261010000000.ts +185 -0
- package/src/modules/notifications/models/index.ts +125 -0
- package/src/modules/notifications/renderer/block-helpers.ts +51 -0
- package/src/modules/notifications/renderer/email.ts +281 -0
- package/src/modules/notifications/renderer/index.ts +301 -0
- package/src/modules/notifications/renderer/slack.ts +168 -0
- package/src/modules/notifications/renderer/telegram.ts +161 -0
- package/src/modules/notifications/renderer/whatsapp.ts +156 -0
- package/src/modules/notifications/service-helpers/channel-config.ts +65 -0
- package/src/modules/notifications/service-helpers/consent.ts +88 -0
- package/src/modules/notifications/service-helpers/delivery-log.ts +56 -0
- package/src/modules/notifications/service-helpers/event-conditions.ts +66 -0
- package/src/modules/notifications/service-helpers/external-templates.ts +127 -0
- package/src/modules/notifications/service-helpers/recipient-resolution.ts +124 -0
- package/src/modules/notifications/service-helpers/sender-profiles.ts +34 -0
- package/src/modules/notifications/service-helpers/template-blocks.ts +116 -0
- package/src/modules/notifications/service-helpers/template-catalog.ts +170 -0
- package/src/modules/notifications/service-helpers/template-render.ts +113 -0
- package/src/modules/notifications/service.ts +588 -0
- package/src/modules/notifications/types.ts +1 -0
- package/src/modules/notifications/utils/auth-presets.ts +145 -0
- package/src/modules/notifications/utils/interpolation.ts +231 -0
- package/src/modules/notifications/utils/security.ts +378 -0
- package/src/modules/notifications/utils/system-presets.ts +695 -0
- package/src/modules/notifications/utils/transformers.ts +456 -0
- package/src/providers/README.md +92 -0
- package/src/providers/dummy-sandbox/index.ts +143 -0
- package/src/providers/email-smtp/index.ts +257 -0
- package/src/providers/index.ts +38 -0
- package/src/providers/slack/index.ts +82 -0
- package/src/providers/sms/index.ts +342 -0
- package/src/providers/telegram/index.ts +181 -0
- package/src/providers/whatsapp/index.ts +505 -0
- package/src/rbac.ts +123 -0
- package/src/sdk/client.ts +319 -0
- package/src/sdk/index.ts +48 -0
- package/src/sdk/query-keys.ts +89 -0
- package/src/subscribers/README.md +50 -0
- package/src/subscribers/customer-and-document-events.ts +36 -0
- package/src/subscribers/fulfillment-and-inventory-events.ts +35 -0
- package/src/subscribers/order-events.ts +31 -0
- package/src/types/constants.ts +143 -0
- package/src/types/core.ts +96 -0
- package/src/types/domain.ts +84 -0
- package/src/types/enums.ts +73 -0
- package/src/types/errors.ts +32 -0
- package/src/types/hooks.ts +48 -0
- package/src/types/index.ts +11 -0
- package/src/types/providers.ts +96 -0
- package/src/types/records.ts +100 -0
- package/src/types/render.ts +74 -0
- package/src/types/sdk-api.ts +228 -0
- package/src/types/translations.ts +75 -0
- package/src/workflows/README.md +67 -0
- package/src/workflows/index.ts +619 -0
- package/tsconfig.json +40 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
|
|
3
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
4
|
+
import {
|
|
5
|
+
NOTIFICATIONS_MODULE,
|
|
6
|
+
type NotificationChannel,
|
|
7
|
+
} from "../../../../modules/notifications/types"
|
|
8
|
+
import { CreateBroadcastSchema } from "../../../validators"
|
|
9
|
+
|
|
10
|
+
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
11
|
+
const mpnService = req.scope.resolve(
|
|
12
|
+
NOTIFICATIONS_MODULE
|
|
13
|
+
) as NotificationsModuleService
|
|
14
|
+
|
|
15
|
+
const broadcasts = await mpnService.listMpnBroadcastCampaigns({})
|
|
16
|
+
const consents = await mpnService.listMpnConsentRecords({})
|
|
17
|
+
|
|
18
|
+
const optedInSms = consents.filter(
|
|
19
|
+
(c: any) => c.channel === "sms" && c.status === "opted_in"
|
|
20
|
+
).length
|
|
21
|
+
const optedInWhatsApp = consents.filter(
|
|
22
|
+
(c: any) => c.channel === "whatsapp" && c.status === "opted_in"
|
|
23
|
+
).length
|
|
24
|
+
const optedInTelegram = consents.filter(
|
|
25
|
+
(c: any) => c.channel === "telegram" && c.status === "opted_in"
|
|
26
|
+
).length
|
|
27
|
+
const optedOutCount = consents.filter(
|
|
28
|
+
(c: any) => c.status === "opted_out"
|
|
29
|
+
).length
|
|
30
|
+
|
|
31
|
+
return res.json({
|
|
32
|
+
success: true,
|
|
33
|
+
broadcasts,
|
|
34
|
+
consent_summary: {
|
|
35
|
+
total_records: consents.length,
|
|
36
|
+
opted_in_sms: optedInSms,
|
|
37
|
+
opted_in_whatsapp: optedInWhatsApp,
|
|
38
|
+
opted_in_telegram: optedInTelegram,
|
|
39
|
+
opted_out_total: optedOutCount,
|
|
40
|
+
},
|
|
41
|
+
consent_records: consents.slice(0, 50),
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
46
|
+
const parsed = CreateBroadcastSchema.safeParse(req.body || {})
|
|
47
|
+
if (!parsed.success) {
|
|
48
|
+
return res.status(400).json({
|
|
49
|
+
success: false,
|
|
50
|
+
code: "VALIDATION_ERROR",
|
|
51
|
+
message: "Invalid broadcast campaign payload",
|
|
52
|
+
details: parsed.error.flatten(),
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const mpnService = req.scope.resolve(
|
|
57
|
+
NOTIFICATIONS_MODULE
|
|
58
|
+
) as NotificationsModuleService
|
|
59
|
+
const input = parsed.data
|
|
60
|
+
|
|
61
|
+
// Collect opted-in recipients for the selected channel
|
|
62
|
+
const optedInRows = await mpnService.listMpnConsentRecords({
|
|
63
|
+
channel: input.channel,
|
|
64
|
+
status: "opted_in",
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const recipients =
|
|
68
|
+
Array.isArray(input.custom_recipients) && input.custom_recipients.length > 0
|
|
69
|
+
? input.custom_recipients
|
|
70
|
+
: optedInRows.map((r: any) => r.identifier)
|
|
71
|
+
|
|
72
|
+
let sentCount = 0
|
|
73
|
+
let failedCount = 0
|
|
74
|
+
|
|
75
|
+
if (input.send_now) {
|
|
76
|
+
for (const recipient of recipients) {
|
|
77
|
+
// Re-verify consent immediately before each send (@signalhouse-sms pattern)
|
|
78
|
+
const check = await mpnService.verifyRecipientConsent({
|
|
79
|
+
recipient,
|
|
80
|
+
channel: input.channel as NotificationChannel,
|
|
81
|
+
requireExplicitOptIn:
|
|
82
|
+
input.channel === "sms" || input.channel === "whatsapp",
|
|
83
|
+
})
|
|
84
|
+
if (!check.allowed) {
|
|
85
|
+
failedCount++
|
|
86
|
+
continue
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const rendered = await mpnService.renderTemplateById({
|
|
91
|
+
template_id: input.template_id,
|
|
92
|
+
append_sms_consent_footer: check.appendStopFooter,
|
|
93
|
+
})
|
|
94
|
+
await mpnService.captureSandboxMessage({
|
|
95
|
+
channel: input.channel as NotificationChannel,
|
|
96
|
+
recipient,
|
|
97
|
+
subject: rendered.subject || input.name,
|
|
98
|
+
body: rendered.text,
|
|
99
|
+
html: rendered.html,
|
|
100
|
+
})
|
|
101
|
+
sentCount++
|
|
102
|
+
} catch {
|
|
103
|
+
failedCount++
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const campaign = await mpnService.createMpnBroadcastCampaigns({
|
|
109
|
+
name: input.name,
|
|
110
|
+
channel: input.channel,
|
|
111
|
+
template_id: input.template_id,
|
|
112
|
+
customer_group_id: input.customer_group_id ?? null,
|
|
113
|
+
custom_recipients: (input.custom_recipients as any) ?? null,
|
|
114
|
+
status: input.send_now
|
|
115
|
+
? "completed"
|
|
116
|
+
: input.scheduled_at
|
|
117
|
+
? "scheduled"
|
|
118
|
+
: "draft",
|
|
119
|
+
scheduled_at: input.scheduled_at ? new Date(input.scheduled_at) : null,
|
|
120
|
+
sent_at: input.send_now ? new Date() : null,
|
|
121
|
+
total_recipients: recipients.length,
|
|
122
|
+
sent_count: sentCount,
|
|
123
|
+
failed_count: failedCount,
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
return res.status(201).json({
|
|
127
|
+
success: true,
|
|
128
|
+
broadcast: campaign,
|
|
129
|
+
})
|
|
130
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
|
|
3
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
4
|
+
import {
|
|
5
|
+
NOTIFICATION_CHANNELS,
|
|
6
|
+
NOTIFICATIONS_MODULE,
|
|
7
|
+
SMS_DRIVERS,
|
|
8
|
+
WHATSAPP_DRIVERS,
|
|
9
|
+
} from "../../../../modules/notifications/types"
|
|
10
|
+
import { isValidTelegramRecipient } from "../../../../modules/notifications/utils/security"
|
|
11
|
+
import { UpdateChannelConfigSchema } from "../../../validators"
|
|
12
|
+
|
|
13
|
+
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
14
|
+
const mpnService = req.scope.resolve(
|
|
15
|
+
NOTIFICATIONS_MODULE
|
|
16
|
+
) as NotificationsModuleService
|
|
17
|
+
|
|
18
|
+
const channels: Array<Record<string, unknown>> = []
|
|
19
|
+
for (const ch of NOTIFICATION_CHANNELS) {
|
|
20
|
+
const defaultDriver =
|
|
21
|
+
ch === "whatsapp"
|
|
22
|
+
? process.env.WHATSAPP_DRIVER || "meta-cloud"
|
|
23
|
+
: ch === "sms"
|
|
24
|
+
? process.env.SMS_DRIVER || "twilio"
|
|
25
|
+
: ch === "email"
|
|
26
|
+
? "smtp"
|
|
27
|
+
: ch === "dummy"
|
|
28
|
+
? "sandbox"
|
|
29
|
+
: ch
|
|
30
|
+
const cfg = await mpnService.getOrCreateChannelConfig(ch, defaultDriver)
|
|
31
|
+
|
|
32
|
+
const envStatus: Record<string, boolean> = {}
|
|
33
|
+
if (ch === "email") {
|
|
34
|
+
envStatus.smtp_configured = Boolean(
|
|
35
|
+
process.env.MAILER_SMTP_HOST && process.env.MAILER_SMTP_USER
|
|
36
|
+
)
|
|
37
|
+
} else if (ch === "whatsapp") {
|
|
38
|
+
envStatus.meta_cloud_configured = Boolean(
|
|
39
|
+
process.env.WHATSAPP_ACCESS_TOKEN &&
|
|
40
|
+
process.env.WHATSAPP_PHONE_NUMBER_ID
|
|
41
|
+
)
|
|
42
|
+
envStatus.openwa_configured = Boolean(process.env.OPENWA_BASE_URL)
|
|
43
|
+
envStatus.waha_configured = Boolean(process.env.WAHA_BASE_URL)
|
|
44
|
+
envStatus.evolution_configured = Boolean(process.env.EVOLUTION_BASE_URL)
|
|
45
|
+
} else if (ch === "telegram") {
|
|
46
|
+
envStatus.bot_token_configured = Boolean(process.env.TELEGRAM_BOT_TOKEN)
|
|
47
|
+
} else if (ch === "sms") {
|
|
48
|
+
envStatus.twilio_configured = Boolean(
|
|
49
|
+
process.env.TWILIO_ACCOUNT_SID && process.env.TWILIO_AUTH_TOKEN
|
|
50
|
+
)
|
|
51
|
+
envStatus.signalhouse_configured = Boolean(
|
|
52
|
+
process.env.SIGNALHOUSE_API_KEY
|
|
53
|
+
)
|
|
54
|
+
envStatus.msg91_configured = Boolean(process.env.MSG91_AUTH_KEY)
|
|
55
|
+
envStatus.smsapi_configured = Boolean(process.env.SMSAPI_TOKEN)
|
|
56
|
+
} else if (ch === "slack") {
|
|
57
|
+
envStatus.webhook_configured = Boolean(process.env.SLACK_WEBHOOK_URL)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
channels.push({
|
|
61
|
+
...cfg,
|
|
62
|
+
env_status: envStatus,
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return res.json({
|
|
67
|
+
success: true,
|
|
68
|
+
channels,
|
|
69
|
+
sender_profiles: mpnService.getSenderProfiles(),
|
|
70
|
+
whatsapp_drivers: WHATSAPP_DRIVERS,
|
|
71
|
+
sms_drivers: SMS_DRIVERS,
|
|
72
|
+
})
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
76
|
+
const parsed = UpdateChannelConfigSchema.safeParse(req.body || {})
|
|
77
|
+
if (!parsed.success) {
|
|
78
|
+
return res.status(400).json({
|
|
79
|
+
success: false,
|
|
80
|
+
code: "VALIDATION_ERROR",
|
|
81
|
+
message: "Invalid channel configuration payload",
|
|
82
|
+
details: parsed.error.flatten(),
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const mpnService = req.scope.resolve(
|
|
87
|
+
NOTIFICATIONS_MODULE
|
|
88
|
+
) as NotificationsModuleService
|
|
89
|
+
const input = parsed.data
|
|
90
|
+
|
|
91
|
+
if (
|
|
92
|
+
input.channel === "telegram" &&
|
|
93
|
+
input.config_json?.merchant_channel_id &&
|
|
94
|
+
!isValidTelegramRecipient(String(input.config_json.merchant_channel_id))
|
|
95
|
+
) {
|
|
96
|
+
return res.status(400).json({
|
|
97
|
+
success: false,
|
|
98
|
+
code: "INVALID_TELEGRAM_CHANNEL_ID",
|
|
99
|
+
message:
|
|
100
|
+
"Telegram channel ID must be a supergroup/channel ID (-100...), @channel_handle, or numeric chat_id.",
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let verifiedFlag: boolean | undefined = undefined
|
|
105
|
+
let verificationResult: Record<string, any> | undefined = undefined
|
|
106
|
+
|
|
107
|
+
if (input.action === "verify") {
|
|
108
|
+
const targetRecipient =
|
|
109
|
+
input.test_recipient ||
|
|
110
|
+
(input.config_json?.merchant_channel_id as string) ||
|
|
111
|
+
process.env.TELEGRAM_MERCHANT_CHANNEL_ID ||
|
|
112
|
+
"sandbox-test"
|
|
113
|
+
|
|
114
|
+
await mpnService.captureSandboxMessage({
|
|
115
|
+
channel: input.channel,
|
|
116
|
+
recipient: targetRecipient,
|
|
117
|
+
subject: `Channel Verification (${input.channel})`,
|
|
118
|
+
body: `✅ Medusa Notification Studio verification test succeeded for channel '${input.channel}' (${input.driver || "default"}).`,
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
verifiedFlag = true
|
|
122
|
+
verificationResult = {
|
|
123
|
+
verified: true,
|
|
124
|
+
recipient: targetRecipient,
|
|
125
|
+
verified_at: new Date().toISOString(),
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const updated = await mpnService.saveChannelConfig({
|
|
130
|
+
channel: input.channel,
|
|
131
|
+
driver: input.driver,
|
|
132
|
+
is_enabled: input.is_enabled,
|
|
133
|
+
is_verified: verifiedFlag,
|
|
134
|
+
rate_limit_per_sec: input.rate_limit_per_sec,
|
|
135
|
+
config_json: input.config_json as Record<string, any>,
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
return res.json({
|
|
139
|
+
success: true,
|
|
140
|
+
channel_config: updated,
|
|
141
|
+
verification: verificationResult,
|
|
142
|
+
})
|
|
143
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
3
|
+
import { NOTIFICATIONS_MODULE } from "../../../../modules/notifications/types"
|
|
4
|
+
|
|
5
|
+
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
6
|
+
const mpnService = req.scope.resolve(
|
|
7
|
+
NOTIFICATIONS_MODULE
|
|
8
|
+
) as NotificationsModuleService
|
|
9
|
+
|
|
10
|
+
const filters: Record<string, any> = {}
|
|
11
|
+
if (typeof req.query.channel === "string" && req.query.channel) {
|
|
12
|
+
filters.channel = req.query.channel
|
|
13
|
+
}
|
|
14
|
+
if (typeof req.query.status === "string" && req.query.status) {
|
|
15
|
+
filters.status = req.query.status
|
|
16
|
+
}
|
|
17
|
+
if (typeof req.query.entity_id === "string" && req.query.entity_id) {
|
|
18
|
+
filters.entity_id = req.query.entity_id
|
|
19
|
+
}
|
|
20
|
+
if (typeof req.query.event_name === "string" && req.query.event_name) {
|
|
21
|
+
filters.event_name = req.query.event_name
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const limit = Math.min(100, Math.max(1, Number(req.query.limit || 50)))
|
|
25
|
+
const offset = Math.max(0, Number(req.query.offset || 0))
|
|
26
|
+
|
|
27
|
+
const allLogs = await mpnService.listMpnDeliveryLogs(filters)
|
|
28
|
+
const sorted = [...allLogs].toSorted((a: any, b: any) => {
|
|
29
|
+
const tA = a.created_at ? new Date(a.created_at).getTime() : 0
|
|
30
|
+
const tB = b.created_at ? new Date(b.created_at).getTime() : 0
|
|
31
|
+
return tB - tA
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
return res.json({
|
|
35
|
+
success: true,
|
|
36
|
+
logs: sorted.slice(offset, offset + limit),
|
|
37
|
+
count: sorted.length,
|
|
38
|
+
limit,
|
|
39
|
+
offset,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
|
|
3
|
+
import type NotificationsModuleService from "../../../../../modules/notifications/service"
|
|
4
|
+
import { NOTIFICATIONS_MODULE } from "../../../../../modules/notifications/types"
|
|
5
|
+
import { UpsertEventMappingSchema } from "../../../../validators"
|
|
6
|
+
|
|
7
|
+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
8
|
+
const id = req.params.id
|
|
9
|
+
const parsed = UpsertEventMappingSchema.partial().safeParse(req.body || {})
|
|
10
|
+
if (!parsed.success) {
|
|
11
|
+
return res.status(400).json({
|
|
12
|
+
success: false,
|
|
13
|
+
code: "VALIDATION_ERROR",
|
|
14
|
+
message: "Invalid mapping update payload",
|
|
15
|
+
details: parsed.error.flatten(),
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const mpnService = req.scope.resolve(
|
|
20
|
+
NOTIFICATIONS_MODULE
|
|
21
|
+
) as NotificationsModuleService
|
|
22
|
+
|
|
23
|
+
const updated = await mpnService.updateMpnEventMappings({
|
|
24
|
+
id,
|
|
25
|
+
...(parsed.data as any),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
return res.json({
|
|
29
|
+
success: true,
|
|
30
|
+
mapping: updated,
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
35
|
+
const id = req.params.id
|
|
36
|
+
const mpnService = req.scope.resolve(
|
|
37
|
+
NOTIFICATIONS_MODULE
|
|
38
|
+
) as NotificationsModuleService
|
|
39
|
+
|
|
40
|
+
await mpnService.deleteMpnEventMappings([id])
|
|
41
|
+
return res.json({
|
|
42
|
+
success: true,
|
|
43
|
+
deleted: id,
|
|
44
|
+
})
|
|
45
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
|
|
3
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
4
|
+
import {
|
|
5
|
+
NOTIFICATIONS_MODULE,
|
|
6
|
+
RECIPIENT_RULES,
|
|
7
|
+
SUPPORTED_EVENTS,
|
|
8
|
+
} from "../../../../modules/notifications/types"
|
|
9
|
+
import { UpsertEventMappingSchema } from "../../../validators"
|
|
10
|
+
|
|
11
|
+
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
12
|
+
const mpnService = req.scope.resolve(
|
|
13
|
+
NOTIFICATIONS_MODULE
|
|
14
|
+
) as NotificationsModuleService
|
|
15
|
+
|
|
16
|
+
const query: Record<string, any> = {}
|
|
17
|
+
if (typeof req.query.event_name === "string") {
|
|
18
|
+
query.event_name = req.query.event_name
|
|
19
|
+
}
|
|
20
|
+
if (typeof req.query.channel === "string") {
|
|
21
|
+
query.channel = req.query.channel
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const mappings = await mpnService.listMpnEventMappings(query)
|
|
25
|
+
return res.json({
|
|
26
|
+
success: true,
|
|
27
|
+
mappings,
|
|
28
|
+
supported_events: SUPPORTED_EVENTS,
|
|
29
|
+
recipient_rules: RECIPIENT_RULES,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
34
|
+
const parsed = UpsertEventMappingSchema.safeParse(req.body || {})
|
|
35
|
+
if (!parsed.success) {
|
|
36
|
+
return res.status(400).json({
|
|
37
|
+
success: false,
|
|
38
|
+
code: "VALIDATION_ERROR",
|
|
39
|
+
message: "Invalid event mapping payload",
|
|
40
|
+
details: parsed.error.flatten(),
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const mpnService = req.scope.resolve(
|
|
45
|
+
NOTIFICATIONS_MODULE
|
|
46
|
+
) as NotificationsModuleService
|
|
47
|
+
const data = parsed.data
|
|
48
|
+
|
|
49
|
+
const created = await mpnService.createMpnEventMappings({
|
|
50
|
+
event_name: data.event_name,
|
|
51
|
+
channel: data.channel,
|
|
52
|
+
template_id: data.template_id,
|
|
53
|
+
recipient_rule: data.recipient_rule,
|
|
54
|
+
custom_recipient: data.custom_recipient ?? null,
|
|
55
|
+
sender_profile_index: data.sender_profile_index ?? 1,
|
|
56
|
+
telegram_thread_id: data.telegram_thread_id ?? null,
|
|
57
|
+
attach_document_types: (data.attach_document_types as any) ?? null,
|
|
58
|
+
variable_map: data.variable_map ?? null,
|
|
59
|
+
conditions: (data.conditions as any) ?? null,
|
|
60
|
+
is_active: data.is_active ?? true,
|
|
61
|
+
priority: data.priority ?? 0,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
return res.status(201).json({
|
|
65
|
+
success: true,
|
|
66
|
+
mapping: created,
|
|
67
|
+
})
|
|
68
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
|
|
3
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
4
|
+
import { NOTIFICATIONS_MODULE } from "../../../../modules/notifications/types"
|
|
5
|
+
import { hydrateEntityContextData } from "../../../../workflows"
|
|
6
|
+
import { RenderTemplatePreviewSchema } from "../../../validators"
|
|
7
|
+
|
|
8
|
+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
9
|
+
const parsed = RenderTemplatePreviewSchema.safeParse(req.body || {})
|
|
10
|
+
if (!parsed.success) {
|
|
11
|
+
return res.status(400).json({
|
|
12
|
+
success: false,
|
|
13
|
+
code: "VALIDATION_ERROR",
|
|
14
|
+
message: "Invalid render preview request",
|
|
15
|
+
details: parsed.error.flatten(),
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const mpnService = req.scope.resolve(
|
|
20
|
+
NOTIFICATIONS_MODULE
|
|
21
|
+
) as NotificationsModuleService
|
|
22
|
+
const input = parsed.data
|
|
23
|
+
|
|
24
|
+
const hydrated = input.data
|
|
25
|
+
? { transformedData: input.data }
|
|
26
|
+
: await hydrateEntityContextData(req.scope, {
|
|
27
|
+
context_type: input.context_type,
|
|
28
|
+
entity_id: input.entity_id,
|
|
29
|
+
locale: input.locale,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const rendered = await mpnService.renderTemplateById({
|
|
33
|
+
template_id: input.template_id,
|
|
34
|
+
context_type: input.context_type,
|
|
35
|
+
data: hydrated.transformedData,
|
|
36
|
+
locale: input.locale,
|
|
37
|
+
subject: input.subject,
|
|
38
|
+
append_sms_consent_footer: input.append_sms_consent_footer,
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
return res.json({
|
|
42
|
+
success: true,
|
|
43
|
+
rendered,
|
|
44
|
+
context_sample: hydrated.transformedData,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
3
|
+
import { NOTIFICATIONS_MODULE } from "../../../../modules/notifications/types"
|
|
4
|
+
import {
|
|
5
|
+
clearInMemorySandboxMessages,
|
|
6
|
+
getInMemorySandboxMessages,
|
|
7
|
+
} from "../../../../providers/dummy-sandbox"
|
|
8
|
+
|
|
9
|
+
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
10
|
+
const mpnService = req.scope.resolve(
|
|
11
|
+
NOTIFICATIONS_MODULE
|
|
12
|
+
) as NotificationsModuleService
|
|
13
|
+
|
|
14
|
+
const limit = Math.min(100, Math.max(1, Number(req.query.limit || 50)))
|
|
15
|
+
const offset = Math.max(0, Number(req.query.offset || 0))
|
|
16
|
+
|
|
17
|
+
const dbMessages = await mpnService.listMpnSandboxMessages({})
|
|
18
|
+
const memMessages = getInMemorySandboxMessages(limit, offset).messages
|
|
19
|
+
|
|
20
|
+
const combined = [...dbMessages, ...memMessages]
|
|
21
|
+
const seen = new Set<string>()
|
|
22
|
+
const deduped = combined.filter((m: any) => {
|
|
23
|
+
const key = m.id || `${m.recipient}_${m.body}_${m.created_at}`
|
|
24
|
+
if (seen.has(key)) return false
|
|
25
|
+
seen.add(key)
|
|
26
|
+
return true
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
return res.json({
|
|
30
|
+
success: true,
|
|
31
|
+
messages: deduped.slice(offset, offset + limit),
|
|
32
|
+
count: deduped.length,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const DELETE = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
37
|
+
const mpnService = req.scope.resolve(
|
|
38
|
+
NOTIFICATIONS_MODULE
|
|
39
|
+
) as NotificationsModuleService
|
|
40
|
+
|
|
41
|
+
clearInMemorySandboxMessages()
|
|
42
|
+
const dbRows = await mpnService.listMpnSandboxMessages({})
|
|
43
|
+
if (dbRows.length > 0) {
|
|
44
|
+
await mpnService.deleteMpnSandboxMessages(dbRows.map((r: any) => r.id))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return res.json({
|
|
48
|
+
success: true,
|
|
49
|
+
cleared: dbRows.length,
|
|
50
|
+
})
|
|
51
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
|
|
2
|
+
import { Modules } from "@medusajs/framework/utils"
|
|
3
|
+
|
|
4
|
+
import { resolveOrderDocumentAttachments } from "../../../../modules/notifications/integrations/documents-and-b2b-resolver"
|
|
5
|
+
import type NotificationsModuleService from "../../../../modules/notifications/service"
|
|
6
|
+
import {
|
|
7
|
+
NOTIFICATIONS_MODULE,
|
|
8
|
+
type NotificationChannel,
|
|
9
|
+
} from "../../../../modules/notifications/types"
|
|
10
|
+
import {
|
|
11
|
+
executeEventNotificationsDispatch,
|
|
12
|
+
hydrateEntityContextData,
|
|
13
|
+
} from "../../../../workflows"
|
|
14
|
+
import { ManualSendSchema } from "../../../validators"
|
|
15
|
+
|
|
16
|
+
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
|
17
|
+
const parsed = ManualSendSchema.safeParse(req.body || {})
|
|
18
|
+
if (!parsed.success) {
|
|
19
|
+
return res.status(400).json({
|
|
20
|
+
success: false,
|
|
21
|
+
code: "VALIDATION_ERROR",
|
|
22
|
+
message: "Invalid manual send payload",
|
|
23
|
+
details: parsed.error.flatten(),
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const input = parsed.data
|
|
28
|
+
const mpnService = req.scope.resolve(
|
|
29
|
+
NOTIFICATIONS_MODULE
|
|
30
|
+
) as NotificationsModuleService
|
|
31
|
+
|
|
32
|
+
// Mode 1: Trigger all active mappings for an event (e.g. from Order Widget "Trigger Event")
|
|
33
|
+
if (input.event_name && !input.template_id) {
|
|
34
|
+
const outcome = await executeEventNotificationsDispatch(req.scope, {
|
|
35
|
+
event_name: input.event_name,
|
|
36
|
+
event_data: {
|
|
37
|
+
id: input.order_id || input.entity_id,
|
|
38
|
+
order_id: input.order_id,
|
|
39
|
+
...input.custom_data,
|
|
40
|
+
},
|
|
41
|
+
force_send: true,
|
|
42
|
+
})
|
|
43
|
+
return res.json({
|
|
44
|
+
success: true,
|
|
45
|
+
mode: "event_dispatch",
|
|
46
|
+
outcome,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!input.template_id) {
|
|
51
|
+
return res.status(400).json({
|
|
52
|
+
success: false,
|
|
53
|
+
code: "MISSING_TEMPLATE_OR_EVENT",
|
|
54
|
+
message: "Provide either template_id or event_name",
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Mode 2: Direct template send / resend to a specific recipient or order
|
|
59
|
+
const hydrated = await hydrateEntityContextData(req.scope, {
|
|
60
|
+
context_type: input.order_id ? "order" : undefined,
|
|
61
|
+
entity_id: input.order_id || input.entity_id,
|
|
62
|
+
event_data: input.custom_data || {},
|
|
63
|
+
locale: input.locale,
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const attachments =
|
|
67
|
+
input.order_id &&
|
|
68
|
+
Array.isArray(input.attach_document_types) &&
|
|
69
|
+
input.attach_document_types.length > 0
|
|
70
|
+
? await resolveOrderDocumentAttachments(
|
|
71
|
+
req.scope,
|
|
72
|
+
input.order_id,
|
|
73
|
+
input.attach_document_types
|
|
74
|
+
)
|
|
75
|
+
: []
|
|
76
|
+
|
|
77
|
+
const rendered = await mpnService.renderTemplateById({
|
|
78
|
+
template_id: input.template_id,
|
|
79
|
+
data: {
|
|
80
|
+
...hydrated.transformedData,
|
|
81
|
+
...input.custom_data,
|
|
82
|
+
},
|
|
83
|
+
locale: input.locale,
|
|
84
|
+
telegram_thread_id: input.telegram_thread_id,
|
|
85
|
+
attachments,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const channel: NotificationChannel = input.channel || rendered.channel
|
|
89
|
+
const recipient =
|
|
90
|
+
input.recipient?.trim() ||
|
|
91
|
+
mpnService.resolveRecipientForRule({
|
|
92
|
+
rule: "customer_auto",
|
|
93
|
+
channel,
|
|
94
|
+
data: hydrated.transformedData,
|
|
95
|
+
}) ||
|
|
96
|
+
"sandbox-recipient"
|
|
97
|
+
|
|
98
|
+
let externalId = `manual_${channel}_${Date.now()}`
|
|
99
|
+
try {
|
|
100
|
+
const notificationModule = req.scope.resolve(Modules.NOTIFICATION) as any
|
|
101
|
+
if (
|
|
102
|
+
notificationModule &&
|
|
103
|
+
typeof notificationModule.createNotifications === "function"
|
|
104
|
+
) {
|
|
105
|
+
const notifRes = await notificationModule.createNotifications({
|
|
106
|
+
to: recipient,
|
|
107
|
+
channel,
|
|
108
|
+
template: rendered.template_name,
|
|
109
|
+
content: {
|
|
110
|
+
subject: rendered.subject,
|
|
111
|
+
html: rendered.html,
|
|
112
|
+
text: rendered.text,
|
|
113
|
+
},
|
|
114
|
+
data: {
|
|
115
|
+
...hydrated.transformedData,
|
|
116
|
+
subject: rendered.subject,
|
|
117
|
+
html: rendered.html,
|
|
118
|
+
text: rendered.text,
|
|
119
|
+
blocks_json: rendered.blocks_json,
|
|
120
|
+
telegram_options: rendered.telegram_options,
|
|
121
|
+
whatsapp_options: rendered.whatsapp_options,
|
|
122
|
+
sms_options: rendered.sms_options,
|
|
123
|
+
},
|
|
124
|
+
attachments: rendered.attachments,
|
|
125
|
+
})
|
|
126
|
+
if (notifRes?.id) externalId = String(notifRes.id)
|
|
127
|
+
}
|
|
128
|
+
} catch {
|
|
129
|
+
// Always capture in Sandbox so Admin test sends are inspectable even without live credentials
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
await mpnService.captureSandboxMessage({
|
|
133
|
+
channel,
|
|
134
|
+
recipient,
|
|
135
|
+
subject: rendered.subject,
|
|
136
|
+
body: rendered.text,
|
|
137
|
+
html: rendered.html,
|
|
138
|
+
attachments_meta: rendered.attachments?.map((a) => ({
|
|
139
|
+
filename: a.filename,
|
|
140
|
+
content_type: a.content_type,
|
|
141
|
+
})),
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const logEntry = await mpnService.recordDeliveryLog({
|
|
145
|
+
idempotency_key: `manual:${Date.now()}:${channel}:${rendered.template_id}:${recipient}`,
|
|
146
|
+
event_name: input.event_name || "manual.send",
|
|
147
|
+
entity_id: input.order_id || input.entity_id || hydrated.entityId,
|
|
148
|
+
channel,
|
|
149
|
+
template_id: rendered.template_id,
|
|
150
|
+
recipient,
|
|
151
|
+
status: "sent",
|
|
152
|
+
external_message_id: externalId,
|
|
153
|
+
payload: rendered,
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
return res.json({
|
|
157
|
+
success: true,
|
|
158
|
+
mode: "direct_template_send",
|
|
159
|
+
external_message_id: externalId,
|
|
160
|
+
log: logEntry,
|
|
161
|
+
rendered,
|
|
162
|
+
})
|
|
163
|
+
}
|