public AlternativeProductSuggestionItem(Guid id, OrderItem orderItem, int quantity, string description, decimal price, AlternativeProductSuggestion alternativeProductSuggestion,
                                         bool isQuantityChanged) : base(id, orderItem)
 {
     AlternativeProductSuggestion = alternativeProductSuggestion;
     Price             = price;
     Quantity          = quantity;
     Description       = description;
     IsQuantityChanged = isQuantityChanged;
 }
Exemplo n.º 2
0
        public async Task <CreateOrderSuggestionCommandResponse> Handle(CreateOrderSuggestionCommand command)
        {
            _applicationSettingDomainService.CheckDiscountInRangeSetting(command.Discount);
            _applicationSettingDomainService.CheckShippingTime(command.ShippingTime);
            if (_repository.AsQuery().Any(p => p.OrderId == command.OrderId))
            {
                throw new DomainException("پیشنهاد بروی این سفارش زده شده است");
            }
            var order = _orderRepository.AsQuery().SingleOrDefault(p => p.Id == command.OrderId);

            if (order == null)
            {
                throw new DomainException("سفارش یافت نشد");
            }

            if (order.OrderStatus == OrderStatus.Cancel)
            {
                throw new DomainException("این سفارش توسط مشتری لغو شده است");
            }
            _applicationSettingDomainService.CheckOrderExpireTime(order.CreationTime);
            if (order.ResponseExpireTime < DateTime.Now)
            {
                throw new DomainException("زمان پاسخ گویی شما به پایان رسیده است");
            }

            var shop = _personRepository.AsQuery().OfType <Shop>().SingleOrDefault(p => p.UserId == command.UserId);

            if (shop == null)
            {
                throw new DomainException("فروشگاه یافت نشد");
            }

            var appSetting = _applicationSettingRepository.AsQuery().SingleOrDefault();

            if (appSetting == null)
            {
                throw new Exception();
            }

            if (command.Discount < appSetting.MinimumDiscount || command.Discount > appSetting.MaximumDiscount)
            {
                throw new DomainException("تخفیف وارد شده در بازه تخفیفات معتبر نمی باشد");
            }
            if (command.ShippingTime > appSetting.MaximumDeliveryTime)
            {
                throw new DomainException("زمان ارسال سفارش از حداکثر زمان ارسال معتبر بیشتر می باشد");
            }

            var orderId         = order is AreaOrder areaOrder ? areaOrder.PrivateOrder.Id : order.Id;
            var orderSuggestion = new OrderSuggestion(Guid.NewGuid(), orderId, command.Discount, command.Description, shop, command.ShippingTime)
            {
                OrderSuggestionItems = new List <OrderSuggestionItemBase>()
            };
            decimal totalPrice = 0;

            foreach (var item in command.OrderSuggestionItems)
            {
                var orderItem = order.OrderItems.SingleOrDefault(p => p.Id == item.OrderItemId);
                if (orderItem == null)
                {
                    throw new DomainException("آیتم سفارش یافت نشد");
                }
                var findProduct = _productRepository.Find(item.ProductId);
                switch (item.OrderSuggestionItemType)
                {
                case OrderSuggestionItemType.AlternativeProduct:
                    if (findProduct == null)
                    {
                        throw new DomainException("کالای ارسالی جایگزین نامعتبر می باشد");
                    }
                    var alternativeProduct =
                        new AlternativeProductSuggestion(findProduct.Id, findProduct.Name, findProduct.MainImage,
                                                         findProduct.Brand.Id, findProduct.Brand.Name);
                    orderItem.Discount = null;
                    var alternativeProductSuggestionItem = new AlternativeProductSuggestionItem(Guid.NewGuid(),
                                                                                                orderItem, item.Quantity, item.Description, item.Price, alternativeProduct,
                                                                                                orderItem.Quantity != item.Quantity);
                    orderSuggestion.OrderSuggestionItems.Add(alternativeProductSuggestionItem);
                    totalPrice = totalPrice + (item.Price * item.Quantity);
                    break;

                case OrderSuggestionItemType.NoProduct:
                    var noProduct = new NoProductSuggestionItem(Guid.NewGuid(), orderItem);
                    orderSuggestion.OrderSuggestionItems.Add(noProduct);
                    break;

                case OrderSuggestionItemType.HasProduct:
                    if (orderItem.Discount != null)
                    {
                        if (!orderItem.Discount.HasDiscountValid())
                        {
                            orderItem.Discount = null;
                        }
                    }
                    var hasProduct = new HasProductSuggestionItem(Guid.NewGuid(), orderItem, item.Quantity,
                                                                  item.Description, item.Price, orderItem.Quantity != item.Quantity);
                    orderSuggestion.OrderSuggestionItems.Add(hasProduct);
                    totalPrice = totalPrice + (item.Price * item.Quantity);
                    break;
                }
            }
            _repository.Add(orderSuggestion);


            //todo order check it
            var expireMinutes = order.ExpireOpenTime.Subtract(DateTime.Now).TotalMinutes;

            order.ExpireOpenTime = order.ExpireOpenTime.AddMinutes(appSetting.OrderSuggestionExpireTime - expireMinutes);
            order.SuggestionTime = DateTime.Now.AddMinutes(appSetting.OrderSuggestionExpireTime);
            DomainEventDispatcher.Raise(new TheOrderStatusWentToHasSuggestionEvent(order));
            if (order is AreaOrder area)
            {
                area.PrivateOrder.SuggestionTime = DateTime.Now.AddMinutes(appSetting.OrderSuggestionExpireTime);
                DomainEventDispatcher.Raise(new TheOrderStatusWentToHasSuggestionEvent(area.PrivateOrder));
            }
            DomainEventDispatcher.Raise(new CreateHasSuggestionsEvent(order.Id, shop.Id, orderSuggestion));
            await _fcmNotification.SendToIds(order.Customer.GetPushTokens(),
                                             "پیشنهاد سفارش",
                                             $"برای سفارش شما با شناسه {order.Id} یک پیشنهاد ثبت شد",
                                             NotificationType.OrderSuggestionAdded,
                                             AppType.Customer, NotificationSound.Shopper);

            return(new CreateOrderSuggestionCommandResponse());
        }