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 (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; await _omnisendService.UpdateContactAsync(eventMessage.Customer); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EmailSubscribedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; await _omnisendService.UpdateOrCreateContactAsync(eventMessage.Subscription, true); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EmailUnsubscribedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; await _omnisendService.UpdateOrCreateContactAsync(eventMessage.Subscription); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; await _omnisendService.UpdateProductAsync(eventMessage.Entity.ProductId); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityDeletedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; await _omnisendService.UpdateProductAsync(eventMessage.Entity.ProductId); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; 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) { if (!_omnisendService.IsConfigured) return; await _omnisendEventsService.SendStartedCheckoutEventAsync(eventMessage); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public Task HandleEventAsync(EntityUpdatedEvent eventMessage) { if (!_omnisendService.IsConfigured) return Task.CompletedTask; //await _omnisendService.EditShoppingCartItemAsync(eventMessage.Entity); return Task.CompletedTask; } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityDeletedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; await _omnisendService.DeleteShoppingCartItemAsync(eventMessage.Entity); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public Task HandleEventAsync(OrderAuthorizedEvent eventMessage) { if (!_omnisendService.IsConfigured) return Task.CompletedTask; //await _omnisendService.UpdateOrderAsync(eventMessage.Order); return Task.CompletedTask; } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public Task HandleEventAsync(OrderVoidedEvent eventMessage) { if (!_omnisendService.IsConfigured) return Task.CompletedTask; //await _omnisendService.UpdateOrderAsync(eventMessage.Order); return Task.CompletedTask; } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityInsertedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; await _omnisendService.OrderItemAddedAsync(eventMessage.Entity); } /// /// Handle event /// /// Event /// A task that represents the asynchronous operation public async Task HandleEventAsync(EntityUpdatedEvent eventMessage) { if (!_omnisendService.IsConfigured) return; await _omnisendService.CreateOrUpdateProductAsync(eventMessage.Entity); } #endregion }