Ejemplo n.º 1
0
        public async Task <bool> Handle(ApplyVoucherOrderCommand message, CancellationToken cancellationToken)
        {
            if (!IsCommandValid(message))
            {
                return(false);
            }

            var order = await _orderRepository.GetOrderDraftByClientId(message.ClientId);

            if (order == null)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Order", "Order not found!"));

                return(false);
            }

            if (order.ClaimedVoucher)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Order", "This order has already claimed a voucher"));

                return(false);
            }

            var voucher = await _orderRepository.GetVoucherByCode(message.VoucherCode);

            if (voucher == null)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Order", "Voucher not found!"));

                return(false);
            }

            var appliedVoucher = order.ApplyVoucher(voucher);

            if (!appliedVoucher.IsValid)
            {
                foreach (var error in appliedVoucher.Errors)
                {
                    await _mediatorHandler.PublishNotification(new DomainNotification(error.ErrorCode, error.ErrorMessage));
                }

                return(false);
            }

            order.AddEvents(new VoucherAppliedEvent(message.ClientId, order.Id, voucher.Id));
            _orderRepository.Update(order);

            return(await _orderRepository.UnitOfWork.Commit());
        }
Ejemplo n.º 2
0
        private async Task <bool> DebitarItemEstoque(Guid produtoId, int quantidade)
        {
            var produto = await _produtoRepository.GetById(produtoId);

            if (produto == null)
            {
                return(false);
            }

            if (!produto.PossuiEstoqueSuficiente(quantidade))
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Estoque", $"Produto - {produto.Nome} sem estoque"));

                return(false);
            }

            produto.DebitarEstoque(quantidade);

            // TODO: 10 pode ser parametrizavel em arquivo de configuração
            if (produto.QuantidadeEstoque < 10)
            {
                await _mediatorHandler.PublishEvent(new ProdutoAbaixoEstoqueEvent(produto.Id, produto.QuantidadeEstoque));
            }

            _produtoRepository.Update(produto);
            return(true);
        }
Ejemplo n.º 3
0
        private async Task <bool> DebitInventoryItem(Guid productId, int amount)
        {
            var product = await _productRepository.GetById(productId);

            if (product is null)
            {
                return(false);
            }

            if (!product.ContainsInventory(amount))
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Inventory", $"Product - {product.Name} without inventory"));

                return(false);
            }

            product.DebitInventory(amount);

            // TODO: configure the inventory amount through settings
            if (product.InventoryAmount < 10)
            {
                await _mediatorHandler.PublishDomainEvent(new ProductBelowInventoryEvent(product.Id, product.InventoryAmount));
            }

            _productRepository.Update(product);
            return(true);
        }
Ejemplo n.º 4
0
 private bool CommandIsValid(Command message)
 {
     if (message.IsValid())
     {
         return(true);
     }
     foreach (var error in message.ValidationResult.Errors)
     {
         _mediatorHandler.PublishNotification(new DomainNotification(message.MessageType, error.ErrorMessage));
     }
     return(false);
 }
Ejemplo n.º 5
0
        public async Task <bool> Handle(ApplyVoucherOrderCommand message, CancellationToken cancellationToken)
        {
            if (!IsValid(message))
            {
                return(false);
            }

            var order = await _orderRepository.GetDraftOrderByClientId(message.ClientId);

            if (order == null)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification(message.MessageType, "Pedido não encontrado!"));

                return(false);
            }

            var voucher = await _orderRepository.GetVoucherByCode(message.VoucherCode);

            if (voucher == null)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification(message.MessageType, "Voucher não encontrado!"));

                return(false);
            }

            var voucherApplied = order.ApplyVoucher(voucher);

            if (!voucherApplied.IsValid)
            {
                foreach (var error in voucherApplied.Errors)
                {
                    await _mediatorHandler.PublishNotification(new DomainNotification(message.MessageType, error.ErrorMessage));
                }

                return(false);
            }

            //Atualizar voucher como utilizado??? Aqui ou ao finalizar

            _orderRepository.Update(order);

            order.AddEvent(new OrderVoucherAppliedEvent(order.Id, message.ClientId, order.TotalValue));
            order.AddEvent(new OrderUpdatedEvent(order.Id, message.ClientId, order.TotalValue));

            return(await _orderRepository.UnitOfWork.Commit());
        }
