protected override async Task Handle(UpdateUserNotificationSettingsCommand command, CancellationToken cancellationToken)
        {
            var validationResult = _validator.Validate(command);

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

            await _userSettingsRepository.UpdateUserSettings(command.UserRef, command.ReceiveNotifications);

            _logger.Trace($"User settings updated for user {command.UserRef}");
        }
        public async Task <ApprenticeshipSearchQueryResponse> Handle(ApprenticeshipSearchQueryRequest message, CancellationToken cancellationToken)
        {
            _logger.Trace($"Performing apprenticeship search against api for provider {message.ProviderId}",
                          message.ProviderId);

            var data = await _commitmentsApi.GetProviderApprenticeships(message.ProviderId, message.Query);

            return(new ApprenticeshipSearchQueryResponse
            {
                Apprenticeships = data.Apprenticeships.ToList(),
                SearchKeyword = data.SearchKeyword,
                Facets = data.Facets,
                TotalApprenticeships = data.TotalApprenticeships,
                TotalApprenticeshipsBeforeFilter = data.TotalApprenticeshipsBeforeFilter,
                PageNumber = data.PageNumber,
                TotalPages = data.TotalPages,
                PageSize = data.PageSize
            });
        }
        protected override async Task Handle(BulkUploadApprenticeshipsCommand message, CancellationToken cancellationToken)
        {
            var stopwatch = Stopwatch.StartNew();

            var validationResult = new BulkUploadApprenticeshipsCommandValidator().Validate(message);

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

            var request = new BulkApprenticeshipRequest
            {
                UserId            = message.UserId,
                Apprenticeships   = message.Apprenticeships,
                LastUpdatedByInfo = new LastUpdateInfo {
                    EmailAddress = message.UserEmailAddress, Name = message.UserDisplayName
                }
            };

            await _providerCommitmentsApi.BulkUploadApprenticeships(message.ProviderId, message.CommitmentId, request);

            _logger.Trace($"Took {stopwatch.ElapsedMilliseconds} milliseconds to Bulk Upload {message.Apprenticeships.Count} apprentices to Commitments Api");
        }
        public async Task <BulkUploadResult> ValidateFileStructure(UploadApprenticeshipsViewModel uploadApprenticeshipsViewModel, long providerId, CommitmentView commitment)
        {
            if (uploadApprenticeshipsViewModel.Attachment == null)
            {
                return new BulkUploadResult {
                           Errors = new List <UploadError> {
                               new UploadError("No file chosen")
                           }
                }
            }
            ;

            var fileContent = new StreamReader(uploadApprenticeshipsViewModel.Attachment.InputStream).ReadToEnd();
            var fileName    = uploadApprenticeshipsViewModel?.Attachment?.FileName ?? "<- NO NAME ->";

            _logger.Trace($"Saving bulk upload file. {fileName}");
            var bulkUploadId = await _mediator.Send(
                new SaveBulkUploadFileCommand
            {
                ProviderId   = uploadApprenticeshipsViewModel.ProviderId,
                CommitmentId = commitment.Id,
                FileContent  = fileContent,
                FileName     = fileName
            });

            _logger.Info($"Saved bulk upload with Id: {bulkUploadId}");

            var fileAttributeErrors = _bulkUploadValidator.ValidateFileSize(uploadApprenticeshipsViewModel.Attachment).ToList();

            if (fileAttributeErrors.Any())
            {
                foreach (var error in fileAttributeErrors)
                {
                    _logger.Warn($"File Structure Error  -->  {error.Message}", uploadApprenticeshipsViewModel.ProviderId, commitment.Id);
                }

                _logger.Info($"Failed validation bulk upload file with {fileAttributeErrors.Count} errors", uploadApprenticeshipsViewModel.ProviderId, commitment.Id);

                return(new BulkUploadResult {
                    Errors = fileAttributeErrors
                });
            }

            var uploadResult = _fileParser.CreateViewModels(providerId, commitment, fileContent);

            if (uploadResult.HasErrors)
            {
                return(uploadResult);
            }

            var errors = _bulkUploadValidator.ValidateCohortReference(uploadResult.Data, uploadApprenticeshipsViewModel.HashedCommitmentId).ToList();

            errors.AddRange(_bulkUploadValidator.ValidateUlnUniqueness(uploadResult.Data).ToList());

            return(new BulkUploadResult
            {
                Errors = errors,
                Data = uploadResult.Data,
                BulkUploadId = bulkUploadId
            });
        }