private static void UpdateFundingLineDistributionPeriods(BatchProfilingResponseModel response,
                                                                 ProfilingBatch batch)
        {
            DistributionPeriod[] distributionPeriods = response.DistributionPeriods.Select(_ => new DistributionPeriod
            {
                DistributionPeriodId = _.DistributionPeriodCode,
                Value          = _.Value,
                ProfilePeriods = response.DeliveryProfilePeriods.Where(pp =>
                                                                       pp.DistributionPeriod == _.DistributionPeriodCode)
                                 .Select(pp => new ProfilePeriod
                {
                    Occurrence           = pp.Occurrence,
                    Type                 = pp.Type.AsEnum <ProfilePeriodType>(),
                    Year                 = pp.Year,
                    TypeValue            = pp.Period,
                    ProfiledValue        = pp.Value,
                    DistributionPeriodId = _.DistributionPeriodCode
                })
                                 .ToArray()
            }).ToArray();

            foreach (FundingLine fundingLine in batch.FundingLines)
            {
                fundingLine.DistributionPeriods = distributionPeriods.DeepCopy();
            }
        }
        public async Task ProfilesBatchesFromSuppliedContext()
        {
            BatchProfilingRequestModel requestOne   = NewBatchProfilingRequestModel();
            BatchProfilingRequestModel requestTwo   = NewBatchProfilingRequestModel();
            BatchProfilingRequestModel requestThree = NewBatchProfilingRequestModel();

            BatchProfilingResponseModel responseOne   = NewBatchProfilingResponseModel();
            BatchProfilingResponseModel responseTwo   = NewBatchProfilingResponseModel();
            BatchProfilingResponseModel responseThree = NewBatchProfilingResponseModel();
            BatchProfilingResponseModel responseFour  = NewBatchProfilingResponseModel();
            BatchProfilingResponseModel responseFive  = NewBatchProfilingResponseModel();
            BatchProfilingResponseModel responseSix   = NewBatchProfilingResponseModel();

            int batchSize = new RandomNumberBetween(1, int.MaxValue);

            GivenThePagesOfRequests(requestOne, requestTwo, requestThree);
            AndTheBatchSize(batchSize);
            AndTheProfilingResponses(requestOne, responseOne, responseTwo);
            AndTheProfilingResponses(requestTwo, responseThree);
            AndTheProfilingResponses(requestThree, responseFour, responseFive, responseSix);

            await WhenTheBatchesAreProfiled();

            ThenTheContextItemsWereInitialisedWithABatchSize(batchSize);
            AndTheResponseWereReconciled(responseOne,
                                         responseTwo,
                                         responseThree,
                                         responseFour,
                                         responseFive,
                                         responseSix);
        }
        public void ReconcileBatchProfilingResponse(BatchProfilingResponseModel response)
        {
            if (ProfilingBatches == null ||
                !ProfilingBatches.TryGetValue(response.Key, out ProfilingBatch batch))
            {
                throw new NonRetriableException(
                          $"Unable to reconcile profiling response. Could not locate batch for response {response.Key}");
            }

            UpdateFundingLineDistributionPeriods(response, batch);
            EnsurePublishedProvidersHaveProfilePatternDetails(response, batch);
        }
        private static void EnsurePublishedProvidersHaveProfilePatternDetails(BatchProfilingResponseModel response,
                                                                              ProfilingBatch batch)
        {
            ProfilePatternKey profilePatternKey = new ProfilePatternKey
            {
                Key             = response.ProfilePatternKey,
                FundingLineCode = batch.FundingLineCode
            };

            foreach (PublishedProviderVersion publishedProviderVersion in batch.PublishedProviders)
            {
                lock (publishedProviderVersion)
                {
                    publishedProviderVersion.SetProfilePatternKey(profilePatternKey.DeepCopy());
                }
            }
        }