public async Task <DeleteProviderFavouriteCommandResponse> Handle(DeleteProviderFavouriteCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Handling {nameof(DeleteProviderFavouriteCommand)} for {request.EmployerAccountId}, Apprenticeship: {request.ApprenticeshipId} and Ukprn {request.Ukprn}");

            if (String.IsNullOrWhiteSpace(request.ApprenticeshipId))
            {
                throw new ArgumentException($"ApprenticeshipId must be provided for {nameof(DeleteProviderFavouriteCommand)}");
            }

            if (String.IsNullOrWhiteSpace(request.EmployerAccountId))
            {
                throw new ArgumentException($"EmployerAccountId must be provided for {nameof(DeleteProviderFavouriteCommand)}");
            }

            if (request.Ukprn >= 999999999 || request.Ukprn <= 1000000)
            {
                throw new ArgumentException($"Valid Ukprn must be provided for {nameof(DeleteProviderFavouriteCommand)}");
            }

            var existingFavourites = await _readRepository.GetApprenticeshipFavourites(request.EmployerAccountId);

            var hasExistingRecord = existingFavourites != null && existingFavourites.Count > 0;

            if (hasExistingRecord == false)
            {
                _logger.LogWarning($"No favourites exist for account {request.EmployerAccountId}");
                return(new DeleteProviderFavouriteCommandResponse {
                    CommandResult = DomainUpdateStatus.Failed
                });
            }

            if (!existingFavourites.Exists(request.ApprenticeshipId, request.Ukprn))
            {
                _logger.LogWarning($"requested Apprenticeship Id ({request.ApprenticeshipId}) or ukprn ({request.Ukprn} )doesnt exist for account {request.EmployerAccountId}");
                return(new DeleteProviderFavouriteCommandResponse {
                    CommandResult = DomainUpdateStatus.Failed
                });
            }

            var updatedFavourites = existingFavourites.MapToWriteModel();


            updatedFavourites.Remove(request.ApprenticeshipId, request.Ukprn);


            await _writeRepository.SaveApprenticeshipFavourites(request.EmployerAccountId, updatedFavourites);

            return(new DeleteProviderFavouriteCommandResponse {
                CommandResult = DomainUpdateStatus.Deleted
            });
        }
Esempio n. 2
0
        public async Task <string> Handle(SaveApprenticeshipFavouriteBasketCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Handling SaveApprenticeshipFavouriteCommand for basket: {basketId}", request.BasketId);

            var basketContentTask = _basketStore.GetAsync(request.BasketId);
            var employerAccountId = request.ChosenHashedAccountId ?? await GetEmployerAccountId(request.UserId);

            var favouritesTask = _readRepository.GetApprenticeshipFavourites(employerAccountId);

            await Task.WhenAll(basketContentTask, favouritesTask);

            var favourites    = favouritesTask.Result ?? new Domain.ReadModel.ApprenticeshipFavourites();
            var basketContent = basketContentTask.Result;

            var writeModel = favourites.MapToWriteModel();

            if (basketContent == null || !basketContent.Any())
            {
                return(employerAccountId);
            }

            bool changesMade = false;

            foreach (var item in basketContent)
            {
                changesMade |= writeModel.Update(item.ApprenticeshipId, item.Providers);
            }

            if (changesMade)
            {
                await _writeRepository.SaveApprenticeshipFavourites(employerAccountId, writeModel);

                _logger.LogDebug("Updated basket: {basketId}", request.BasketId);
            }
            else
            {
                _logger.LogDebug("No changes required for basket: {basketId}", request.BasketId);
            }

            _logger.LogDebug("Post save clean up. Deleting basket: {basketId}", request.BasketId);
            await _basketStore.RemoveAsync(request.BasketId);

            return(employerAccountId);
        }
Esempio n. 3
0
        public async Task <SaveApprenticeshipFavouriteCommandResponse> Handle(SaveApprenticeshipFavouriteCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Handling {nameof(SaveApprenticeshipFavouriteCommand)} for {request.EmployerAccountId}");

            var existingFavourites = await _readRepository.GetApprenticeshipFavourites(request.EmployerAccountId);

            var hasExistingRecord = existingFavourites != null && existingFavourites.Count > 0;

            var providers = request.Favourites.SelectMany(s => s.Providers);

            foreach (var provider in providers)
            {
                provider.Name = await _fatRepository.GetProviderNameAsync(provider.Ukprn);
            }

            await _writeRepository.SaveApprenticeshipFavourites(request.EmployerAccountId, request.Favourites);

            return(new SaveApprenticeshipFavouriteCommandResponse {
                CommandResult = hasExistingRecord ? DomainUpdateStatus.Updated : DomainUpdateStatus.Created
            });
        }