private IDictionary <string, string> GetMetadata(PublishedFundingVersion publishedFundingVersion)
 {
     return(new Dictionary <string, string>
     {
         { "specification-id", publishedFundingVersion.SpecificationId }
     });
 }
        protected async Task UndoPublishedFunding(CancellationToken cancellationToken,
                                                  dynamic context,
                                                  IEnumerable <PublishedFunding> publishedFunding)
        {
            PublishedFundingUndoTaskContext taskContext = GetTaskContext(context);
            List <PublishedFunding>         publishedFundingToDelete = new List <PublishedFunding>();
            List <PublishedFunding>         publishedFundingToUpdate = new List <PublishedFunding>();

            foreach (PublishedFunding document in publishedFunding)
            {
                PublishedFundingVersion previousVersion = await GetPreviousPublishedFundingVersion(document.Current, taskContext);

                if (previousVersion == null)
                {
                    publishedFundingToDelete.Add(document);
                }
                else
                {
                    document.Current = previousVersion;
                    publishedFundingToUpdate.Add(document);
                }
            }

            Task[] updateTasks = new[]
            {
                UpdateDocuments(publishedFundingToUpdate, _ => _.ParitionKey),
                DeleteDocuments(publishedFundingToDelete, _ => _.ParitionKey, IsHardDelete)
            };

            await TaskHelper.WhenAllAndThrow(updateTasks);
        }
Exemplo n.º 3
0
        public string GenerateContents(PublishedFundingVersion publishedFundingVersion,
                                       TemplateMetadataContents templateMetadataContents)
        {
            Guard.ArgumentNotNull(publishedFundingVersion, nameof(publishedFundingVersion));
            Guard.ArgumentNotNull(templateMetadataContents, nameof(templateMetadataContents));

            SchemaJson contents = new SchemaJson
            {
                Schema        = "https://fundingschemas.blob.core.windows.net/schemas/logicalmodel-1.0.json#schema",
                SchemaVersion = publishedFundingVersion.SchemaVersion,
                Funding       = new
                {
                    publishedFundingVersion.TemplateVersion,
                    Id             = publishedFundingVersion.FundingId,
                    FundingVersion = $"{publishedFundingVersion.MajorVersion}_{publishedFundingVersion.MinorVersion}",
                    Status         = publishedFundingVersion.Status.ToString(),
                    FundingStream  = new
                    {
                        Code = publishedFundingVersion.FundingStreamId,
                        Name = publishedFundingVersion.FundingStreamName
                    },
                    FundingPeriod = new
                    {
                        publishedFundingVersion.FundingPeriod.Id,
                        publishedFundingVersion.FundingPeriod.Period,
                        publishedFundingVersion.FundingPeriod.Name,
                        Type = publishedFundingVersion.FundingPeriod.Type.ToString(),
                        publishedFundingVersion.FundingPeriod.StartDate,
                        publishedFundingVersion.FundingPeriod.EndDate
                    },
                    OrganisationGroup = new
                    {
                        GroupTypeCode           = publishedFundingVersion.OrganisationGroupTypeCode,
                        GroupTypeIdentifier     = publishedFundingVersion.OrganisationGroupTypeIdentifier,
                        IdentifierValue         = publishedFundingVersion.OrganisationGroupIdentifierValue,
                        GroupTypeClassification = publishedFundingVersion.OrganisationGroupTypeClassification,
                        Name           = publishedFundingVersion.OrganisationGroupName,
                        SearchableName = publishedFundingVersion.OrganisationGroupSearchableName,
                        Identifiers    = publishedFundingVersion.OrganisationGroupIdentifiers?.Select(groupTypeIdentifier => new
                        {
                            groupTypeIdentifier.Type,
                            groupTypeIdentifier.Value
                        }).ToArray()
                    },
                    FundingValue = new
                    {
                        TotalValue   = publishedFundingVersion.TotalFunding,
                        FundingLines = templateMetadataContents.RootFundingLines?.Select(_ => BuildSchemaJsonFundingLines(publishedFundingVersion.ReferenceData, publishedFundingVersion.Calculations, publishedFundingVersion.FundingLines, _, publishedFundingVersion.OrganisationGroupTypeIdentifier, publishedFundingVersion.OrganisationGroupIdentifierValue))
                    },
                    ProviderFundings = publishedFundingVersion.ProviderFundings?.ToArray(),
                    publishedFundingVersion.GroupingReason,
                    publishedFundingVersion.StatusChangedDate,
                    publishedFundingVersion.ExternalPublicationDate,
                    publishedFundingVersion.EarliestPaymentAvailableDate,
                    VariationReasons = publishedFundingVersion.VariationReasons?.ToArray()
                }
            };

            return(contents.AsJson());
        }
        public Task <HttpStatusCode> UpsertAsync <T>(T entity, string partitionKey = null, bool undelete = false, bool maintainCreatedDate = true) where T : IIdentifiable
        {
            if (typeof(T).Name == "PublishedProviderVersion")
            {
                PublishedProviderVersion publishedProviderVersion = entity as PublishedProviderVersion;
                if (!PublishedProviderVersions.ContainsKey(publishedProviderVersion.SpecificationId))
                {
                    PublishedProviderVersions.TryAdd(publishedProviderVersion.SpecificationId, new ConcurrentDictionary <string, PublishedProviderVersion>());
                }

                PublishedProviderVersions[publishedProviderVersion.SpecificationId][publishedProviderVersion.Id] = publishedProviderVersion;
                return(Task.FromResult(HttpStatusCode.OK));
            }
            else if (typeof(T).Name == "PublishedFundingVersion")
            {
                PublishedFundingVersion publishedFundingVersion = entity as PublishedFundingVersion;
                if (!PublishedFundingVersions.ContainsKey(publishedFundingVersion.SpecificationId))
                {
                    PublishedFundingVersions.TryAdd(publishedFundingVersion.SpecificationId, new ConcurrentDictionary <string, PublishedFundingVersion>());
                }

                PublishedFundingVersions[publishedFundingVersion.SpecificationId][publishedFundingVersion.Id] = publishedFundingVersion;
                return(Task.FromResult(HttpStatusCode.OK));
            }

            return(Task.FromResult(HttpStatusCode.BadRequest));
        }
        public async Task DeletesCosmosDocumentsAndMatchingBlobStoreDocumentsForFeedItems()
        {
            PublishedFundingVersion publishedFundingVersionOne   = NewPublishedFundingVersion();
            PublishedFundingVersion publishedFundingVersionTwo   = NewPublishedFundingVersion();
            PublishedFundingVersion publishedFundingVersionThree = NewPublishedFundingVersion();
            PublishedFundingVersion publishedFundingVersionFour  = NewPublishedFundingVersion();

            GivenThePublishedFundingVersionFeed(NewFeedIterator(
                                                    WithPages(Page(publishedFundingVersionOne, publishedFundingVersionTwo),
                                                              Page(publishedFundingVersionThree, publishedFundingVersionFour),
                                                              Page <PublishedFundingVersion>())));

            await WhenTheTaskIsRun();

            ThenTheDocumentsWereDeleted(new [] { publishedFundingVersionOne, publishedFundingVersionTwo },
                                        new [] { publishedFundingVersionOne.PartitionKey, publishedFundingVersionTwo.PartitionKey },
                                        true);
            AndTheDocumentsWereDeleted(new [] { publishedFundingVersionThree, publishedFundingVersionFour },
                                       new [] { publishedFundingVersionThree.PartitionKey, publishedFundingVersionFour.PartitionKey },
                                       true);
            AndThePublishedFundingVersionBlobDocumentsWereRemoved(publishedFundingVersionOne,
                                                                  publishedFundingVersionTwo,
                                                                  publishedFundingVersionThree,
                                                                  publishedFundingVersionFour);
        }
