Suggestion: provide general methods to send messages

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
It would be nice if the WorkflowMessageService provided general methods to send messages, so that plugin authors don't have to replicate all the logic (and dependencies).  Domain entity params can be optional, and each entity param that is not null would have its tokens added:


public virtual int SendMessageToCustomer(string messageTemplateName, List<Token> tokens, Customer customer,  Order order = null, ...)
public virtual int SendMessageToStoreOwner(.. Vendor ...
public virtual int SendMessageToVendor(... Vendor ...

Example method


public virtual int SendMessageToCustomer(string messageTemplateName, List<Token> tokens, Customer customer,  Order order = null, ...)
{
    var store = order == null ? _storeContext.CurrentStore : _storeService.GetStoreById(order.StoreId) ?? _storeContext.CurrentStore;
    
    //determine language id automatically, based on the type (ToCustomer, ToStoreOwner, etc.) and other input parameters,...
    //i.e. determine the language id using any of
    //  _localizationSettings.DefaultAdminLanguageId  (e.g. if ToStoreOwner)
    //  _workContext.WorkingLanguage.Id  
    //  order.CustomerLanguageId  (e.g. if order != null)
    var languageId = ...
    languageId = EnsureLanguageIsActive(languageId, store.Id);

    var messageTemplate = GetActiveMessageTemplate("OrderPaid.StoreOwnerNotification", store.Id);
    if (messageTemplate == null)
        return 0;

    //from email
    var emailAccount = GetEmailAccountOfMessageTemplate(messageTemplate, languageId);

    //to email
    var toEmail = customer.Email;
    var toName = customer.GetFullName();
    
    //tokens
    if (tokens == null)
        tokens = new List<Token>();
    _messageTokenProvider.AddStoreTokens(tokens, store, emailAccount);
    _messageTokenProvider.AddCustomerTokens(tokens, order.Customer);
    if (order != null)
        _messageTokenProvider.AddOrderTokens(tokens, order, languageId);
    //... each entity param that is not null would have its tokens added similarly
                
    //event notification
    _eventPublisher.MessageTokensAdded(messageTemplate, tokens);

    return SendNotification(messageTemplate, emailAccount,
        languageId, tokens,
        toEmail, toName);
}
        
        
        
Additionally, a plugin may want to create a MessageTemplate.  Using "copy" would be ideal, but the Copy method should take an optional newName parameter:

\Libraries\Nop.Services\Messages\MessageTemplateService.cs

public virtual MessageTemplate CopyMessageTemplate(MessageTemplate messageTemplate, string newName = null)
{
7 years ago
Thanks a lot! I've just created a work item
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.