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.Catalog;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Messages;
using Nop.Core.Domain.Orders;
using Nop.Core.Events;
using Nop.Plugin.Misc.Omnisend.Services;
using Nop.Services.Common;
using Nop.Services.Events;
using Nop.Web.Framework.Events;

namespace Nop.Plugin.Misc.Omnisend.Infrastructure;

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

    private readonly IGenericAttributeService _genericAttributeService;
    private readonly OmnisendEventsService _omnisendEventsService;
    private readonly OmnisendService _omnisendService;
    private readonly OmnisendSettings _settings;

    #endregion

    #region Ctor

    public EventConsumer(IGenericAttributeService genericAttributeService,
        OmnisendEventsService omnisendEventsService,
        OmnisendService omnisendService,
        OmnisendSettings settings)
    {
        _genericAttributeService = genericAttributeService;
        _omnisendEventsService = omnisendEventsService;
        _omnisendService = omnisendService;
        _settings = settings;
    }

    #endregion

    #region Methods

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(CustomerLoggedinEvent eventMessage)
    {
        if (string.IsNullOrEmpty(_settings.IdentifyContactScript))
            return;

        var script = _settings.IdentifyContactScript.Replace(OmnisendDefaults.Email, eventMessage.Customer.Email);

        await _genericAttributeService.SaveAttributeAsync(eventMessage.Customer,
            OmnisendDefaults.IdentifyContactAttribute, script);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(CustomerRegisteredEvent eventMessage)
    {
        await _omnisendService.UpdateContactAsync(eventMessage.Customer);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EmailSubscribedEvent eventMessage)
    {
        await _omnisendService.UpdateOrCreateContactAsync(eventMessage.Subscription, true);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EmailUnsubscribedEvent eventMessage)
    {
        await _omnisendService.UpdateOrCreateContactAsync(eventMessage.Subscription);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        var history = eventMessage.Entity;

        await _omnisendService.UpdateProductAsync(history.ProductId);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        await _omnisendService.UpdateProductAsync(eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        await _omnisendService.UpdateProductAsync(eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        var entity = eventMessage.Entity;

        if (entity.ShoppingCartType != ShoppingCartType.ShoppingCart)
            return;

        await _omnisendEventsService.SendAddedProductToCartEventAsync(entity);
        //await _omnisendService.AddShoppingCartItemAsync(eventMessage.Entity);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(OrderPlacedEvent eventMessage)
    {
        await _omnisendEventsService.SendOrderPlacedEventAsync(eventMessage.Order);
        await _omnisendService.PlaceOrderAsync(eventMessage.Order);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(OrderPaidEvent eventMessage)
    {
        await _omnisendEventsService.SendOrderPaidEventAsync(eventMessage);
        //await _omnisendService.UpdateOrderAsync(eventMessage.Order);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(OrderRefundedEvent eventMessage)
    {
        await _omnisendEventsService.SendOrderRefundedEventAsync(eventMessage);
        //await _omnisendService.UpdateOrderAsync(eventMessage.Order);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(OrderStatusChangedEvent eventMessage)
    {
        await _omnisendEventsService.SendOrderStatusChangedEventAsync(eventMessage);
        //await _omnisendService.UpdateOrderAsync(eventMessage.Order);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(PageRenderingEvent eventMessage)
    {
        await _omnisendEventsService.SendStartedCheckoutEventAsync(eventMessage);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        //await _omnisendService.EditShoppingCartItemAsync(eventMessage.Entity);
            
        return Task.CompletedTask;
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        await _omnisendService.DeleteShoppingCartItemAsync(eventMessage.Entity);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public Task HandleEventAsync(OrderAuthorizedEvent eventMessage)
    {
        //await _omnisendService.UpdateOrderAsync(eventMessage.Order);

        return Task.CompletedTask;
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public Task HandleEventAsync(OrderVoidedEvent eventMessage)
    {
        //await _omnisendService.UpdateOrderAsync(eventMessage.Order);

        return Task.CompletedTask;
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        await _omnisendService.OrderItemAddedAsync(eventMessage.Entity);
    }

    /// 
    /// Handle event
    /// 
    /// Event
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        await _omnisendService.CreateOrUpdateProductAsync(eventMessage.Entity);
    }

    #endregion
}