2.48.0 • Published 4 months ago

@memberjunction/entity-communications-server v2.48.0

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

@memberjunction/entity-communications-server

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.

Overview

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:

  • Bulk message sending to entity record sets
  • Template parameter resolution with related entity data
  • Multi-provider support (email, SMS, etc.)
  • Context data population for personalized messages
  • Scheduled message sending (when supported by provider)

Installation

npm install @memberjunction/entity-communications-server

Dependencies

This package depends on the following MemberJunction packages:

  • @memberjunction/global - Core global utilities
  • @memberjunction/core - Core MJ functionality including metadata, entities, and views
  • @memberjunction/core-entities - Core entity definitions
  • @memberjunction/communication-engine - Base communication engine
  • @memberjunction/entity-communications-base - Base entity communications types and interfaces

Usage

Basic Example

import { EntityCommunicationsEngine } from '@memberjunction/entity-communications-server';
import { EntityCommunicationParams } from '@memberjunction/entity-communications-base';
import { UserInfo } from '@memberjunction/core';

// Get the singleton instance
const engine = EntityCommunicationsEngine.Instance;

// Configure the engine with user context
const currentUser = new UserInfo(); // Your current user
await engine.Config(false, currentUser);

// Set up communication parameters
const params: EntityCommunicationParams = {
    EntityID: 'entity-uuid-here',
    RunViewParams: {
        EntityName: 'Contacts',
        ViewName: 'Active Contacts',
        ExtraFilter: 'Status = "Active"'
    },
    ProviderName: 'SendGrid',
    ProviderMessageTypeName: 'Email',
    Message: {
        Subject: 'Welcome to our service',
        Body: 'Hello {{FirstName}}, welcome!',
        // Optional: Use templates for dynamic content
        SubjectTemplate: subjectTemplateEntity,
        BodyTemplate: bodyTemplateEntity,
        HTMLBodyTemplate: htmlBodyTemplateEntity
    },
    PreviewOnly: false // Set to true to preview without sending
};

// Execute the communication
const result = await engine.RunEntityCommunication(params);

if (result.Success) {
    console.log(`Successfully sent ${result.Results.length} messages`);
} else {
    console.error(`Failed: ${result.ErrorMessage}`);
}

Using Templates with Related Data

// Example with templates that include related entity data
const params: EntityCommunicationParams = {
    EntityID: 'customer-entity-id',
    RunViewParams: {
        EntityName: 'Customers',
        ViewName: 'Premium Customers'
    },
    ProviderName: 'Twilio',
    ProviderMessageTypeName: 'SMS',
    Message: {
        // Templates can reference both record fields and related entity data
        BodyTemplate: templateWithOrdersParam, // Template with "Orders" parameter
        // The engine will automatically fetch related orders for each customer
    }
};

const result = await engine.RunEntityCommunication(params);

Checking Entity Communication Support

// Check if an entity supports communication
const entityID = 'entity-uuid';
if (engine.EntitySupportsCommunication(entityID)) {
    // Get available message types for the entity
    const messageTypes = engine.GetEntityCommunicationMessageTypes(entityID);
    
    messageTypes.forEach(mt => {
        console.log(`Message Type: ${mt.BaseMessageType}`);
        // Each message type has associated fields that can be used for recipient addresses
        mt.CommunicationFields.forEach(field => {
            console.log(`  Field: ${field.FieldName} (Priority: ${field.Priority})`);
        });
    });
}

API Reference

EntityCommunicationsEngine

The main engine class that handles entity-based communications.

Methods

RunEntityCommunication(params: EntityCommunicationParams): Promise<EntityCommunicationResult>

Executes a communication request against a view of entity records.

Parameters:

  • params: Configuration object containing:
    • EntityID: The UUID of the entity to communicate with
    • RunViewParams: Parameters for the view query to retrieve records
    • ProviderName: Name of the communication provider (e.g., "SendGrid", "Twilio")
    • ProviderMessageTypeName: Type of message for the provider (e.g., "Email", "SMS")
    • Message: The message content and optional templates
    • PreviewOnly: (Optional) If true, preview without sending
    • IncludeProcessedMessages: (Optional) Include processed message content in results

Returns: EntityCommunicationResult with success status and sent messages

Config(forceRefresh?: boolean, contextUser?: UserInfo, provider?: IMetadataProvider): Promise<void>

Configures the engine with user context and loads metadata.

EntitySupportsCommunication(entityID: string): boolean

Checks if an entity has communication capabilities configured.

GetEntityCommunicationMessageTypes(entityID: string): EntityCommunicationMessageTypeExtended[]

Retrieves available message types for an entity.

Types

EntityCommunicationParams

class EntityCommunicationParams {
    EntityID: string;
    RunViewParams: RunViewParams;
    ProviderName: string;
    ProviderMessageTypeName: string;
    Message: Message;
    PreviewOnly?: boolean;
    IncludeProcessedMessages?: boolean;
}

