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);
        }
Exemple #2
0
 private async Task ResolveDataLocksForApprenticeship(long apprenticeshipId)
 {
     var apprenticeshipDataLocks = (await _dataLockRepository.GetDataLocks(apprenticeshipId)).Select(x => x.DataLockEventId);
     await _dataLockRepository.ResolveDataLock(apprenticeshipDataLocks);
 }