Exemplo n.º 1
0
        public CustomValidationResult ValidateExposureCountDifferences(Clash clash, IEnumerable <Clash> allClashes)
        {
            if (!string.IsNullOrWhiteSpace(clash.ParentExternalidentifier))
            {
                var parentClash = _clashRepository.FindByExternal(clash.ParentExternalidentifier).FirstOrDefault();

                if (parentClash != null)
                {
                    var validationResult = ValidateParentCountDifferences(clash.Differences, parentClash);

                    if (validationResult != CustomValidationResult.Success())
                    {
                        return(validationResult);
                    }
                }
            }

            var childClashes = allClashes
                               .Where(c => c.ParentExternalidentifier == clash.Externalref && c.Differences.Any()).ToList();

            if (childClashes.Any())
            {
                return(ValidateChildCountDifferences(childClashes, clash));
            }

            return(CustomValidationResult.Success());
        }
Exemplo n.º 2
0
        public CustomValidationResult ValidateClashIsNotLinkedToActiveProduct(string clashExternalReference)
        {
            var lookupProducts = _productRepository.GetAll()
                                 .Where(p => p.ClashCode == clashExternalReference && p.EffectiveEndDate >= DateTime.UtcNow);

            return(lookupProducts.Any()
                ? CustomValidationResult.Failed()
                : CustomValidationResult.Success());
        }
Exemplo n.º 3
0
        public CustomValidationResult ValidateClashHasNoChildClashes(string clashExternalReference)
        {
            var lookupClashes = _clashRepository.GetAll()
                                .Where(c => c.ParentExternalidentifier == clashExternalReference);

            return(lookupClashes.Any()
                ? CustomValidationResult.Failed()
                : CustomValidationResult.Success());
        }
Exemplo n.º 4
0
        public CustomValidationResult ValidateParentCountDifferences(List <ClashDifference> childClashDifferences, Clash parentClash)
        {
            var parentClashDifferences = parentClash.Differences;

            foreach (var difference in childClashDifferences)
            {
                var differencesForSalesArea = parentClashDifferences.Where(d => d.SalesArea == difference.SalesArea).ToList();

                if (!differencesForSalesArea.Any())
                {
                    if (parentClash.DefaultPeakExposureCount < difference.PeakExposureCount)
                    {
                        return(CustomValidationResult.Failed(
                                   $"Peak exposure count should be less than or equal to the parent default value for {difference.SalesArea} Sales Area"));
                    }

                    if (parentClash.DefaultOffPeakExposureCount < difference.OffPeakExposureCount)
                    {
                        return(CustomValidationResult.Failed(
                                   $"Non-peak exposure count should be less than or equal to the parent default value for {difference.SalesArea} Sales Area"));
                    }

                    continue;
                }

                var minParentPeakExposureCount = differencesForSalesArea.OrderBy(d => d.PeakExposureCount).FirstOrDefault().PeakExposureCount;

                if (minParentPeakExposureCount == null)
                {
                    minParentPeakExposureCount = parentClash.DefaultPeakExposureCount;
                }

                if (difference.PeakExposureCount > minParentPeakExposureCount)
                {
                    return(CustomValidationResult.Failed(
                               $"Peak exposure count should be less than or equal to the parent difference value for {difference.SalesArea} Sales Area"));
                }

                var minParentOffPeakExposureCount = differencesForSalesArea.OrderBy(d => d.OffPeakExposureCount).FirstOrDefault().OffPeakExposureCount;

                if (minParentOffPeakExposureCount == null)
                {
                    minParentOffPeakExposureCount = parentClash.DefaultOffPeakExposureCount;
                }

                if (difference.OffPeakExposureCount > minParentOffPeakExposureCount)
                {
                    return(CustomValidationResult.Failed(
                               $"Non-peak exposure count should be less than or equal to the parent difference value for {difference.SalesArea} Sales Area"));
                }
            }

            return(CustomValidationResult.Success());
        }