Exemplo n.º 6
0
 private void PublishFundingDocument(FeedOptions options, PublishedFundingVersion publishedFundingVersion, string generatedDocumentContent)
 {
     if (options.FeedStorageType == FeedStorageType.File)
     {
         File.WriteAllText(Path.Combine(options.OutputFolderPath, $"{publishedFundingVersion.FundingId}.json"), generatedDocumentContent);
     }
 }
 private void PropagateProviderVariationReasons(PublishedFundingVersion publishedFundingVersion, IEnumerable <PublishedProvider> publishedProviders)
 {
     foreach (PublishedProvider publishedProvider in publishedProviders)
     {
         publishedFundingVersion.AddVariationReasons(publishedProvider.Current.VariationReasons);
     }
 }
Exemplo n.º 8
0
        public void GeneratesJsonConformingToThe11Schema()
        {
            TemplateMetadataContents templateMetadataContents = GetTemplateMetaDataContents("example-funding-template1.1.json");
            PublishedFundingVersion  publishedFundingVersion  = GetPublishedFundingVersion("example-published-funding-version.json");

            string funding = WhenThePublishedFundingVersionIsTransformed(publishedFundingVersion, templateMetadataContents);

            ThenTheJsonValidatesAgainstThe1_1FundingSchema(funding);
        }
Exemplo n.º 9
0
        public async Task RemovePublishedFundingVersionBlobRemovesBlobDocumentWithSuppliedProviderFundingIdFromVersionsCollection()
        {
            PublishedFundingVersion publishedFundingVersion = NewPublishedFundingVersion();

            GivenTheCloudBlobIsForBlobNameAndContainer(publishedFundingVersion.FundingId, "publishedfunding");

            await WhenThePublishedFundingDocumentIsRemoved(publishedFundingVersion);

            ThenTheBlobWasDeleted();
        }
Exemplo n.º 10
0
        public async Task <int> Generate(FeedOptions options)
        {
            IEnumerable <Provider> records = GetRecords(options.InputFilePath);

            foreach (Provider provider in records)
            {
                PublishedProviderVersion publishedProviderVersion = GetPublishedProviderVersion(provider);
                Common.TemplateMetadata.Models.TemplateMetadataContents templateMetadataContents = GetProviderTemplateMetadataContents();
                TemplateMapping         templateMapping         = GetTemplateMapping();
                GeneratedProviderResult generatedProviderResult = GetGeneratedProviderResult(provider);

                string generatedProviderDocumentContent = _publishedProviderContentsGenerator.GenerateContents(publishedProviderVersion, templateMetadataContents, templateMapping);
                PublishProviderDocument(options, publishedProviderVersion, generatedProviderDocumentContent);
            }

            int fundingIndex = 0;

            foreach (IGrouping <(string, string, string), Provider> groupingKey in records.GroupBy(provider => (provider.LACode, provider.MajorVersionNo, provider.AllocationID)))
            {
                OrganisationGroupLookupParameters organisationGroupLookupParameters = new OrganisationGroupLookupParameters
                {
                    OrganisationGroupTypeCode = GetOrganisationGroupTypeCode(groupingKey.Key.Item3),
                    IdentifierValue           = groupingKey.Key.Item1,
                    GroupTypeIdentifier       = CalculateFunding.Common.ApiClient.Policies.Models.OrganisationGroupTypeIdentifier.LACode,
                    ProviderVersionId         = options.ProviderVersion
                };
                IEnumerable <ProviderApiClient> apiClientProviders      = GetApiClientProviders(groupingKey);
                TargetOrganisationGroup         targetOrganisationGroup = null;

                try
                {
                    targetOrganisationGroup = await organisationGroupTargetProviderLookup.GetTargetProviderDetails(organisationGroupLookupParameters, Common.ApiClient.Policies.Models.GroupingReason.Payment, apiClientProviders);
                }
                catch (Exception ex)
                {
                    string message = $"Could not find provider with ID:{organisationGroupLookupParameters.IdentifierValue} with error message {ex}";
                    _logger.Error(message);
                    Console.WriteLine(message);

                    continue;
                }

                PublishedFundingVersion publishedFundingVersion = GetPublishedFundingVersion(groupingKey, targetOrganisationGroup, fundingIndex);
                Common.TemplateMetadata.Models.TemplateMetadataContents templateMetadataContents = GetFundingTemplateMetadataContents();
                string generatedFundingDocumentContent = _publishedFundingContentsGenerator.GenerateContents(publishedFundingVersion, templateMetadataContents);

                PublishFundingDocument(options, publishedFundingVersion, generatedFundingDocumentContent);

                fundingIndex++;
            }

            _logger.Information("NAV Data generation completed.");

            return(1);
        }
