Exemplo n.º 1
0
        /** CANCLE ORDER ASYNC */
        public async Task <UpdateResult <Order> > CancelOrderAsync(
            Order order,
            CancelOrderReport cancelOrderReport)
        {
            var customer = await _context.Customers
                           .Where(c => c.Id == order.CustomerId)
                           .Include(c => c.User)
                           .FirstOrDefaultAsync();

            try
            {
                if (!CheckValidCanceledOrder(order))
                {
                    throw new Exception("Huỷ đơn hàng bị lỗi, xin thử lại");
                }

                using (var transaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        /** Update status of order to Canceled */
                        order.OrderStatus = OrderStatus.Canceled;
                        _context.Orders.Update(order);
                        var updated = await _context.SaveChangesAsync();

                        if (!(updated > 0))
                        {
                            transaction.Dispose();
                            return(new UpdateResult <Order>
                            {
                                IsSuccess = false,
                                Errors = new List <string>()
                                {
                                    "Huỷ hoá đơn không thành công, xin thử lại"
                                }
                            });
                        }

                        /** Create CancelOrderReport */
                        cancelOrderReport.OrderId   = order.Id;
                        cancelOrderReport.CreatedAt = DateTime.UtcNow;
                        cancelOrderReport.IsDeleted = false;
                        await _context.CancelOrderReports.AddAsync(cancelOrderReport);

                        var created = await _context.SaveChangesAsync();

                        if (!(created > 0))
                        {
                            transaction.Dispose();
                            return(new UpdateResult <Order>
                            {
                                IsSuccess = false,
                                Errors = new List <string>()
                                {
                                    "Huỷ hoá đơn không thành công, xin thử lại"
                                }
                            });
                        }

                        await transaction.CommitAsync();
                    }
                    catch (Exception e)
                    {
                        return(new UpdateResult <Order>
                        {
                            IsSuccess = false,
                            Errors = new List <string>()
                            {
                                e.Message.ToString()
                            }
                        });
                    }
                }
            }
            catch (Exception e)
            {
                return(new UpdateResult <Order>
                {
                    IsSuccess = false,
                    Errors = new List <string>()
                    {
                        e.Message.ToString()
                    }
                });
            }

            /** Send mail to customer */
            string cancelOrderTemplate = _emailTemplateService
                                         .CancelOrderTemplate(
                customer.Name,
                order.Code,
                cancelOrderReport.CancelReason
                );

            _emailService.SendMail(
                customer.User.Email,
                customer.Name,
                $"Huỷ hoá đơn {order.Code} tại Banana Boys Fruit",
                cancelOrderTemplate
                );

            return(new UpdateResult <Order>
            {
                IsSuccess = true
            });
        }