@astralibx/email-rule-engine 12.10.1 → 14.0.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/README.md CHANGED
@@ -1,52 +1,33 @@
1
1
  # @astralibx/email-rule-engine
2
2
 
3
+ Email automation wrapper over `@astralibx/rule-engine`
4
+
3
5
  [![npm version](https://img.shields.io/npm/v/@astralibx/email-rule-engine.svg)](https://www.npmjs.com/package/@astralibx/email-rule-engine)
4
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
5
7
 
6
- Rule-based email automation engine for Node.js. Define targeting rules with conditions, design responsive emails with MJML + Handlebars templates, and let the engine handle per-user throttling, send deduplication, distributed locking, and delivery scheduling. Ships with Express REST routes for admin UIs and a programmatic API for direct service access.
7
-
8
- ## Install
8
+ ## What This Package Does
9
9
 
10
- ```bash
11
- npm install @astralibx/email-rule-engine
12
- ```
10
+ `@astralibx/email-rule-engine` is a thin wrapper over [`@astralibx/rule-engine`](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/README.md). It adds MJML rendering and email-specific Handlebars helpers (`currency`, `formatDate`) on top of the core rule engine, adapting the generic `send` adapter into an email-aware `sendEmail` adapter that receives rendered `htmlBody` and `textBody` instead of raw template output.
13
11
 
14
- **Peer dependencies:**
12
+ ## What It Adds Over Core
15
13
 
16
- | Peer Dependency | Version |
17
- |-----------------|---------|
18
- | `@astralibx/core` | `^1.2.0` |
19
- | `express` | `^4.18.0 \|\| ^5.0.0` |
20
- | `mongoose` | `^7.0.0 \|\| ^8.0.0` |
21
- | `ioredis` | `^5.0.0` |
22
- | `handlebars` | `^4.7.0` |
23
- | `mjml` | `^4.0.0` |
24
- | `html-to-text` | `^9.0.0` |
25
-
26
- ```bash
27
- npm install express mongoose ioredis handlebars mjml html-to-text
28
- ```
14
+ - MJML to HTML conversion — body content is auto-wrapped in a full MJML structure and compiled to cross-client HTML
15
+ - HTML to plain text conversion — a `textBody` is automatically generated from rendered HTML via `html-to-text`
16
+ - `currency` Handlebars helper — formats numbers as INR (e.g. `₹1,234`)
17
+ - `formatDate` Handlebars helper formats dates in en-IN locale (e.g. `19 Mar 2026`)
18
+ - `sendEmail` adapter mapping converts the core's generic `send` adapter to an email-specific signature with `htmlBody`, `textBody`, and `subject`
29
19
 
30
20
  ## Quick Start
31
21
 
32
22
  ```typescript
33
- import express from 'express';
34
- import mongoose from 'mongoose';
35
- import Redis from 'ioredis';
36
23
  import { createEmailRuleEngine } from '@astralibx/email-rule-engine';
37
24
 
38
- const app = express();
39
- app.use(express.json());
40
-
41
- const dbConnection = mongoose.createConnection('mongodb://localhost/myapp');
42
- const redis = new Redis();
43
-
44
25
  const engine = createEmailRuleEngine({
45
- db: { connection: dbConnection },
26
+ db: { connection },
46
27
  redis: { connection: redis },
47
28
  adapters: {
48
29
  queryUsers: async (target, limit) => {
49
- return User.find({ role: target.role, platform: target.platform }).limit(limit).lean();
30
+ return User.find({ role: target.role }).limit(limit).lean();
50
31
  },
51
32
  resolveData: (user) => ({
52
33
  user: { name: user.name, email: user.email },
@@ -68,79 +49,34 @@ const engine = createEmailRuleEngine({
68
49
  },
69
50
  });
70
51
 
71
- // Mount REST routes
72
52
  app.use('/api/email-rules', engine.routes);
73
-
74
- // Trigger a run programmatically (e.g., from a cron job)
75
- // await engine.runner.runAllRules('cron');
76
-
77
- app.listen(3000);
78
53
  ```
79
54
 
80
- The factory returns an `EmailRuleEngine` instance with:
81
-
82
- - `routes` -- Express Router with all CRUD and runner endpoints
83
- - `runner` -- `RuleRunnerService` for triggering runs programmatically
84
- - `scheduler` -- `SchedulerService` for cron-based scheduled rule execution
85
- - `templateService` -- `TemplateService` for CRUD, preview, and cloning (`POST /templates/:id/clone`, `POST /templates/:id/preview-with-data`)
86
- - `ruleService` -- `RuleService` for CRUD, dry runs, and cloning (`POST /rules/:id/clone`)
87
- - `models` -- Direct Mongoose model access (`EmailTemplate`, `EmailRule`, `EmailRuleSend`, `EmailRuleRunLog`, `EmailThrottleConfig`)
88
-
89
- The `routes` router includes `GET /sends` for querying individual send log records and `schedule` field support on rules for cron-based execution.
90
-
91
- ## Configuration
92
-
93
- The `createEmailRuleEngine(config)` factory accepts an `EmailRuleEngineConfig` object validated at startup with Zod.
94
-
95
- | Section | Required | Description |
96
- |---------|----------|-------------|
97
- | `db` | Yes | Mongoose connection and optional collection prefix |
98
- | `redis` | Yes | ioredis connection for distributed locking |
99
- | `adapters` | Yes | 5 required + 1 optional function bridging your app to the engine |
100
- | `collections` | No | MongoDB collection schemas for field dropdowns, type-aware operators, and validation |
101
- | `platforms` | No | Valid platform values for schema validation |
102
- | `audiences` | No | Valid audience/role values for schema validation |
103
- | `categories` | No | Valid template categories for schema validation |
104
- | `options` | No | Lock TTL, max per run, send window, delays |
105
- | `hooks` | No | Callbacks at key execution points |
106
- | `logger` | No | Logger with `info`, `warn`, `error` methods |
107
-
108
- See [docs/configuration.md](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/configuration.md) for the full reference with examples.
109
-
110
- ## Getting Started Guide
111
-
112
- 1. [Configuration](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/configuration.md) — Set up database, Redis, and options
113
- 2. [Adapters](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/adapters.md) — Implement the 6 required adapter functions
114
- 3. [Templates](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/templates.md) — Create email templates with MJML + Handlebars
115
- 4. [Rules](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/rules.md) — Define targeting rules with conditions or explicit lists
116
- 5. [Execution Flow](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/execution-flow.md) — Understand how the runner processes rules
117
- 6. [Throttling](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/throttling.md) — Configure per-user send limits
118
-
119
- 7. [Collections](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/collections.md) — Register collection schemas for field dropdowns and type-aware validation
120
-
121
- Reference: [API Routes](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/api-routes.md) | [Programmatic API](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/programmatic-api.md) | [Types](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/types.md) | [Constants](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/constants.md) | [Error Handling](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/error-handling.md)
122
-
123
55
  ### Redis Key Prefix (Required for Multi-Project Deployments)
124
56
 
125
- > **WARNING:** If multiple projects share the same Redis server, you MUST set a unique `keyPrefix` per project. Without this, run locks, cancel flags, and progress keys will collide between projects.
57
+ > **WARNING:** If multiple projects share the same Redis server, set a unique `keyPrefix` per project. Without this, run locks and cancel flags will collide between projects.
126
58
 
127
59
  ```typescript
128
60
  const engine = createEmailRuleEngine({
129
- redis: {
130
- connection: redis,
131
- keyPrefix: 'myproject:', // REQUIRED if sharing Redis
132
- },
61
+ redis: { connection: redis, keyPrefix: 'myproject:' },
133
62
  // ...
134
63
  });
135
64
  ```
136
65
 
137
- | Default | Risk |
138
- |---------|------|
139
- | `''` (empty) | Two projects share global keys like `email-rule-runner:lock` and `run:{id}:cancel` Project A can cancel Project B's runs |
66
+ ## Configuration
67
+
68
+ See [`docs/configuration.md`](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/configuration.md) for the `EmailRuleEngineConfig` interface and how `sendEmail` maps to the core's generic `send`.
69
+
70
+ ## Core Documentation
140
71
 
141
- **Always set a unique prefix** like `projectname:` when sharing Redis.
72
+ For templates, rules, conditions, collections, joins, throttling, hooks, and the full API reference, see the core documentation:
142
73
 
143
- > **Important:** Configure throttle settings before running rules. Default limits (1/day, 2/week) may be too restrictive. See [Throttling](https://github.com/Hariprakash1997/astralib/blob/main/packages/email/rule-engine/docs/throttling.md).
74
+ - [Quick Start Tutorial](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/docs/quick-start-tutorial.md)
75
+ - [Adapters](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/docs/adapters.md)
76
+ - [Collections & Joins](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/docs/collections-and-joins.md)
77
+ - [Templates & Rules](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/docs/templates-and-rules.md)
78
+ - [Throttling & Hooks](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/docs/throttling-and-hooks.md)
79
+ - [Glossary](https://github.com/Hariprakash1997/astralib/blob/main/packages/rule-engine/core/docs/glossary.md)
144
80
 
145
81
  ## License
146
82