示例#1
0
        private async Task CapturePayment(OrderWritedOffStockIntegrationEvent message)
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var paymentService = scope.ServiceProvider.GetRequiredService <IPaymentService>();

                var response = await paymentService.CapturePayment(message.OrderId);

                if (!response.ValidationResult.IsValid)
                {
                    throw new DomainException($"Failed to capture order payment {message.OrderId}");
                }

                await _bus.PublishAsync(new OrderPaidIntegrationEvent(message.CustomerId, message.OrderId));
            }
        }
示例#2
0
        private async Task ManageInventory(OrderAuthorizedIntegrationEvent message)
        {
            using (var scope = _serviceProvider.CreateScope())
            {
                var productsWithStock = new List <Product>();
                var productRepository = scope.ServiceProvider.GetRequiredService <IProductRepository>();

                var idsProducts = string.Join(",", message.Items.Select(c => c.Key));
                var products    = await productRepository.GetPoductsById(idsProducts);

                if (products.Count != message.Items.Count)
                {
                    CancelOrderWithOutStock(message);
                    return;
                }

                foreach (var product in products)
                {
                    var quantityProduct = message.Items.FirstOrDefault(p => p.Key == product.Id).Value;

                    if (product.IsAvailable(quantityProduct))
                    {
                        product.RemoveStock(quantityProduct);
                        productsWithStock.Add(product);
                    }
                }

                if (productsWithStock.Count != message.Items.Count)
                {
                    CancelOrderWithOutStock(message);
                    return;
                }

                foreach (var product in productsWithStock)
                {
                    productRepository.Update(product);
                }

                if (!await productRepository.UnitOfWork.Commit())
                {
                    throw new DomainException($"Failed to update inventory - {message.OrderId}");
                }

                var orderWriteOfftStock = new OrderWritedOffStockIntegrationEvent(message.CustomerId, message.OrderId);
                await _bus.PublishAsync(orderWriteOfftStock);
            }
        }