Exemplo n.º 11
0
        public async Task GetLatestEarlierPublishedFundingVersionFromVersion()
        {
            PublishedFundingVersion document = await _repository.GetLatestEarlierPublishedFundingVersionFromVersion("DSG",
                                                                                                                    "FY-2021",
                                                                                                                    4M,
                                                                                                                    "LACode",
                                                                                                                    "213",
                                                                                                                    ModelGroupingReason.Information);

            document?
            .MajorVersion
            .Should()
            .Be(3);
        }
Exemplo n.º 12
0
        public async Task GetLatestEarlierPublishedFundingVersion()
        {
            PublishedFundingVersion document = await _repository.GetLatestEarlierPublishedFundingVersion("DSG",
                                                                                                         "FY-2021",
                                                                                                         1584980585, //timestamp is for version 3 so test should find me version 2
                                                                                                         "LACode",
                                                                                                         "213",
                                                                                                         ModelGroupingReason.Information);

            document?
            .MajorVersion
            .Should()
            .Be(2);
        }
        private void GivenPublishedFunding()
        {
            _publishedFundingVersion = NewPublishedFundingVersion(_ => _.WithFundingId("funding1")
                                                                  .WithProviderFundings(new List <string> {
                "providerfunding1", "providerfunding2"
            })
                                                                  .WithFundingPeriod(_publishedFundingPeriod)
                                                                  .WithFundingStreamId("stream1")
                                                                  .WithOrganisationGroupTypeClassification(OrganisationGroupTypeClassification.LegalEntity)
                                                                  .WithOrganisationGroupTypeIdentifier(OrganisationGroupTypeIdentifier.AcademyTrustCode)
                                                                  .WithOrganisationGroupTypeCode(OrganisationGroupTypeCode.AcademyTrust)
                                                                  .WithOrganisationGroupIdentifierValue("101"));

            _publishedFunding = NewPublishedFunding(_ => _.WithCurrent(_publishedFundingVersion));
        }
Exemplo n.º 14
0
        public void SavePublishedFundingContents_GivenPublishedFundingVersionsButInvalidTemplate_ExceptionIsThrown()
        {
            // Arrange
            _publishedFundingVersion = NewPublishedFundingVersion(version => version.WithFundingId("funding1")
                                                                  .WithFundingPeriod(_publishedFundingPeriod)
                                                                  .WithFundingStreamId(_fundingStream)
                                                                  .WithGroupReason(CalculateFunding.Models.Publishing.GroupingReason.Payment)
                                                                  .WithOrganisationGroupTypeClassification(OrganisationGroupTypeClassification.LegalEntity)
                                                                  .WithOrganisationGroupTypeIdentifier(OrganisationGroupTypeIdentifier.AcademyTrustCode)
                                                                  .WithOrganisationGroupTypeCode(OrganisationGroupTypeCode.AcademyTrust)
                                                                  .WithOrganisationGroupIdentifierValue("101"));

            // Act
            Func <Task> invocation = async() => await WhenPublishedFundingContentsSaved();

            // Assert
            ThenExceptionShouldBeThrown($"Generator failed to generate content for published provider version with id: '{_publishedFundingVersion.Id}'", invocation);
        }