Exemplo n.º 5
0
        public CustomValidationResult ValidateChildCountDifferences(List <Clash> childClashes, Clash parentClash)
        {
            var parentClashDifferences = parentClash.Differences;

            if (parentClashDifferences != null && parentClashDifferences.Any())
            {
                var differencesForSalesAreas = parentClashDifferences.GroupBy(c => c.SalesArea);

                foreach (var differences in differencesForSalesAreas)
                {
                    var minParentPeakExposureCount    = differences.OrderBy(d => d.PeakExposureCount).FirstOrDefault();
                    var minParentOffPeakExposureCount = differences.OrderBy(d => d.OffPeakExposureCount).FirstOrDefault();

                    var salesArea = string.IsNullOrWhiteSpace(differences.Key)
                        ? "all sales areas"
                        : $"{differences.Key} sales area";

                    if (childClashes.Any(c =>
                                         c.Differences.Any(d => d.PeakExposureCount > minParentPeakExposureCount?.PeakExposureCount && d.SalesArea == differences.Key)))
                    {
                        return(CustomValidationResult.Failed(
                                   $"Parent difference peak exposure count should be higher than or equal to the child value for {salesArea} Sales Area"));
                    }

                    if (childClashes.Any(c =>
                                         c.Differences.Any(d => d.OffPeakExposureCount > minParentOffPeakExposureCount?.OffPeakExposureCount && d.SalesArea == differences.Key)))
                    {
                        return(CustomValidationResult.Failed(
                                   $"Parent difference non-peak exposure count should be higher than or equal to the child value for {salesArea} Sales Area"));
                    }
                }

                var clashesWithOtherSalesAreaDifferences = childClashes.Where(c =>
                                                                              c.Differences.Any(d =>
                                                                                                !differencesForSalesAreas.Any(pd => pd.Key == d.SalesArea))).ToList();

                if (clashesWithOtherSalesAreaDifferences != null)
                {
                    return(ValidateChildExposureCount(clashesWithOtherSalesAreaDifferences, parentClash));
                }
            }
            else
            {
                return(ValidateChildExposureCount(childClashes, parentClash));
            }

            return(CustomValidationResult.Success());
        }
Exemplo n.º 6
0
        public CustomValidationResult ValidateClashExceptionForSameStructureRulesViolation(ClashException clashException)
        {
            if (clashException.FromType != ClashExceptionType.Clash ||
                clashException.ToType != ClashExceptionType.Clash)
            {
                return(CustomValidationResult.Success());
            }

            var fromClash = _clashRepository.FindByExternal(clashException.FromValue).FirstOrDefault();

            if (fromClash is null)
            {
                return(CustomValidationResult.Failed(
                           $"Could not find Clash with external reference {clashException.FromValue}"));
            }

            var toClash = _clashRepository.FindByExternal(clashException.ToValue).FirstOrDefault();

            if (toClash is null)
            {
                return(CustomValidationResult.Failed(
                           $"Could not find Clash with external reference {clashException.ToValue}"));
            }

            bool clashesAreFromTheSameStructure;

            try
            {
                clashesAreFromTheSameStructure = AreClashesFromTheSameStructure(fromClash, toClash);
            }
            catch (ArgumentException exception)
            {
                return(CustomValidationResult.Failed(exception.Message));
            }

            if (clashesAreFromTheSameStructure)
            {
                if (clashException.IncludeOrExclude == IncludeOrExclude.I)
                {
                    return(CustomValidationResult.Failed(
                               $"Clash exception with Include rule and values from: {clashException.FromValue}, " +
                               $"to: {clashException.ToValue} is not allowed as clashes are from the same structure"));
                }
            }

            return(CustomValidationResult.Success());
        }
Exemplo n.º 7
0
        public CustomValidationResult ValidateTimeRanges(
            IEnumerable <ClashDifference> clashDifferences)
        {
            var filteredExistingDifferences = clashDifferences.Where(d => d.EndDate == null || d.EndDate >= DateTime.Today.Date).ToList();

            foreach (var difference in clashDifferences)
            {
                if (DoesOverlapExistWithExistingDifferences(difference, filteredExistingDifferences))
                {
                    var salesAreaName = string.IsNullOrWhiteSpace(difference.SalesArea)
                        ? "all sales areas"
                        : $"{difference.SalesArea} sales area";
                    return(CustomValidationResult.Failed($"Overlapping dates exist for {salesAreaName} difference"));
                }
            }

            return(CustomValidationResult.Success());
        }
