@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
package/README.md
CHANGED
|
@@ -1,3 +1,390 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@vicacha-devs/medusa-plugin-notifications` (`medusa-plugin-notifications`)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Unified **Multi-Channel Notification Studio, Visual Block & Raw Template Builder, DB Event Orchestrator, TCPA/10DLC Consent Ledger, Abandoned Cart Recovery, Customer Group Broadcasts, and Multi-Driver Transport Provider Suite** for **Medusa v2 (`>= 2.21.0`)**.
|
|
4
|
+
|
|
5
|
+
Combines a full-featured database-driven **Notification Studio Module (`notificationsModuleService`)** with **6 Medusa `Modules.NOTIFICATION` Transport Providers** (`mpn-email`, `mpn-whatsapp`, `mpn-telegram`, `mpn-sms`, `mpn-slack`, `mpn-dummy-sandbox`), a **Decoupled Medusa Admin UI (`/app/mpn`)** across 7 routes with 10-locale i18n (including RTL Arabic), a **Typed SDK & React Hooks layer**, and **strictly optional `medusa-plugin-documents` PDF attachment & B2B buyer contact resolution**.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
- [`@vicacha-devs/medusa-plugin-notifications` (`medusa-plugin-notifications`)](#vicacha-devsmedusa-plugin-notifications-medusa-plugin-notifications)
|
|
12
|
+
- [Table of Contents](#table-of-contents)
|
|
13
|
+
- [1. Overview \& Highlights](#1-overview--highlights)
|
|
14
|
+
- [2. Installation \& `medusa-config.ts` Configuration](#2-installation--medusa-configts-configuration)
|
|
15
|
+
- [1. Install the package](#1-install-the-package)
|
|
16
|
+
- [2. Configure `medusa-config.ts`](#2-configure-medusa-configts)
|
|
17
|
+
- [3. Run Database Migrations](#3-run-database-migrations)
|
|
18
|
+
- [3. Architecture \& Data Model (`mpn_` DML Models)](#3-architecture--data-model-mpn_-dml-models)
|
|
19
|
+
- [4. Dual-Mode Template Renderer \& i18n](#4-dual-mode-template-renderer--i18n)
|
|
20
|
+
- [Visual Block Tree Mode (`mode: "blocks"`)](#visual-block-tree-mode-mode-blocks)
|
|
21
|
+
- [Raw Template Mode (`mode: "raw"`)](#raw-template-mode-mode-raw)
|
|
22
|
+
- [Multi-Pass Variable Interpolation](#multi-pass-variable-interpolation)
|
|
23
|
+
- [System Presets (`SYSTEM_TEMPLATE_PRESETS`)](#system-presets-system_template_presets)
|
|
24
|
+
- [10 Built-In Locales](#10-built-in-locales)
|
|
25
|
+
- [5. 6 Notification Transport Providers](#5-6-notification-transport-providers)
|
|
26
|
+
- [6. Event Orchestration, Subscribers \& Scheduled Jobs](#6-event-orchestration-subscribers--scheduled-jobs)
|
|
27
|
+
- [Subscribers (`src/subscribers/`)](#subscribers-srcsubscribers)
|
|
28
|
+
- [Scheduled Jobs (`src/jobs/`)](#scheduled-jobs-srcjobs)
|
|
29
|
+
- [7. Optional `medusa-plugin-documents` \& B2B Integration](#7-optional-medusa-plugin-documents--b2b-integration)
|
|
30
|
+
- [8. REST API Reference (Admin \& Store)](#8-rest-api-reference-admin--store)
|
|
31
|
+
- [Admin Endpoints (`/admin/mpn/*` \& `/admin/plugin`)](#admin-endpoints-adminmpn--adminplugin)
|
|
32
|
+
- [Storefront \& Webhook Endpoints (`/store/mpn/*` \& `/store/plugin`)](#storefront--webhook-endpoints-storempn--storeplugin)
|
|
33
|
+
- [9. Typed SDK \& React Hooks](#9-typed-sdk--react-hooks)
|
|
34
|
+
- [Typed SDK (`@vicacha-devs/medusa-plugin-notifications/sdk`)](#typed-sdk-vicacha-devsmedusa-plugin-notificationssdk)
|
|
35
|
+
- [React Hooks (`@vicacha-devs/medusa-plugin-notifications/hooks`)](#react-hooks-vicacha-devsmedusa-plugin-notificationshooks)
|
|
36
|
+
- [10. Decoupled Admin Notification Studio (`/app/mpn`)](#10-decoupled-admin-notification-studio-appmpn)
|
|
37
|
+
- [11. Security Architecture](#11-security-architecture)
|
|
38
|
+
- [12. Development \& Testing](#12-development--testing)
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## 1. Overview & Highlights
|
|
43
|
+
|
|
44
|
+
| Capability | Description |
|
|
45
|
+
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
46
|
+
| **Dual Architecture** | **1. Standalone Notification Studio Module (`notificationsModuleService`)**: Visual Block Tree Builder, Raw HTML/Text/JSON templates, DB-driven Event-to-Template Mappings, PII-redacted Delivery Ledger, TCPA/10DLC Consent Ledger, Customer Group Broadcasts, and Dummy OTP Sandbox.<br>**2. 6 Built-in `Modules.NOTIFICATION` Providers**: `mpn-email`, `mpn-whatsapp`, `mpn-telegram`, `mpn-sms`, `mpn-slack`, and `mpn-dummy-sandbox`. |
|
|
47
|
+
| **Dual-Mode Template Engine** | Build templates visually using a 12-block hierarchical tree (`section`, `heading`, `text`, `row`, `separator`, `group`, `repeater`, `product-item`, `button`, `actions`, `divider`, `attachment`) or write Raw HTML/Text/JSON with multi-pass interpolation (`{{data.*}}`, `{{translations.*}}`, Go-style `{{ .var }}`, and `variable_map`). |
|
|
48
|
+
| **11+ System Presets & 1-Click Clone** | Ships with read-only `system_*` presets across Email, WhatsApp/OpenWA, Telegram, SMS, and Slack (`system_email_order_placed`, `system_whatsapp_order_placed`, `system_telegram_order_placed`, `system_sms_otp_code`, `system_sms_cart_recovery`, etc.) that clone into editable database rows (`mpntpl_*` + `mpnblk_*`) with 1 click. |
|
|
49
|
+
| **Multi-Driver WhatsApp & OpenWA** | `mpn-whatsapp` supports **Official Meta Cloud API** (`meta-cloud` — approved WABA templates, interactive buttons, session text) AND **Self-Hosted Gateways** (**`rmyndharis/OpenWA`** with `ENGINE_TYPE=baileys` on port `2785` via `/api/sessions/{sessionId}/messages/send-text` & `/send-document` with `X-API-Key`, **WAHA**, and **Evolution API v2**) for free-form rich text and PDF invoice attachments. |
|
|
50
|
+
| **Telegram Merchant Topics & Customer Bot** | `mpn-telegram` supports **Merchant Supergroup / Forum Topic Alerts** (`-100...` or `@channel` with `message_thread_id` routing + Admin verification gate) AND **Customer Direct Messages** (`chat_id` linked via `/start <customer_id>` bot webhook, `InlineKeyboardMarkup` buttons, and `sendDocument` PDF uploads) with 30 msg/s global & 1 msg/s per-chat rate limiting. |
|
|
51
|
+
| **Multi-Driver SMS, Consent & Cart Recovery** | `mpn-sms` supports **Twilio** (SMS/MMS + Messaging Service SID + `20xxx` error classification), **Signal House**, **MSG91 Flow**, **SMSAPI**, and **Generic HTTP Webhook**, backed by GSM-7 / UCS-2 segment calculation, TCPA/10DLC `JOIN`/`STOP` consent tracking, and a 15-minute Abandoned Cart Recovery job with a 24h per-phone cooldown. |
|
|
52
|
+
| **Zero-Hard-Dependency PDF Attachments** | Automatically attaches generated PDF Invoices, Packing Slips, or Credit Notes from `medusa-plugin-documents` (`documentsModuleService`) to Email, WhatsApp/OpenWA, and Telegram notifications when `attach_document_types` is configured, and resolves B2B accounting contacts. |
|
|
53
|
+
| **10 Built-In Locales (Backend + Admin UI)** | Ships with `en`, `es`, `pl`, `de`, `fr`, `pt`, `it`, `nl`, `tr`, and `ar` dictionaries for both notification template rendering (`{{translations.*}}`) and the Medusa Admin UI (`react-i18next` with RTL support for Arabic). |
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
## 2. Installation & `medusa-config.ts` Configuration
|
|
58
|
+
|
|
59
|
+
### 1. Install the package
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pnpm add @vicacha-devs/medusa-plugin-notifications
|
|
63
|
+
# or
|
|
64
|
+
npm install @vicacha-devs/medusa-plugin-notifications
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### 2. Configure `medusa-config.ts`
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { defineConfig } from "@medusajs/framework/utils"
|
|
71
|
+
|
|
72
|
+
export default defineConfig({
|
|
73
|
+
plugins: [
|
|
74
|
+
{
|
|
75
|
+
resolve: "@vicacha-devs/medusa-plugin-notifications",
|
|
76
|
+
options: {
|
|
77
|
+
defaultLocale: "en",
|
|
78
|
+
storeName: "My Medusa Store",
|
|
79
|
+
storeUrl: "https://store.example.com",
|
|
80
|
+
enableCartRecovery: true,
|
|
81
|
+
cartAbandonmentThresholdMinutes: 60,
|
|
82
|
+
cartRecoveryUrlBase: "https://store.example.com/cart/recover",
|
|
83
|
+
logRetentionDays: 30,
|
|
84
|
+
sendConcurrency: 2,
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
modules: [
|
|
89
|
+
{
|
|
90
|
+
resolve:
|
|
91
|
+
"@vicacha-devs/medusa-plugin-notifications/modules/notifications",
|
|
92
|
+
options: {
|
|
93
|
+
defaultLocale: "en",
|
|
94
|
+
storeName: "My Medusa Store",
|
|
95
|
+
storeUrl: "https://store.example.com",
|
|
96
|
+
enableCartRecovery: true,
|
|
97
|
+
cartAbandonmentThresholdMinutes: 60,
|
|
98
|
+
logRetentionDays: 30,
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
resolve: "@medusajs/medusa/notification",
|
|
103
|
+
options: {
|
|
104
|
+
providers: [
|
|
105
|
+
{
|
|
106
|
+
resolve:
|
|
107
|
+
"@vicacha-devs/medusa-plugin-notifications/providers/email-smtp",
|
|
108
|
+
id: "mpn-email",
|
|
109
|
+
options: {
|
|
110
|
+
channels: ["email"],
|
|
111
|
+
host: process.env.SMTP_HOST,
|
|
112
|
+
port: Number(process.env.SMTP_PORT || 587),
|
|
113
|
+
user: process.env.SMTP_USER,
|
|
114
|
+
pass: process.env.SMTP_PASS,
|
|
115
|
+
from_name: "My Medusa Store",
|
|
116
|
+
from_address: "orders@example.com",
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
resolve:
|
|
121
|
+
"@vicacha-devs/medusa-plugin-notifications/providers/whatsapp",
|
|
122
|
+
id: "mpn-whatsapp",
|
|
123
|
+
options: {
|
|
124
|
+
channels: ["whatsapp"],
|
|
125
|
+
driver: "openwa", // "meta-cloud" | "openwa" | "waha" | "evolution"
|
|
126
|
+
gatewayUrl: process.env.OPENWA_BASE_URL,
|
|
127
|
+
apiKey: process.env.OPENWA_API_KEY,
|
|
128
|
+
accessToken: process.env.WHATSAPP_ACCESS_TOKEN,
|
|
129
|
+
phoneNumberId: process.env.WHATSAPP_PHONE_NUMBER_ID,
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
resolve:
|
|
134
|
+
"@vicacha-devs/medusa-plugin-notifications/providers/telegram",
|
|
135
|
+
id: "mpn-telegram",
|
|
136
|
+
options: {
|
|
137
|
+
channels: ["telegram"],
|
|
138
|
+
botToken: process.env.TELEGRAM_BOT_TOKEN,
|
|
139
|
+
defaultChatId: process.env.TELEGRAM_MERCHANT_CHAT_ID,
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
resolve: "@vicacha-devs/medusa-plugin-notifications/providers/sms",
|
|
144
|
+
id: "mpn-sms",
|
|
145
|
+
options: {
|
|
146
|
+
channels: ["sms"],
|
|
147
|
+
driver: "twilio", // "twilio" | "signalhouse" | "msg91" | "smsapi" | "webhook"
|
|
148
|
+
twilioAccountSid: process.env.TWILIO_ACCOUNT_SID,
|
|
149
|
+
twilioAuthToken: process.env.TWILIO_AUTH_TOKEN,
|
|
150
|
+
twilioFrom: process.env.TWILIO_FROM_NUMBER,
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
resolve:
|
|
155
|
+
"@vicacha-devs/medusa-plugin-notifications/providers/slack",
|
|
156
|
+
id: "mpn-slack",
|
|
157
|
+
options: {
|
|
158
|
+
channels: ["slack"],
|
|
159
|
+
webhook_url: process.env.SLACK_WEBHOOK_URL,
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
resolve:
|
|
164
|
+
"@vicacha-devs/medusa-plugin-notifications/providers/dummy-sandbox",
|
|
165
|
+
id: "mpn-dummy-sandbox",
|
|
166
|
+
options: {
|
|
167
|
+
channels: ["dummy"],
|
|
168
|
+
log_to_console: true,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
})
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### 3. Run Database Migrations
|
|
179
|
+
|
|
180
|
+
```bash
|
|
181
|
+
npx medusa db:migrate
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## 3. Architecture & Data Model (`mpn_` DML Models)
|
|
187
|
+
|
|
188
|
+
All database tables managed by `notificationsModuleService` (`NOTIFICATIONS_MODULE = "notificationsModuleService"`) use the `mpn_` prefix to prevent naming collisions.
|
|
189
|
+
|
|
190
|
+
| Model | Table Name | ID Prefix | Purpose |
|
|
191
|
+
| ---------------------- | ------------------------ | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
192
|
+
| `MpnTemplate` | `mpn_template` | `mpntpl` | Stores multi-channel templates (`channel`, `mode: "blocks" \| "raw"`, `locale`, `subject`, `raw_content`, `variable_map`, `meta_template_name`, `meta_language_code`, `styles`, `source_preset_id`). |
|
|
193
|
+
| `MpnTemplateBlock` | `mpn_template_block` | `mpnblk` | Hierarchical visual block tree nodes (`template_id`, `type`, `position`, `parent_id`, `metadata`). |
|
|
194
|
+
| `MpnEventMapping` | `mpn_event_mapping` | `mpnmap` | Database-driven event routing rules (`event_name`, `channel`, `template_id`, `recipient_rule`, `custom_recipient`, `sender_profile_index`, `telegram_thread_id`, `attach_document_types`, `variable_map`, `conditions`, `priority`). |
|
|
195
|
+
| `MpnDeliveryLog` | `mpn_delivery_log` | `mpnlog` | Idempotent, PII-masked audit ledger (`idempotency_key`, `event_name`, `entity_id`, `channel`, `driver`, `recipient_masked`, `recipient_hash`, `status`, `external_message_id`, `payload_redacted`, `error_code`, `error_message`). Enforced by partial unique index `UQ_mpn_delivery_log_idempotency_active`. |
|
|
196
|
+
| `MpnConsentRecord` | `mpn_consent_record` | `mpncns` | TCPA/10DLC & bot opt-in ledger (`identifier`, `channel`, `customer_id`, `status: "opted_in" \| "opted_out" \| "pending"`, `source`, `first_message_sent`, `last_cart_recovery_at`). |
|
|
197
|
+
| `MpnBroadcastCampaign` | `mpn_broadcast_campaign` | `mpnbrc` | Customer Group or custom recipient broadcast campaigns (`name`, `channel`, `template_id`, `customer_group_id`, `custom_recipients`, `status`, `scheduled_at`, `sent_count`, `failed_count`, `attributed_revenue`). |
|
|
198
|
+
| `MpnChannelConfig` | `mpn_channel_config` | `mpncfg` | Operational channel & driver configuration (`channel`, `driver`, `is_enabled`, `is_verified`, `verified_at`, `rate_limit_per_sec`, `config_json`). |
|
|
199
|
+
| `MpnSandboxMessage` | `mpn_sandbox_message` | `mpnsbx` | Captured local/staging notifications and extracted OTP codes (`channel`, `recipient`, `subject`, `body`, `html`, `otp_code`, `media_urls`, `attachments_meta`). |
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## 4. Dual-Mode Template Renderer & i18n
|
|
204
|
+
|
|
205
|
+
### Visual Block Tree Mode (`mode: "blocks"`)
|
|
206
|
+
|
|
207
|
+
Compiles a parent-child tree of `mpn_template_block` rows into channel-specific outputs (responsive HTML + plain-text fallback for Email, Markdown + interactive buttons for WhatsApp, sanitized HTML + `InlineKeyboardMarkup` for Telegram, compact text + GSM-7/UCS-2 metrics for SMS, and Block Kit JSON for Slack):
|
|
208
|
+
|
|
209
|
+
- **12 Supported Block Types**: `section`, `heading`, `text`, `row`, `separator`, `group`, `repeater`, `product-item`, `button`, `actions`, `divider`, `attachment`.
|
|
210
|
+
|
|
211
|
+
### Raw Template Mode (`mode: "raw"`)
|
|
212
|
+
|
|
213
|
+
Allows merchants to author raw HTML, Markdown, plain text, or JSON directly while enjoying full multi-pass variable interpolation.
|
|
214
|
+
|
|
215
|
+
### Multi-Pass Variable Interpolation
|
|
216
|
+
|
|
217
|
+
1. **`variable_map` Aliasing**: Maps friendly keys (e.g., `CustomerName: "shipping_address.first_name"`) before interpolation.
|
|
218
|
+
2. **Two-Prefix Recursive Interpolation**: Resolves `{{data.*}}` against hydrated entity data and `{{translations.*}}` against the active locale dictionary.
|
|
219
|
+
3. **Go-Style Template Syntax**: Resolves `{{ .VariableName }}` and `{{ .nested.path }}` (compatible with `@sam-ael/medusa-plugin-mailer` and `@sam-ael/medusa-plugin-whatsapp` templates).
|
|
220
|
+
|
|
221
|
+
### System Presets (`SYSTEM_TEMPLATE_PRESETS`)
|
|
222
|
+
|
|
223
|
+
Includes built-in read-only presets across all channels (`system_email_order_placed`, `system_email_shipment_created`, `system_email_document_ready`, `system_email_inventory_low`, `system_whatsapp_order_placed`, `system_whatsapp_shipment_created`, `system_telegram_order_placed`, `system_telegram_inventory_low`, `system_sms_order_placed`, `system_sms_shipment_created`, `system_sms_otp_code`, `system_sms_cart_recovery`, `system_slack_order_placed`). Any preset can be cloned into an editable DB template via `POST /admin/mpn/templates` (`clone_from_preset_id`).
|
|
224
|
+
|
|
225
|
+
### 10 Built-In Locales
|
|
226
|
+
|
|
227
|
+
Both the backend renderer (`src/modules/notifications/i18n/locales/`) and the Admin UI (`src/admin/i18n/json/`) include complete dictionaries for:
|
|
228
|
+
|
|
229
|
+
- `en` (English), `es` (Spanish), `pl` (Polish), `de` (German), `fr` (French), `pt` (Portuguese), `it` (Italian), `nl` (Dutch), `tr` (Turkish), and `ar` (Arabic with automatic RTL layout).
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## 5. 6 Notification Transport Providers
|
|
234
|
+
|
|
235
|
+
| Provider ID | Module Import Path | Channel | Drivers & Capabilities |
|
|
236
|
+
| ------------------- | ------------------------------------------------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
237
|
+
| `mpn-email` | `@vicacha-devs/medusa-plugin-notifications/providers/email-smtp` | `email` | Multi-profile (`1..5` via `MAILER_FROM_NAME_1..5` / `MAILER_FROM_ADDRESS_1..5`) Nodemailer SMTP transport (`MAILER_SMTP_*`, e.g. Resend `smtp.resend.com:465`) + HTTP Email API Relay (`MAILER_HTTP_WEBHOOK_URL=https://api.resend.com/emails`, `MAILER_HTTP_API_KEY`) with base64 PDF attachment support. |
|
|
238
|
+
| `mpn-whatsapp` | `@vicacha-devs/medusa-plugin-notifications/providers/whatsapp` | `whatsapp` | **4 Drivers**: `meta-cloud` (Official Meta Graph API v21/v25 approved WABA templates, interactive buttons, documents, session text with digits-only recipient formatting), `openwa` (**`rmyndharis/OpenWA`** `:2785` `/api/sessions/{sessionId}/messages/send-text` & `/send-document` with `X-API-Key` + `Authorization: Bearer`), `waha` (WAHA `/api/sendText`, `/api/sendFile`), and `evolution` (Evolution API v2 `/message/sendText/{instance}`, `/message/sendMedia/{instance}`). |
|
|
239
|
+
| `mpn-telegram` | `@vicacha-devs/medusa-plugin-notifications/providers/telegram` | `telegram` | Supports **Merchant Supergroup / Forum Topic Alerts** (`-100...` or `@channel` with `message_thread_id` and Admin verification gate) and **Customer Direct Messages** (`chat_id` linked via `/start` bot webhook, `InlineKeyboardMarkup`, and multipart `sendDocument` PDF uploads) with 30 msg/s global & 1 msg/s per-chat rate limiting. |
|
|
240
|
+
| `mpn-sms` | `@vicacha-devs/medusa-plugin-notifications/providers/sms` | `sms` | **5 Drivers**: `twilio` (SMS/MMS + Messaging Service SID + `20000`–`21999` permanent vs. retryable error classification), `signalhouse` (10DLC SMS/MMS), `msg91` (Flow API), `smsapi` (OAuth Bearer SMS), and `webhook` (Generic HTTP Webhook), with GSM-7 (160/153 chars) and UCS-2 (70/67 chars) segment calculation. |
|
|
241
|
+
| `mpn-slack` | `@vicacha-devs/medusa-plugin-notifications/providers/slack` | `slack` | Dispatches Slack Block Kit JSON (`blocks` + `text`) to SSRF-validated Slack Incoming Webhook URLs. |
|
|
242
|
+
| `mpn-dummy-sandbox` | `@vicacha-devs/medusa-plugin-notifications/providers/dummy-sandbox` | `dummy` | Intercepts notifications and automatically extracts 4–8 digit OTP codes into `mpn_sandbox_message` and in-memory storage for zero-cost local/staging inspection. |
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## 6. Event Orchestration, Subscribers & Scheduled Jobs
|
|
247
|
+
|
|
248
|
+
### Subscribers (`src/subscribers/`)
|
|
249
|
+
|
|
250
|
+
All subscribers invoke `executeEventNotificationsDispatch` non-blockingly so store operations never fail if an external carrier is unreachable:
|
|
251
|
+
|
|
252
|
+
- **`order-events.ts`**: `order.placed`, `order.completed`, `order.canceled`, `order.updated`, `order.fulfillment_created`, `order.fulfillment_canceled`, `order.return_requested`, `order.return_received`, `order.refund_created`.
|
|
253
|
+
- **`fulfillment-and-inventory-events.ts`**: `shipment.created`, `delivery.created`, `fulfillment.created`, `fulfillment.shipment_created` (normalized to `shipment.created`), `fulfillment.delivery_created` (normalized to `delivery.created`), `inventory-level.updated`.
|
|
254
|
+
- **`customer-and-document-events.ts`**: `customer.created`, `customer.updated`, `document.generated`, `auth.otp_generated`, `loyalty.points_credited`, `loyalty.points_debited`, `wallet.credited`, `wallet.debited`, `membership.tier_changed`, `contact.form_submitted`.
|
|
255
|
+
|
|
256
|
+
### Scheduled Jobs (`src/jobs/`)
|
|
257
|
+
|
|
258
|
+
- **`abandoned-cart-recovery.ts` (`mpn-abandoned-cart-recovery`, schedule: `*/15 * * * *`)**: Scans active incomplete carts older than `cartAbandonmentThresholdMinutes` (default `60` min), verifies explicit TCPA opt-in in `mpn_consent_record`, enforces a strict **24-hour cooldown per phone number** (`last_cart_recovery_at`), and dispatches `cart.abandoned` notifications with a direct checkout recovery URL.
|
|
259
|
+
- **`log-retention-and-broadcasts.ts` (`mpn-log-retention-and-broadcasts`, schedule: `0 * * * *`)**: Prunes expired `mpn_delivery_log` rows older than `logRetentionDays` (default `30` days) and processes due scheduled broadcast campaigns.
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
## 7. Optional `medusa-plugin-documents` & B2B Integration
|
|
264
|
+
|
|
265
|
+
Implemented in `src/modules/notifications/integrations/documents-and-b2b-resolver.ts` with **zero hard dependency**:
|
|
266
|
+
|
|
267
|
+
1. **Automatic PDF Document Attachments (`resolveOrderDocumentAttachments`)**: When an event mapping specifies `attach_document_types` (e.g., `["invoice", "packing_slip"]`), the orchestrator checks if `documentsModuleService` (`medusa-plugin-documents`) is registered in the Medusa container, generates or retrieves the base64 PDF buffers for the order, and attaches them to Email, WhatsApp (`openwa`, `waha`, `evolution`, `meta-cloud`), or Telegram (`sendDocument`) dispatches.
|
|
268
|
+
2. **B2B Accounting Contact Resolution (`resolveB2BNotificationContacts`)**: Resolves B2B company accounting email, phone, WhatsApp, and Telegram chat ID from an optional `companyModuleService` / `b2b` module or `order.metadata` / `customer.metadata` when `recipient_rule: "b2b_accounting"` is selected.
|
|
269
|
+
|
|
270
|
+
---
|
|
271
|
+
|
|
272
|
+
## 8. REST API Reference (Admin & Store)
|
|
273
|
+
|
|
274
|
+
### Admin Endpoints (`/admin/mpn/*` & `/admin/plugin`)
|
|
275
|
+
|
|
276
|
+
| Method | Path | Description |
|
|
277
|
+
| -------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
278
|
+
| `GET` | `/admin/mpn/templates` | List all templates (`system_*` presets, `external_*`, and `mpntpl_*` DB templates) with optional `channel`, `source`, and `locale` filters. |
|
|
279
|
+
| `POST` | `/admin/mpn/templates` | Create a new DB template with blocks, or clone a read-only `system_*` preset via `clone_from_preset_id`. |
|
|
280
|
+
| `GET` | `/admin/mpn/templates/:id` | Retrieve a single template and its hierarchical block tree (`blocks` and `flatBlocks`). |
|
|
281
|
+
| `POST` | `/admin/mpn/templates/:id` | Update template metadata, raw content, variable map, and replace its block tree. |
|
|
282
|
+
| `DELETE` | `/admin/mpn/templates/:id` | Delete a custom database template (`mpntpl_*`) and its blocks. |
|
|
283
|
+
| `POST` | `/admin/mpn/render-template` | Live-render any template or ad-hoc block tree against real entity data (`entity_id`) or mock context (`order`, `shipment`, `inventory-level`, `product`, `cart`, `document`, `otp`) in any of the 10 locales. |
|
|
284
|
+
| `GET` | `/admin/mpn/mappings` | List all event-to-template mappings (`mpn_event_mapping`), supported events, recipient rules, and SMTP sender profiles. |
|
|
285
|
+
| `POST` | `/admin/mpn/mappings` | Create a new Event → Channel → Template → Recipient Rule mapping. |
|
|
286
|
+
| `POST` | `/admin/mpn/mappings/:id` | Update an existing event mapping. |
|
|
287
|
+
| `DELETE` | `/admin/mpn/mappings/:id` | Delete an event mapping. |
|
|
288
|
+
| `GET` | `/admin/mpn/channels` | List operational channel configs (`mpn_channel_config`), SMTP sender profiles, and supported WhatsApp/SMS drivers. |
|
|
289
|
+
| `POST` | `/admin/mpn/channels` | Save channel driver/gateway settings (automatically resets `is_verified = false` if Telegram `channel_id` changes) or run `action: "verify"` to send a live test message. |
|
|
290
|
+
| `GET` | `/admin/mpn/logs` | List PII-masked delivery logs (`mpn_delivery_log`) with optional `channel`, `event_name`, `entity_id`, `status`, and `limit` filters. |
|
|
291
|
+
| `GET` | `/admin/mpn/broadcasts` | List customer group broadcast campaigns (`mpn_broadcast_campaign`) and consent ledger summary counts (`opted_in_count`, `opted_out_count`). |
|
|
292
|
+
| `POST` | `/admin/mpn/broadcasts` | Create and optionally immediately dispatch (`send_now: true`) a broadcast campaign to opted-in recipients. |
|
|
293
|
+
| `GET` | `/admin/mpn/sandbox` | List captured sandbox messages and extracted OTP codes (`mpn_sandbox_message` + in-memory store). |
|
|
294
|
+
| `DELETE` | `/admin/mpn/sandbox` | Clear all captured sandbox messages. |
|
|
295
|
+
| `POST` | `/admin/mpn/send` | Ad-hoc manual notification dispatch or full event simulation (`event_name` + `event_data`), with optional `attach_invoice_pdf`. |
|
|
296
|
+
| `GET` | `/admin/plugin` | Health and capability summary endpoint for the Admin plugin surface. |
|
|
297
|
+
|
|
298
|
+
### Storefront & Webhook Endpoints (`/store/mpn/*` & `/store/plugin`)
|
|
299
|
+
|
|
300
|
+
| Method | Path | Description |
|
|
301
|
+
| ------ | ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
302
|
+
| `POST` | `/store/mpn/consent` | Record customer SMS/WhatsApp/Telegram opt-in or opt-out consent (`mpn_consent_record`). Enforces Anti-IDOR check via `verifyStoreCustomerSession`. |
|
|
303
|
+
| `GET` | `/store/mpn/webhooks/:provider` | Meta WhatsApp Cloud API webhook verification challenge (`hub.mode=subscribe`, `hub.verify_token`, `hub.challenge`). |
|
|
304
|
+
| `POST` | `/store/mpn/webhooks/:provider` | Inbound webhook handler (with `preserveRawBody: true` and timing-safe HMAC verification) for `whatsapp` / `openwa` / `waha` / `evolution` (`JOIN`/`STOP` + delivery receipts), `telegram` (`/start <customer_id>`, `/stop`), and `sms` / `signalhouse` / `twilio` (`JOIN`/`START`/`YES` and `STOP`/`UNSUBSCRIBE`). |
|
|
305
|
+
| `GET` | `/store/plugin` | Public storefront health endpoint. |
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
## 9. Typed SDK & React Hooks
|
|
310
|
+
|
|
311
|
+
### Typed SDK (`@vicacha-devs/medusa-plugin-notifications/sdk`)
|
|
312
|
+
|
|
313
|
+
Provides `NotificationsApiClient`, pre-configured singletons (`notificationsAdminSdk`, `notificationsStoreSdk`), and hierarchical query key factories (`notificationsQueryKeys`, `invalidateNotificationsQueries`):
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
import {
|
|
317
|
+
notificationsAdminSdk,
|
|
318
|
+
notificationsStoreSdk,
|
|
319
|
+
notificationsQueryKeys,
|
|
320
|
+
} from "@vicacha-devs/medusa-plugin-notifications/sdk"
|
|
321
|
+
|
|
322
|
+
// Admin: list templates or trigger a manual send
|
|
323
|
+
const { templates } = await notificationsAdminSdk.listTemplates({
|
|
324
|
+
channel: "whatsapp",
|
|
325
|
+
})
|
|
326
|
+
await notificationsAdminSdk.sendNotification({
|
|
327
|
+
channel: "telegram",
|
|
328
|
+
recipient: "-1001234567890",
|
|
329
|
+
template_id: "system_telegram_order_placed",
|
|
330
|
+
order_id: "order_01J...",
|
|
331
|
+
attach_invoice_pdf: true,
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
// Storefront: record checkout SMS marketing opt-in
|
|
335
|
+
await notificationsStoreSdk.updateConsent({
|
|
336
|
+
identifier: "+14155552671",
|
|
337
|
+
channel: "sms",
|
|
338
|
+
status: "opted_in",
|
|
339
|
+
})
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### React Hooks (`@vicacha-devs/medusa-plugin-notifications/hooks`)
|
|
343
|
+
|
|
344
|
+
- **Template Hooks (`use-mpn-templates.ts`)**: `useMpnTemplates`, `useMpnTemplateDetail`, `useCreateOrCloneMpnTemplate`, `useUpdateMpnTemplate`, `useDeleteMpnTemplate`, `useRenderMpnTemplatePreview`.
|
|
345
|
+
- **Event Mapping Hooks (`use-mpn-mappings.ts`)**: `useMpnMappings`, `useCreateMpnMapping`, `useUpdateMpnMapping`, `useDeleteMpnMapping`.
|
|
346
|
+
- **Channel Config Hooks (`use-mpn-channels.ts`)**: `useMpnChannels`, `useSaveOrVerifyMpnChannel`.
|
|
347
|
+
- **Dispatch, Logs, Broadcasts, Sandbox & Consent Hooks (`use-mpn-dispatch.ts`)**: `useMpnLogs`, `useMpnOrderLogs`, `useMpnBroadcasts`, `useCreateMpnBroadcast`, `useMpnSandboxMessages`, `useClearMpnSandbox`, `useSendMpnNotification`, `useUpdateStoreMpnConsent`.
|
|
348
|
+
- **Widget State Hook (`use-order-notifications-widget-state.ts`)**: `useOrderNotificationsWidgetState`.
|
|
349
|
+
|
|
350
|
+
---
|
|
351
|
+
|
|
352
|
+
## 10. Decoupled Admin Notification Studio (`/app/mpn`)
|
|
353
|
+
|
|
354
|
+
The Medusa Admin extension uses `LayoutComposer` and modular components (`src/admin/components/mpn/`, each `< 170` lines) across 7 dedicated routes plus an Order Details widget:
|
|
355
|
+
|
|
356
|
+
| Route / Zone | File | Layout | Purpose |
|
|
357
|
+
| --------------------- | -------------------------------------------------- | --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
358
|
+
| `/app/mpn` | `src/admin/routes/mpn/page.tsx` | `SINGLE_COLUMN` | Overview dashboard with `StatCard` metrics and `NavigationCard` links to all 6 studio workspaces. |
|
|
359
|
+
| `/app/mpn/templates` | `src/admin/routes/mpn/templates/page.tsx` | `SINGLE_COLUMN` | Visual Block Tree & Raw Template Builder, 1-click preset cloning, and live multi-locale entity preview. |
|
|
360
|
+
| `/app/mpn/mappings` | `src/admin/routes/mpn/mappings/page.tsx` | `SINGLE_COLUMN` | Store Event → Channel → Template → Recipient Rule mapping orchestrator with PDF invoice attachment toggle. |
|
|
361
|
+
| `/app/mpn/channels` | `src/admin/routes/mpn/channels/page.tsx` | `TWO_COLUMN` | Multi-driver gateway configuration (Meta Cloud / OpenWA / WAHA / Evolution, Telegram Supergroup verification gate, SMS carrier selector). |
|
|
362
|
+
| `/app/mpn/logs` | `src/admin/routes/mpn/logs/page.tsx` | `SINGLE_COLUMN` | PII-masked, idempotent delivery ledger table with status badges and error inspection. |
|
|
363
|
+
| `/app/mpn/broadcasts` | `src/admin/routes/mpn/broadcasts/page.tsx` | `TWO_COLUMN` | Customer group broadcast campaign dispatcher & history (main) + TCPA/10DLC consent summary (side). |
|
|
364
|
+
| `/app/mpn/sandbox` | `src/admin/routes/mpn/sandbox/page.tsx` | `SINGLE_COLUMN` | Local/Staging Dummy OTP & multi-channel sandbox message inspector. |
|
|
365
|
+
| `order.details.after` | `src/admin/widgets/order-notifications-widget.tsx` | Widget | Displays per-order notification history and 1-click manual resend across Email, WhatsApp, Telegram, or SMS with optional PDF invoice attachment. |
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
369
|
+
## 11. Security Architecture
|
|
370
|
+
|
|
371
|
+
1. **Meta WhatsApp Graph API Digits-Only Formatting (`toWhatsAppDigitsOnly`)**: Strips the leading `+` from E.164 phone numbers specifically when building Official Meta Cloud API payloads (`type: "template"`, `"interactive"`, `"text"`), preventing Meta's HTTP 200 silent drop bug while preserving standard E.164 (`+1...`) for Twilio, Signal House, and self-hosted gateways.
|
|
372
|
+
2. **PII Masking & Payload Redaction (`maskRecipientPii`, `redactSensitivePayload`)**: Every `mpn_delivery_log` row masks recipient emails/phones (`j***e@example.com`, `+141****671`), stores a salted SHA-256 `recipient_hash`, and redacts secrets, tokens, passwords, OTP codes, and large base64 buffers from `payload_redacted`.
|
|
373
|
+
3. **SSRF Protection (`validateSafeExternalUrl`)**: Validates all merchant-configured gateway URLs (OpenWA, WAHA, Evolution, Slack Webhooks, SMS Webhooks, MMS `MediaUrl`) against private/loopback IPv4/IPv6 ranges and cloud metadata endpoints (`169.254.169.254`) in production.
|
|
374
|
+
4. **Timing-Safe Webhook HMAC Verification (`verifyWebhookHmacSignature`)**: Uses `crypto.timingSafeEqual` on raw request bodies (`preserveRawBody: true`) for inbound provider webhooks (`/store/mpn/webhooks/:provider`).
|
|
375
|
+
5. **Partial Unique Idempotency Index (`UQ_mpn_delivery_log_idempotency_active`)**: Prevents duplicate notifications for the same `event_name:entity_id:channel:template_id:recipient` when `status IN ('pending', 'sent', 'delivered')`, while allowing manual retries of `'failed'` dispatches.
|
|
376
|
+
|
|
377
|
+
---
|
|
378
|
+
|
|
379
|
+
## 12. Development & Testing
|
|
380
|
+
|
|
381
|
+
```bash
|
|
382
|
+
# Run the unit, multi-channel rendering, provider, SDK & security test suite
|
|
383
|
+
pnpm test
|
|
384
|
+
|
|
385
|
+
# Type-check all TypeScript sources (backend + Admin UI)
|
|
386
|
+
pnpm run typecheck
|
|
387
|
+
|
|
388
|
+
# Build the Medusa v2 plugin
|
|
389
|
+
pnpm run build
|
|
390
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,136 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vicacha-devs/medusa-plugin-notifications",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Unified multi-channel notification studio for Medusa v2: visual and raw template builder, event mappings, consent ledger, abandoned cart recovery, broadcasts, and Email, WhatsApp, Telegram, SMS, Slack and sandbox providers.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"abandoned-cart",
|
|
7
|
+
"email",
|
|
8
|
+
"evolution-api",
|
|
9
|
+
"medusa",
|
|
10
|
+
"medusa-plugin",
|
|
11
|
+
"medusa-plugin-integration",
|
|
12
|
+
"medusa-plugin-notification",
|
|
13
|
+
"medusa-v2",
|
|
14
|
+
"notifications",
|
|
15
|
+
"openwa",
|
|
16
|
+
"otp",
|
|
17
|
+
"signalhouse",
|
|
18
|
+
"slack",
|
|
19
|
+
"sms",
|
|
20
|
+
"telegram",
|
|
21
|
+
"twilio",
|
|
22
|
+
"waha",
|
|
23
|
+
"whatsapp"
|
|
24
|
+
],
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "Vicacha Devs",
|
|
27
|
+
"files": [
|
|
28
|
+
".medusa/server",
|
|
29
|
+
"src",
|
|
30
|
+
"!src/__tests__",
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE",
|
|
33
|
+
"tsconfig.json"
|
|
34
|
+
],
|
|
35
|
+
"type": "module",
|
|
36
|
+
"exports": {
|
|
37
|
+
"./package.json": "./package.json",
|
|
38
|
+
".": "./.medusa/server/src/index.js",
|
|
39
|
+
"./admin": {
|
|
40
|
+
"import": "./.medusa/server/src/admin/index.js",
|
|
41
|
+
"default": "./.medusa/server/src/admin/index.js"
|
|
42
|
+
},
|
|
43
|
+
"./hooks": {
|
|
44
|
+
"import": "./src/hooks/index.ts",
|
|
45
|
+
"default": "./.medusa/server/src/hooks/index.js"
|
|
46
|
+
},
|
|
47
|
+
"./sdk": {
|
|
48
|
+
"import": "./src/sdk/index.ts",
|
|
49
|
+
"default": "./.medusa/server/src/sdk/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./types": {
|
|
52
|
+
"import": "./src/types/index.ts",
|
|
53
|
+
"default": "./.medusa/server/src/types/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./workflows": "./.medusa/server/src/workflows/index.js",
|
|
56
|
+
"./.medusa/server/src/modules/*": "./.medusa/server/src/modules/*/index.js",
|
|
57
|
+
"./modules/*": "./.medusa/server/src/modules/*/index.js",
|
|
58
|
+
"./providers/*": "./.medusa/server/src/providers/*/index.js",
|
|
59
|
+
"./rbac": {
|
|
60
|
+
"import": "./src/rbac.ts",
|
|
61
|
+
"default": "./.medusa/server/src/rbac.js"
|
|
62
|
+
},
|
|
63
|
+
"./*": "./.medusa/server/src/*.js"
|
|
64
|
+
},
|
|
65
|
+
"devDependencies": {
|
|
66
|
+
"@hookform/resolvers": "5.7.1",
|
|
67
|
+
"@medusajs/admin-sdk": "2.23.0",
|
|
68
|
+
"@medusajs/admin-shared": "2.23.0",
|
|
69
|
+
"@medusajs/cli": "2.23.0",
|
|
70
|
+
"@medusajs/core-flows": "2.23.0",
|
|
71
|
+
"@medusajs/dashboard": "2.23.0",
|
|
72
|
+
"@medusajs/framework": "2.23.0",
|
|
73
|
+
"@medusajs/icons": "2.23.0",
|
|
74
|
+
"@medusajs/js-sdk": "2.23.0",
|
|
75
|
+
"@medusajs/medusa": "2.23.0",
|
|
76
|
+
"@medusajs/test-utils": "2.23.0",
|
|
77
|
+
"@medusajs/types": "2.23.0",
|
|
78
|
+
"@medusajs/ui": "4.3.0",
|
|
79
|
+
"@mikro-orm/migrations": "6.4.3",
|
|
80
|
+
"@swc/core": "1.7.28",
|
|
81
|
+
"@types/node": "20.0.0",
|
|
82
|
+
"@types/nodemailer": "^6.4.17",
|
|
83
|
+
"@types/react": "^19.3.0",
|
|
84
|
+
"@types/react-dom": "^19.3.0",
|
|
85
|
+
"jiti": "2.0.0",
|
|
86
|
+
"nodemailer": "^6.10.0",
|
|
87
|
+
"prop-types": "15.8.1",
|
|
88
|
+
"react": "^19.3.1",
|
|
89
|
+
"react-dom": "^19.3.1",
|
|
90
|
+
"react-hook-form": "7.92.0",
|
|
91
|
+
"react-i18next": "18.0.1",
|
|
92
|
+
"react-router-dom": "7.18.5",
|
|
93
|
+
"ts-node": "10.9.2",
|
|
94
|
+
"tsc-alias": "^1.9.2",
|
|
95
|
+
"typescript": "5.6.2",
|
|
96
|
+
"vite": "^8.3.8"
|
|
97
|
+
},
|
|
98
|
+
"peerDependencies": {
|
|
99
|
+
"@hookform/resolvers": "5.7.1",
|
|
100
|
+
"@medusajs/admin-sdk": "2.23.0",
|
|
101
|
+
"@medusajs/admin-shared": "2.23.0",
|
|
102
|
+
"@medusajs/cli": "2.23.0",
|
|
103
|
+
"@medusajs/core-flows": "2.23.0",
|
|
104
|
+
"@medusajs/framework": "2.23.0",
|
|
105
|
+
"@medusajs/icons": "2.23.0",
|
|
106
|
+
"@medusajs/js-sdk": "2.23.0",
|
|
107
|
+
"@medusajs/medusa": "2.23.0",
|
|
108
|
+
"@medusajs/test-utils": "2.23.0",
|
|
109
|
+
"@medusajs/types": "2.23.0",
|
|
110
|
+
"@medusajs/ui": "4.3.0",
|
|
111
|
+
"nodemailer": ">=6.0.0",
|
|
112
|
+
"react": "^19.3.1",
|
|
113
|
+
"react-dom": "^19.3.1",
|
|
114
|
+
"react-hook-form": "7.92.0",
|
|
115
|
+
"react-i18next": "18.0.1",
|
|
116
|
+
"react-router-dom": "7.18.5",
|
|
117
|
+
"zod": "4.6.7"
|
|
118
|
+
},
|
|
119
|
+
"peerDependenciesMeta": {
|
|
120
|
+
"nodemailer": {
|
|
121
|
+
"optional": true
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
"engines": {
|
|
125
|
+
"node": ">=22.22.0"
|
|
126
|
+
},
|
|
127
|
+
"scripts": {
|
|
128
|
+
"build": "medusa plugin:build && tsc-alias -p tsconfig.json --resolve-full-paths",
|
|
129
|
+
"publish:local": "medusa plugin:publish",
|
|
130
|
+
"dev": "medusa plugin:develop",
|
|
131
|
+
"lint": "medusa lint",
|
|
132
|
+
"typecheck": "tsc --noEmit",
|
|
133
|
+
"test": "node --import ./src/__tests__/register-ts-resolver.mjs --import ../plugin-testing/register-js-extension.mjs --test src/__tests__/*.spec.ts",
|
|
134
|
+
"clean": "rm -rf .medusa node_modules"
|
|
135
|
+
}
|
|
6
136
|
}
|