protected override async Task HandleCore(TriageDataLocksCommand command)
        {
            var validationResult = _validator.Validate(command);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            var dataLocksToBeUpdated = (await _dataLockRepository
                                        .GetDataLocks(command.ApprenticeshipId))
                                       .Where(DataLockExtensions.UnHandled)
                                       .ToList();

            var apprenticeship = await _apprenticeshipRepository.GetApprenticeship(command.ApprenticeshipId);

            Validate(command, dataLocksToBeUpdated, apprenticeship);

            if (apprenticeship.HasHadDataLockSuccess && command.TriageStatus == TriageStatus.Change)
            {
                dataLocksToBeUpdated = dataLocksToBeUpdated.Where(DataLockExtensions.IsPriceOnly).ToList();
            }

            if (dataLocksToBeUpdated.Any(m => m.TriageStatus == command.TriageStatus))
            {
                throw new ValidationException($"Trying to update data lock for apprenticeship: {command.ApprenticeshipId} with the same TriageStatus ({command.TriageStatus}) ");
            }

            await _dataLockRepository.UpdateDataLockTriageStatus(dataLocksToBeUpdated.Select(m => m.DataLockEventId), command.TriageStatus);
        }
Beispiel #2
0
        protected override async Task HandleCore(RejectDataLockTriageCommand command)
        {
            var validationResult = _validator.Validate(command);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            var datalocks = await _dataLockRepository.GetDataLocks(command.ApprenticeshipId);

            var dataLocksToBeUpdated = datalocks
                                       .Where(DataLockExtensions.UnHandled)
                                       .Where(x => x.TriageStatus == TriageStatus.Change);

            var apprenticeship = await _apprenticeshipRepository.GetApprenticeship(command.ApprenticeshipId);

            if (apprenticeship.HasHadDataLockSuccess)
            {
                dataLocksToBeUpdated = dataLocksToBeUpdated.Where(DataLockExtensions.IsPriceOnly);
            }

            if (!dataLocksToBeUpdated.Any())
            {
                return;
            }

            await _dataLockRepository.UpdateDataLockTriageStatus(
                dataLocksToBeUpdated.Select(m => m.DataLockEventId),
                TriageStatus.Unknown);
        }
        protected override async Task HandleCore(ApproveDataLockTriageCommand command)
        {
            var validationResult = _validator.Validate(command);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            var datalocksForApprenticeship = await _dataLockRepository.GetDataLocks(command.ApprenticeshipId);

            var dataLocksToBeUpdated = datalocksForApprenticeship
                                       .Where(DataLockExtensions.UnHandled)
                                       .Where(m => m.TriageStatus == TriageStatus.Change);

            var apprenticeship = await _apprenticeshipRepository.GetApprenticeship(command.ApprenticeshipId);

            if (apprenticeship.HasHadDataLockSuccess)
            {
                dataLocksToBeUpdated = dataLocksToBeUpdated.Where(DataLockExtensions.IsPriceOnly);
            }

            var dataLockPasses = datalocksForApprenticeship.Where(x => x.Status == Status.Pass || x.PreviousResolvedPriceDataLocks());

            if (!dataLocksToBeUpdated.Any())
            {
                return;
            }

            var newPriceHistory = CreatePriceHistory(command, dataLocksToBeUpdated, dataLockPasses);

            await _apprenticeshipRepository.InsertPriceHistory(command.ApprenticeshipId, newPriceHistory);

            apprenticeship.PriceHistory = newPriceHistory.ToList();

            if (!apprenticeship.HasHadDataLockSuccess)
            {
                var dataLockWithUpdatedTraining = dataLocksToBeUpdated.FirstOrDefault(m => m.IlrTrainingCourseCode != apprenticeship.TrainingCode);
                if (dataLockWithUpdatedTraining != null)
                {
                    var training = await
                                   _apprenticeshipTrainingService.GetTrainingProgram(dataLockWithUpdatedTraining.IlrTrainingCourseCode);

                    _logger.Info($"Updating course for apprenticeship {apprenticeship.Id} from training code {apprenticeship.TrainingCode} to {dataLockWithUpdatedTraining.IlrTrainingCourseCode}");

                    apprenticeship.TrainingCode = dataLockWithUpdatedTraining.IlrTrainingCourseCode;
                    apprenticeship.TrainingName = training.Title;
                    apprenticeship.TrainingType = dataLockWithUpdatedTraining.IlrTrainingType;
                    await _apprenticeshipRepository.UpdateApprenticeship(apprenticeship, command.Caller);
                }
            }

            await _dataLockRepository.ResolveDataLock(dataLocksToBeUpdated.Select(m => m.DataLockEventId));

            await PublishEvents(apprenticeship);
        }
        public async Task <GetDataLocksResponse> Handle(GetDataLocksRequest message)
        {
            var validationResult = _validator.Validate(message);

            if (!validationResult.IsValid)
            {
                throw new ValidationException(validationResult.Errors);
            }

            var data = await _dataLockRepository.GetDataLocks(message.ApprenticeshipId);

            return(new GetDataLocksResponse
            {
                Data = data
            });
        }
        public async Task Filter(long apprenticeshipId)
        {
            var apprenticeshipDataLocks = await _dataLockRepository.GetDataLocks(apprenticeshipId, true);

            if (apprenticeshipDataLocks == null || apprenticeshipDataLocks.Count == 0)
            {
                return;
            }

            var haveDuplicates = apprenticeshipDataLocks
                                 .GroupBy(x => new
            {
                x.IlrTrainingCourseCode,
                x.IlrTrainingType,
                x.IlrActualStartDate,
                x.IlrEffectiveFromDate,
                x.IlrTotalCost
            })
                                 .Where(g => g.Count() > 1);

            foreach (var group in haveDuplicates)
            {
                var augustDataLock = group
                                     .Select(x => new
                {
                    DataLockEventId        = x.DataLockEventId,
                    PriceEpisodeIdentifier = x.PriceEpisodeIdentifier,
                    PriceEpisodeIdDateTime = x.GetDateFromPriceEpisodeIdentifier(),
                    IsAugustPriceEpisode   = _augustPricePeriodFormat.IsMatch(x.PriceEpisodeIdentifier)
                })
                                     .OrderByDescending(x => x.PriceEpisodeIdDateTime).First();

                if (!augustDataLock.IsAugustPriceEpisode)
                {
                    var message   = $"Unexpected price episode identifier matched: {augustDataLock.PriceEpisodeIdentifier} for apprenticeship: {apprenticeshipId}";
                    var exception = new AcademicYearFilterException(message);
                    _logger.Error(exception, message);
                    continue;
                }

                _logger.Info($"Found an academic year rollover data lock to delete: DataLockEventId: {augustDataLock.DataLockEventId}");
                await _dataLockRepository.Delete(augustDataLock.DataLockEventId);
            }
        }
        private async Task ResolveAnyTriagedCourseDataLocks(long apprenticeshipId)
        {
            var dataLocks = (await _dataLockRepository.GetDataLocks(apprenticeshipId))
                            .Where(x => !x.IsResolved && x.TriageStatus == TriageStatus.Restart && IsCourseChangeError(x.ErrorCode)).ToList();

            if (dataLocks.Any())
            {
                if (dataLocks.Count() > 1)
                {
                    _logger.Debug($"More than one unresolved data lock with triage status of reset found when stopping apprenticeship. ApprenticeshipId: {apprenticeshipId}", apprenticeshipId);
                }

                foreach (var dataLock in dataLocks)
                {
                    dataLock.IsResolved = true;
                    await _dataLockRepository.UpdateDataLockStatus(dataLock);
                }
            }
        }
Beispiel #7
0
 private async Task ResolveDataLocksForApprenticeship(long apprenticeshipId)
 {
     var apprenticeshipDataLocks = (await _dataLockRepository.GetDataLocks(apprenticeshipId)).Select(x => x.DataLockEventId);
     await _dataLockRepository.ResolveDataLock(apprenticeshipDataLocks);
 }