public async Task <IActionResult> ApplyCustomProfile(ApplyCustomProfileRequest request, Reference author) { Guard.ArgumentNotNull(request, nameof(request)); Guard.ArgumentNotNull(author, nameof(author)); ValidationResult validationResult = await _requestValidation.ValidateAsync(request); if (!validationResult.IsValid) { string validationErrors = validationResult.Errors.Select(_ => _.ErrorMessage).Join(", "); _logger.Information( $"Unable to process apply custom profile request. Request was invalid. \n{validationErrors}"); return(validationResult.AsBadRequest()); } string publishedProviderId = request.PublishedProviderId; string fundingLineCode = request.FundingLineCode; PublishedProvider publishedProvider = await _publishedFundingResilience.ExecuteAsync(() => _publishedFundingRepository.GetPublishedProviderById(publishedProviderId, publishedProviderId)); PublishedProviderVersion currentProviderVersion = publishedProvider.Current; foreach (IGrouping <string, ProfilePeriod> profilePeriods in request.ProfilePeriods.GroupBy(_ => _.DistributionPeriodId)) { string distributionPeriodId = profilePeriods.Key; currentProviderVersion.UpdateDistributionPeriodForFundingLine( fundingLineCode, distributionPeriodId, profilePeriods); currentProviderVersion.AddOrUpdateCustomProfile(fundingLineCode, request.CarryOver, distributionPeriodId); } if (request.HasCarryOver) { currentProviderVersion.AddCarryOver(fundingLineCode, ProfilingCarryOverType.CustomProfile, request.CarryOver.GetValueOrDefault()); } else { currentProviderVersion.RemoveCarryOver(fundingLineCode); } currentProviderVersion.AddProfilingAudit(fundingLineCode, author); await _publishedProviderVersionCreation.UpdatePublishedProviderStatus(new[] { publishedProvider }, author, currentProviderVersion.Status switch { PublishedProviderStatus.Draft => PublishedProviderStatus.Draft, _ => PublishedProviderStatus.Updated },
private async Task ReProfileFundingLine(PublishedProviderVersion newPublishedProviderVersion, ProfilePatternKey profilePatternKey, string fundingLineCode, FundingLine fundingLine) { ReProfileRequest reProfileRequest = await _profilingRequestBuilder.BuildReProfileRequest(newPublishedProviderVersion.SpecificationId, newPublishedProviderVersion.FundingStreamId, newPublishedProviderVersion.FundingPeriodId, newPublishedProviderVersion.ProviderId, fundingLineCode, profilePatternKey.Key, ProfileConfigurationType.Custom, fundingLine.Value); ReProfileResponse reProfileResponse = (await _profilingPolicy.ExecuteAsync(() => _profiling.ReProfile(reProfileRequest)))?.Content; if (reProfileResponse == null) { string error = $"Unable to re-profile funding line {fundingLineCode} on specification {newPublishedProviderVersion.SpecificationId} with profile pattern {profilePatternKey.Key}"; _logger.Error(error); throw new InvalidOperationException(error); } fundingLine.DistributionPeriods = _reProfilingResponseMapper.MapReProfileResponseIntoDistributionPeriods(reProfileResponse); newPublishedProviderVersion.RemoveCarryOver(fundingLineCode); if (reProfileResponse.CarryOverAmount > 0) { newPublishedProviderVersion.AddCarryOver(fundingLineCode, ProfilingCarryOverType.CustomProfile, reProfileResponse.CarryOverAmount); } }
private void WhenTheFundingLineCarryOverIsRemoved(string fundingLineId) { _publishedProviderVersion.RemoveCarryOver(fundingLineId); }