async Task <PlanViewModel> CheckForExceptionsAsync(SubscribesIndexViewModel indexModel, PlanViewModel selectedPlan)
        {
            if (indexModel.Current != null)
            {
                //目前仍在訂閱期
                throw new CreateWhileCurrentSubscribeExist(CurrentUserId);
            }

            if (indexModel.Plan == null)
            {
                throw new Exception();
            }


            var activePlan = indexModel.Plan;

            if (selectedPlan.Id != activePlan.Id)
            {
                //找不到指定的方案
                throw new SelectedPlanDifferentFromActivePlan(selectedPlan.Id, activePlan.Id);
            }

            //查看是否已經有帳單未繳
            var bills = await _billsService.FetchByUserAsync(new User { Id = CurrentUserId }, new Plan { Id = selectedPlan.Id });

            if (bills.HasItems())
            {
                //帳單有繳的話, 應該在訂閱期內
                //所以應該只會有未繳的
                var unPayedBills = bills.Where(x => !x.Payed).ToList();
                if (unPayedBills.IsNullOrEmpty())
                {
                    //沒有未繳帳單,異常
                    //例外
                    _logger.LogException(new BillPayedButNoCurrentSubscribe(new User {
                        Id = CurrentUserId
                    }, new Plan {
                        Id = selectedPlan.Id
                    }));
                }
                //有未繳帳單或帳單異常
                //試圖建立第二張帳單
                throw new TryToCreateSecondValidBill(new User {
                    Id = CurrentUserId
                }, new Plan {
                    Id = selectedPlan.Id
                });
            }


            if (selectedPlan.Price != activePlan.Price)
            {
                //金額不對
                throw new SelectedPriceDifferentFromActivePlan(selectedPlan.Id, activePlan.Id);
            }

            return(activePlan);
        }
Esempio n. 2
0
        public async Task <SubscribesIndexViewModel> GetSubscribesIndexViewAsync(string userId, bool allData = false)
        {
            var subscribes = await _subscribesService.FetchByUserAsync(userId);

            var model = new SubscribesIndexViewModel();

            if (subscribes.HasItems())
            {
                subscribes = subscribes.GetOrdered();

                //User訂閱紀錄
                model.Records = subscribes.MapViewModelList(_mapper);


                //訂閱期之內(Active)的訂閱紀錄
                var activeSubscribe = subscribes.Where(item => item.Active).FirstOrDefault();
                if (activeSubscribe != null)
                {
                    model.Current = activeSubscribe.MapViewModel(_mapper);
                }
            }

            if (model.Current == null)
            {
                //目前不在訂閱期
                var plan = await FindActivePlanAsync();

                if (plan != null)
                {
                    bool canDiscount = subscribes.HasItems();
                    model.Plan = plan.MapViewModel(_mapper, canDiscount);
                }
            }

            if (allData)
            {
                var bills = await _billsService.FetchByUserAsync(userId);

                if (bills.HasItems())
                {
                    bills = bills.OrderByDescending(x => x.CreatedAt);
                }

                model.Bills = bills.MapViewModelList(_mapper);

                var payways = (await _paysService.FetchPayWaysAsync()).GetOrdered();
                model.PayWays = payways.MapViewModelList(_mapper);
            }


            return(model);
        }
        async Task <SubscribesIndexViewModel> GetIndexViewAsync()
        {
            var subscribes = await _subscribesService.FetchByUserAsync(CurrentUserId);

            var model = new SubscribesIndexViewModel();

            if (subscribes.HasItems())
            {
                subscribes = subscribes.GetOrdered();

                //User訂閱紀錄
                model.Records = subscribes.MapViewModelList(_mapper);


                //訂閱期之內(Active)的訂閱紀錄
                var activeSubscribe = subscribes.Where(item => item.Active).FirstOrDefault();
                if (activeSubscribe != null)
                {
                    model.Current = activeSubscribe.MapViewModel(_mapper);
                }
            }

            if (model.Current == null)
            {
                //目前不在訂閱期
                var plan = await FindPlanAsync();

                if (plan != null)
                {
                    bool canDiscount = subscribes.HasItems();
                    model.Plan = plan.MapViewModel(_mapper, canDiscount);
                }
            }

            return(model);
        }