2.48.0 • Published 4 months ago

@memberjunction/entity-communications-base v2.48.0

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

@memberjunction/entity-communications-base

Base types and abstractions for implementing entity-level communications in MemberJunction. This package provides the foundation for building communication engines that can send messages (email, SMS, etc.) to entity records.

Overview

The Entity Communications Base package enables developers to:

  • Define communication message types for specific entities
  • Configure communication fields and templates per entity
  • Execute bulk communications against entity record sets
  • Support multiple communication providers (email, SMS, etc.)
  • Preview messages before sending

Installation

npm install @memberjunction/entity-communications-base

Core Components

EntityCommunicationsEngineBase

Abstract base class for implementing entity communication engines. Provides metadata loading, configuration, and validation capabilities.

import { EntityCommunicationsEngineBase } from '@memberjunction/entity-communications-base';

class MyEntityCommunicationEngine extends EntityCommunicationsEngineBase {
    async RunEntityCommunication(params: EntityCommunicationParams): Promise<EntityCommunicationResult> {
        // Implementation for sending communications
    }
}

EntityCommunicationMessageTypeExtended

Extended entity class that includes related communication fields.

const messageTypes = engine.GetEntityCommunicationMessageTypes(entityID);
messageTypes.forEach(messageType => {
    console.log(`Message Type: ${messageType.Name}`);
    console.log(`Fields: ${messageType.CommunicationFields.map(f => f.Name).join(', ')}`);
});

EntityCommunicationParams

Parameters for executing entity communications:

const params: EntityCommunicationParams = {
    EntityID: 'entity-id-here',
    RunViewParams: {
        EntityName: 'Contacts',
        ExtraFilter: 'Status = "Active"'
    },
    ProviderName: 'SendGrid',
    ProviderMessageTypeName: 'Email',
    Message: {
        Subject: 'Hello {{FirstName}}',
        Body: 'Welcome to our service!',
        HTMLBody: '<h1>Welcome {{FirstName}}!</h1>'
    },
    PreviewOnly: false,
    IncludeProcessedMessages: true
};

Usage Examples

Basic Implementation

import { 
    EntityCommunicationsEngineBase, 
    EntityCommunicationParams, 
    EntityCommunicationResult 
} from '@memberjunction/entity-communications-base';
import { RegisterClass } from '@memberjunction/global';

@RegisterClass(EntityCommunicationsEngineBase)
export class CustomEntityCommunicationEngine extends EntityCommunicationsEngineBase {
    
    async RunEntityCommunication(params: EntityCommunicationParams): Promise<EntityCommunicationResult> {
        try {
            // Validate entity supports communication
            if (!this.EntitySupportsCommunication(params.EntityID)) {
                return {
                    Success: false,
                    ErrorMessage: 'Entity does not support communications'
                };
            }

            // Get message types for entity
            const messageTypes = this.GetEntityCommunicationMessageTypes(params.EntityID);
            
            // Process and send messages
            const results = await this.processMessages(params);
            
            return {
                Success: true,
                Results: results
            };
        } catch (error) {
            return {
                Success: false,
                ErrorMessage: error.message
            };
        }
    }
    
    private async processMessages(params: EntityCommunicationParams) {
        // Implementation specific to your communication provider
    }
}

Configuration and Initialization

import { CustomEntityCommunicationEngine } from './custom-engine';
import { UserInfo } from '@memberjunction/core';

// Initialize the engine
const engine = CustomEntityCommunicationEngine.Instance;

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

// Check if entity supports communication
const entityID = 'some-entity-id';
if (engine.EntitySupportsCommunication(entityID)) {
    // Get available message types
    const messageTypes = engine.GetEntityCommunicationMessageTypes(entityID);
    console.log(`Found ${messageTypes.length} message types`);
}

Sending Communications

// Send email to all active contacts
const result = await engine.RunEntityCommunication({
    EntityID: 'contact-entity-id',
    RunViewParams: {
        EntityName: 'Contacts',
        ExtraFilter: 'IsActive = 1',
        OrderBy: 'LastName, FirstName'
    },
    ProviderName: 'SendGrid',
    ProviderMessageTypeName: 'Email',
    Message: {
        Subject: 'Monthly Newsletter',
        HTMLBody: '<html>...</html>',
        Body: 'Plain text version...'
    },
    PreviewOnly: false,
    IncludeProcessedMessages: true
});

if (result.Success) {
    console.log(`Sent ${result.Results.length} messages`);
    result.Results.forEach(item => {
        console.log(`Sent to: ${item.RecipientData.Email}`);
        console.log(`Message ID: ${item.Message.MessageID}`);
    });
} else {
    console.error(`Communication failed: ${result.ErrorMessage}`);
}

API Reference

EntityCommunicationsEngineBase

Properties

  • EntityCommunicationMessageTypes: Array of extended message type entities
  • EntityCommunicationFields: Array of communication field entities
  • Instance: Static singleton instance of the engine

Methods

  • Config(forceRefresh?, contextUser?, provider?): Initialize engine configuration
  • GetEntityCommunicationMessageTypes(entityID): Get message types for an entity
  • EntitySupportsCommunication(entityID): Check if entity supports communications
  • RunEntityCommunication(params): Abstract method to implement communication execution

EntityCommunicationParams

  • EntityID: ID of the entity to communicate with
  • RunViewParams: Parameters for fetching entity records
  • ProviderName: Name of the communication provider
  • ProviderMessageTypeName: Type of message (e.g., 'Email', 'SMS')
  • Message: Message content object
  • PreviewOnly?: If true, preview without sending
  • IncludeProcessedMessages?: Include processed message data in results

EntityCommunicationResult

  • Success: Boolean indicating operation success
  • ErrorMessage?: Error details if operation failed
  • Results?: Array of EntityCommunicationResultItem objects

EntityCommunicationResultItem

  • RecipientData: Entity record data for recipient
  • Message: Processed message with provider-specific details

Dependencies

  • @memberjunction/global: Global utilities and registration
  • @memberjunction/core: Core MemberJunction functionality
  • @memberjunction/core-entities: Core entity definitions
  • @memberjunction/communication-types: Communication type definitions

Integration with Other MJ Packages

This package integrates with:

  • @memberjunction/communication-types: Provides base communication types and interfaces
  • @memberjunction/core: Uses BaseEngine pattern and metadata providers
  • @memberjunction/templates: Can be used with template engine for dynamic content
  • @memberjunction/server: Server-side implementations typically extend this base

Build Notes

  • TypeScript compilation: npm run build
  • Source files in /src, compiled to /dist
  • Supports both CommonJS and ES modules
  • Type definitions included

License

ISC

2.27.1

8 months ago

2.23.2

8 months ago

2.46.0

5 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.15.1

9 months ago

2.38.0

5 months ago

2.45.0

5 months ago

2.22.1

9 months ago

2.22.0

9 months ago

2.41.0

5 months ago

2.22.2

9 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

6 months ago

2.14.0

10 months ago

2.21.0

9 months ago

2.44.0

5 months ago

2.40.0

5 months ago

2.29.0

8 months ago

2.29.2

8 months ago

2.29.1

8 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.1.0

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