Ejemplo n.º 1
0
 protected virtual async Task PublishResultEventAsync(OrderPaidEto orderPaidEto, bool isSuccess)
 {
     await _distributedEventBus.PublishAsync(new ProductInventoryReductionAfterOrderPaidResultEto
     {
         TenantId      = orderPaidEto.Order.TenantId,
         OrderId       = orderPaidEto.Order.Id,
         PaymentId     = orderPaidEto.PaymentId,
         PaymentItemId = orderPaidEto.PaymentItemId,
         IsSuccess     = isSuccess
     });
 }
Ejemplo n.º 2
0
        public virtual async Task HandleEventAsync(OrderPaidEto eventData)
        {
            using var changeTenant = _currentTenant.Change(eventData.Order.TenantId);

            var models = new List <ConsumeInventoryModel>();

            foreach (var orderLine in eventData.Order.OrderLines)
            {
                // Todo: Should use ProductHistory.
                var product = await _productRepository.FindAsync(orderLine.ProductId);

                var productSku = product?.ProductSkus.FirstOrDefault(sku => sku.Id == orderLine.ProductSkuId);

                if (productSku == null)
                {
                    await PublishInventoryReductionResultEventAsync(eventData, false);

                    return;
                }

                if (product.InventoryStrategy != InventoryStrategy.ReduceAfterPayment)
                {
                    continue;
                }

                if (!await _productManager.IsInventorySufficientAsync(product, productSku, orderLine.Quantity))
                {
                    await PublishInventoryReductionResultEventAsync(eventData, false);

                    return;
                }

                models.Add(new ConsumeInventoryModel(product, productSku, eventData.Order.StoreId, orderLine.Quantity));
            }

            foreach (var model in models)
            {
                if (await _productManager.TryReduceInventoryAsync(
                        model.Product, model.ProductSku, model.Quantity, true))
                {
                    continue;
                }

                await TryRollbackInventoriesAsync(models);

                await _unitOfWorkManager.Current.RollbackAsync();

                await PublishInventoryReductionResultEventAsync(eventData, false, true);

                return;
            }

            await PublishInventoryReductionResultEventAsync(eventData, true);
        }