Exemplo n.º 15
0
        public void FlattensTemplateCalculationsAndProviderMetaDataIntoRows(
            CalculateFunding.Models.Publishing.GroupingReason expectedGroupingReason)
        {
            PublishedFundingVersion publishedFunding =
                NewPublishedFundingVersion(pfv =>
                                           pfv.WithOrganisationGroupTypeCode(OrganisationGroupTypeCode.LocalAuthority)
                                           .WithOrganisationGroupName("Enfield")
                                           .WithGroupReason(expectedGroupingReason)
                                           .WithProviderFundings(new [] { "one", "two" })
                                           .WithPublishedProviderStatus(PublishedFundingStatus.Released)
                                           .WithMajor(1)
                                           .WithAuthor(NewReference(rf => rf.WithName("system")))
                                           .WithDate("2020-02-05T20:03:55")
                                           .WithFundingLines(
                                               NewFundingLines(fl =>
                                                               fl.WithName("fundingLine1")
                                                               .WithValue(123M),
                                                               fl =>
                                                               fl.WithName("fundingLine2")
                                                               .WithValue(456M))));

            dynamic[] expectedCsvRows =
            {
                new Dictionary <string, object>
                {
                    { "Grouping Reason",expectedGroupingReason.ToString()           },
                    { "Grouping Code",  "LocalAuthority"                            },
                    { "Grouping Name",  "Enfield"                                   },
                    { "Allocation Status","Released"                                  },
                    { "Allocation Major Version","1"                                         },
                    { "Allocation Author","system"                                    },
                    { "Allocation DateTime","2020-02-05T20:03:55"                       },
                    { "Provider Count",                                           2 },
                    { "fundingLine1",   123M.ToString(CultureInfo.InvariantCulture) }, //funding lines to be alpha numerically ordered on name
                    { "fundingLine2",   456M.ToString(CultureInfo.InvariantCulture) }
                }
            };

            ExpandoObject[] transformProviderResultsIntoCsvRows = _transformation.Transform(new [] { publishedFunding }).ToArray();

            transformProviderResultsIntoCsvRows
            .Should()
            .BeEquivalentTo(expectedCsvRows);
        }
        public async Task GetLatestEarlierPublishedFundingVersion()
        {
            string fundingStreamId              = NewRandomString();
            string fundingPeriodId              = NewRandomString();
            long   timeStamp                    = NewRandomTimeStamp();
            string groupTypeIdentifier          = NewRandomString();
            string groupTypeIdentifierValue     = NewRandomString();
            ModelsGroupingReason groupingReason = NewRandomGroupingReason();

            PublishedFundingVersion expectedLatestEarlierDocument = NewPublishedFundingVersion();

            GivenTheLatestEarlierDocument(@"SELECT
                              TOP 1 *
                        FROM publishedFundingVersion p
                        WHERE p.documentType = 'PublishedFundingVersion'
                        AND p._ts < @sinceTimeStamp                        
                        AND p.content.fundingStreamId = @fundingStreamId
                        AND p.content.fundingPeriod.id = @fundingPeriodId
                        AND p.content.organisationGroupTypeIdentifier = @groupTypeIdentifier
                        AND p.content.organisationGroupIdentifierValue = @groupTypeIdentifierValue
                        AND p.content.groupingReason = @groupingReason
                        AND p.deleted = false
                        ORDER BY p._ts DESC",
                                          expectedLatestEarlierDocument,
                                          ("@fundingPeriodId", fundingPeriodId),
                                          ("@fundingStreamId", fundingStreamId),
                                          ("@sinceTimeStamp", timeStamp),
                                          ("@groupTypeIdentifier", groupTypeIdentifier),
                                          ("@groupTypeIdentifierValue", groupTypeIdentifierValue),
                                          ("@groupingReason", groupingReason.ToString()));

            PublishedFundingVersion actualLatestEarlierDocument = await _repository.GetLatestEarlierPublishedFundingVersion(fundingStreamId,
                                                                                                                            fundingPeriodId,
                                                                                                                            timeStamp,
                                                                                                                            groupTypeIdentifier,
                                                                                                                            groupTypeIdentifierValue,
                                                                                                                            groupingReason);

            actualLatestEarlierDocument
            .Should()
            .BeSameAs(expectedLatestEarlierDocument);
        }
        public async Task GetLatestEarlierPublishedFundingVersionFromVersion()
        {
            string  fundingStreamId             = NewRandomString();
            string  fundingPeriodId             = NewRandomString();
            decimal version                     = NewRandomInteger();
            string  groupTypeIdentifier         = NewRandomString();
            string  groupTypeIdentifierValue    = NewRandomString();
            ModelsGroupingReason groupingReason = NewRandomGroupingReason();

            PublishedFundingVersion expectedLatestEarlierDocument = NewPublishedFundingVersion();

            GivenTheLatestEarlierDocument(@"SELECT
                              TOP 1 *
                        FROM publishedFundingVersion p
                        WHERE p.documentType = 'PublishedFundingVersion'
                        AND StringToNumber(CONCAT(Tostring(p.content.majorVersion), '.', Tostring(p.content.minorVersion))) < @version
                        AND p.content.fundingStreamId = @fundingStreamId
                        AND p.content.fundingPeriod.id = @fundingPeriodId
                        AND p.content.organisationGroupTypeIdentifier = @groupTypeIdentifier
                        AND p.content.organisationGroupIdentifierValue = @groupTypeIdentifierValue
                        AND p.content.groupingReason = @groupingReason
                        AND p.deleted = false
                        ORDER BY p.content.majorVersion DESC, p.content.minorVersion DESC",
                                          expectedLatestEarlierDocument,
                                          ("@fundingPeriodId", fundingPeriodId),
                                          ("@fundingStreamId", fundingStreamId),
                                          ("@version", version),
                                          ("@groupTypeIdentifier", groupTypeIdentifier),
                                          ("@groupTypeIdentifierValue", groupTypeIdentifierValue),
                                          ("@groupingReason", groupingReason.ToString()));

            PublishedFundingVersion actualLatestEarlierDocument = await _repository.GetLatestEarlierPublishedFundingVersionFromVersion(fundingStreamId,
                                                                                                                                       fundingPeriodId,
                                                                                                                                       version,
                                                                                                                                       groupTypeIdentifier,
                                                                                                                                       groupTypeIdentifierValue,
                                                                                                                                       groupingReason);

            actualLatestEarlierDocument
            .Should()
            .BeSameAs(expectedLatestEarlierDocument);
        }
