public async Task <IActionResult> GetLatestPublishedProvidersForSpecificationId(string specificationId)
        {
            ValidationResult validationResults = _validator.Validate(specificationId);

            if (!validationResults.IsValid)
            {
                return(validationResults.AsBadRequest());
            }

            SpecificationSummary specificationSummary = await _specificationService.GetSpecificationSummaryById(specificationId);

            List <PublishedProviderVersion> results = new List <PublishedProviderVersion>();

            foreach (var fundingStream in specificationSummary.FundingStreams)
            {
                IEnumerable <PublishedProvider> publishedProviders = await _resiliencePolicy.ExecuteAsync(() =>
                                                                                                          _publishedFunding.GetCurrentPublishedProviders(fundingStream.Id, specificationSummary.FundingPeriod.Id));

                if (publishedProviders.AnyWithNullCheck())
                {
                    results.AddRange(publishedProviders.Select(_ => _.Current));
                }
            }

            return(new OkObjectResult(results));
        }
        public void SetUp()
        {
            _specificationId  = NewRandomString();
            _validationResult = new ValidationResult();

            _validator = Substitute.For <ISpecificationIdServiceRequestValidator>();
            _validator.Validate(_specificationId)
            .Returns(_validationResult);

            _specificationService       = Substitute.For <ISpecificationService>();
            _publishedFundingRepository = Substitute.For <IPublishedFundingRepository>();
            _fundingCountProcessor      = Substitute.For <IPublishedProviderFundingCountProcessor>();
            _fundingCsvDataProcessor    = Substitute.For <IPublishedProviderFundingCsvDataProcessor>();
            _csvUtils   = Substitute.For <ICsvUtils>();
            _blobClient = Substitute.For <IBlobClient>();

            _service = new PublishedProviderStatusService(_validator, _specificationService, _publishedFundingRepository, new ResiliencePolicies
            {
                PublishedFundingRepository     = Polly.Policy.NoOpAsync(),
                SpecificationsRepositoryPolicy = Polly.Policy.NoOpAsync(),
                BlobClient = Polly.Policy.NoOpAsync()
            },
                                                          _fundingCountProcessor,
                                                          _fundingCsvDataProcessor,
                                                          _csvUtils,
                                                          _blobClient);
        }