Ejemplo n.º 6
0
        public async Task <Transaction> ExectureOrderPayment(PaymentOrderDto paymentOrder)
        {
            var order = new Order
            {
                Id       = paymentOrder.OrderId,
                ClientId = paymentOrder.ClientId,
                Total    = paymentOrder.Total
            };

            var payment = new Payment
            {
                Total              = paymentOrder.Total,
                NameCard           = paymentOrder.NameCard,
                NumberCard         = paymentOrder.NumberCard,
                ExpirationDateCard = paymentOrder.ExpirationDateCard,
                CvvCard            = paymentOrder.CvvCard,
                OrderId            = paymentOrder.OrderId
            };

            var transaction = _paymentCreditCardFacade.ExecutePayment(order, payment);

            if (transaction.StatusTransaction == StatusTransaction.Paid)
            {
                payment.AddEvent(new PaymentRealizedEvent(payment.Id, transaction.Id, order.Id, order.ClientId, order.Total));

                _orderRepository.Add(payment);
                _orderRepository.Add(transaction);

                await _orderRepository.UnitOfWork.Commit();

                return(transaction);
            }

            await _mediatorHandler.PublishNotification(new DomainNotification(this.GetType().Name, "A operadora recusou o pagamento"));

            await _mediatorHandler.PublishEvent(new PaymentRefusedEvent(payment.Id, transaction.Id, order.Id, order.ClientId, order.Total));

            return(transaction);
        }
Ejemplo n.º 7
0
        public async Task <Transacao> RealizarPagamentoPedido(PagamentoPedido pagamentoPedido)
        {
            var pedido = new Pedido
            {
                Id    = pagamentoPedido.PedidoId,
                Valor = pagamentoPedido.Total
            };

            var pagamento = new Pagamento
            {
                Valor           = pagamentoPedido.Total,
                NomeCartao      = pagamentoPedido.NomeCartao,
                NumeroCartao    = pagamentoPedido.NumeroCartao,
                ExpiracaoCartao = pagamentoPedido.ExpiracaoCartao,
                CvvCartao       = pagamentoPedido.CvvCartao,
                PedidoId        = pagamentoPedido.PedidoId
            };

            var transacao = _pagamentoCartaoCreditoFacade.RealizarPagamento(pedido, pagamento);

            if (transacao.StatusTransacao == StatusTransacao.Pago)
            {
                pagamento.AddEvent(new PagamentoRealizadoEvent(pedido.Id, pagamentoPedido.ClienteId, transacao.PagamentoId, transacao.Id, pedido.Valor));

                _pagamentoRepository.Adicionar(pagamento);
                _pagamentoRepository.AdicionarTransacao(transacao);

                await _pagamentoRepository.UnitOfWork.Commit();

                return(transacao);
            }

            await _mediatorHandler.PublishNotification(new DomainNotification("pagamento", "A operadora recusou o pagamento"));

            await _mediatorHandler.PublishEvent(new PagamentoRecusadoEvent(pedido.Id, pagamentoPedido.ClienteId, transacao.PagamentoId, transacao.Id, pedido.Valor));

            return(transacao);
        }