Exemplo n.º 18
0
        public async Task <int> Generate(FeedOptions options)
        {
            IEnumerable <Provider> records = GetRecords(options.InputFilePath);

            foreach (Provider provider in records)
            {
                PublishedProviderVersion publishedProviderVersion = GetPublishedProviderVersion(provider);
                Common.TemplateMetadata.Models.TemplateMetadataContents templateMetadataContents = GetProviderTemplateMetadataContents();
                TemplateMapping         templateMapping         = GetTemplateMapping();
                GeneratedProviderResult generatedProviderResult = GetGeneratedProviderResult(provider);

                string generatedProviderDocumentContent = _publishedProviderContentsGenerator.GenerateContents(publishedProviderVersion, templateMetadataContents, templateMapping);
                PublishProviderDocument(options, publishedProviderVersion, generatedProviderDocumentContent);
            }

            int fundingIndex = 0;

            foreach (IGrouping <string, Provider> laGroup in records.GroupBy(x => x.ProviderLaCode))
            {
                OrganisationGroupLookupParameters organisationGroupLookupParameters = new OrganisationGroupLookupParameters
                {
                    OrganisationGroupTypeCode = Common.ApiClient.Policies.Models.OrganisationGroupTypeCode.LocalAuthority,
                    IdentifierValue           = laGroup.Key,
                    GroupTypeIdentifier       = Common.ApiClient.Policies.Models.OrganisationGroupTypeIdentifier.LACode,
                    ProviderVersionId         = options.ProviderVersion
                };
                IEnumerable <ProviderApiClient> apiClientProviders      = GetApiClientProviders(laGroup);
                TargetOrganisationGroup         targetOrganisationGroup = await organisationGroupTargetProviderLookup.GetTargetProviderDetails(organisationGroupLookupParameters, Common.ApiClient.Policies.Models.GroupingReason.Payment, apiClientProviders);

                PublishedFundingVersion publishedFundingVersion = GetPublishedFundingVersion(laGroup, targetOrganisationGroup, fundingIndex);
                Common.TemplateMetadata.Models.TemplateMetadataContents templateMetadataContents = GetFundingTemplateMetadataContents();
                string generatedFundingDocumentContent = _publishedFundingContentsGenerator.GenerateContents(publishedFundingVersion, templateMetadataContents);

                PublishFundingDocument(options, publishedFundingVersion, generatedFundingDocumentContent);

                fundingIndex++;
            }

            _logger.Information("NAV Data generation completed.");

            return(1);
        }
Exemplo n.º 19
0
        public void TransformsSuppliedPublishedFundingVersionIntoSchema10Json_2()
        {
            PublishedFundingVersion publishedFundingVersion =
                ReadResourceFileContents("CalculateFunding.Generators.Schema10.UnitTests.Resources.examplePublishedFundingVersion_2.json")
                .AsPoco <PublishedFundingVersion>();

            string expectedJson = ReadResourceFileContents("CalculateFunding.Generators.Schema10.UnitTests.Resources.expectedPublishedFundingSchema1Contents_2.json")
                                  .Prettify();

            TemplateMetadataContents templateMetadataContents = ReadTemplateContents(ReadResourceFileContents(
                                                                                         "CalculateFunding.Generators.Schema10.UnitTests.Resources.exampleTemplate1.json"));

            string actualJson = WhenThePublishedFundingVersionIsTransformed(publishedFundingVersion, templateMetadataContents);

            actualJson = actualJson
                         .Prettify();

            actualJson
            .Should()
            .Be(expectedJson);
        }
Exemplo n.º 20
0
        public async Task SavePublishedFundingContents_GivenPublishedFundingVersions_FileUploaded()
        {
            // Arrange
            _publishedFundingVersion = NewPublishedFundingVersion(version => version.WithFundingId("funding1")
                                                                  .WithFundingPeriod(_publishedFundingPeriod)
                                                                  .WithFundingStreamId(_fundingStream)
                                                                  .WithGroupReason(CalculateFunding.Models.Publishing.GroupingReason.Payment)
                                                                  .WithOrganisationGroupTypeClassification(OrganisationGroupTypeClassification.LegalEntity)
                                                                  .WithOrganisationGroupTypeIdentifier(OrganisationGroupTypeIdentifier.AcademyTrustCode)
                                                                  .WithOrganisationGroupTypeCode(OrganisationGroupTypeCode.AcademyTrust)
                                                                  .WithOrganisationGroupIdentifierValue("101"));

            GivenTheGeneratedContentsIsReturned();
            GivenTheBlobReference();

            // Act
            await WhenPublishedFundingContentsSaved();

            // Assert
            await ThenFileIsUploaded();
        }
