public async Task <SubscriptionViewModel> AskAsync(SubscriptionViewModelCriterion criterion)
        {
            var allProductsCriterion = new AllProductsCriterion();
            var products             = await _queryBuilder.For <List <Product> >().WithAsync(allProductsCriterion);

            var productsSumInSubscriptionCriterion = new CalculateProductsSumInSubscriptionCriterion();
            var sum = await _queryBuilder.For <double>().WithAsync(productsSumInSubscriptionCriterion);

            var deliveryIntervalForSubscriptionCriterion = new DeliveryIntervalWithTemplateForSubscriptionCriterion();
            var deliveryInterval = await _queryBuilder.For <DeliveryInterval>().WithAsync(deliveryIntervalForSubscriptionCriterion);

            var subscriptionDatesCriterion = new SubscriptionDatesForSubscriptionCriterion();
            var subscriptionDates          = await _queryBuilder.For <List <SubscriptionDate> >().WithAsync(subscriptionDatesCriterion);

            var calculateSpentAmountCriterion = new CalculateSpentAmountCriterion(criterion.PointedTodayDate, sum);
            var spentAmount = await _queryBuilder.For <double>().WithAsync(calculateSpentAmountCriterion);

            var deliveryDatesCriterion = new DeliveryDatesCriterion(criterion.PointedTodayDate, criterion.PointedTodayDate.AddMonths(3));
            var deliveryDates          = await _queryBuilder.For <List <DateTime> >().WithAsync(deliveryDatesCriterion);

            var viewModel = new SubscriptionViewModel
            {
                Today             = criterion.PointedTodayDate,
                Products          = products,
                DeliveryInterval  = deliveryInterval,
                SubscriptionDates = subscriptionDates,
                ProductsPricesSum = sum,
                SpentAmount       = spentAmount,
                DeliveryDates     = deliveryDates
            };

            return(viewModel);
        }
Пример #2
0
        public async Task ShouldReturnDeliveryDatesForInterval_WhenSubscrsiptionIntervalIsEveryMonthAt5And8DaysOfMonth(int suspendDateMonthValueAdd)
        {
            var monthsAhead       = 3;
            var today             = DateTime.Today;
            var showUntil         = DateTime.Today.AddMonths(monthsAhead);
            var subscriptionDates = new List <SubscriptionDate>
            {
                new SubscriptionDate
                {
                    Date = today.AddYears(-1),
                    Type = SubscriptionDateType.Start
                },
                new SubscriptionDate
                {
                    Date = today.AddMonths(suspendDateMonthValueAdd),
                    Type = SubscriptionDateType.Suspend
                }
            };
            var frequencyInMonth = 2;
            var deliveryInterval = new DeliveryInterval
            {
                CronString = "0 0 0 8,5 12/1 *"
            };

            SetupMocks(subscriptionDates, deliveryInterval);
            var criterion = new DeliveryDatesCriterion(today, showUntil);
            var query     = new DeliveryDatesQuery(_queryBuilderMock.Object);

            var queryResult = await query.AskAsync(criterion);

            Assert.AreEqual(queryResult.Count, frequencyInMonth * suspendDateMonthValueAdd);
        }
Пример #3
0
        public async Task ShouldReturnEmptyList_WhenSubscriptionDatesAreNotExist()
        {
            var today     = DateTime.Today;
            var showUntil = DateTime.Today.AddMonths(3);

            SetupMocks(new List <SubscriptionDate>());
            var criterion = new DeliveryDatesCriterion(today, showUntil);
            var query     = new DeliveryDatesQuery(_queryBuilderMock.Object);

            var queryResult = await query.AskAsync(criterion);

            Assert.AreEqual(queryResult, new List <DateTime>());
        }
Пример #4
0
        public async Task ShouldReturnEmptyList_WhenLastSubscriptionDateIsTypeOfSuspend()
        {
            var today             = DateTime.Today;
            var showUntil         = DateTime.Today.AddMonths(3);
            var subscriptionDates = new List <SubscriptionDate>
            {
                new SubscriptionDate
                {
                    Date = today,
                    Type = SubscriptionDateType.Suspend
                }
            };

            SetupMocks(subscriptionDates);
            var criterion = new DeliveryDatesCriterion(today, showUntil);
            var query     = new DeliveryDatesQuery(_queryBuilderMock.Object);

            var queryResult = await query.AskAsync(criterion);

            Assert.AreEqual(queryResult, new List <DateTime>());
        }
        public async Task <List <DateTime> > AskAsync(DeliveryDatesCriterion criterion)
        {
            var deliveryDates = new List <DateTime>();

            var today = criterion.Today;
            var showThreeMonthsAhead       = criterion.ShowUntil;
            var subscriptionDatesCriterion = new SubscriptionDatesForSubscriptionCriterion();
            var subscriptionDates          = await _queryBuilder.For <List <SubscriptionDate> >().WithAsync(subscriptionDatesCriterion);

            subscriptionDates = subscriptionDates.OrderByDescending(x => x.Date).ToList();
            var lastSubscriptionDate = subscriptionDates.FirstOrDefault();

            if (lastSubscriptionDate == null)
            {
                return(deliveryDates);
            }
            if (lastSubscriptionDate.Type == SubscriptionDateType.Suspend && lastSubscriptionDate.Date <= today)
            {
                return(deliveryDates);
            }

            var subscriptionActiveIntervals = new List <SubscriptionActiveInterval>();

            SubscriptionActiveInterval activeInterval = null;

            foreach (var subscriptionDate in subscriptionDates)
            {
                if (subscriptionDate.Date <= today)
                {
                    if (subscriptionActiveIntervals.Any())
                    {
                        break;
                    }

                    activeInterval = new SubscriptionActiveInterval
                    {
                        BeginAt = today,
                        EndAt   = showThreeMonthsAhead
                    };
                    subscriptionActiveIntervals.Add(activeInterval);

                    break;
                }

                if (subscriptionDate.Type == SubscriptionDateType.Suspend)
                {
                    activeInterval = new SubscriptionActiveInterval
                    {
                        BeginAt = today,
                        EndAt   = subscriptionDate.Date
                    };
                    subscriptionActiveIntervals.Add(activeInterval);
                }
                else
                {
                    if (activeInterval == null)
                    {
                        activeInterval = new SubscriptionActiveInterval(showThreeMonthsAhead);
                    }
                    activeInterval.BeginAt = subscriptionDate.Date;
                }
            }

            var deliveryIntervalForSubscriptionCriterion = new DeliveryIntervalWithTemplateForSubscriptionCriterion();
            var deliveryInterval = await _queryBuilder.For <DeliveryInterval>().WithAsync(deliveryIntervalForSubscriptionCriterion);

            foreach (var subscriptionActiveInterval in subscriptionActiveIntervals)
            {
                var cronInstance    = CrontabSchedule.Parse(deliveryInterval.CronString, CronStringFormat.WithSeconds);
                var nextOccurrences = cronInstance.GetNextOccurrences(subscriptionActiveInterval.BeginAt, subscriptionActiveInterval.EndAt);
                deliveryDates.AddRange(nextOccurrences);
            }

            return(deliveryDates);
        }