public async Task ThenSummaryShowsDataLockCourseTriage(bool hasUntriagedCourseDataLock, bool hasTriagedCourseDataLock, bool expectEnabled)
        {
            //Arrange
            var courseDataLocks = new List <DataLockStatus>();

            if (hasUntriagedCourseDataLock)
            {
                courseDataLocks.Add(
                    new DataLockStatus {
                    IlrTrainingCourseCode = "TEST", TriageStatus = TriageStatus.Unknown
                });
            }

            if (hasTriagedCourseDataLock)
            {
                courseDataLocks.Add(
                    new DataLockStatus {
                    IlrTrainingCourseCode = "TEST", TriageStatus = TriageStatus.Restart
                });
            }

            var source = new DataLockSummary
            {
                DataLockWithOnlyPriceMismatch = new List <DataLockStatus>(),
                DataLockWithCourseMismatch    = courseDataLocks
            };

            //Act
            var result = await _mapper.MapDataLockSummary(source, false);

            //Assert
            Assert.AreEqual(expectEnabled, result.ShowCourseDataLockTriageLink);
        }
        public async Task <DataLockSummary> GetDataLockSummary(long apprenticeshipId, Caller caller)
        {
            _logger.Trace($"Getting data lock summary for apprenticeship: {apprenticeshipId}", apprenticeshipId: apprenticeshipId, caller: caller);

            var response = await _mediator.SendAsync(new GetDataLocksRequest
            {
                ApprenticeshipId = apprenticeshipId
            });

            var courseMismatch = response.Data
                                 .Where(DataLockExtensions.UnHandled)
                                 .Where(DataLockExtensions.WithCourseError).ToList();

            var withPriceOnly = response.Data
                                .Where(DataLockExtensions.UnHandled)
                                .Where(DataLockExtensions.IsPriceOnly).ToList();

            var summary = new DataLockSummary
            {
                DataLockWithCourseMismatch    = courseMismatch.Select(_dataLockMapper.Map),
                DataLockWithOnlyPriceMismatch = withPriceOnly.Select(_dataLockMapper.Map)
            };

            _logger.Info($"Retrieved data lock summary for apprenticeship: {apprenticeshipId}. {summary.DataLockWithCourseMismatch.Count(): CourseMismatch}, {summary.DataLockWithOnlyPriceMismatch}: Price Mismatch ", apprenticeshipId: apprenticeshipId, caller: caller);

            return(summary);
        }
        public async Task ThenSummaryShowsChangesPendingWhenADataLockPricehasBeenTriaged()
        {
            //Arrange
            var source = new DataLockSummary
            {
                DataLockWithOnlyPriceMismatch = new List <DataLockStatus>
                {
                    new DataLockStatus {
                        IlrTrainingCourseCode = "TEST", TriageStatus = TriageStatus.Change
                    }
                },
                DataLockWithCourseMismatch = new List <DataLockStatus>()
            };

            //Act
            var result = await _mapper.MapDataLockSummary(source, false);

            //Assert
            Assert.IsTrue(result.ShowChangesPending);
        }
        public async Task ThenSummaryShowsDataLockPriceTriage(bool priceTriagePending, bool expectEnabled)
        {
            var source = new DataLockSummary
            {
                DataLockWithOnlyPriceMismatch = priceTriagePending ? new List <DataLockStatus>
                {
                    {
                        new DataLockStatus {
                            IlrTrainingCourseCode = "TEST", TriageStatus = TriageStatus.Unknown
                        }
                    }
                } : new List <DataLockStatus>(),
                DataLockWithCourseMismatch = new List <DataLockStatus>()
            };

            //Act
            var result = await _mapper.MapDataLockSummary(source, false);

            //Assert
            Assert.AreEqual(expectEnabled, result.ShowPriceDataLockTriageLink);
        }
        public async Task <DataLockSummaryViewModel> MapDataLockSummary(DataLockSummary source, bool hasHadDataLockSuccess)
        {
            var result = new DataLockSummaryViewModel
            {
                DataLockWithCourseMismatch    = new List <DataLockViewModel>(),
                DataLockWithOnlyPriceMismatch = new List <DataLockViewModel>()
            };

            var trainingProgrammes = await GetTrainingProgrammes();

            foreach (var dataLock in source.DataLockWithCourseMismatch)
            {
                var training = trainingProgrammes.SingleOrDefault(x => x.CourseCode == dataLock.IlrTrainingCourseCode);
                if (training == null)
                {
                    throw new InvalidOperationException(
                              $"Datalock {dataLock.DataLockEventId} IlrTrainingCourseCode {dataLock.IlrTrainingCourseCode} not found; possible expiry");
                }
                result.DataLockWithCourseMismatch.Add(MapDataLockStatus(dataLock, training));
            }

            foreach (var dataLock in source.DataLockWithOnlyPriceMismatch)
            {
                var training = trainingProgrammes.SingleOrDefault(x => x.CourseCode == dataLock.IlrTrainingCourseCode);
                if (training == null)
                {
                    throw new InvalidOperationException(
                              $"Datalock {dataLock.DataLockEventId} IlrTrainingCourseCode {dataLock.IlrTrainingCourseCode} not found; possible expiry");
                }
                result.DataLockWithOnlyPriceMismatch.Add(MapDataLockStatus(dataLock, training));
            }

            result.ShowChangesRequested =
                result.DataLockWithCourseMismatch.Any(
                    x => x.TriageStatusViewModel == TriageStatusViewModel.RestartApprenticeship);

            result.ShowChangesPending =
                result.DataLockWithOnlyPriceMismatch.Any(
                    x => x.TriageStatusViewModel == TriageStatusViewModel.ChangeApprenticeship)
                ||
                result.DataLockWithCourseMismatch.Any(
                    x => x.TriageStatusViewModel == TriageStatusViewModel.ChangeApprenticeship);

            //Can triage a course datalock if there is one that has not been triaged, and if there isn't one that
            //has been triaged but is pending approval by employer (dealt with one at a time)
            result.ShowCourseDataLockTriageLink =
                result.DataLockWithCourseMismatch.Any(x => x.TriageStatusViewModel == TriageStatusViewModel.Unknown) &&
                result.DataLockWithCourseMismatch.All(x => x.TriageStatusViewModel != TriageStatusViewModel.RestartApprenticeship);

            // Show ChangeLink if any not triaged PriceOnly AND NO Course+Price DL
            result.ShowPriceDataLockTriageLink =
                result.DataLockWithOnlyPriceMismatch.Any(x => x.TriageStatusViewModel == TriageStatusViewModel.Unknown);

            if (result.DataLockWithCourseMismatch.Any(m => m.DataLockErrorCode.HasFlag(DataLockErrorCode.Dlock07)))
            {
                result.ShowPriceDataLockTriageLink = false;
            }
            result.ShowPriceDataLockTriageLink = result.ShowPriceDataLockTriageLink || result.ShowCourseDataLockTriageLink && !hasHadDataLockSuccess;

            return(result);
        }