protected async Task OutOfStockNotifications(Product product, int prevStockQuantity, List <ProductWarehouseInventory> prevMultiWarehouseStock)
 {
     if (product.ManageInventoryMethodId == ManageInventoryMethod.ManageStock &&
         product.BackorderModeId == BackorderMode.NoBackorders &&
         product.AllowOutOfStockSubscriptions &&
         _stockQuantityService.GetTotalStockQuantity(product) > 0 &&
         prevStockQuantity <= 0 && !product.UseMultipleWarehouses &&
         product.Published)
     {
         await _outOfStockSubscriptionService.SendNotificationsToSubscribers(product, "");
     }
     if (product.ManageInventoryMethodId == ManageInventoryMethod.ManageStock &&
         product.BackorderModeId == BackorderMode.NoBackorders &&
         product.AllowOutOfStockSubscriptions &&
         product.UseMultipleWarehouses &&
         product.Published)
     {
         foreach (var prevstock in prevMultiWarehouseStock)
         {
             if (prevstock.StockQuantity - prevstock.ReservedQuantity <= 0)
             {
                 var actualStock = product.ProductWarehouseInventory.FirstOrDefault(x => x.WarehouseId == prevstock.WarehouseId);
                 if (actualStock != null)
                 {
                     if (actualStock.StockQuantity - actualStock.ReservedQuantity > 0)
                     {
                         await _outOfStockSubscriptionService.SendNotificationsToSubscribers(product, prevstock.WarehouseId);
                     }
                 }
             }
         }
         if (product.ProductWarehouseInventory.Sum(x => x.StockQuantity - x.ReservedQuantity) > 0)
         {
             if (prevMultiWarehouseStock.Sum(x => x.StockQuantity - x.ReservedQuantity) <= 0)
             {
                 await _outOfStockSubscriptionService.SendNotificationsToSubscribers(product, "");
             }
         }
     }
 }
        public async Task <ProductDto> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            //product
            var product = await _productService.GetProductById(request.Model.Id);

            var prevStockQuantity = product.StockQuantity;
            var prevPublished     = product.Published;

            product = request.Model.ToEntity(product);
            product.UpdatedOnUtc = DateTime.UtcNow;
            request.Model.SeName = await product.ValidateSeName(request.Model.SeName, product.Name, true, _seoSettings, _slugService, _languageService);

            product.SeName = request.Model.SeName;
            //search engine name
            await _slugService.SaveSlug(product, request.Model.SeName, "");

            //update product
            await _productService.UpdateProduct(product);

            if (product.ManageInventoryMethodId == ManageInventoryMethod.ManageStock &&
                product.BackorderModeId == BackorderMode.NoBackorders &&
                product.AllowOutOfStockSubscriptions &&
                _stockQuantityService.GetTotalStockQuantity(product) > 0 &&
                prevStockQuantity <= 0 && product.Published)
            {
                await _outOfStockSubscriptionService.SendNotificationsToSubscribers(product, "");
            }

            //activity log
            await _customerActivityService.InsertActivity("EditProduct", product.Id, _translationService.GetResource("ActivityLog.EditProduct"), product.Name);

            //raise event
            if (!prevPublished && product.Published)
            {
                await _mediator.Publish(new ProductPublishEvent(product));
            }

            if (prevPublished && !product.Published)
            {
                await _mediator.Publish(new ProductUnPublishEvent(product));
            }

            return(product.ToModel());
        }