Ejemplo n.º 8
0
        public async Task <Transaction> Pay(OrderPayment orderPayment)
        {
            var order = new Order
            {
                Id    = orderPayment.OrderId,
                Total = orderPayment.Total
            };

            var payment = new Payment
            {
                Amount     = orderPayment.Total,
                CardName   = orderPayment.CardName,
                CardNumber = orderPayment.CardNumber,
                ExpiryDate = orderPayment.ExpiryDate,
                CvvCode    = orderPayment.CvvCode,
                OrderId    = orderPayment.OrderId
            };

            var transaction = _creditCardPaymentFacade.Pay(order, payment);

            if (transaction.TransactionStatus == TransactionStatus.Paid)
            {
                payment.AddEvent(new OrderPaymentConfirmedEvent(order.Id, orderPayment.CustomerId, transaction.PaymentId, transaction.Id, order.Total));

                _paymentRepository.Add(payment);
                _paymentRepository.AddTransaction(transaction);

                await _paymentRepository.UnitOfWork.Commit();

                return(transaction);
            }

            await _mediatorHandler.PublishNotification(new DomainNotification("Payment", "The provider has denied payment"));

            await _mediatorHandler.PublishEvent(new OrderPaymentRejectedEvent(order.Id, orderPayment.CustomerId, transaction.PaymentId, transaction.Id, order.Total));

            return(transaction);
        }
Ejemplo n.º 9
0
 protected virtual Task Notify(DomainNotification notification)
 {
     return(_bus.PublishNotification(notification));
 }
Ejemplo n.º 10
0
 protected void NotifyError(string key, string value)
 {
     _mediatorHandler.PublishNotification(new DomainNotification(key, value));
 }
Ejemplo n.º 11
0
 protected void NotifyError(string code, string message)
 {
     _mediator.PublishNotification(new DomainNotification(code, message));
 }
Ejemplo n.º 12
0
 protected void NotificarErro(string codigo, string mensagem)
 {
     _mediatorHandler.PublishNotification(new DomainNotification(codigo, mensagem));
 }
Ejemplo n.º 13
0
        public async Task <bool> Handle(AtualizarItemPedidoCommand message, CancellationToken cancellationToken)
        {
            if (!ValidateCommand(message))
            {
                return(false);
            }

            var pedido = await _pedidoRepository.GetPedidoRascunhoByClienteId(message.ClienteId);

            if (pedido == null)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("pedido", "Pedido não encontrado."));

                return(false);
            }

            var pedidoItem = await _pedidoRepository.GetItemByPedido(pedido.Id, message.ProdutoId);

            if (!pedido.PedidoItemExistente(pedidoItem))
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("pedido", "Item do pedido não encontrado."));

                return(false);
            }

            pedido.UpdateUnidades(pedidoItem, message.Quantidade);

            pedido.AddEvent(new PedidoAtualizadoEvent(pedido.ClienteId, pedido.Id, pedido.ValorTotal));
            pedido.AddEvent(new PedidoProdutoAtualizadoEvent(message.ClienteId, pedido.Id, message.ProdutoId, message.Quantidade));

            _pedidoRepository.UpdateItem(pedidoItem);
            _pedidoRepository.Update(pedido);

            return(await _pedidoRepository.UnitOfWork.Commit());
        }
Ejemplo n.º 14
0
        public async Task <bool> Handle(UpdateOrderItemCommand command, CancellationToken cancellationToken)
        {
            if (!ValidateCommand(command))
            {
                return(false);
            }

            var order = await _orderRepository.GetOrderDraftByCustomerId(command.CustomerId);

            if (order == null)
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Order", "Order not found!"));

                return(false);
            }

            var orderItem = await _orderRepository.GetItemByOrder(order.Id, command.ProductId);

            if (!order.OrderItemExists(orderItem))
            {
                await _mediatorHandler.PublishNotification(new DomainNotification("Order", "Order item not found!"));

                return(false);
            }

            order.UpdateQuantity(orderItem, command.Quantity);
            order.AddEvent(new ProductUpdatedOrderEvent(command.CustomerId, order.Id, command.ProductId, command.Quantity));

            _orderRepository.UpdateItem(orderItem);
            _orderRepository.Update(order);

            return(await _orderRepository.UnitOfWork.Commit());
        }