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.Events;
using Nop.Plugin.Misc.Zettle.Domain;
using Nop.Services.Catalog;
using Nop.Services.Events;

namespace Nop.Plugin.Misc.Zettle.Services;

/// 
/// Represents plugin event consumer
/// 
public class EventConsumer :
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>,
    IConsumer>

{
    #region Fields

    protected readonly ICategoryService _categoryService;
    protected readonly IProductAttributeParser _productAttributeParser;
    protected readonly IProductAttributeService _productAttributeService;
    protected readonly IProductService _productService;
    protected readonly ZettleRecordService _zettleRecordService;
    protected readonly ZettleService _zettleService;
    protected readonly ZettleSettings _zettleSettings;

    #endregion

    #region Ctor

    public EventConsumer(ICategoryService categoryService,
        IProductAttributeParser productAttributeParser,
        IProductAttributeService productAttributeService,
        IProductService productService,
        ZettleRecordService zettleRecordService,
        ZettleService zettleService,
        ZettleSettings zettleSettings)
    {
        _categoryService = categoryService;
        _productAttributeParser = productAttributeParser;
        _productAttributeService = productAttributeService;
        _productService = productService;
        _zettleRecordService = zettleRecordService;
        _zettleService = zettleService;
        _zettleSettings = zettleSettings;
    }

    #endregion

    #region Methods

    /// 
    /// Handle entity created event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Create, eventMessage.Entity.Id);
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        if (!eventMessage.Entity.Deleted)
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, eventMessage.Entity.Id);
        else
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Delete, eventMessage.Entity.Id);
            var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(eventMessage.Entity.Id);
            foreach (var combination in combinations)
            {
                await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Delete, combination.ProductId, combination.Id);
            }
        }
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Delete, eventMessage.Entity.Id);
        var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(eventMessage.Entity.Id);
        foreach (var combination in combinations)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Delete, combination.ProductId, combination.Id);
        }
    }

    /// 
    /// Handle entity created event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        if (!_zettleSettings.CategorySyncEnabled)
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        if (!_zettleSettings.CategorySyncEnabled)
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        if (!_zettleSettings.CategorySyncEnabled)
            return;

        var mappings = await _categoryService.GetProductCategoriesByCategoryIdAsync(eventMessage.Entity.Id, showHidden: true);
        foreach (var mapping in mappings)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, mapping.ProductId);
        }
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        if (!_zettleSettings.CategorySyncEnabled)
            return;

        var mappings = await _categoryService.GetProductCategoriesByCategoryIdAsync(eventMessage.Entity.Id, showHidden: true);
        foreach (var mapping in mappings)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, mapping.ProductId);
        }
    }

    /// 
    /// Handle entity created event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        var pictures = await _productService.GetProductPicturesByProductIdAsync(eventMessage.Entity.ProductId);
        if (eventMessage.Entity.DisplayOrder <= pictures.Min(picture => picture.DisplayOrder))
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.ImageChanged, eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        var pictures = await _productService.GetProductPicturesByProductIdAsync(eventMessage.Entity.ProductId);
        if (eventMessage.Entity.DisplayOrder <= pictures.Min(picture => picture.DisplayOrder))
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.ImageChanged, eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.ImageChanged, eventMessage.Entity.ProductId);
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        var products = await _productService.GetProductsByProductAttributeIdAsync(eventMessage.Entity.Id);
        foreach (var product in products)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, product.Id);
            var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(product.Id);
            foreach (var combination in combinations)
            {
                await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
            }
        }
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        var products = await _productService.GetProductsByProductAttributeIdAsync(eventMessage.Entity.Id);
        foreach (var product in products)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, product.Id);
            var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(product.Id);
            foreach (var combination in combinations)
            {
                var mappings = await _productAttributeParser.ParseProductAttributeMappingsAsync(combination.AttributesXml);
                if (mappings.Any(mapping => mapping.ProductAttributeId == eventMessage.Entity.Id))
                    await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Delete, combination.ProductId, combination.Id);
                else
                    await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
            }
        }
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        var mapping = await _productAttributeService.GetProductAttributeMappingByIdAsync(eventMessage.Entity.ProductAttributeMappingId);
        if (mapping is null)
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, mapping.ProductId);
        var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(mapping.ProductId);
        foreach (var combination in combinations)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
        }
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        var mapping = await _productAttributeService.GetProductAttributeMappingByIdAsync(eventMessage.Entity.ProductAttributeMappingId);
        if (mapping is null)
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, mapping.ProductId);
        var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(mapping.ProductId);
        foreach (var combination in combinations)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
        }
    }

    /// 
    /// Handle entity created event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, eventMessage.Entity.ProductId);
        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Create, eventMessage.Entity.ProductId, eventMessage.Entity.Id);
        var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(eventMessage.Entity.ProductId);
        foreach (var combination in combinations)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
        }
    }

    /// 
    /// Handle entity updated event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityUpdatedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, eventMessage.Entity.ProductId);
        var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(eventMessage.Entity.ProductId);
        foreach (var combination in combinations)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
        }
    }

    /// 
    /// Handle entity deleted event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityDeletedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, eventMessage.Entity.ProductId);
        await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Delete, eventMessage.Entity.ProductId, eventMessage.Entity.Id);
        var combinations = await _productAttributeService.GetAllProductAttributeCombinationsAsync(eventMessage.Entity.ProductId);
        foreach (var combination in combinations)
        {
            await _zettleRecordService.CreateOrUpdateRecordAsync(OperationType.Update, combination.ProductId, combination.Id);
        }
    }

    /// 
    /// Handle entity created event
    /// 
    /// Event message
    /// A task that represents the asynchronous operation
    public async Task HandleEventAsync(EntityInsertedEvent eventMessage)
    {
        if (eventMessage.Entity is null)
            return;

        if (!ZettleService.IsConfigured(_zettleSettings))
            return;

        if (eventMessage.Entity.QuantityAdjustment == 0)
            return;

        if (eventMessage.Entity.Message.StartsWith(ZettleDefaults.SystemName))
            return;

        await _zettleService.ChangeInventoryBalanceAsync(eventMessage.Entity.ProductId,
            eventMessage.Entity.CombinationId ?? 0, eventMessage.Entity.QuantityAdjustment);
    }

    #endregion
}