Exemplo n.º 8
0
        public CustomValidationResult ValidateChildExposureCount(List <Clash> childClashes, Clash parentClash)
        {
            if (childClashes.Any(c =>
                                 c.Differences.Any(d => d.PeakExposureCount > parentClash.DefaultPeakExposureCount)))
            {
                return(CustomValidationResult.Failed(
                           $"Parent peak exposure count should be higher than or equal to the child difference value"));
            }

            if (childClashes.Any(c =>
                                 c.Differences.Any(d => d.OffPeakExposureCount > parentClash.DefaultOffPeakExposureCount)))
            {
                return(CustomValidationResult.Failed(
                           $"Parent non-peak exposure count should be higher than or equal to the child difference value"));
            }

            return(CustomValidationResult.Success());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Validate ClashExceptions overlay with existing ones, skipping those which has end date less than today
        /// </summary>
        /// <returns></returns>
        public CustomValidationResult ValidateTimeRanges(
            IEnumerable <ClashException> givenExceptions,
            int offsetHours,
            IEnumerable <ClashException> existingExceptions = null)
        {
            if (existingExceptions == null || !existingExceptions.Any())
            {
                return(CustomValidationResult.Success());
            }

            var filteredExistingExceptions = existingExceptions.Where(e => e.EndDate == null || e.EndDate >= DateTime.Today.Date).ToList();

            var overlapExists = givenExceptions.Any(ne => DoesOverlapExistWithExistingClashExceptions(ne, filteredExistingExceptions, offsetHours));

            return(overlapExists
                ? CustomValidationResult.Failed("Overlapping Dates Exist for given exceptions")
                : CustomValidationResult.Success());
        }
Exemplo n.º 10
0
        private CustomValidationResult ValidateClashDifferencesForSave(Clash clash, IEnumerable <Clash> allClashes)
        {
            var acceptableDaysOfTheWeek = new List <string>()
            {
                "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY",
                "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"
            };

            if (clash.Differences is null)
            {
                return(CustomValidationResult.Failed("Clash differences can not be null"));
            }

            var defaultEndTime = new TimeSpan(5, 59, 59);

            var clashDifferences = clash.Differences;

            if (clashDifferences.Count == 0)
            {
                return(CustomValidationResult.Success());
            }

            foreach (var difference in clashDifferences)
            {
                if (difference.PeakExposureCount == null && difference.OffPeakExposureCount == null)
                {
                    return(CustomValidationResult.Failed("At least one exposure count should be specified"));
                }

                if (difference.StartDate.HasValue && difference.EndDate.HasValue && difference.StartDate >= difference.EndDate)
                {
                    return(CustomValidationResult.Failed("Clash difference start date must be before the end date"));
                }

                var timeAndDow = difference.TimeAndDow;
                if (timeAndDow.StartTime.HasValue && timeAndDow.EndTime.HasValue)
                {
                    var startTime = timeAndDow.StartTime.Value;
                    var endTime   = timeAndDow.EndTime.Value;
                    endTime = endTime > defaultEndTime ? endTime : endTime.Add(TimeSpan.FromHours(24));

                    if (startTime >= endTime)
                    {
                        return(CustomValidationResult.Failed("Clash difference start time must be before the end time"));
                    }
                }

                if (timeAndDow.DaysOfWeek is null || !timeAndDow.DaysOfWeek.Any())
                {
                    return(CustomValidationResult.Failed("Clash difference should contain minimum one day of the week"));
                }

                if (timeAndDow.DaysOfWeek.Count > 7)
                {
                    return(CustomValidationResult.Failed("Clash difference should contain maximum 7 days of the week"));
                }

                var checkList = new List <string>();
                foreach (var dayOfWeek in timeAndDow.DaysOfWeek)
                {
                    var day = dayOfWeek.ToUpper(CultureInfo.InvariantCulture);
                    if (acceptableDaysOfTheWeek.Contains(day))
                    {
                        var index = acceptableDaysOfTheWeek.IndexOf(day);
                        day = acceptableDaysOfTheWeek[index < 7 ? index : index - 7];
                        if (!checkList.Contains(day))
                        {
                            checkList.Add(day);
                        }
                        else
                        {
                            return(CustomValidationResult.Failed("Clash difference should not contain a duplicate of days of the week"));
                        }
                    }
                    else
                    {
                        return(CustomValidationResult.Failed("Clash difference should contain only acceptable days of the week"));
                    }
                }
            }

            var validationResult = _clashValidator.ValidateTimeRanges(clashDifferences);

            if (!validationResult.Successful)
            {
                return(validationResult);
            }

            validationResult = _clashValidator.ValidateExposureCountDifferences(clash, allClashes);

            if (!validationResult.Successful)
            {
                return(validationResult);
            }

            return(CustomValidationResult.Success());
        }