Exemplo n.º 1
0
        private SlotSimple GetFreeSlotsFromDatabase(string responseStream)
        {
            var slotSimple = new SlotSimple()
            {
                CurrentSlotEntrySimple = new List <SlotEntrySimple>(),
                PastSlotEntrySimple    = new List <SlotEntrySimple>(),
                Issue = new List <Issue>()
            };

            var results = JsonConvert.DeserializeObject <Bundle>(responseStream);

            var slotResources = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Slot).ToList();

            if (slotResources == null || slotResources?.Count == 0)
            {
                return(slotSimple);
            }

            var practitionerResources = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Practitioner).ToList();
            var locationResources     = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Location).ToList();
            var scheduleResources     = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Schedule).ToList();

            var slotList = (from slot in slotResources?.Where(s => s.resource != null)
                            let practitioner = GetPractitionerDetails(slot.resource.schedule.reference, scheduleResources, practitionerResources)
                                               let location = GetLocation(slot.resource.schedule.reference, scheduleResources, locationResources)
                                                              let schedule = GetSchedule(slot.resource.schedule.reference, scheduleResources)
                                                                             select new SlotEntrySimple
            {
                AppointmentDate = slot.resource.start.GetValueOrDefault().DateTime,
                SessionName = schedule.resource.serviceCategory?.text,
                StartTime = slot.resource.start.GetValueOrDefault().DateTime,
                Duration = slot.resource.start.DurationBetweenTwoDates(slot.resource.end),
                SlotType = slot.resource.serviceType.FirstOrDefault()?.text,
                DeliveryChannel = slot.resource.extension?.FirstOrDefault()?.valueCode,
                PractitionerGivenName = practitioner?.name?.FirstOrDefault()?.given?.FirstOrDefault(),
                PractitionerFamilyName = practitioner?.name?.FirstOrDefault()?.family,
                PractitionerPrefix = practitioner?.name?.FirstOrDefault()?.prefix?.FirstOrDefault(),
                PractitionerRole = schedule.resource.extension?.FirstOrDefault()?.valueCodeableConcept?.coding?.FirstOrDefault()?.display,
                PractitionerGender = practitioner?.gender,
                LocationName = location?.name,
                LocationAddressLines = location?.address?.line,
                LocationAddressLinesAsString = AddressBuilder.GetFullAddress(location?.address?.line, location?.address?.district, location?.address?.city, location?.address?.postalCode, location?.address?.country),
                LocationCity = location?.address?.city,
                LocationCountry = location?.address?.country,
                LocationDistrict = location?.address?.district,
                LocationPostalCode = location?.address?.postalCode,
                SlotInPast = slot.resource.start.GetValueOrDefault().DateTime <= _currentDateTime
            }).OrderBy(z => z.LocationName)
                           .ThenBy(s => s.AppointmentDate)
                           .ThenBy(s => s.StartTime);

            slotSimple.CurrentSlotEntrySimple.AddRange(slotList.Where(x => !x.SlotInPast));
            slotSimple.PastSlotEntrySimple.AddRange(slotList.Where(x => x.SlotInPast));

            return(slotSimple);
        }
        private async Task <SlotSimple> GetFreeSlots(RequestParameters requestParameters, DateTime startDate, DateTime endDate, string baseAddress)
        {
            var getRequest = new HttpRequestMessage();

            try
            {
                var spineMessageType = (_configurationService.GetSpineMessageTypes()).FirstOrDefault(x =>
                                                                                                     x.SpineMessageTypeId == (int)SpineMessageTypes.GpConnectSearchFreeSlots);
                requestParameters.SpineMessageTypeId = (int)SpineMessageTypes.GpConnectSearchFreeSlots;
                requestParameters.InteractionId      = spineMessageType?.InteractionId;

                var stopWatch = new Stopwatch();
                stopWatch.Start();
                _spineMessage.SpineMessageTypeId = requestParameters.SpineMessageTypeId;

                var client = _httpClientFactory.CreateClient("GpConnectClient");

                client.Timeout = new TimeSpan(0, 0, 30);
                AddRequiredRequestHeaders(requestParameters, client);
                _spineMessage.RequestHeaders = client.DefaultRequestHeaders.ToString();
                var requestUri = new Uri($"{requestParameters.EndpointAddressWithSpineSecureProxy}/Slot");
                var uriBuilder = AddQueryParameters(requestParameters, startDate, endDate, requestUri);

                getRequest.Method     = HttpMethod.Get;
                getRequest.RequestUri = uriBuilder.Uri;

                var response = await client.SendAsync(getRequest);

                var responseStream = await response.Content.ReadAsStringAsync();

                _spineMessage.ResponsePayload = responseStream;
                _spineMessage.ResponseStatus  = response.StatusCode.ToString();
                _spineMessage.RequestPayload  = getRequest.ToString();
                _spineMessage.ResponseHeaders = response.Headers.ToString();

                stopWatch.Stop();
                _spineMessage.RoundTripTimeMs = stopWatch.ElapsedMilliseconds;
                _logService.AddSpineMessageLog(_spineMessage);

                var slotSimple = new SlotSimple()
                {
                    CurrentSlotEntrySimple = new List <SlotEntrySimple>(),
                    PastSlotEntrySimple    = new List <SlotEntrySimple>(),
                    Issue = new List <Issue>()
                };

                var results = JsonConvert.DeserializeObject <Bundle>(responseStream);

                if (results.Issue?.Count > 0)
                {
                    slotSimple.Issue = results.Issue;
                    return(slotSimple);
                }

                var slotResources = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Slot).ToList();
                if (slotResources == null || slotResources?.Count == 0)
                {
                    return(slotSimple);
                }

                var practitionerResources = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Practitioner).ToList();
                var locationResources     = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Location).ToList();
                var scheduleResources     = results.entry?.Where(x => x.resource.resourceType == ResourceTypes.Schedule).ToList();

                var slotList = (from slot in slotResources?.Where(s => s.resource != null)
                                let practitioner = GetPractitionerDetails(slot.resource.schedule.reference, scheduleResources, practitionerResources)
                                                   let location = GetLocation(slot.resource.schedule.reference, scheduleResources, locationResources)
                                                                  let schedule = GetSchedule(slot.resource.schedule.reference, scheduleResources)
                                                                                 select new SlotEntrySimple
                {
                    AppointmentDate = slot.resource.start.GetValueOrDefault().DateTime,
                    SessionName = schedule.resource.serviceCategory?.text,
                    StartTime = slot.resource.start.GetValueOrDefault().DateTime,
                    Duration = slot.resource.start.DurationBetweenTwoDates(slot.resource.end),
                    SlotType = slot.resource.serviceType.FirstOrDefault()?.text,
                    DeliveryChannel = slot.resource.extension?.FirstOrDefault()?.valueCode,
                    PractitionerGivenName = practitioner?.name?.FirstOrDefault()?.given?.FirstOrDefault(),
                    PractitionerFamilyName = practitioner?.name?.FirstOrDefault()?.family,
                    PractitionerPrefix = practitioner?.name?.FirstOrDefault()?.prefix?.FirstOrDefault(),
                    PractitionerRole = schedule.resource.extension?.FirstOrDefault()?.valueCodeableConcept?.coding?.FirstOrDefault()?.display,
                    PractitionerGender = practitioner?.gender,
                    LocationName = location?.name,
                    LocationAddressLines = location?.address?.line,
                    LocationAddressLinesAsString = AddressBuilder.GetFullAddress(location?.address?.line, location?.address?.district, location?.address?.city, location?.address?.postalCode, location?.address?.country),
                    LocationCity = location?.address?.city,
                    LocationCountry = location?.address?.country,
                    LocationDistrict = location?.address?.district,
                    LocationPostalCode = location?.address?.postalCode,
                    SlotInPast = slot.resource.start.GetValueOrDefault().DateTime <= _currentDateTime
                }).OrderBy(z => z.LocationName)
                               .ThenBy(s => s.AppointmentDate)
                               .ThenBy(s => s.StartTime);

                slotSimple.CurrentSlotEntrySimple.AddRange(slotList.Where(x => !x.SlotInPast));
                slotSimple.PastSlotEntrySimple.AddRange(slotList.Where(x => x.SlotInPast));

                return(slotSimple);
            }
            catch (TimeoutException timeoutException)
            {
                _logger.LogError(timeoutException, "A timeout error has occurred");
                throw;
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, $"An error occurred in trying to execute a GET request - {getRequest}");
                throw;
            }
        }