@memberjunction/entity-communications-server 2.43.0 → 2.45.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.
Files changed (2) hide show
  1. package/README.md +263 -0
  2. package/package.json +6 -6
package/README.md ADDED
@@ -0,0 +1,263 @@
1
+ # @memberjunction/entity-communications-server
2
+
3
+ A server-side implementation of the MemberJunction Entity Communications Engine that connects the MJ entities framework to the communication framework, enabling bulk communications to entity records.
4
+
5
+ ## Overview
6
+
7
+ This package provides a server-side engine for sending communications (emails, SMS, etc.) to records retrieved from entity views. It extends the base entity communications functionality with server-specific capabilities, including:
8
+
9
+ - Bulk message sending to entity record sets
10
+ - Template parameter resolution with related entity data
11
+ - Multi-provider support (email, SMS, etc.)
12
+ - Context data population for personalized messages
13
+ - Scheduled message sending (when supported by provider)
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @memberjunction/entity-communications-server
19
+ ```
20
+
21
+ ## Dependencies
22
+
23
+ This package depends on the following MemberJunction packages:
24
+
25
+ - `@memberjunction/global` - Core global utilities
26
+ - `@memberjunction/core` - Core MJ functionality including metadata, entities, and views
27
+ - `@memberjunction/core-entities` - Core entity definitions
28
+ - `@memberjunction/communication-engine` - Base communication engine
29
+ - `@memberjunction/entity-communications-base` - Base entity communications types and interfaces
30
+
31
+ ## Usage
32
+
33
+ ### Basic Example
34
+
35
+ ```typescript
36
+ import { EntityCommunicationsEngine } from '@memberjunction/entity-communications-server';
37
+ import { EntityCommunicationParams } from '@memberjunction/entity-communications-base';
38
+ import { UserInfo } from '@memberjunction/core';
39
+
40
+ // Get the singleton instance
41
+ const engine = EntityCommunicationsEngine.Instance;
42
+
43
+ // Configure the engine with user context
44
+ const currentUser = new UserInfo(); // Your current user
45
+ await engine.Config(false, currentUser);
46
+
47
+ // Set up communication parameters
48
+ const params: EntityCommunicationParams = {
49
+ EntityID: 'entity-uuid-here',
50
+ RunViewParams: {
51
+ EntityName: 'Contacts',
52
+ ViewName: 'Active Contacts',
53
+ ExtraFilter: 'Status = "Active"'
54
+ },
55
+ ProviderName: 'SendGrid',
56
+ ProviderMessageTypeName: 'Email',
57
+ Message: {
58
+ Subject: 'Welcome to our service',
59
+ Body: 'Hello {{FirstName}}, welcome!',
60
+ // Optional: Use templates for dynamic content
61
+ SubjectTemplate: subjectTemplateEntity,
62
+ BodyTemplate: bodyTemplateEntity,
63
+ HTMLBodyTemplate: htmlBodyTemplateEntity
64
+ },
65
+ PreviewOnly: false // Set to true to preview without sending
66
+ };
67
+
68
+ // Execute the communication
69
+ const result = await engine.RunEntityCommunication(params);
70
+
71
+ if (result.Success) {
72
+ console.log(`Successfully sent ${result.Results.length} messages`);
73
+ } else {
74
+ console.error(`Failed: ${result.ErrorMessage}`);
75
+ }
76
+ ```
77
+
78
+ ### Using Templates with Related Data
79
+
80
+ ```typescript
81
+ // Example with templates that include related entity data
82
+ const params: EntityCommunicationParams = {
83
+ EntityID: 'customer-entity-id',
84
+ RunViewParams: {
85
+ EntityName: 'Customers',
86
+ ViewName: 'Premium Customers'
87
+ },
88
+ ProviderName: 'Twilio',
89
+ ProviderMessageTypeName: 'SMS',
90
+ Message: {
91
+ // Templates can reference both record fields and related entity data
92
+ BodyTemplate: templateWithOrdersParam, // Template with "Orders" parameter
93
+ // The engine will automatically fetch related orders for each customer
94
+ }
95
+ };
96
+
97
+ const result = await engine.RunEntityCommunication(params);
98
+ ```
99
+
100
+ ### Checking Entity Communication Support
101
+
102
+ ```typescript
103
+ // Check if an entity supports communication
104
+ const entityID = 'entity-uuid';
105
+ if (engine.EntitySupportsCommunication(entityID)) {
106
+ // Get available message types for the entity
107
+ const messageTypes = engine.GetEntityCommunicationMessageTypes(entityID);
108
+
109
+ messageTypes.forEach(mt => {
110
+ console.log(`Message Type: ${mt.BaseMessageType}`);
111
+ // Each message type has associated fields that can be used for recipient addresses
112
+ mt.CommunicationFields.forEach(field => {
113
+ console.log(` Field: ${field.FieldName} (Priority: ${field.Priority})`);
114
+ });
115
+ });
116
+ }
117
+ ```
118
+
119
+ ## API Reference
120
+
121
+ ### EntityCommunicationsEngine
122
+
123
+ The main engine class that handles entity-based communications.
124
+
125
+ #### Methods
126
+
127
+ ##### `RunEntityCommunication(params: EntityCommunicationParams): Promise<EntityCommunicationResult>`
128
+
129
+ Executes a communication request against a view of entity records.
130
+
131
+ **Parameters:**
132
+ - `params`: Configuration object containing:
133
+ - `EntityID`: The UUID of the entity to communicate with
134
+ - `RunViewParams`: Parameters for the view query to retrieve records
135
+ - `ProviderName`: Name of the communication provider (e.g., "SendGrid", "Twilio")
136
+ - `ProviderMessageTypeName`: Type of message for the provider (e.g., "Email", "SMS")
137
+ - `Message`: The message content and optional templates
138
+ - `PreviewOnly`: (Optional) If true, preview without sending
139
+ - `IncludeProcessedMessages`: (Optional) Include processed message content in results
140
+
141
+ **Returns:** `EntityCommunicationResult` with success status and sent messages
142
+
143
+ ##### `Config(forceRefresh?: boolean, contextUser?: UserInfo, provider?: IMetadataProvider): Promise<void>`
144
+
145
+ Configures the engine with user context and loads metadata.
146
+
147
+ ##### `EntitySupportsCommunication(entityID: string): boolean`
148
+
149
+ Checks if an entity has communication capabilities configured.
150
+
151
+ ##### `GetEntityCommunicationMessageTypes(entityID: string): EntityCommunicationMessageTypeExtended[]`
152
+
153
+ Retrieves available message types for an entity.
154
+
155
+ ### Types
156
+
157
+ #### EntityCommunicationParams
158
+
159
+ ```typescript
160
+ class EntityCommunicationParams {
161
+ EntityID: string;
162
+ RunViewParams: RunViewParams;
163
+ ProviderName: string;
164
+ ProviderMessageTypeName: string;
165
+ Message: Message;
166
+ PreviewOnly?: boolean;
167
+ IncludeProcessedMessages?: boolean;
168
+ }
169
+ ```
170
+
171
+ #### EntityCommunicationResult
172
+
173
+ ```typescript
174
+ class EntityCommunicationResult {
175
+ Success: boolean;
176
+ ErrorMessage?: string;
177
+ Results?: EntityCommunicationResultItem[];
178
+ }
179
+ ```
180
+
181
+ #### EntityCommunicationResultItem
182
+
183
+ ```typescript
184
+ class EntityCommunicationResultItem {
185
+ RecipientData: any;
186
+ Message: ProcessedMessage;
187
+ }
188
+ ```
189
+
190
+ ## Configuration
191
+
192
+ ### Entity Communication Setup
193
+
194
+ For an entity to support communications:
195
+
196
+ 1. Configure `Entity Communication Message Types` in the MJ metadata
197
+ 2. Set up `Entity Communication Fields` to define recipient address fields
198
+ 3. Configure field priorities for automatic recipient resolution
199
+
200
+ ### Template Parameters
201
+
202
+ Templates support multiple parameter types:
203
+
204
+ - **Record**: The current entity record being processed
205
+ - **Entity**: Related entity data fetched based on relationships
206
+ - **Array/Scalar/Object**: Direct programmatic use (not supported in messaging)
207
+
208
+ When using related entity parameters, the engine automatically:
209
+ 1. Identifies unique relationships needed
210
+ 2. Fetches all related data in batch queries
211
+ 3. Filters related data per recipient record
212
+ 4. Populates template context with filtered data
213
+
214
+ ### Provider Requirements
215
+
216
+ Communication providers must:
217
+ - Be registered in the CommunicationEngine
218
+ - Support sending (`SupportsSending = true`)
219
+ - Support scheduled sending if `SendAt` is specified
220
+ - Have matching message types configured
221
+
222
+ ## Error Handling
223
+
224
+ The engine validates:
225
+ - Entity existence and communication support
226
+ - Provider availability and capabilities
227
+ - Message type compatibility
228
+ - Template parameter alignment (no conflicting parameter definitions)
229
+ - Field availability for recipient addresses
230
+
231
+ All errors are returned in the `EntityCommunicationResult.ErrorMessage` field.
232
+
233
+ ## Performance Considerations
234
+
235
+ - The engine fetches all entity fields to ensure template access
236
+ - Related entity data is batch-loaded for all recipients
237
+ - Large recipient sets should be paginated using view parameters
238
+ - Use `PreviewOnly` mode for testing before bulk sends
239
+
240
+ ## Integration with MemberJunction
241
+
242
+ This package integrates with:
243
+ - **Metadata System**: For entity and field definitions
244
+ - **View Engine**: For querying recipient records
245
+ - **Template Engine**: For message personalization
246
+ - **Communication Engine**: For actual message delivery
247
+ - **Security**: Respects user context and permissions
248
+
249
+ ## Building
250
+
251
+ ```bash
252
+ npm run build
253
+ ```
254
+
255
+ The package is built using TypeScript and outputs to the `dist` directory.
256
+
257
+ ## License
258
+
259
+ ISC
260
+
261
+ ## Support
262
+
263
+ For issues and questions, please refer to the main MemberJunction documentation at [MemberJunction.com](https://www.memberjunction.com)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@memberjunction/entity-communications-server",
3
- "version": "2.43.0",
3
+ "version": "2.45.0",
4
4
  "description": "MemberJunction: Library that connects the MJ entities framework to the communication framework",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,10 +19,10 @@
19
19
  "typescript": "^5.4.5"
20
20
  },
21
21
  "dependencies": {
22
- "@memberjunction/global": "2.43.0",
23
- "@memberjunction/core": "2.43.0",
24
- "@memberjunction/core-entities": "2.43.0",
25
- "@memberjunction/communication-engine": "2.43.0",
26
- "@memberjunction/entity-communications-base": "2.43.0"
22
+ "@memberjunction/global": "2.45.0",
23
+ "@memberjunction/core": "2.45.0",
24
+ "@memberjunction/core-entities": "2.45.0",
25
+ "@memberjunction/communication-engine": "2.45.0",
26
+ "@memberjunction/entity-communications-base": "2.45.0"
27
27
  }
28
28
  }