예제 #3
0
        public async Task <IActionResult> GetProviderStatusCounts(string specificationId, string providerType, string localAuthority, string status)
        {
            ValidationResult validationResults = _validator.Validate(specificationId);

            if (!validationResults.IsValid)
            {
                return(validationResults.AsBadRequest());
            }

            SpecificationSummary specificationSummary =
                await _specificationsRepositoryPolicy.ExecuteAsync(() => _specificationService.GetSpecificationSummaryById(specificationId));

            IEnumerable <PublishedProviderFundingStreamStatus> publishedProviderFundingStreamStatuses =
                await _publishedFundingRepositoryResilience.ExecuteAsync(() => _publishedFundingRepository.GetPublishedProviderStatusCounts(specificationId, providerType, localAuthority, status));

            List <ProviderFundingStreamStatusResponse> response = new List <ProviderFundingStreamStatusResponse>();

            foreach (IGrouping <string, PublishedProviderFundingStreamStatus> publishedProviderFundingStreamGroup in publishedProviderFundingStreamStatuses.GroupBy(x => x.FundingStreamId))
            {
                if (!specificationSummary.FundingStreams.Select(x => x.Id).Contains(publishedProviderFundingStreamGroup.Key))
                {
                    continue;
                }

                response.Add(new ProviderFundingStreamStatusResponse
                {
                    FundingStreamId       = publishedProviderFundingStreamGroup.Key,
                    ProviderApprovedCount = GetCountValueOrDefault(publishedProviderFundingStreamGroup, "Approved"),
                    ProviderDraftCount    = GetCountValueOrDefault(publishedProviderFundingStreamGroup, "Draft"),
                    ProviderReleasedCount = GetCountValueOrDefault(publishedProviderFundingStreamGroup, "Released"),
                    ProviderUpdatedCount  = GetCountValueOrDefault(publishedProviderFundingStreamGroup, "Updated"),
                    TotalFunding          = publishedProviderFundingStreamGroup.Sum(x => x.TotalFunding)
                });
            }

            return(new OkObjectResult(response));
        }
        public void SetUp()
        {
            _specificationId  = NewRandomString();
            _validationResult = new ValidationResult();
            _fundingPeriodId  = NewRandomString();
            _fundingStreamId  = NewRandomString();

            _validator = Substitute.For <ISpecificationIdServiceRequestValidator>();
            _validator.Validate(_specificationId)
            .Returns(_validationResult);

            _publishedFunding     = Substitute.For <IPublishedFundingDataService>();
            _specificationService = Substitute.For <ISpecificationService>();

            _service = new PublishedProviderFundingService(new ResiliencePolicies
            {
                PublishedFundingRepository = Polly.Policy.NoOpAsync()
            },
                                                           _publishedFunding,
                                                           _specificationService,
                                                           _validator);
        }
        public async Task GetProviderDataForBatchApprovalAsCsv_ShouldSaveCsvFileInStorageConstinerAndReturnsBlobUrl()
        {
            IEnumerable <string> publishedProviderIds = new[] { NewRandomString(), NewRandomString() };
            string specificationId = NewRandomString();
            string fundingPeriodId = NewRandomString();
            string fundingStreamId = NewRandomString();
            string providerName1   = NewRandomString();
            string providerName2   = NewRandomString();

            _validator.Validate(Arg.Is(specificationId))
            .Returns(new ValidationResult());

            IEnumerable <PublishedProviderFundingCsvData> expectedCsvData = new[]
            {
                new PublishedProviderFundingCsvData()
                {
                    FundingStreamId = fundingStreamId,
                    FundingPeriodId = fundingPeriodId,
                    SpecificationId = specificationId,
                    ProviderName    = providerName1,
                    TotalFunding    = 123
                },
                new PublishedProviderFundingCsvData()
                {
                    FundingStreamId = fundingStreamId,
                    FundingPeriodId = fundingPeriodId,
                    SpecificationId = specificationId,
                    ProviderName    = providerName2,
                    TotalFunding    = 4567
                }
            };

            ICloudBlob blob = Substitute.For <ICloudBlob>();

            blob.Properties.Returns(new BlobProperties());
            blob.Metadata.Returns(new Dictionary <string, string>());

            string expectedUrl = "https://blob.test.com/tst";

            PublishedProviderStatus[] statuses = new[] { PublishedProviderStatus.Draft, PublishedProviderStatus.Updated };
            string blobNamePrefix = $"{fundingStreamId}-{fundingPeriodId}";

            GivenTheFundingDataForCsv(expectedCsvData, publishedProviderIds, specificationId, statuses);
            GivenBolbReference(blobNamePrefix, blob);
            GivenBlobUrl(blobNamePrefix, expectedUrl);

            OkObjectResult result = await WhenTheGetProviderDataForBatchApprovalAsCsvExecuted(publishedProviderIds, specificationId) as OkObjectResult;

            result
            .Should()
            .NotBeNull();

            result.Value
            .Should()
            .BeOfType <PublishedProviderDataDownload>()
            .Which
            .Url
            .Should()
            .Be(expectedUrl);

            await _fundingCsvDataProcessor
            .Received(1)
            .GetFundingData(
                Arg.Is <IEnumerable <string> >(ids => ids.SequenceEqual(publishedProviderIds)),
                Arg.Is(specificationId),
                Arg.Is <PublishedProviderStatus[]>(s => s.SequenceEqual(statuses)));

            _blobClient
            .Received(1)
            .GetBlockBlobReference(Arg.Is <string>(x => x.StartsWith(blobNamePrefix)), Arg.Is(BlobContainerName));
            await blob
            .Received(1)
            .UploadFromStreamAsync(Arg.Any <Stream>());

            _blobClient
            .Received(1)
            .GetBlobSasUrl(Arg.Is <string>(x => x.StartsWith(blobNamePrefix)),
                           Arg.Any <DateTimeOffset>(),
                           Arg.Is(SharedAccessBlobPermissions.Read),
                           Arg.Is(BlobContainerName));
        }