EntityCommunicationResult

class EntityCommunicationResult {
    Success: boolean;
    ErrorMessage?: string;
    Results?: EntityCommunicationResultItem[];
}

EntityCommunicationResultItem

class EntityCommunicationResultItem {
    RecipientData: any;
    Message: ProcessedMessage;
}

Configuration

Entity Communication Setup

For an entity to support communications:

  1. Configure Entity Communication Message Types in the MJ metadata
  2. Set up Entity Communication Fields to define recipient address fields
  3. Configure field priorities for automatic recipient resolution

Template Parameters

Templates support multiple parameter types:

  • Record: The current entity record being processed
  • Entity: Related entity data fetched based on relationships
  • Array/Scalar/Object: Direct programmatic use (not supported in messaging)

When using related entity parameters, the engine automatically: 1. Identifies unique relationships needed 2. Fetches all related data in batch queries 3. Filters related data per recipient record 4. Populates template context with filtered data

Provider Requirements

Communication providers must:

  • Be registered in the CommunicationEngine
  • Support sending (SupportsSending = true)
  • Support scheduled sending if SendAt is specified
  • Have matching message types configured

Error Handling

The engine validates:

  • Entity existence and communication support
  • Provider availability and capabilities
  • Message type compatibility
  • Template parameter alignment (no conflicting parameter definitions)
  • Field availability for recipient addresses

All errors are returned in the EntityCommunicationResult.ErrorMessage field.

Performance Considerations

  • The engine fetches all entity fields to ensure template access
  • Related entity data is batch-loaded for all recipients
  • Large recipient sets should be paginated using view parameters
  • Use PreviewOnly mode for testing before bulk sends

Integration with MemberJunction

This package integrates with:

  • Metadata System: For entity and field definitions
  • View Engine: For querying recipient records
  • Template Engine: For message personalization
  • Communication Engine: For actual message delivery
  • Security: Respects user context and permissions

Building

npm run build

The package is built using TypeScript and outputs to the dist directory.

License

ISC

Support

For issues and questions, please refer to the main MemberJunction documentation at MemberJunction.com

2.27.1

8 months ago

2.23.2

8 months ago

2.46.0

4 months ago

2.23.1

8 months ago

2.27.0

8 months ago

2.34.0

6 months ago

2.30.0

7 months ago

2.19.4

9 months ago

2.19.5

9 months ago

2.19.2

9 months ago

2.19.3

9 months ago

2.19.0

9 months ago

2.19.1

9 months ago

2.15.2

9 months ago

2.34.2

6 months ago

2.34.1

6 months ago

2.38.0

5 months ago

2.45.0

5 months ago

2.22.1

8 months ago

2.22.0

8 months ago

2.41.0

5 months ago

2.22.2

8 months ago

2.26.1

8 months ago

2.26.0

8 months ago

2.33.0

6 months ago

2.18.3

9 months ago

2.18.1

9 months ago

2.18.2

9 months ago

2.18.0

9 months ago

2.37.1

5 months ago

2.37.0

5 months ago

2.14.0

9 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.40.0

5 months ago

2.29.0

7 months ago

2.29.2

7 months ago

2.29.1

7 months ago

2.25.0

8 months ago

2.48.0

4 months ago

2.32.0

7 months ago

2.32.2

7 months ago

2.32.1

7 months ago

2.17.0

9 months ago

2.13.4

10 months ago

2.36.0

6 months ago

2.13.2

11 months ago

2.13.3

10 months ago

2.13.0

11 months ago

2.36.1

6 months ago

2.13.1

11 months ago

2.43.0

5 months ago

2.20.2

9 months ago

2.20.3

9 months ago

2.20.0

9 months ago

2.20.1

9 months ago

2.28.0

8 months ago

2.47.0

4 months ago

2.24.1

8 months ago

2.24.0

8 months ago

2.31.0

7 months ago

2.12.0

12 months ago

2.39.0

5 months ago

2.16.1

9 months ago

2.35.1

6 months ago

2.35.0

6 months ago

2.16.0

9 months ago

2.42.1

5 months ago

2.42.0

5 months ago

2.23.0

8 months ago

2.11.0

12 months ago

2.10.0

12 months ago

2.9.0

12 months ago

2.8.0

1 year ago

2.7.0

1 year ago

2.6.1

1 year ago

2.5.2

1 year ago

2.6.0

1 year ago

2.7.1

1 year ago

2.5.1

1 year ago

2.5.0

1 year ago

2.4.1

1 year ago

2.4.0

1 year ago

2.3.3

1 year ago

2.3.2

1 year ago

2.3.1

1 year ago

2.3.0

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.5

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.0.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

1.7.1

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.3

1 year ago