Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

1.4 KB
Raw Blame
using Grand.Data;
using Grand.Domain.Catalog;
using Grand.Domain.Customers;
using Grand.Infrastructure.Caching;
using Grand.Infrastructure.Caching.Constants;
using Grand.Infrastructure.Events;
using Grand.Mediator;

namespace Grand.Business.Common.Events;

public class GroupDeletedEventHandler : INotificationHandler<EntityDeleted<CustomerGroup>>
{
    private readonly ICacheBase _cacheBase;
    private readonly IRepository<Customer> _customerRepository;
    private readonly IRepository<Product> _productRepository;

    public GroupDeletedEventHandler(
        IRepository<Customer> customerRepository,
        IRepository<Product> productRepository,
        ICacheBase cacheBase)
    {
        _customerRepository = customerRepository;
        _productRepository = productRepository;
        _cacheBase = cacheBase;
    }

    public async Task Handle(EntityDeleted<CustomerGroup> notification, CancellationToken cancellationToken)
    {
        //delete from customers
        await _customerRepository.RemoveCollectionFieldItem(string.Empty, x => x.Groups, y => y == notification.Entity.Id);

        //delete tier prices on the product
        await _productRepository.RemoveCollectionFieldItem(string.Empty, x => x.TierPrices,
            z => z.CustomerGroupId == notification.Entity.Id);

        //clear cache
        await _cacheBase.RemoveByPrefix(CacheKey.PRODUCTS_PATTERN_KEY);
    }
}