Exemplo n.º 21
0
        public Task <IEnumerable <PublishedFundingVersion> > GetPublishedFundingVersions(
            IEnumerable <KeyValuePair <string, string> > publishedFundingVersionIds)
        {
            List <PublishedFundingVersion> publishedFundingVersions = new List <PublishedFundingVersion>();

            foreach (KeyValuePair <string, string> publishedFundingVersionId in publishedFundingVersionIds)
            {
                PublishedFundingVersion publishedFundingVersion = _repo
                                                                  .PublishedFundingVersions
                                                                  .SelectMany(c => c.Value)
                                                                  .FirstOrDefault(p => p.Value.Id == publishedFundingVersionId.Key)
                                                                  .Value;

                if (publishedFundingVersion != null)
                {
                    publishedFundingVersions.Add(publishedFundingVersion);
                }
            }

            return(Task.FromResult(publishedFundingVersions.AsEnumerable()));
        }
        public async Task SetsCurrentToLatestPreviousVersionAndDeletesIfInitialVersion()
        {
            PublishedFunding publishedFundingOne   = NewPublishedFunding();
            PublishedFunding publishedFundingTwo   = NewPublishedFunding();
            PublishedFunding publishedFundingThree = NewPublishedFunding();
            PublishedFunding publishedFundingFour  = NewPublishedFunding();
            PublishedFunding publishedFundingFive  = NewPublishedFunding();

            PublishedFundingVersion previousVersionTwo   = NewPublishedFundingVersion();
            PublishedFundingVersion previousVersionThree = NewPublishedFundingVersion();
            PublishedFundingVersion previousVersionFive  = NewPublishedFundingVersion();

            GivenThePublishedFundingFeed(NewFeedIterator(WithPages(Page(publishedFundingOne, publishedFundingTwo),
                                                                   Page(publishedFundingThree, publishedFundingFour),
                                                                   Page(publishedFundingFive))));
            AndThePreviousLatestVersion(publishedFundingTwo.Current, previousVersionTwo);
            AndThePreviousLatestVersion(publishedFundingThree.Current, previousVersionThree);
            AndThePreviousLatestVersion(publishedFundingFive.Current, previousVersionFive);

            await WhenTheTaskIsRun();

            ThenTheDocumentsWereDeleted(new[] { publishedFundingOne },
                                        new[] { publishedFundingOne.ParitionKey },
                                        _isHardDelete);
            ThenTheDocumentsWereDeleted(new[] { publishedFundingFour },
                                        new[] { publishedFundingFour.ParitionKey },
                                        _isHardDelete);
            AndTheDocumentsWereUpdated(new[] { publishedFundingTwo },
                                       new[] { publishedFundingTwo.ParitionKey });
            AndTheDocumentsWereUpdated(new[] { publishedFundingThree },
                                       new[] { publishedFundingThree.ParitionKey });
            AndTheDocumentsWereUpdated(new[] { publishedFundingFive },
                                       new[] { publishedFundingFive.ParitionKey });
            AndThePublishedFundingHasCurrent((publishedFundingFive, previousVersionFive),
                                             (publishedFundingTwo, previousVersionTwo),
                                             (publishedFundingThree, previousVersionThree));
        }
 private string GetBlobName(PublishedFundingVersion publishedFundingVersion)
 {
     return($"{publishedFundingVersion.FundingStreamId}-{publishedFundingVersion.FundingPeriod.Id}-{publishedFundingVersion.GroupingReason}-{publishedFundingVersion.OrganisationGroupTypeCode}-{publishedFundingVersion.OrganisationGroupIdentifierValue}-{publishedFundingVersion.MajorVersion}_{publishedFundingVersion.MinorVersion}.json");
 }
Exemplo n.º 24
0
 private async Task WhenThePublishedFundingDocumentIsRemoved(PublishedFundingVersion publishedFundingVersion)
 {
     await _repository.RemovePublishedFundingVersionBlob(publishedFundingVersion);
 }
        public Task <PublishedFundingVersion> GetPublishedFundingVersionById(string cosmosId, string partitionKey)
        {
            PublishedFundingVersion publishedFunding = _repo.PublishedFundingVersions.SelectMany(c => c.Value).FirstOrDefault(p => p.Value.Id == cosmosId).Value;

            return(Task.FromResult(publishedFunding));
        }
 public async Task RemovePublishedFundingVersionBlob(PublishedFundingVersion publishedFundingVersion)
 {
     await RemoveBlob(publishedFundingVersion.FundingId, "publishedfunding");
 }
Exemplo n.º 27
0
        protected override async Task <PublishedFundingVersion> GetPreviousPublishedFundingVersion(PublishedFundingVersion currentVersion,
                                                                                                   PublishedFundingUndoTaskContext taskContext)
        {
            LogInformation($"Querying latest earlier published funding version for '{taskContext.Parameters}'");

            UndoTaskDetails details = taskContext.PublishedFundingVersionDetails;

            return(await Cosmos.GetLatestEarlierPublishedFundingVersionFromVersion(details.FundingStreamId,
                                                                                   details.FundingPeriodId,
                                                                                   details.Version,
                                                                                   currentVersion.OrganisationGroupTypeIdentifier,
                                                                                   currentVersion.OrganisationGroupIdentifierValue,
                                                                                   currentVersion.GroupingReason));
        }
 public string GetFundingId(PublishedFundingVersion publishedFunding)
 {
     return($"{publishedFunding.FundingStreamId}-{publishedFunding.FundingPeriod.Id}-{publishedFunding.GroupingReason}-{publishedFunding.OrganisationGroupTypeCode}-{publishedFunding.OrganisationGroupIdentifierValue}-{publishedFunding.MajorVersion}_{publishedFunding.MinorVersion}");
 }
