Пример #1
0
        private async Task PopulateSearchResults(Spine providerGpConnectDetails, Organisation providerOrganisationDetails,
                                                 Spine consumerGpConnectDetails, Organisation consumerOrganisationDetails)
        {
            var requestParameters = _tokenService.ConstructRequestParameters(
                _contextAccessor.HttpContext.GetAbsoluteUri(), providerGpConnectDetails, providerOrganisationDetails,
                consumerGpConnectDetails, consumerOrganisationDetails, (int)SpineMessageTypes.GpConnectSearchFreeSlots);
            var startDate = Convert.ToDateTime(SelectedDateRange.Split(":")[0]);
            var endDate   = Convert.ToDateTime(SelectedDateRange.Split(":")[1]);

            if (requestParameters != null)
            {
                //Step 3 - CALL PROVIDER METADATA ENDPOINT
                //Get capability statement
                var capabilityStatement = await _queryExecutionService.ExecuteFhirCapabilityStatement(requestParameters, providerGpConnectDetails.ssp_hostname);

                CapabilityStatementOk = (capabilityStatement.Issue?.Count == 0 || capabilityStatement.Issue == null);

                if (CapabilityStatementOk)
                {
                    var searchResults = await _queryExecutionService.ExecuteFreeSlotSearch(requestParameters, startDate, endDate, providerGpConnectDetails.ssp_hostname);

                    SlotSearchOk = searchResults?.Issue == null;

                    if (SlotSearchOk)
                    {
                        SearchResults = new List <List <SlotEntrySimple> >();
                        var locationGrouping = searchResults?.SlotEntrySimple.GroupBy(l => l.LocationName)
                                               .Select(grp => grp.ToList()).ToList();
                        SearchResultsCount = searchResults?.SlotEntrySimple.Count;

                        if (locationGrouping != null)
                        {
                            SearchResults.AddRange(locationGrouping);
                        }
                    }
                    else
                    {
                        ProviderErrorDisplay     = searchResults.Issue.FirstOrDefault()?.Details.Coding.FirstOrDefault()?.Display;
                        ProviderErrorCode        = searchResults.Issue.FirstOrDefault()?.Details.Coding.FirstOrDefault()?.Code;
                        ProviderErrorDiagnostics = StringExtensions.Coalesce(searchResults.Issue.FirstOrDefault()?.Diagnostics, searchResults.Issue.FirstOrDefault()?.Details.Text);
                        _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHSENDINGMESSAGETOPROVIDERSYSTEMTEXT, ProviderErrorDisplay, ProviderErrorCode));
                    }
                }
                else
                {
                    if (capabilityStatement?.Issue != null)
                    {
                        ProviderErrorDisplay     = capabilityStatement?.Issue?.FirstOrDefault()?.Details.Coding.FirstOrDefault()?.Display;
                        ProviderErrorCode        = capabilityStatement?.Issue?.FirstOrDefault()?.Details.Coding.FirstOrDefault()?.Code;
                        ProviderErrorDiagnostics = StringExtensions.Coalesce(capabilityStatement?.Issue?.FirstOrDefault()?.Diagnostics, capabilityStatement?.Issue.FirstOrDefault()?.Details.Text);
                        _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHSENDINGMESSAGETOPROVIDERSYSTEMTEXT, ProviderErrorDisplay, ProviderErrorCode));
                    }
                }
            }
        }
        private async Task PopulateSearchResults(Spine providerSpineDetails, Organisation providerOrganisationDetails, Spine consumerEnablement, Organisation consumerOrganisationDetails, string consumerOrganisationType = "")
        {
            var requestParameters = _tokenService.ConstructRequestParameters(
                _contextAccessor.HttpContext.GetAbsoluteUri(), providerSpineDetails, providerOrganisationDetails,
                consumerEnablement, consumerOrganisationDetails, (int)SpineMessageTypes.GpConnectSearchFreeSlots, consumerOrganisationType);
            var startDate = Convert.ToDateTime(SelectedDateRange.Split(":")[0]);
            var endDate   = Convert.ToDateTime(SelectedDateRange.Split(":")[1]);

            if (requestParameters != null)
            {
                var capabilityStatement = await _queryExecutionService.ExecuteFhirCapabilityStatement(requestParameters, providerSpineDetails.SspHostname);

                CapabilityStatementOk = capabilityStatement.CapabilityStatementNoIssues;

                if (CapabilityStatementOk)
                {
                    var searchResults = await _queryExecutionService.ExecuteFreeSlotSearch(requestParameters, startDate, endDate, providerSpineDetails.SspHostname, User.GetClaimValue("UserId").StringToInteger());

                    SlotSearchOk = searchResults.SlotSearchNoIssues;

                    if (SlotSearchOk)
                    {
                        SearchExportId    = searchResults.SearchExportId;
                        SearchResults     = new List <List <SlotEntrySimple> >();
                        SearchResultsPast = new List <List <SlotEntrySimple> >();

                        SearchResultsTotalCount   = searchResults.SlotCount;
                        SearchResultsCurrentCount = searchResults.CurrentSlotCount;
                        SearchResultsPastCount    = searchResults.PastSlotCount;

                        SearchResults.AddRange(searchResults.CurrentSlotEntriesByLocationGrouping);
                        SearchResultsPast.AddRange(searchResults.PastSlotEntriesByLocationGrouping);
                    }
                    else
                    {
                        ProviderErrorDisplay     = searchResults.ProviderError;
                        ProviderErrorCode        = searchResults.ProviderErrorCode;
                        ProviderErrorDiagnostics = searchResults.ProviderErrorDiagnostics;
                        _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHSENDINGMESSAGETOPROVIDERSYSTEMTEXT, ProviderErrorDisplay, ProviderErrorCode));
                    }
                }
                else
                {
                    ProviderErrorDisplay     = capabilityStatement.ProviderError;
                    ProviderErrorCode        = capabilityStatement.ProviderErrorCode;
                    ProviderErrorDiagnostics = capabilityStatement.ProviderErrorDiagnostics;
                    _auditSearchIssues.Add(string.Format(SearchConstants.ISSUEWITHSENDINGMESSAGETOPROVIDERSYSTEMTEXT, ProviderErrorDisplay, ProviderErrorCode));
                }
            }
        }
        private async Task <List <SlotEntrySummary> > PopulateSearchResultsMulti(List <SpineList> providerGpConnectDetails, List <OrganisationList> providerOrganisationDetails,
                                                                                 List <SpineList> consumerGpConnectDetails, List <OrganisationList> consumerOrganisationDetails, string consumerOrganisationType = "")
        {
            var createdSearchGroup = _applicationService.AddSearchGroup(new DTO.Request.Application.SearchGroup
            {
                UserSessionId      = User.GetClaimValue("UserSessionId").StringToInteger(),
                ProviderOdsTextbox = ProviderOdsCode,
                ConsumerOdsTextbox = ConsumerOdsCode,
                SearchDateRange    = SelectedDateRange,
                ConsumerOrganisationTypeDropdown = SelectedOrganisationType
            });

            _logger.LogInformation("Executing PopulateSearchResultsMulti - generating Request Parameters");

            SearchGroupId = createdSearchGroup.SearchGroupId;

            var slotEntrySummary = new List <SlotEntrySummary>();

            var requestParameters = await _tokenService.ConstructRequestParameters(_contextAccessor.HttpContext.GetAbsoluteUri(), providerGpConnectDetails, providerOrganisationDetails,
                                                                                   consumerGpConnectDetails, consumerOrganisationDetails, (int)SpineMessageTypes.GpConnectSearchFreeSlots, consumerOrganisationType);

            var startDate = Convert.ToDateTime(SelectedDateRange.Split(":")[0]);
            var endDate   = Convert.ToDateTime(SelectedDateRange.Split(":")[1]);

            var providerOdsCount = ProviderOdsCodeAsList.Count;
            var consumerOdsCount = ConsumerOdsCodeAsList.Count;
            List <SlotEntrySummaryCount>                slotSearchSummaryList                = new List <SlotEntrySummaryCount>();
            List <OrganisationErrorCodeOrDetail>        organisationErrorCodeOrDetail        = new List <OrganisationErrorCodeOrDetail>();
            List <CapabilityStatementErrorCodeOrDetail> capabilityStatementErrorCodeOrDetail = new List <CapabilityStatementErrorCodeOrDetail>();

            if (providerOdsCount > consumerOdsCount)
            {
                _logger.LogInformation("Executing PopulateSearchResultsMulti - Getting capability statement list");

                var consumerCode            = ConsumerOdsCodeAsList.Count == 0 ? null : ConsumerOdsCodeAsList[0];
                var capabilityStatementList = await _queryExecutionService.ExecuteFhirCapabilityStatement(requestParameters);

                for (var i = 0; i < providerOdsCount; i++)
                {
                    _stopwatch.Start();
                    var errorCodeOrDetail = GetOrganisationErrorCodeOrDetail(ProviderOdsCodeAsList[i], consumerCode, providerGpConnectDetails, providerOrganisationDetails, consumerGpConnectDetails, consumerOrganisationDetails, consumerOrganisationType);
                    organisationErrorCodeOrDetail.Add(errorCodeOrDetail);

                    var capabilityStatementResult = GetCapabilityStatementErrorCodeOrDetail(ProviderOdsCodeAsList[i], capabilityStatementList);
                    capabilityStatementErrorCodeOrDetail.Add(capabilityStatementResult);
                }

                var slotCount = 0;

                if (requestParameters != null && organisationErrorCodeOrDetail != null && capabilityStatementErrorCodeOrDetail != null)
                {
                    slotSearchSummaryList = await _queryExecutionService.ExecuteFreeSlotSearchSummary(organisationErrorCodeOrDetail, requestParameters, startDate, endDate, SearchType.Provider);

                    for (var i = 0; i < providerOdsCount; i++)
                    {
                        var organisationErrorCodeOrDetailForCode        = organisationErrorCodeOrDetail.FirstOrDefault(x => x.SuppliedProviderOdsCode == ProviderOdsCodeAsList[i]);
                        var capabilityStatementErrorCodeOrDetailForCode = capabilityStatementErrorCodeOrDetail.FirstOrDefault(x => x.SuppliedProviderOdsCode == ProviderOdsCodeAsList[i]);

                        if (organisationErrorCodeOrDetailForCode.errorSource == ErrorCode.None)
                        {
                            var slotSearchErrorCodeOrDetail = GetSlotSearchErrorCodeOrDetail(ProviderOdsCodeAsList[i], slotSearchSummaryList);
                            organisationErrorCodeOrDetailForCode.errorSource = slotSearchErrorCodeOrDetail.Item1;
                            organisationErrorCodeOrDetailForCode.details     = AppendAdditionalDetails(slotSearchErrorCodeOrDetail.Item2, organisationErrorCodeOrDetailForCode.additionalDetails);
                            slotCount = slotSearchErrorCodeOrDetail.Item3.GetValueOrDefault();
                        }
                        else if (organisationErrorCodeOrDetailForCode.errorSource == ErrorCode.None && capabilityStatementErrorCodeOrDetailForCode.errorSource != ErrorCode.None)
                        {
                            organisationErrorCodeOrDetailForCode.errorSource = capabilityStatementErrorCodeOrDetailForCode.errorSource;
                            organisationErrorCodeOrDetailForCode.details     = capabilityStatementErrorCodeOrDetailForCode.details;
                            slotCount = 0;
                        }

                        _stopwatch.Stop();

                        var searchResultToAdd = new SearchResult
                        {
                            SearchGroupId            = createdSearchGroup.SearchGroupId,
                            ProviderCode             = ProviderOdsCodeAsList[i],
                            ConsumerCode             = consumerCode,
                            ConsumerOrganisationType = OrganisationTypes.FirstOrDefault(x => x.Value == consumerOrganisationType).Text,
                            ErrorCode             = (int)organisationErrorCodeOrDetailForCode.errorSource,
                            Details               = organisationErrorCodeOrDetailForCode.details,
                            ProviderPublisher     = organisationErrorCodeOrDetailForCode.providerSpine?.ProductName,
                            SearchDurationSeconds = _stopwatch.Elapsed.TotalSeconds
                        };

                        _stopwatch.Reset();

                        if (slotSearchSummaryList != null)
                        {
                            var spineMessageId = slotSearchSummaryList.FirstOrDefault(x => x.OdsCode == ProviderOdsCodeAsList[i])?.SpineMessageId;
                            searchResultToAdd.SpineMessageId = spineMessageId;
                        }

                        var searchResult = _applicationService.AddSearchResult(searchResultToAdd);

                        slotEntrySummary.Add(new SlotEntrySummary
                        {
                            ProviderLocationName     = organisationErrorCodeOrDetailForCode.providerOrganisation?.OrganisationLocation,
                            ProviderOdsCode          = ProviderOdsCodeAsList[i],
                            ConsumerLocationName     = organisationErrorCodeOrDetailForCode.consumerOrganisation?.OrganisationLocation,
                            ConsumerOdsCode          = consumerCode,
                            ConsumerOrganisationType = OrganisationTypes.FirstOrDefault(x => x.Value == consumerOrganisationType).Text,
                            SearchSummaryDetail      = organisationErrorCodeOrDetailForCode.details,
                            ProviderPublisher        = organisationErrorCodeOrDetailForCode.providerSpine?.ProductName,
                            SearchResultId           = searchResult.SearchResultId,
                            SearchGroupId            = searchResultToAdd.SearchGroupId,
                            DetailsEnabled           = (organisationErrorCodeOrDetailForCode.errorSource == ErrorCode.None && slotCount > 0),
                            DisplayProvider          = organisationErrorCodeOrDetailForCode.providerOrganisation != null,
                            DisplayConsumer          = organisationErrorCodeOrDetailForCode.consumerOrganisation != null,
                            DisplayClass             = (organisationErrorCodeOrDetailForCode.errorSource != ErrorCode.None) ? "nhsuk-slot-summary-error" : "nhsuk-slot-summary"
                        });
                    }
                }
            }
            else if (consumerOdsCount > providerOdsCount)
            {
                for (var i = 0; i < consumerOdsCount; i++)
                {
                    _stopwatch.Start();
                    var errorCodeOrDetail = GetOrganisationErrorCodeOrDetail(ProviderOdsCodeAsList[0], ConsumerOdsCodeAsList[i], providerGpConnectDetails, providerOrganisationDetails, consumerGpConnectDetails, consumerOrganisationDetails, consumerOrganisationType);
                    organisationErrorCodeOrDetail.Add(errorCodeOrDetail);
                }

                var capabilityStatementList = await _queryExecutionService.ExecuteFhirCapabilityStatement(requestParameters);

                var capabilityStatementResult = GetCapabilityStatementErrorCodeOrDetail(ProviderOdsCodeAsList[0], capabilityStatementList);
                capabilityStatementErrorCodeOrDetail.Add(capabilityStatementResult);

                var slotCount = 0;

                if (requestParameters != null && organisationErrorCodeOrDetail != null && capabilityStatementErrorCodeOrDetail != null)
                {
                    slotSearchSummaryList = await _queryExecutionService.ExecuteFreeSlotSearchSummary(organisationErrorCodeOrDetail, requestParameters, startDate, endDate, SearchType.Consumer);

                    for (var i = 0; i < consumerOdsCount; i++)
                    {
                        var organisationErrorCodeOrDetailForCode        = organisationErrorCodeOrDetail.FirstOrDefault(x => x.SuppliedConsumerOdsCode == ConsumerOdsCodeAsList[i]);
                        var capabilityStatementErrorCodeOrDetailForCode = capabilityStatementErrorCodeOrDetail.FirstOrDefault(x => x.SuppliedConsumerOdsCode == ConsumerOdsCodeAsList[i]);

                        if (organisationErrorCodeOrDetailForCode.errorSource == ErrorCode.None)
                        {
                            var slotSearchErrorCodeOrDetail = GetSlotSearchErrorCodeOrDetail(ConsumerOdsCodeAsList[i], slotSearchSummaryList);
                            organisationErrorCodeOrDetailForCode.errorSource = slotSearchErrorCodeOrDetail.Item1;
                            organisationErrorCodeOrDetailForCode.details     = AppendAdditionalDetails(slotSearchErrorCodeOrDetail.Item2, organisationErrorCodeOrDetailForCode.additionalDetails);
                            slotCount = slotSearchErrorCodeOrDetail.Item3.GetValueOrDefault();
                        }
                        else if (organisationErrorCodeOrDetailForCode.errorSource == ErrorCode.None && capabilityStatementErrorCodeOrDetailForCode.errorSource != ErrorCode.None)
                        {
                            organisationErrorCodeOrDetailForCode.errorSource = capabilityStatementErrorCodeOrDetailForCode.errorSource;
                            organisationErrorCodeOrDetailForCode.details     = capabilityStatementErrorCodeOrDetailForCode.details;
                            slotCount = 0;
                        }

                        _stopwatch.Stop();

                        var searchResultToAdd = new SearchResult
                        {
                            SearchGroupId            = createdSearchGroup.SearchGroupId,
                            ProviderCode             = ProviderOdsCodeAsList[0],
                            ConsumerCode             = ConsumerOdsCodeAsList[i],
                            ConsumerOrganisationType = OrganisationTypes.FirstOrDefault(x => x.Value == consumerOrganisationType).Text,
                            ErrorCode             = (int)organisationErrorCodeOrDetailForCode.errorSource,
                            Details               = organisationErrorCodeOrDetailForCode.details,
                            ProviderPublisher     = organisationErrorCodeOrDetailForCode.providerSpine?.ProductName,
                            SearchDurationSeconds = _stopwatch.Elapsed.TotalSeconds,
                        };

                        _stopwatch.Reset();

                        if (slotSearchSummaryList != null)
                        {
                            var spineMessageId = slotSearchSummaryList.FirstOrDefault(x => x.OdsCode == ConsumerOdsCodeAsList[i])?.SpineMessageId;
                            searchResultToAdd.SpineMessageId = spineMessageId;
                        }

                        var searchResult = _applicationService.AddSearchResult(searchResultToAdd);

                        slotEntrySummary.Add(new SlotEntrySummary
                        {
                            ProviderLocationName     = organisationErrorCodeOrDetailForCode.providerOrganisation?.OrganisationLocation,
                            ProviderOdsCode          = ProviderOdsCodeAsList[0],
                            ConsumerLocationName     = organisationErrorCodeOrDetailForCode.consumerOrganisation?.OrganisationLocation,
                            ConsumerOdsCode          = ConsumerOdsCodeAsList[i],
                            ConsumerOrganisationType = OrganisationTypes.FirstOrDefault(x => x.Value == consumerOrganisationType).Text,
                            SearchSummaryDetail      = organisationErrorCodeOrDetailForCode.details,
                            ProviderPublisher        = organisationErrorCodeOrDetailForCode.providerSpine?.ProductName,
                            SearchResultId           = searchResult.SearchResultId,
                            DetailsEnabled           = (organisationErrorCodeOrDetailForCode.errorSource == ErrorCode.None && slotCount > 0),
                            DisplayProvider          = organisationErrorCodeOrDetailForCode.providerOrganisation != null,
                            DisplayConsumer          = organisationErrorCodeOrDetailForCode.consumerOrganisation != null,
                            DisplayClass             = (organisationErrorCodeOrDetailForCode.errorSource != ErrorCode.None) ? "nhsuk-slot-summary-error" : "nhsuk-slot-summary"
                        });
                    }
                }
            }

            _applicationService.UpdateSearchGroup(SearchGroupId);

            return(slotEntrySummary.OrderBy(x => x.SearchResultId).ToList());
        }