Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

EventConsumer.cs

using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Messages;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Stores;
using Nop.Core.Events;
using Nop.Services.Events;
using Nop.Services.Messages;

namespace Nop.Plugin.Misc.Brevo.Services;

/// 
/// Represents event consumer
/// 
public class EventConsumer :
    IConsumer,
    IConsumer,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer,
    IConsumer,
    IConsumer>,
    IConsumer>
{
    #region Fields

    protected readonly BrevoManager _brevoEmailManager;
    protected readonly MarketingAutomationManager _marketingAutomationManager;

    #endregion

    #region Ctor

    public EventConsumer(BrevoManager brevoEmailManager,
        MarketingAutomationManager marketingAutomationManager)
    {
        _brevoEmailManager = brevoEmailManager;
        _marketingAutomationManager = marketingAutomationManager;
    }

    #endregion

    #region Methods

    /// 
    /// Handle the email unsubscribed event.
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EmailUnsubscribedEvent eventMessage)
    {
        //unsubscribe contact
        await _brevoEmailManager.UnsubscribeAsync(eventMessage.Subscription);
    }

    /// 
    /// Handle the email subscribed event.
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EmailSubscribedEvent eventMessage)
    {
        //subscribe contact
        await _brevoEmailManager.SubscribeAsync(eventMessage.Subscription);
    }

    /// 
    /// Handle the add shopping cart item event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        //handle event
        await _marketingAutomationManager.HandleShoppingCartChangedEventAsync(eventMessage.Entity);
    }

    /// 
    /// Handle the update shopping cart item event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        //handle event
        await _marketingAutomationManager.HandleShoppingCartChangedEventAsync(eventMessage.Entity);
    }

    /// 
    /// Handle the delete shopping cart item event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        //handle event
        await _marketingAutomationManager.HandleShoppingCartChangedEventAsync(eventMessage.Entity);
    }

    /// 
    /// Handle the order paid event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(OrderPaidEvent eventMessage)
    {
        //handle event
        await _marketingAutomationManager.HandleOrderCompletedEventAsync(eventMessage.Order);
        await _brevoEmailManager.UpdateContactAfterCompletingOrderAsync(eventMessage.Order);
    }

    /// 
    /// Handle the order placed event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(OrderPlacedEvent eventMessage)
    {
        //handle event
        await _marketingAutomationManager.HandleOrderPlacedEventAsync(eventMessage.Order);
    }

    /// 
    /// Handle the store tokens added event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public Task HandleEventAsync(EntityTokensAddedEvent eventMessage)
    {
        //handle event
        eventMessage.Tokens.Add(new Token("Store.Id", eventMessage.Entity.Id));

        return Task.CompletedTask;
    }

    /// 
    /// Handle the customer tokens added event
    /// 
    /// The event message.
    /// A task that represents the asynchronous operation
    public Task HandleEventAsync(EntityTokensAddedEvent eventMessage)
    {
        //handle event
        eventMessage.Tokens.Add(new Token("Customer.PhoneNumber", eventMessage.Entity.Phone));

        return Task.CompletedTask;
    }

    #endregion
}