private void AndTheReProfilingStrategyResponse(AllocationProfileResponse profileResponse,
                                                ReProfileRequest request,
                                                FundingStreamPeriodProfilePattern profilePattern,
                                                ReProfileStrategyResult response)
 => _reProfilingStrategy.Setup(_ => _.ReProfile(It.Is <ReProfileContext>(ctx =>
                                                                         ReferenceEquals(ctx.Request, request) &&
                                                                         ReferenceEquals(ctx.ProfilePattern, profilePattern) &&
                                                                         ReferenceEquals(ctx.ProfileResult, profileResponse))))
 .Returns(response);
        public async Task <ActionResult <ReProfileResponse> > ReProfile(ReProfileRequest reProfileRequest)
        {
            FundingStreamPeriodProfilePattern profilePattern = await _profilePatternService.GetProfilePattern(reProfileRequest.FundingStreamId,
                                                                                                              reProfileRequest.FundingPeriodId,
                                                                                                              reProfileRequest.FundingLineCode,
                                                                                                              reProfileRequest.ProfilePatternKey);

            if (profilePattern == null)
            {
                return(new NotFoundObjectResult("Profile pattern not found"));
            }

            if (profilePattern.ReProfilingConfiguration == null || !profilePattern.ReProfilingConfiguration.ReProfilingEnabled)
            {
                return(new BadRequestObjectResult("Re-profiling is not enabled or has not been configured"));
            }

            IReProfilingStrategy strategy = GetReProfilingStrategy(reProfileRequest, profilePattern);

            if (strategy == null)
            {
                return(new BadRequestObjectResult("Re-profiling is not enabled for this scenario or the strategy was not found"));
            }

            ReProfileContext context = CreateReProfilingContext(reProfileRequest, profilePattern);

            ReProfileStrategyResult strategyResult = strategy.ReProfile(context);

            VerifyProfileAmountsReturnedMatchRequestedFundingLineValue(reProfileRequest, strategyResult);

            return(new ReProfileResponse
            {
                DeliveryProfilePeriods = strategyResult.DeliveryProfilePeriods,
                DistributionPeriods = strategyResult.DistributionPeriods,
                ProfilePatternDisplayName = profilePattern.ProfilePatternDisplayName,
                ProfilePatternKey = profilePattern.ProfilePatternKey,
                CarryOverAmount = strategyResult.CarryOverAmount
            });
        }
        private static void VerifyProfileAmountsReturnedMatchRequestedFundingLineValue(ReProfileRequest reProfileRequest,
                                                                                       ReProfileStrategyResult strategyResult)
        {
            decimal profileTotals = (strategyResult.DeliveryProfilePeriods?.Sum(_ => _.ProfileValue)).GetValueOrDefault();
            decimal totalAmount   = profileTotals + strategyResult.CarryOverAmount;

            if (totalAmount != reProfileRequest.FundingLineTotal)
            {
                throw new InvalidOperationException(
                          $"Profile amounts ({profileTotals}) and carry over amount ({strategyResult.CarryOverAmount}) does not equal funding line total requested ({reProfileRequest.FundingLineTotal}) from strategy.");
            }
        }
Beispiel #4
0
 private void WhenTheFundingLineIsReProfiled()
 => _result = _reProfiling.ReProfile(_context);