public async Task <CommandResult> ExecuteAsync(SaveDeliveryIntervalContext commandContext) { var templateByIdCriterion = new DeliveryIntervalTemplateByIdCriterion(commandContext.DeliveryIntervalTemplate.Id); var template = await _queryBuilder.For <DeliveryIntervalTemplate>().WithAsync(templateByIdCriterion); if (template == null) { throw new Exception("Delivery Problem!"); } var cronDaysString = BuildCronDaysString(commandContext.MonthDays); var cronFormatMonthFrequency = template.CronFormatMonthFrequency; var cronMonthString = cronFormatMonthFrequency.Substring(1, cronFormatMonthFrequency.Length - 1); cronMonthString = $"{DateTime.Today.Month}{cronMonthString}"; var delivery = new DeliveryInterval { DeliveryIntervalTemplateId = commandContext.DeliveryIntervalTemplate.Id, CronString = $"0 0 0 {cronDaysString} {cronMonthString} *" }; var repositoryContext = new SaveDeliveryIntervalRepositoryContext(delivery); return(await _commandBuilder.ExecuteAsync(repositoryContext)); }
public async Task ShouldReturnSpentSumOfSubscriptedProductsBetweenDates_WhenSubscrsiptionIntervalIsEveryMonthAt5And8DaysOfMonth() { var today = DateTime.Today; var sumOfProductsInSubscription = 10; var subscriptionDates = new List <SubscriptionDate> { new SubscriptionDate { Date = today.AddYears(-1), Type = SubscriptionDateType.Start }, new SubscriptionDate { Date = today, Type = SubscriptionDateType.Suspend } }; var countOfMonths = 12; var frequencyInMonth = 2; var deliveryInterval = new DeliveryInterval { CronString = "0 0 0 8,5 12/1 *" }; SetupMocks(subscriptionDates, deliveryInterval); var criterion = new CalculateSpentAmountCriterion(today, sumOfProductsInSubscription); var query = new CalculateSpentAmountQuery(_queryBuilderMock.Object); var queryResult = await query.AskAsync(criterion); Assert.AreEqual(queryResult, sumOfProductsInSubscription * countOfMonths * frequencyInMonth); }
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); }
private void SetupMocks(List <SubscriptionDate> subscriptionDates, DeliveryInterval deliveryInterval = null) { _queryBuilderMock.Setup(x => x.For <List <SubscriptionDate> >() .WithAsync(It.IsAny <SubscriptionDatesForSubscriptionCriterion>())) .ReturnsAsync(subscriptionDates); _queryBuilderMock.Setup(x => x.For <DeliveryInterval>() .WithAsync(It.IsAny <DeliveryIntervalWithTemplateForSubscriptionCriterion>())) .ReturnsAsync(deliveryInterval); }
public Parcel( DeliveryInterval deliveryInterval, TrackingCode trackingCode, Carrier carrier, Customer customer, string code ) { DeliveryInterval = deliveryInterval; TrackingCode = trackingCode; Carrier = carrier; Customer = customer; Code = code; }
private static bool IsTimeAvailable(DeliveryInterval interval, DateTime startDate, DayOfWeek currentDayOfWeek, uint?delta, int weekNumber) { // urgent delivery is available only for today if (interval.Type == DeliveryIntervalType.Urgent) { return(CheckTime(interval, startDate, currentDayOfWeek)); } var result = CheckTime(interval, startDate, currentDayOfWeek); if (result) { return(true); } if (!delta.HasValue) { return(false); } var intervalDate = GetIntervalDate(startDate, currentDayOfWeek, weekNumber, interval); return(startDate >= intervalDate.AddHours((int)delta * -1)); }
private static DateTime GetIntervalDate(DateTime startDate, DayOfWeek currentDayOfWeek, int weekNumber, DeliveryInterval interval) { // calculate how much days pass from startDate var currentDaysPass = (int)currentDayOfWeek - (int)startDate.DayOfWeek + (weekNumber - 1) * 7; var date = startDate.AddDays(currentDaysPass); return(new DateTime(date.Year, date.Month, date.Day, interval.AvailableFrom.Hours, interval.AvailableFrom.Minutes, 0)); }
private static bool CheckTime(DeliveryInterval interval, DateTime startDate, DayOfWeek currentDayOfWeek) { return(startDate.DayOfWeek == currentDayOfWeek && startDate.TimeOfDay >= interval.AvailableFrom && startDate.TimeOfDay <= interval.AvailableTo); }
public SaveDeliveryIntervalRepositoryContext(DeliveryInterval deliveryInterval) { DeliveryInterval = deliveryInterval; }