/// <summary> /// Returns the target organisations groups details, based on the configuration of the funding output. /// eg will lookup /// - Parlimentary Consituency code and name, based on the Parlimentary Consituency code for information /// </summary> /// <param name="organisationGroupLookupParameters">Grouping Lookup Parameters</param> /// <param name="providersInGroup">Providers in group</param> /// <returns></returns> public async Task <TargetOrganisationGroup> GetTargetProviderDetails(OrganisationGroupLookupParameters organisationGroupLookupParameters, GroupingReason groupReason, IEnumerable <Provider> providersInGroup) { Guard.ArgumentNotNull(organisationGroupLookupParameters, nameof(organisationGroupLookupParameters)); if (groupReason == GroupingReason.Payment || groupReason == GroupingReason.Contracting || groupReason == GroupingReason.Indicative) { Guard.IsNullOrWhiteSpace(organisationGroupLookupParameters.IdentifierValue, nameof(organisationGroupLookupParameters.IdentifierValue)); Guard.IsNullOrWhiteSpace(organisationGroupLookupParameters.ProviderVersionId, nameof(organisationGroupLookupParameters.ProviderVersionId)); return(await GetTargetProviderDetailsForPaymentOrContracting(organisationGroupLookupParameters.IdentifierValue, organisationGroupLookupParameters.OrganisationGroupTypeCode, organisationGroupLookupParameters.ProviderVersionId, providersInGroup)); } else { Guard.ArgumentNotNull(organisationGroupLookupParameters.GroupTypeIdentifier, nameof(organisationGroupLookupParameters.GroupTypeIdentifier)); // Get the first of the scoped providers, then obtain the ID and name from the properties of that. Provider firstProvider = providersInGroup.First(); return(new TargetOrganisationGroup() { Name = GetOrganisationGroupName(firstProvider, organisationGroupLookupParameters.GroupTypeIdentifier.Value), Identifier = GetOrganisationGroupIdentifier(firstProvider, organisationGroupLookupParameters.GroupTypeIdentifier.Value), Identifiers = organisationGroupLookupParameters.OrganisationGroupTypeCode.HasValue ? GenerateIdentifiersForProvider(groupReason, organisationGroupLookupParameters.OrganisationGroupTypeCode.Value, firstProvider) : null }); } }
private async Task <TargetOrganisationGroup> ObtainTargetOrganisationGroupFromProviderData(FundingConfiguration fundingConfiguration, string providerVersionId, OrganisationGroupingConfiguration grouping, IGrouping <string, Common.ApiClient.Providers.Models.Provider> providerGrouping, TargetOrganisationGroup targetOrganisationGroup) { OrganisationGroupLookupParameters organisationGroupLookupParameters = new OrganisationGroupLookupParameters { IdentifierValue = providerGrouping.Key, OrganisationGroupTypeCode = grouping.OrganisationGroupTypeCode, ProviderVersionId = providerVersionId, GroupTypeIdentifier = grouping.GroupTypeIdentifier }; targetOrganisationGroup = await _organisationGroupTargetProviderLookup.GetTargetProviderDetails(organisationGroupLookupParameters, grouping.GroupingReason, providerGrouping); return(targetOrganisationGroup); }
public async Task <IEnumerable <OrganisationGroupResult> > GenerateOrganisationGroup(FundingConfiguration fundingConfiguration, IEnumerable <Provider> scopedProviders, string providerVersionId) { Guard.ArgumentNotNull(fundingConfiguration, nameof(fundingConfiguration)); Guard.ArgumentNotNull(scopedProviders, nameof(scopedProviders)); Guard.IsNullOrWhiteSpace(providerVersionId, nameof(providerVersionId)); List <OrganisationGroupResult> results = new List <OrganisationGroupResult>(); foreach (OrganisationGroupingConfiguration grouping in fundingConfiguration.OrganisationGroupings) { // Get the provider attribute required to group Func <Provider, string> providerFilterAttribute = GetProviderFieldForGrouping(grouping.GroupTypeIdentifier, grouping.OrganisationGroupTypeCode, grouping.GroupingReason); // Filter providers based on provider type and subtypes IEnumerable <Provider> providersForGroup = grouping.ProviderTypeMatch.IsNullOrEmpty() ? scopedProviders : scopedProviders.Where(_ => ShouldIncludeProvider(_, grouping.ProviderTypeMatch)); // Group providers by the fields and discard any providers with null values for that field IEnumerable <IGrouping <string, Provider> > groupedProviders = providersForGroup.GroupBy(providerFilterAttribute); // Common values for all groups Enums.OrganisationGroupTypeClassification organisationGroupTypeClassification = grouping.GroupingReason == GroupingReason.Payment ? Enums.OrganisationGroupTypeClassification.LegalEntity : Enums.OrganisationGroupTypeClassification.GeographicalBoundary; Enums.OrganisationGroupTypeCode organisationGroupTypeCode = grouping.OrganisationGroupTypeCode.AsMatchingEnum <Enums.OrganisationGroupTypeCode>(); // Generate Organisation Group results based on the grouped providers foreach (IGrouping <string, Provider> providerGrouping in groupedProviders) { // Ignore providers without the matching data in the key if (string.IsNullOrWhiteSpace(providerGrouping.Key)) { continue; } TargetOrganisationGroup targetOrganisationGroup = null; OrganisationGroupLookupParameters organisationGroupLookupParameters = new OrganisationGroupLookupParameters { IdentifierValue = providerGrouping.Key, OrganisationGroupTypeCode = grouping.OrganisationGroupTypeCode, ProviderVersionId = providerVersionId, GroupTypeIdentifier = grouping.GroupTypeIdentifier }; targetOrganisationGroup = await _organisationGroupTargetProviderLookup.GetTargetProviderDetails(organisationGroupLookupParameters, grouping.GroupingReason, providerGrouping); if (targetOrganisationGroup == null) { // TODO: improve logging throw new Exception($"Target Organisation Group could not be found for identifier '{providerGrouping.Key}'"); } OrganisationGroupResult organisationGroupResult = new OrganisationGroupResult() { GroupTypeClassification = organisationGroupTypeClassification, GroupTypeCode = organisationGroupTypeCode, GroupTypeIdentifier = grouping.GroupTypeIdentifier.AsMatchingEnum <Enums.OrganisationGroupTypeIdentifier>(), GroupReason = grouping.GroupingReason.AsMatchingEnum <Enums.OrganisationGroupingReason>(), IdentifierValue = targetOrganisationGroup.Identifier, Name = targetOrganisationGroup.Name, Identifiers = targetOrganisationGroup.Identifiers, SearchableName = GenerateSearchableName(targetOrganisationGroup.Name), Providers = providerGrouping, }; results.Add(organisationGroupResult); } } return(results); }