@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.
- 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 +340 -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 +133 -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 +589 -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,188 @@
|
|
|
1
|
+
import { Toaster, toast } from "@medusajs/ui"
|
|
2
|
+
import React, { useState } from "react"
|
|
3
|
+
import { useTranslation } from "react-i18next"
|
|
4
|
+
import {
|
|
5
|
+
useCreateOrCloneMpnTemplate,
|
|
6
|
+
useRenderMpnTemplatePreview,
|
|
7
|
+
useUpdateMpnTemplate,
|
|
8
|
+
} from "../../../hooks"
|
|
9
|
+
import { notificationsAdminSdk } from "../../../sdk"
|
|
10
|
+
import type { StatusBannerState } from "./constants"
|
|
11
|
+
import { TemplateEditorSection } from "./template-editor-section"
|
|
12
|
+
import { TemplatesListSection } from "./templates-list-section"
|
|
13
|
+
|
|
14
|
+
export interface TemplatesBuilderTabProps {
|
|
15
|
+
templates: Array<Record<string, any>>
|
|
16
|
+
onRefresh: () => Promise<void>
|
|
17
|
+
onStatus?: (status: StatusBannerState) => void
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const TemplatesBuilderTab: React.FC<TemplatesBuilderTabProps> = ({
|
|
21
|
+
templates,
|
|
22
|
+
onRefresh,
|
|
23
|
+
onStatus,
|
|
24
|
+
}) => {
|
|
25
|
+
const { t } = useTranslation()
|
|
26
|
+
const [channelFilter, setChannelFilter] = useState<string>("all")
|
|
27
|
+
const [selectedTemplate, setSelectedTemplate] = useState<Record<
|
|
28
|
+
string,
|
|
29
|
+
any
|
|
30
|
+
> | null>(null)
|
|
31
|
+
const [selectedBlocks, setSelectedBlocks] = useState<
|
|
32
|
+
Array<Record<string, any>>
|
|
33
|
+
>([])
|
|
34
|
+
const [previewContextType, setPreviewContextType] = useState<string>("order")
|
|
35
|
+
const [previewLocale, setPreviewLocale] = useState<string>("en")
|
|
36
|
+
const [previewResult, setPreviewResult] = useState<Record<
|
|
37
|
+
string,
|
|
38
|
+
any
|
|
39
|
+
> | null>(null)
|
|
40
|
+
|
|
41
|
+
const renderPreviewMutation = useRenderMpnTemplatePreview()
|
|
42
|
+
const cloneTemplateMutation = useCreateOrCloneMpnTemplate()
|
|
43
|
+
const updateTemplateMutation = useUpdateMpnTemplate()
|
|
44
|
+
|
|
45
|
+
const handlePreviewTemplate = async (
|
|
46
|
+
templateId: string,
|
|
47
|
+
contextType: string,
|
|
48
|
+
locale: string
|
|
49
|
+
) => {
|
|
50
|
+
try {
|
|
51
|
+
const data = await renderPreviewMutation.mutateAsync({
|
|
52
|
+
template_id: templateId,
|
|
53
|
+
context_type: contextType,
|
|
54
|
+
locale,
|
|
55
|
+
append_sms_consent_footer: true,
|
|
56
|
+
})
|
|
57
|
+
setPreviewResult(data.rendered || null)
|
|
58
|
+
} catch {
|
|
59
|
+
// Ignore preview errors
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const handleSelectTemplate = async (templateId: string) => {
|
|
64
|
+
try {
|
|
65
|
+
const data = await notificationsAdminSdk.getTemplate(templateId)
|
|
66
|
+
setSelectedTemplate(data.template || null)
|
|
67
|
+
setSelectedBlocks(data.flat_blocks || [])
|
|
68
|
+
await handlePreviewTemplate(templateId, previewContextType, previewLocale)
|
|
69
|
+
} catch {
|
|
70
|
+
// Ignore selection error
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const handleClonePresetToDb = async (presetId: string) => {
|
|
75
|
+
try {
|
|
76
|
+
const data = await cloneTemplateMutation.mutateAsync({
|
|
77
|
+
clone_from_preset_id: presetId,
|
|
78
|
+
})
|
|
79
|
+
const msg = t("messages.clonedPresetSuccess", {
|
|
80
|
+
label: data.template?.label || presetId,
|
|
81
|
+
})
|
|
82
|
+
toast.success(msg)
|
|
83
|
+
onStatus?.({ type: "success", text: msg })
|
|
84
|
+
await onRefresh()
|
|
85
|
+
if (data.template?.id) {
|
|
86
|
+
await handleSelectTemplate(data.template.id)
|
|
87
|
+
}
|
|
88
|
+
} catch {
|
|
89
|
+
const msg = t("messages.clonePresetError")
|
|
90
|
+
toast.error(msg)
|
|
91
|
+
onStatus?.({ type: "error", text: msg })
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const handleSaveDbTemplate = async () => {
|
|
96
|
+
if (!selectedTemplate || selectedTemplate.source === "system") return
|
|
97
|
+
try {
|
|
98
|
+
await updateTemplateMutation.mutateAsync({
|
|
99
|
+
id: selectedTemplate.id,
|
|
100
|
+
payload: {
|
|
101
|
+
label: selectedTemplate.label,
|
|
102
|
+
subject: selectedTemplate.subject,
|
|
103
|
+
mode: selectedTemplate.mode,
|
|
104
|
+
raw_content: selectedTemplate.raw_content,
|
|
105
|
+
meta_template_name: selectedTemplate.meta_template_name,
|
|
106
|
+
blocks: selectedBlocks,
|
|
107
|
+
},
|
|
108
|
+
})
|
|
109
|
+
const msg = t("messages.savedTemplateSuccess")
|
|
110
|
+
toast.success(msg)
|
|
111
|
+
onStatus?.({ type: "success", text: msg })
|
|
112
|
+
await handlePreviewTemplate(
|
|
113
|
+
selectedTemplate.id,
|
|
114
|
+
previewContextType,
|
|
115
|
+
previewLocale
|
|
116
|
+
)
|
|
117
|
+
await onRefresh()
|
|
118
|
+
} catch {
|
|
119
|
+
const msg = t("messages.saveTemplateError")
|
|
120
|
+
toast.error(msg)
|
|
121
|
+
onStatus?.({ type: "error", text: msg })
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const handleAddBlock = (type: string) => {
|
|
126
|
+
const nextPos = selectedBlocks.length
|
|
127
|
+
setSelectedBlocks([
|
|
128
|
+
...selectedBlocks,
|
|
129
|
+
{
|
|
130
|
+
id: `blk_${Date.now()}`,
|
|
131
|
+
type,
|
|
132
|
+
position: nextPos,
|
|
133
|
+
parent_id: null,
|
|
134
|
+
metadata:
|
|
135
|
+
type === "row"
|
|
136
|
+
? { label: "Field", value: "{{data.total_formatted}}" }
|
|
137
|
+
: type === "button" || type === "actions"
|
|
138
|
+
? { text: "View Order", url: "{{data.order_url}}" }
|
|
139
|
+
: { text: "New content block ({{data.first_name}})" },
|
|
140
|
+
},
|
|
141
|
+
])
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<div className="grid grid-cols-1 gap-4 lg:grid-cols-12">
|
|
146
|
+
<Toaster />
|
|
147
|
+
<TemplatesListSection
|
|
148
|
+
templates={templates}
|
|
149
|
+
channelFilter={channelFilter}
|
|
150
|
+
onChannelFilterChange={setChannelFilter}
|
|
151
|
+
onSelectTemplate={(id) => void handleSelectTemplate(id)}
|
|
152
|
+
onClonePreset={(id) => void handleClonePresetToDb(id)}
|
|
153
|
+
isCloning={cloneTemplateMutation.isPending}
|
|
154
|
+
/>
|
|
155
|
+
|
|
156
|
+
<TemplateEditorSection
|
|
157
|
+
selectedTemplate={selectedTemplate}
|
|
158
|
+
selectedBlocks={selectedBlocks}
|
|
159
|
+
previewContextType={previewContextType}
|
|
160
|
+
previewLocale={previewLocale}
|
|
161
|
+
previewResult={previewResult}
|
|
162
|
+
isSaving={updateTemplateMutation.isPending}
|
|
163
|
+
onTemplateChange={setSelectedTemplate}
|
|
164
|
+
onAddBlock={handleAddBlock}
|
|
165
|
+
onRemoveBlock={(idx) =>
|
|
166
|
+
setSelectedBlocks(selectedBlocks.filter((_, i) => i !== idx))
|
|
167
|
+
}
|
|
168
|
+
onSaveTemplate={() => void handleSaveDbTemplate()}
|
|
169
|
+
onContextChange={(val) => {
|
|
170
|
+
setPreviewContextType(val)
|
|
171
|
+
if (selectedTemplate?.id) {
|
|
172
|
+
void handlePreviewTemplate(selectedTemplate.id, val, previewLocale)
|
|
173
|
+
}
|
|
174
|
+
}}
|
|
175
|
+
onLocaleChange={(loc) => {
|
|
176
|
+
setPreviewLocale(loc)
|
|
177
|
+
if (selectedTemplate?.id) {
|
|
178
|
+
void handlePreviewTemplate(
|
|
179
|
+
selectedTemplate.id,
|
|
180
|
+
previewContextType,
|
|
181
|
+
loc
|
|
182
|
+
)
|
|
183
|
+
}
|
|
184
|
+
}}
|
|
185
|
+
/>
|
|
186
|
+
</div>
|
|
187
|
+
)
|
|
188
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useCallback } from "react"
|
|
2
|
+
import { toast } from "@medusajs/ui"
|
|
3
|
+
import { useTranslation } from "react-i18next"
|
|
4
|
+
import {
|
|
5
|
+
useMpnBroadcasts,
|
|
6
|
+
useMpnChannels,
|
|
7
|
+
useMpnLogs,
|
|
8
|
+
useMpnMappings,
|
|
9
|
+
useMpnSandboxMessages,
|
|
10
|
+
useMpnTemplates,
|
|
11
|
+
} from "../../../hooks"
|
|
12
|
+
import {
|
|
13
|
+
invalidateNotificationsQueries,
|
|
14
|
+
notificationsQueryKeys,
|
|
15
|
+
} from "../../../sdk"
|
|
16
|
+
import type { StatusBannerState } from "./constants"
|
|
17
|
+
|
|
18
|
+
export function useNotificationStudio() {
|
|
19
|
+
const { t } = useTranslation()
|
|
20
|
+
|
|
21
|
+
const handleLoadError = useCallback(() => {
|
|
22
|
+
toast.error(t("messages.loadStudioError"))
|
|
23
|
+
}, [t])
|
|
24
|
+
|
|
25
|
+
const { data: tplData, isLoading: templatesLoading } = useMpnTemplates(
|
|
26
|
+
undefined,
|
|
27
|
+
{
|
|
28
|
+
onError: handleLoadError,
|
|
29
|
+
}
|
|
30
|
+
)
|
|
31
|
+
const { data: mapData, isLoading: mappingsLoading } = useMpnMappings({
|
|
32
|
+
onError: handleLoadError,
|
|
33
|
+
})
|
|
34
|
+
const { data: chData, isLoading: channelsLoading } = useMpnChannels({
|
|
35
|
+
onError: handleLoadError,
|
|
36
|
+
})
|
|
37
|
+
const { data: logData, isLoading: logsLoading } = useMpnLogs(
|
|
38
|
+
{ limit: 50 },
|
|
39
|
+
{ onError: handleLoadError }
|
|
40
|
+
)
|
|
41
|
+
const { data: brcData, isLoading: broadcastsLoading } = useMpnBroadcasts({
|
|
42
|
+
onError: handleLoadError,
|
|
43
|
+
})
|
|
44
|
+
const { data: sbxData, isLoading: sandboxLoading } = useMpnSandboxMessages(
|
|
45
|
+
{ limit: 50 },
|
|
46
|
+
{ onError: handleLoadError }
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
const setStatusBanner = useCallback((status: StatusBannerState | null) => {
|
|
50
|
+
if (!status) return
|
|
51
|
+
if (status.type === "success") {
|
|
52
|
+
toast.success(status.text)
|
|
53
|
+
} else {
|
|
54
|
+
toast.error(status.text)
|
|
55
|
+
}
|
|
56
|
+
}, [])
|
|
57
|
+
|
|
58
|
+
const loadAllStudioData = useCallback(async () => {
|
|
59
|
+
invalidateNotificationsQueries(notificationsQueryKeys.all)
|
|
60
|
+
}, [])
|
|
61
|
+
|
|
62
|
+
const isLoading =
|
|
63
|
+
templatesLoading ||
|
|
64
|
+
mappingsLoading ||
|
|
65
|
+
channelsLoading ||
|
|
66
|
+
logsLoading ||
|
|
67
|
+
broadcastsLoading ||
|
|
68
|
+
sandboxLoading
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
isLoading,
|
|
72
|
+
statusBanner: null as StatusBannerState | null,
|
|
73
|
+
setStatusBanner,
|
|
74
|
+
templates: tplData?.templates || [],
|
|
75
|
+
mappings: mapData?.mappings || [],
|
|
76
|
+
supportedEvents: mapData?.supported_events || [],
|
|
77
|
+
channelsConfig: chData?.channels || [],
|
|
78
|
+
logs: logData?.logs || [],
|
|
79
|
+
broadcasts: brcData?.broadcasts || [],
|
|
80
|
+
consentSummary: brcData?.consent_summary || null,
|
|
81
|
+
sandboxMessages: sbxData?.messages || [],
|
|
82
|
+
loadAllStudioData,
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { Container, Heading, Text } from "@medusajs/ui"
|
|
2
|
+
import { useEffect, useState, type JSX, type ReactNode } from "react"
|
|
3
|
+
|
|
4
|
+
interface CachedRbacResult {
|
|
5
|
+
rbacActive: boolean
|
|
6
|
+
isSuperAdmin: boolean
|
|
7
|
+
permissions: string[]
|
|
8
|
+
fetchedAt: number
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const CACHE_TTL_MS = 30_000
|
|
12
|
+
let cachedResult: CachedRbacResult | null = null
|
|
13
|
+
let inflightPromise: Promise<CachedRbacResult> | null = null
|
|
14
|
+
|
|
15
|
+
function matchRbacPermission(
|
|
16
|
+
granted: readonly string[],
|
|
17
|
+
required: string,
|
|
18
|
+
isSuperAdmin = false
|
|
19
|
+
): boolean {
|
|
20
|
+
if (isSuperAdmin || !required) return true
|
|
21
|
+
if (
|
|
22
|
+
granted.includes("*") ||
|
|
23
|
+
granted.includes("*:*") ||
|
|
24
|
+
granted.includes(required)
|
|
25
|
+
) {
|
|
26
|
+
return true
|
|
27
|
+
}
|
|
28
|
+
const sep = required.includes(".") ? "." : required.includes(":") ? ":" : null
|
|
29
|
+
if (sep) {
|
|
30
|
+
const ns = required.split(sep)[0]
|
|
31
|
+
if (granted.includes(`${ns}.*`) || granted.includes(`${ns}:*`)) {
|
|
32
|
+
return true
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return false
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function fetchRbacMe(): Promise<CachedRbacResult> {
|
|
39
|
+
if (cachedResult && Date.now() - cachedResult.fetchedAt < CACHE_TTL_MS) {
|
|
40
|
+
return cachedResult
|
|
41
|
+
}
|
|
42
|
+
if (inflightPromise) return inflightPromise
|
|
43
|
+
|
|
44
|
+
inflightPromise = (async () => {
|
|
45
|
+
try {
|
|
46
|
+
const res = await fetch("/admin/rbac/me", {
|
|
47
|
+
credentials: "include",
|
|
48
|
+
headers: { Accept: "application/json" },
|
|
49
|
+
})
|
|
50
|
+
if (res.status === 404) {
|
|
51
|
+
cachedResult = {
|
|
52
|
+
rbacActive: false,
|
|
53
|
+
isSuperAdmin: false,
|
|
54
|
+
permissions: [],
|
|
55
|
+
fetchedAt: Date.now(),
|
|
56
|
+
}
|
|
57
|
+
return cachedResult
|
|
58
|
+
}
|
|
59
|
+
if (!res.ok) {
|
|
60
|
+
cachedResult = {
|
|
61
|
+
rbacActive: true,
|
|
62
|
+
isSuperAdmin: false,
|
|
63
|
+
permissions: [],
|
|
64
|
+
fetchedAt: Date.now(),
|
|
65
|
+
}
|
|
66
|
+
return cachedResult
|
|
67
|
+
}
|
|
68
|
+
const data = (await res.json()) as {
|
|
69
|
+
isSuperAdmin?: boolean
|
|
70
|
+
permissions?: string[]
|
|
71
|
+
}
|
|
72
|
+
cachedResult = {
|
|
73
|
+
rbacActive: true,
|
|
74
|
+
isSuperAdmin: Boolean(data?.isSuperAdmin),
|
|
75
|
+
permissions: Array.isArray(data?.permissions) ? data.permissions : [],
|
|
76
|
+
fetchedAt: Date.now(),
|
|
77
|
+
}
|
|
78
|
+
return cachedResult
|
|
79
|
+
} catch {
|
|
80
|
+
cachedResult = {
|
|
81
|
+
rbacActive: false,
|
|
82
|
+
isSuperAdmin: false,
|
|
83
|
+
permissions: [],
|
|
84
|
+
fetchedAt: Date.now(),
|
|
85
|
+
}
|
|
86
|
+
return cachedResult
|
|
87
|
+
} finally {
|
|
88
|
+
inflightPromise = null
|
|
89
|
+
}
|
|
90
|
+
})()
|
|
91
|
+
|
|
92
|
+
return inflightPromise
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function useRbacAccess() {
|
|
96
|
+
const [state, setState] = useState<CachedRbacResult | null>(() =>
|
|
97
|
+
cachedResult && Date.now() - cachedResult.fetchedAt < CACHE_TTL_MS
|
|
98
|
+
? cachedResult
|
|
99
|
+
: null
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
let active = true
|
|
104
|
+
fetchRbacMe().then((res) => {
|
|
105
|
+
if (active) setState(res)
|
|
106
|
+
})
|
|
107
|
+
return () => {
|
|
108
|
+
active = false
|
|
109
|
+
}
|
|
110
|
+
}, [])
|
|
111
|
+
|
|
112
|
+
if (!state) {
|
|
113
|
+
return { isLoading: true, rbacActive: false, hasPermission: () => false }
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
isLoading: false,
|
|
118
|
+
rbacActive: state.rbacActive,
|
|
119
|
+
hasPermission: (permission?: string) => {
|
|
120
|
+
if (!state.rbacActive || !permission) return true
|
|
121
|
+
return matchRbacPermission(
|
|
122
|
+
state.permissions,
|
|
123
|
+
permission,
|
|
124
|
+
state.isSuperAdmin
|
|
125
|
+
)
|
|
126
|
+
},
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export const RbacGuard = ({
|
|
131
|
+
permission,
|
|
132
|
+
children,
|
|
133
|
+
hideOnDenied = false,
|
|
134
|
+
}: {
|
|
135
|
+
permission: string
|
|
136
|
+
children: ReactNode
|
|
137
|
+
hideOnDenied?: boolean
|
|
138
|
+
}): JSX.Element | null => {
|
|
139
|
+
const { isLoading, hasPermission } = useRbacAccess()
|
|
140
|
+
if (isLoading) return null
|
|
141
|
+
if (!hasPermission(permission)) {
|
|
142
|
+
if (hideOnDenied) return null
|
|
143
|
+
return (
|
|
144
|
+
<Container className="flex flex-col gap-y-2 p-6">
|
|
145
|
+
<Heading level="h2">Access denied</Heading>
|
|
146
|
+
<Text size="small" className="text-ui-fg-subtle">
|
|
147
|
+
You do not have permission to view this section. ({permission})
|
|
148
|
+
</Text>
|
|
149
|
+
</Container>
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
return <>{children}</>
|
|
153
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# i18n
|
|
2
|
+
|
|
3
|
+
Internationalization setup for the Notification Studio Admin extension using [`react-i18next`](https://react.i18next.com/).
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
i18n/
|
|
7
|
+
├── index.ts Initializes and exports the 10-locale translation map
|
|
8
|
+
└── json/ One JSON file per locale
|
|
9
|
+
├── ar.json Arabic (RTL)
|
|
10
|
+
├── de.json German
|
|
11
|
+
├── en.json English (default)
|
|
12
|
+
├── es.json Spanish
|
|
13
|
+
├── fr.json French
|
|
14
|
+
├── it.json Italian
|
|
15
|
+
├── nl.json Dutch
|
|
16
|
+
├── pl.json Polish
|
|
17
|
+
├── pt.json Portuguese
|
|
18
|
+
└── tr.json Turkish
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Supported Locales & RTL Support
|
|
24
|
+
|
|
25
|
+
Both the Admin UI (`src/admin/i18n/json/`) and the backend template renderer (`src/modules/notifications/i18n/locales/`) ship with **10 built-in locales**:
|
|
26
|
+
|
|
27
|
+
| Code | Language | Direction |
|
|
28
|
+
| ---- | ---------- | ------------------------------------ |
|
|
29
|
+
| `en` | English | `ltr` |
|
|
30
|
+
| `es` | Spanish | `ltr` |
|
|
31
|
+
| `pl` | Polish | `ltr` |
|
|
32
|
+
| `de` | German | `ltr` |
|
|
33
|
+
| `fr` | French | `ltr` |
|
|
34
|
+
| `it` | Italian | `ltr` |
|
|
35
|
+
| `nl` | Dutch | `ltr` |
|
|
36
|
+
| `pt` | Portuguese | `ltr` |
|
|
37
|
+
| `tr` | Turkish | `ltr` |
|
|
38
|
+
| `ar` | Arabic | `rtl` (`isRtlLocale("ar") === true`) |
|
|
39
|
+
|
|
40
|
+
When Arabic (`ar`) is active, the Notification Studio automatically sets `dir="rtl"` on the container and preview surfaces so forms, tables, and rendered templates align right-to-left.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Adding a Translation Key
|
|
45
|
+
|
|
46
|
+
1. Add the key to `json/en.json` and the other 9 locale files in `json/*.json`.
|
|
47
|
+
2. Reference the key in any Admin route, component, or widget via `useTranslation` (or the studio `t` helper scoped to `"mpn"`):
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { useTranslation } from "react-i18next"
|
|
51
|
+
|
|
52
|
+
export const StudioHeader = () => {
|
|
53
|
+
const { t } = useTranslation("mpn")
|
|
54
|
+
return <span>{t("header.title")}</span>
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Adding a New Locale
|
|
61
|
+
|
|
62
|
+
1. Create `json/<locale>.json` with the same key hierarchy as `json/en.json`.
|
|
63
|
+
2. Import it in `src/admin/i18n/index.ts` and add it to the default export map:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import ar from "./json/ar.json"
|
|
67
|
+
import en from "./json/en.json"
|
|
68
|
+
import ja from "./json/ja.json"
|
|
69
|
+
|
|
70
|
+
export default {
|
|
71
|
+
ar: { translation: ar },
|
|
72
|
+
en: { translation: en },
|
|
73
|
+
ja: { translation: ja },
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
3. If the locale is right-to-left, add its code to `RTL_LOCALES` in `src/modules/notifications/i18n/index.ts`.
|
|
78
|
+
|
|
79
|
+
---
|
|
80
|
+
|
|
81
|
+
## Key Structure (`mpn` Namespace)
|
|
82
|
+
|
|
83
|
+
All Admin UI translation keys are organized by functional domain so related labels stay grouped across routes and components:
|
|
84
|
+
|
|
85
|
+
```json
|
|
86
|
+
{
|
|
87
|
+
"nav": {
|
|
88
|
+
"label": "Notifications"
|
|
89
|
+
},
|
|
90
|
+
"header": {
|
|
91
|
+
"title": "Multi-Channel Notification Studio (Email, WhatsApp/OpenWA, Telegram, SMS, Slack)",
|
|
92
|
+
"subtitle": "Visual Block & Raw Template Builder, DB Event Mappings, Multi-Driver Gateways, Consent Ledger, and Dummy OTP Sandbox.",
|
|
93
|
+
"refresh": "Refresh Studio"
|
|
94
|
+
},
|
|
95
|
+
"tabs": {
|
|
96
|
+
"templates": "Templates & Visual Builder ({{count}})",
|
|
97
|
+
"mappings": "Event Mappings ({{count}})",
|
|
98
|
+
"channels": "Channels (OpenWA / Telegram / SMS)",
|
|
99
|
+
"logs": "Delivery Logs ({{count}})",
|
|
100
|
+
"broadcasts": "Broadcasts & Consent Ledger",
|
|
101
|
+
"sandbox": "Dummy OTP Sandbox ({{count}})"
|
|
102
|
+
},
|
|
103
|
+
"templates": {
|
|
104
|
+
"title": "Templates",
|
|
105
|
+
"inspectPreview": "Inspect / Preview",
|
|
106
|
+
"cloneToDb": "Clone to DB",
|
|
107
|
+
"saveButton": "Save Template & Blocks"
|
|
108
|
+
},
|
|
109
|
+
"mappings": {
|
|
110
|
+
"title": "Create Event → Channel → Template Mapping",
|
|
111
|
+
"addMappingButton": "Add Active Mapping"
|
|
112
|
+
},
|
|
113
|
+
"channels": {
|
|
114
|
+
"whatsappTitle": "WhatsApp & OpenWA Gateway",
|
|
115
|
+
"telegramTitle": "Telegram Notifications",
|
|
116
|
+
"smsTitle": "SMS Carrier Driver"
|
|
117
|
+
},
|
|
118
|
+
"logs": {
|
|
119
|
+
"title": "Redacted Delivery Ledger (PII Masked & Idempotent)"
|
|
120
|
+
},
|
|
121
|
+
"broadcasts": {
|
|
122
|
+
"title": "Customer Group Broadcasts",
|
|
123
|
+
"consentTitle": "TCPA / 10DLC & Bot Consent Ledger"
|
|
124
|
+
},
|
|
125
|
+
"sandbox": {
|
|
126
|
+
"title": "Dummy OTP & Multi-Channel Sandbox",
|
|
127
|
+
"clearButton": "Clear Sandbox"
|
|
128
|
+
},
|
|
129
|
+
"widget": {
|
|
130
|
+
"title": "Multi-Channel Notifications",
|
|
131
|
+
"sendButton": "Send Notification"
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
```
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ar from "./json/ar.json"
|
|
2
|
+
import de from "./json/de.json"
|
|
3
|
+
import en from "./json/en.json"
|
|
4
|
+
import es from "./json/es.json"
|
|
5
|
+
import fr from "./json/fr.json"
|
|
6
|
+
import it from "./json/it.json"
|
|
7
|
+
import nl from "./json/nl.json"
|
|
8
|
+
import pl from "./json/pl.json"
|
|
9
|
+
import pt from "./json/pt.json"
|
|
10
|
+
import tr from "./json/tr.json"
|
|
11
|
+
|
|
12
|
+
export default {
|
|
13
|
+
ar: { translation: ar },
|
|
14
|
+
de: { translation: de },
|
|
15
|
+
en: { translation: en },
|
|
16
|
+
es: { translation: es },
|
|
17
|
+
fr: { translation: fr },
|
|
18
|
+
it: { translation: it },
|
|
19
|
+
nl: { translation: nl },
|
|
20
|
+
pl: { translation: pl },
|
|
21
|
+
pt: { translation: pt },
|
|
22
|
+
tr: { translation: tr },
|
|
23
|
+
}
|