Exemplo n.º 29
0
        /// <summary>
        /// Generate instances of the PublishedFundingVersion to save into cosmos for the Organisation Group Results
        /// </summary>
        /// <param name="publishedFundingInput"></param>
        /// <param name="publishedProviders"></param>
        /// <returns></returns>
        public IEnumerable <(PublishedFunding, PublishedFundingVersion)> GeneratePublishedFunding(PublishedFundingInput publishedFundingInput,
                                                                                                  IEnumerable <PublishedProvider> publishedProviders)
        {
            Guard.ArgumentNotNull(publishedFundingInput, nameof(publishedFundingInput));
            Guard.ArgumentNotNull(publishedFundingInput.FundingPeriod, nameof(publishedFundingInput.FundingPeriod));
            Guard.ArgumentNotNull(publishedFundingInput.FundingStream, nameof(publishedFundingInput.FundingStream));
            Guard.ArgumentNotNull(publishedFundingInput.OrganisationGroupsToSave, nameof(publishedFundingInput.OrganisationGroupsToSave));
            Guard.ArgumentNotNull(publishedProviders, nameof(publishedProviders));
            Guard.ArgumentNotNull(publishedFundingInput.PublishingDates, nameof(publishedFundingInput.PublishingDates));
            Guard.ArgumentNotNull(publishedFundingInput.TemplateMetadataContents, nameof(publishedFundingInput.TemplateMetadataContents));
            Guard.IsNullOrWhiteSpace(publishedFundingInput.TemplateVersion, nameof(publishedFundingInput.TemplateVersion));
            Guard.IsNullOrWhiteSpace(publishedFundingInput.SpecificationId, nameof(publishedFundingInput.SpecificationId));

            IEnumerable <(PublishedFunding PublishedFunding, OrganisationGroupResult OrganisationGroupResult)> organisationGroupsToSave = publishedFundingInput.OrganisationGroupsToSave;

            TemplateMetadataContents templateMetadataContents = publishedFundingInput.TemplateMetadataContents;
            string        templateVersion = publishedFundingInput.TemplateVersion;
            FundingPeriod fundingPeriod   = publishedFundingInput.FundingPeriod;

            FundingValueAggregator fundingValueAggregator = new FundingValueAggregator();

            foreach ((PublishedFunding PublishedFunding, OrganisationGroupResult OrganisationGroupResult)organisationGroup in organisationGroupsToSave)
            {
                // TODO: extract interface
                IEnumerable <string> providerIds           = organisationGroup.OrganisationGroupResult.Providers.Select(p => p.ProviderId);
                IEnumerable <string> publishedProvidersIds = publishedProviders.Select(p => p.Current.ProviderId);

                List <PublishedProvider> publishedProvidersForOrganisationGroup = new List <PublishedProvider>(publishedProviders.Where(p
                                                                                                                                        => providerIds.Contains(p.Current.ProviderId)));
                List <PublishedProviderVersion> publishedProviderVersionsForOrganisationGroup = new List <PublishedProviderVersion>(
                    publishedProvidersForOrganisationGroup.Select(p => p.Current));

                IEnumerable <string> missingProviders = providerIds.Except(publishedProvidersIds);

                if (missingProviders.AnyWithNullCheck())
                {
                    string providerIdsString = string.Join(", ", missingProviders);
                    throw new Exception($"Missing PublishedProvider result for organisation group '{organisationGroup.OrganisationGroupResult.GroupReason}' '{organisationGroup.OrganisationGroupResult.GroupTypeCode}' '{organisationGroup.OrganisationGroupResult.GroupTypeIdentifier}' '{organisationGroup.OrganisationGroupResult.IdentifierValue}'. Provider IDs={providerIdsString}");
                }

                List <AggregateFundingLine> fundingLineAggregates = new List <AggregateFundingLine>(
                    fundingValueAggregator.GetTotals(templateMetadataContents, publishedProviderVersionsForOrganisationGroup));

                IEnumerable <Common.TemplateMetadata.Models.FundingLine> fundingLineDefinitions = templateMetadataContents.RootFundingLines.Flatten(_ => _.FundingLines) ??
                                                                                                  Enumerable.Empty <Common.TemplateMetadata.Models.FundingLine>();

                // Add in calculations in numerator/demoninator and percentagechange targets

                List <PublishingModels.FundingLine> fundingLines = GenerateFundingLines(fundingLineAggregates, fundingLineDefinitions);
                List <FundingCalculation>           calculations = GenerateCalculations(fundingLineAggregates.Flatten(_ => _.FundingLines)
                                                                                        .SelectMany(c => c.Calculations ?? Enumerable.Empty <AggregateFundingCalculation>()));

                decimal?totalFunding = publishedProviderVersionsForOrganisationGroup.Sum(_ => _.TotalFunding);

                PublishedFundingVersion publishedFundingVersion = new PublishedFundingVersion
                {
                    FundingStreamId   = publishedFundingInput.FundingStream.Id,
                    FundingStreamName = publishedFundingInput.FundingStream.Name,
                    TotalFunding      = totalFunding,
                    FundingPeriod     = new PublishedFundingPeriod
                    {
                        Type      = Enum.Parse <PublishedFundingPeriodType>(fundingPeriod.Type.GetValueOrDefault().ToString()),
                        Period    = fundingPeriod.Period,
                        EndDate   = fundingPeriod.EndDate,
                        StartDate = fundingPeriod.StartDate,
                        Name      = fundingPeriod.Name,
                    },
                    SpecificationId                     = publishedFundingInput.SpecificationId,
                    OrganisationGroupTypeCode           = organisationGroup.OrganisationGroupResult.GroupTypeCode.ToString(),
                    OrganisationGroupTypeIdentifier     = organisationGroup.OrganisationGroupResult.GroupTypeIdentifier.ToString(),
                    OrganisationGroupIdentifierValue    = organisationGroup.OrganisationGroupResult.IdentifierValue,
                    OrganisationGroupTypeClassification = organisationGroup.OrganisationGroupResult.GroupTypeClassification.ToString(),
                    OrganisationGroupName               = organisationGroup.OrganisationGroupResult.Name,
                    OrganisationGroupSearchableName     = organisationGroup.OrganisationGroupResult.SearchableName,
                    OrganisationGroupIdentifiers        = _mapper.Map <IEnumerable <PublishedOrganisationGroupTypeIdentifier> >(organisationGroup.OrganisationGroupResult.Identifiers),
                    FundingLines                 = fundingLines,
                    Calculations                 = calculations,
                    SchemaVersion                = templateMetadataContents.SchemaVersion,
                    Status                       = PublishedFundingStatus.Approved,
                    GroupingReason               = organisationGroup.OrganisationGroupResult.GroupReason.AsMatchingEnum <PublishingModels.GroupingReason>(),
                    ProviderFundings             = publishedProviderVersionsForOrganisationGroup.Select(_ => _.FundingId),
                    TemplateVersion              = templateVersion,
                    StatusChangedDate            = publishedFundingInput.PublishingDates.StatusChangedDate.TrimToTheSecond(),
                    EarliestPaymentAvailableDate = publishedFundingInput.PublishingDates.EarliestPaymentAvailableDate.TrimToTheMinute(),
                    ExternalPublicationDate      = publishedFundingInput.PublishingDates.ExternalPublicationDate.TrimToTheMinute(),
                };

                publishedFundingVersion.FundingId = _publishedFundingIdGeneratorResolver.GetService(templateMetadataContents.SchemaVersion).GetFundingId(publishedFundingVersion);

                PublishedFunding publishedFundingResult = organisationGroup.PublishedFunding;

                if (publishedFundingResult == null)
                {
                    publishedFundingResult = new PublishedFunding()
                    {
                        Current = publishedFundingVersion,
                    };
                }

                yield return(publishedFundingResult, publishedFundingVersion);
            }
        }
        public string GenerateContents(PublishedFundingVersion publishedFundingVersion,
                                       TemplateMetadataContents templateMetadataContents)
        {
            Guard.ArgumentNotNull(publishedFundingVersion, nameof(publishedFundingVersion));
            Guard.ArgumentNotNull(templateMetadataContents, nameof(templateMetadataContents));

            IEnumerable <TemplateFundingLine> fundingLines = templateMetadataContents.RootFundingLines?.Flatten(x => x.FundingLines);
            IEnumerable <Calculation>         calculations = fundingLines?.SelectMany(fl => fl.Calculations.Flatten(cal => cal.Calculations));

            SchemaJson contents = new SchemaJson
            {
                Schema        = "https://fundingschemas.blob.core.windows.net/schemas/logicalmodel-1.1.json#schema",
                SchemaVersion = publishedFundingVersion.SchemaVersion,
                Funding       = new
                {
                    publishedFundingVersion.TemplateVersion,
                    Id             = publishedFundingVersion.FundingId,
                    FundingVersion = $"{publishedFundingVersion.MajorVersion}_{publishedFundingVersion.MinorVersion}",
                    Status         = publishedFundingVersion.Status.ToString(),
                    FundingStream  = new
                    {
                        Code = publishedFundingVersion.FundingStreamId,
                        Name = publishedFundingVersion.FundingStreamName
                    },
                    FundingPeriod = new
                    {
                        publishedFundingVersion.FundingPeriod.Id,
                        publishedFundingVersion.FundingPeriod.Period,
                        publishedFundingVersion.FundingPeriod.Name,
                        Type = publishedFundingVersion.FundingPeriod.Type.ToString(),
                        publishedFundingVersion.FundingPeriod.StartDate,
                        publishedFundingVersion.FundingPeriod.EndDate
                    },
                    OrganisationGroup = new
                    {
                        GroupTypeCode           = publishedFundingVersion.OrganisationGroupTypeCode,
                        GroupTypeClassification = publishedFundingVersion.OrganisationGroupTypeClassification,
                        Name                = publishedFundingVersion.OrganisationGroupName,
                        SearchableName      = publishedFundingVersion.OrganisationGroupSearchableName,
                        GroupTypeIdentifier = new
                        {
                            Type  = publishedFundingVersion.OrganisationGroupTypeIdentifier,
                            Value = publishedFundingVersion.OrganisationGroupIdentifierValue
                        },
                        Identifiers = publishedFundingVersion.OrganisationGroupIdentifiers?.Select(groupIdentifier => new
                        {
                            groupIdentifier.Type,
                            groupIdentifier.Value
                        }).ToArray()
                    },
                    FundingValue = new
                    {
                        TotalValue   = publishedFundingVersion.TotalFunding,
                        FundingLines = fundingLines?.DistinctBy(x => x.TemplateLineId).Select(rootFundingLine =>
                                                                                              BuildSchemaJsonFundingLines(publishedFundingVersion.FundingLines,
                                                                                                                          rootFundingLine)).ToDictionary(_ => _.TemplateLineId),
                        Calculations = calculations?.DistinctBy(x => x.TemplateCalculationId).Where(IsAggregationOrHasChildCalculations).Select(calculation =>
                                                                                                                                                BuildSchemaJsonCalculations(publishedFundingVersion.Calculations,
                                                                                                                                                                            calculation,
                                                                                                                                                                            publishedFundingVersion.OrganisationGroupTypeIdentifier,
                                                                                                                                                                            publishedFundingVersion.OrganisationGroupIdentifierValue)).Where(_ => _ != null).ToDictionary(_ => _.TemplateCalculationId)
                    },
                    ProviderFundings = publishedFundingVersion.ProviderFundings.ToArray(),
                    publishedFundingVersion.GroupingReason,
                    publishedFundingVersion.StatusChangedDate,
                    publishedFundingVersion.ExternalPublicationDate,
                    publishedFundingVersion.EarliestPaymentAvailableDate
                }
            };

            return(contents.AsJson());
        }