Exemplo n.º 1
0
        public async Task <IActionResult> SendManifest([FromBody] SendManifestPackageDTO packageDto)
        {
            if (!packageDto.IsValid())
            {
                return(BadRequest());
            }

            string version = GetType().Assembly.GetName().Version.ToString();
            await _mediator.Publish(new ExtractSent("MigrationService", version));

            try
            {
                var result = await _mgsSendService.SendManifestAsync(packageDto);

                return(Ok(result));
            }
            catch (Exception e)
            {
                var msg = $"Error sending  Manifest {e.Message}";
                Log.Error(e, msg);
                return(StatusCode(500, msg));
            }
        }
Exemplo n.º 2
0
        public async Task <List <SendManifestResponse> > SendManifestAsync(SendManifestPackageDTO sendTo, ManifestMessageBag manifestMessage, string version)
        {
            var responses = new List <SendManifestResponse>();
            await _mediator.Publish(new HandshakeStart("MPISendStart", version, manifestMessage.Session));

            var client = Client ?? new HttpClient();

            foreach (var message in manifestMessage.Messages)
            {
                try
                {
                    var msg      = JsonConvert.SerializeObject(message);
                    var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}manifest"), message);

                    if (response.IsSuccessStatusCode)
                    {
                        var content = await response.Content.ReadAsJsonAsync <SendManifestResponse>();

                        responses.Add(content);
                    }
                    else
                    {
                        var error = await response.Content.ReadAsStringAsync();

                        throw new Exception(error);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e, $"Send Manifest Error");
                    throw;
                }
            }

            return(responses);
        }
Exemplo n.º 3
0
        public async Task <List <string> > SendExtractsAsync(SendManifestPackageDTO sendTo)
        {
            //hack to prevent hang fire from requeueing the job every 30 minutes
            if (_patientExtractStatus.LastStatus != nameof(ExtractStatus.Sending))
            {
                var responses             = new List <Task <string> >();
                var output                = new List <string>();
                HttpClientHandler handler = new HttpClientHandler()
                {
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
                };
                using (var client = new HttpClient(handler))
                {
                    client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
                    client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.Timeout = new TimeSpan(0, 1, 0);
                    var ids = _reader.ReadAllIds().ToList();
                    _total = ids.Count;
                    var sentPatients             = new List <Guid>();
                    var sentPatientArts          = new List <Guid>();
                    var sentPatientBaselines     = new List <Guid>();
                    var sentPatientLabs          = new List <Guid>();
                    var sentPatientPharmacies    = new List <Guid>();
                    var sentPatientStatuses      = new List <Guid>();
                    var sentPatientVisits        = new List <Guid>();
                    var sentPatientAdverseEvents = new List <Guid>();

                    //update status to sending
                    UpdateUiNumbers(ExtractStatus.Sending);
                    //Show error message in UI
                    DomainEvents.Dispatch(new DwhMessageNotification(false, $"Sending started..."));

                    var httpResponseMessages = new List <Task <HttpResponseMessage> >();

                    foreach (var id in ids)
                    {
                        _count++;
                        var patient                 = _packager.GenerateExtracts(id);
                        var artMessageBag           = ArtMessageBag.Create(patient);
                        var artMessage              = artMessageBag.Message;
                        var baselineMessageBag      = BaselineMessageBag.Create(patient);
                        var baselineMessage         = baselineMessageBag.Message;
                        var labMessageBag           = LabMessageBag.Create(patient);
                        var labMessage              = labMessageBag.Message;
                        var pharmacyMessageBag      = PharmacyMessageBag.Create(patient);
                        var pharmacyMessage         = pharmacyMessageBag.Message;
                        var statusMessageBag        = StatusMessageBag.Create(patient);
                        var statusMessage           = statusMessageBag.Message;
                        var visitsMessageBag        = VisitsMessageBag.Create(patient);
                        var visitsMessage           = visitsMessageBag.Message;
                        var adverseEventsMessageBag = AdverseEventsMessageBag.Create(patient);
                        var adverseEventsMessage    = adverseEventsMessageBag.Message;

                        if (artMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{artMessageBag.EndPoint}"), artMessage);
                                httpResponseMessages.Add(response);
                                _artCount += artMessage.ArtExtracts.Count;
                                sentPatientArts.AddRange(artMessage.ArtExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientArt.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientArt, artMessage.ArtExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(artMessage);
                                throw;
                            }
                        }

                        if (baselineMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{baselineMessageBag.EndPoint}"), baselineMessage);
                                httpResponseMessages.Add(response);
                                _baselineCount += baselineMessage.BaselinesExtracts.Count;
                                sentPatientBaselines.AddRange(baselineMessage.BaselinesExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientBaseline.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientBaseline, baselineMessage.BaselinesExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(baselineMessage);
                                throw;
                            }
                        }

                        if (labMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{labMessageBag.EndPoint}"), labMessage);
                                httpResponseMessages.Add(response);
                                _labCount += labMessage.LaboratoryExtracts.Count;
                                sentPatientLabs.AddRange(labMessage.LaboratoryExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientLab.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientLab, labMessage.LaboratoryExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(labMessage);
                                throw;
                            }
                        }

                        if (pharmacyMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{pharmacyMessageBag.EndPoint}"), pharmacyMessage);
                                httpResponseMessages.Add(response);
                                _pharmacyCount += pharmacyMessage.PharmacyExtracts.Count;
                                sentPatientPharmacies.AddRange(pharmacyMessage.PharmacyExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientPharmacy.ToString()}s for patient id {id}"));
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientPharmacy, pharmacyMessage.PharmacyExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                Log.Error(e, $"Send Error");
                                PrintMessage(pharmacyMessage);
                                throw;
                            }
                        }

                        if (statusMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{statusMessageBag.EndPoint}"), statusMessage);
                                httpResponseMessages.Add(response);
                                _statusCount += statusMessage.StatusExtracts.Count;
                                sentPatientStatuses.AddRange(statusMessage.StatusExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientStatus.ToString()}es for patient id {id}"));
                                Log.Error(e, $"Send Error");
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientStatus, statusMessage.StatusExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                                PrintMessage(statusMessage);
                                throw;
                            }
                        }

                        if (visitsMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{visitsMessageBag.EndPoint}"), visitsMessage);
                                httpResponseMessages.Add(response);
                                _visitCount += visitsMessage.VisitExtracts.Count;
                                sentPatientVisits.AddRange(visitsMessage.VisitExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientVisit.ToString()}s for patient id {id}"));
                                Log.Error(e, $"Send Error");
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientVisit, visitsMessage.VisitExtracts.Select(x => x.Id).ToList(), SendStatus.Sent, e.Message));
                                PrintMessage(visitsMessage);
                                throw;
                            }
                        }

                        if (adverseEventsMessage.HasContents)
                        {
                            try
                            {
                                var response = client.PostAsCompressedAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{adverseEventsMessageBag.EndPoint}"), adverseEventsMessage);
                                httpResponseMessages.Add(response);
                                _adverseEventCount += adverseEventsMessage.AdverseEventExtracts.Count;
                                sentPatientAdverseEvents.AddRange(adverseEventsMessage.AdverseEventExtracts.Select(x => x.Id).ToList());
                            }
                            catch (Exception e)
                            {
                                //Show error message in UI
                                DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending {ExtractType.PatientAdverseEvent.ToString()}s for patient id {id}"));
                                Log.Error(e, $"Send Error");
                                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientAdverseEvent, adverseEventsMessage.AdverseEventExtracts.Select(x => x.Id).ToList(), SendStatus.Sent, e.Message));
                                PrintMessage(adverseEventsMessage);
                                throw;
                            }
                        }

                        //update UI in set number of batches
                        if (_count % Batch == 0)
                        {
                            foreach (var httpResponseMessage in httpResponseMessages)
                            {
                                var response = await httpResponseMessage;
                                if (response.IsSuccessStatusCode)
                                {
                                    var content = response.Content.ReadAsStringAsync();
                                    responses.Add(content);
                                    sentPatients.Add(id);
                                }
                                //Retry failed requests for set number of retries
                                else
                                {
                                    int retry = 0;
                                    for (int i = 0; i < MaxRetries; i++)
                                    {
                                        retry = i;
                                        var r = await client.PostAsCompressedAsync(sendTo.GetUrl($"{response.RequestMessage.RequestUri}"), response.RequestMessage.Content);

                                        if (r.IsSuccessStatusCode)
                                        {
                                            var content = response.Content.ReadAsStringAsync();
                                            responses.Add(content);
                                            break;
                                        }
                                    }
                                    //if all retries fail throw error
                                    if (retry == 3)
                                    {
                                        //Show error message in UI
                                        DomainEvents.Dispatch(new DwhMessageNotification(true, $"Error sending Extracts for patient id {id}"));
                                        var error = await response.Content.ReadAsStringAsync();

                                        Log.Error(error, $"Host Response Error");
                                        throw new Exception(error);
                                    }
                                }
                            }
                            httpResponseMessages.Clear();
                            UpdateUiNumbers(ExtractStatus.Sending);
                        }
                    }
                    //update extract sent field
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.Patient, sentPatients));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientArt, sentPatientArts));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientBaseline, sentPatientBaselines));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientLab, sentPatientLabs));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientPharmacy, sentPatientPharmacies));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientStatus, sentPatientStatuses));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientVisit, sentPatientVisits));
                    BackgroundJob.Enqueue(() => UpdateExtractSent(ExtractType.PatientAdverseEvent, sentPatientAdverseEvents));
                    UpdateUiNumbers(ExtractStatus.Sent);
                    //Show error message in UI
                    DomainEvents.Dispatch(new DwhMessageNotification(false, $"Extracts sent successfully!"));
                }

                foreach (var r in responses)
                {
                    var response = await r;
                    output.Add(response);
                }
                return(output);
            }
            //when hang fire tries to re-queue the job after thirty minutes or when sending was interrupted midway
            return(null);
        }
Exemplo n.º 4
0
 public Task <List <SendDhwManifestResponse> > SendManifestAsync(SendManifestPackageDTO sendTo)
 {
     return(SendManifestAsync(sendTo, DwhManifestMessageBag.Create(_packager.GenerateWithMetrics().ToList())));
 }
Exemplo n.º 5
0
 public Task <List <SendMpiResponse> > SendMpiAsync(SendManifestPackageDTO sendTo)
 {
     return(SendMpiAsync(sendTo, MpiMessageBag.Create(_packager.GenerateDtoMpi().ToList())));
 }
Exemplo n.º 6
0
 public Task <List <SendManifestResponse> > SendManifestAsync(SendManifestPackageDTO sendTo, string version)
 {
     return(SendManifestAsync(sendTo, ManifestMessageBag.Create(_packager.GenerateWithMetrics(sendTo.GetEmrDto()).ToList()), version));
 }
Exemplo n.º 7
0
 public Task <List <SendMpiResponse> > SendClientsAsync(SendManifestPackageDTO sendTo)
 {
     return(SendClientsAsync(sendTo, HtsMessageBag.Create(_packager.GenerateClients().ToList())));
 }
Exemplo n.º 8
0
 public Task <List <SendMpiResponse> > SendPartnerTracingAsync(SendManifestPackageDTO sendTo)
 {
     return(SendPartnerTracingAsync(sendTo, HtsMessageBag.Create(_packager.GeneratePartnerTracing().ToList())));
 }
Exemplo n.º 9
0
 private void QueueDwh(SendManifestPackageDTO package)
 {
     BackgroundJob.Enqueue(() => _dwhSendService.SendExtractsAsync(package));
 }
Exemplo n.º 10
0
 public Task <List <SendMpiResponse> > SendMigrationsAsync(SendManifestPackageDTO sendTo)
 {
     return(SendMigrationsAsync(sendTo, MgsMessageBag.Create(_packager.GenerateMigrations().ToList())));
 }
Exemplo n.º 11
0
 public Task <List <SendMpiResponse> > SendPatientMnchsAsync(SendManifestPackageDTO sendTo)
 {
     return(SendPatientMnchsAsync(sendTo, MnchMessageBag.Create(_packager.GeneratePatientMnchs().ToList())));
 }
Exemplo n.º 12
0
 public Task <List <SendMpiResponse> > SendMotherBabyPairsAsync(SendManifestPackageDTO sendTo)
 {
     return(SendMotherBabyPairsAsync(sendTo,
                                     MnchMessageBag.Create(_packager.GenerateMotherBabyPairs().ToList())));
 }
Exemplo n.º 13
0
        public async Task <List <SendCTResponse> > SendBatchExtractsAsync <T>(
            SendManifestPackageDTO sendTo,
            int batchSize,
            IMessageBag <T> messageBag)
            where T : ClientExtract
        {
            HttpClientHandler handler = new HttpClientHandler()
            {
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };
            var client = Client ?? new HttpClient(handler);

            var responses   = new List <SendCTResponse>();
            var packageInfo = _packager.GetPackageInfo <T>(batchSize);
            int sendCound   = 0;
            int count       = 0;
            int total       = packageInfo.PageCount;
            int overall     = 0;

            DomainEvents.Dispatch(new CTStatusNotification(sendTo.ExtractId, sendTo.GetExtractId(messageBag.ExtractName), ExtractStatus.Sending));
            long recordCount = 0;

            try
            {
                for (int page = 1; page <= packageInfo.PageCount; page++)
                {
                    count++;
                    var extracts = _packager.GenerateBatchExtracts <T>(page, packageInfo.PageSize).ToList();
                    recordCount = recordCount + extracts.Count;
                    Log.Debug(
                        $">>>> Sending {messageBag.ExtractName} {recordCount}/{packageInfo.TotalRecords} Page:{page} of {packageInfo.PageCount}");
                    messageBag = messageBag.Generate(extracts);
                    var message = messageBag.Messages;
                    try
                    {
                        /*
                         * var msg = JsonConvert.SerializeObject(message,new JsonSerializerSettings {ReferenceLoopHandling = ReferenceLoopHandling.Serializ});
                         */
                        var response = await client.PostAsJsonAsync(
                            sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}v2/{messageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsJsonAsync <SendCTResponse>();

                            responses.Add(content);

                            var sentIds = messageBag.SendIds;
                            sendCound += sentIds.Count;
                            DomainEvents.Dispatch(new CTExtractSentEvent(sentIds, SendStatus.Sent,
                                                                         messageBag.ExtractType));
                        }
                        else
                        {
                            var sentIds = messageBag.SendIds;
                            var error   = await response.Content.ReadAsStringAsync();

                            DomainEvents.Dispatch(new CTExtractSentEvent(
                                                      sentIds, SendStatus.Failed, messageBag.ExtractType,
                                                      error));
                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, $"Send Extracts{messageBag.ExtractName} Error");
                        throw;
                    }

                    DomainEvents.Dispatch(new CTSendNotification(new SendProgress(messageBag.ExtractName, messageBag.GetProgress(count, total), recordCount)));
                }
            }
            catch (Exception e)
            {
                Log.Error(e, $"Send Extracts {messageBag.ExtractName} Error");
                throw;
            }

            DomainEvents.Dispatch(new CTSendNotification(new SendProgress(messageBag.ExtractName,
                                                                          messageBag.GetProgress(count, total), recordCount, true)));

            DomainEvents.Dispatch(new CTStatusNotification(sendTo.ExtractId, sendTo.GetExtractId(messageBag.ExtractName), ExtractStatus.Sent, sendCound)
            {
                UpdatePatient = (messageBag is ArtMessageBag || messageBag is BaselineMessageBag || messageBag is StatusMessageBag)
            }
                                  );

            return(responses);
        }
Exemplo n.º 14
0
        public async Task <List <SendMpiResponse> > SendPartnerNotificationServicesAsync(SendManifestPackageDTO sendTo, HtsMessageBag messageBag)
        {
            var responses = new List <SendMpiResponse>();

            var client    = Client ?? new HttpClient();
            int sendCound = 0;
            int count     = 0;
            int total     = messageBag.Messages.Count;

            DomainEvents.Dispatch(new HtsStatusNotification(sendTo.ExtractId, ExtractStatus.Sending));

            foreach (var message in messageBag.Messages)
            {
                count++;
                try
                {
                    var msg      = JsonConvert.SerializeObject(message);
                    var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}Pns"), message);

                    if (response.IsSuccessStatusCode)
                    {
                        var content = await response.Content.ReadAsJsonAsync <SendMpiResponse>();

                        responses.Add(content);

                        var sentIds = message.PartnerNotificationServices.Select(x => x.Id).ToList();
                        sendCound += sentIds.Count;
                        DomainEvents.Dispatch(new HtsExtractSentEvent(sentIds, SendStatus.Sent, sendTo.ExtractName));
                    }
                    else
                    {
                        var error = await response.Content.ReadAsStringAsync();

                        DomainEvents.Dispatch(new HtsExtractSentEvent(message.PartnerNotificationServices.Select(x => x.Id).ToList(), SendStatus.Failed, sendTo.ExtractName, error));
                        throw new Exception(error);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e, $"Send Manifest Error");
                    throw;
                }

                DomainEvents.Dispatch(new HtsSendNotification(new SendProgress(nameof(HtsPartnerNotificationServices), Common.GetProgress(count, total), sendCound)));
            }

            DomainEvents.Dispatch(new HtsSendNotification(new SendProgress(nameof(HtsPartnerNotificationServices), Common.GetProgress(count, total), sendCound, true)));

            DomainEvents.Dispatch(new HtsStatusNotification(sendTo.ExtractId, ExtractStatus.Sent, sendCound));

            return(responses);
        }
Exemplo n.º 15
0
 public Task <List <SendMpiResponse> > SendPartnerNotificationServicesAsync(SendManifestPackageDTO sendTo)
 {
     return(SendPartnerNotificationServicesAsync(sendTo, HtsMessageBag.Create(_packager.GeneratePartnerNotificationServices().ToList())));
 }
Exemplo n.º 16
0
 public void SendDiffJobProfiles(SendManifestPackageDTO package)
 {
     var idsA = _ctSendService.SendDiffBatchExtractsAsync(package, 500, new PharmacyMessageBag()).Result;
     var idsB = _ctSendService.SendDiffBatchExtractsAsync(package, 500, new LabMessageBag()).Result;
     var idsC = _ctSendService.SendDiffBatchExtractsAsync(package, 500, new VisitsMessageBag()).Result;
 }
Exemplo n.º 17
0
 public void SendDiffCovidJobProfiles(SendManifestPackageDTO package)
 {
     var idsCovid            = _ctSendService.SendDiffBatchExtractsAsync(package, 200, new CovidsMessageBag()).Result;
     var idsDefaulterTracing = _ctSendService.SendDiffBatchExtractsAsync(package, 200, new DefaulterTracingsMessageBag()).Result;
 }
Exemplo n.º 18
0
        public async Task <List <string> > SendExtractsAsync(SendManifestPackageDTO sendTo)
        {
            var client    = new HttpClient();
            var responses = new List <string>();

            var ids   = _reader.ReadAllIds().ToList();
            int count = 0;
            int total = ids.Count;

            foreach (var id in ids)
            {
                count++;

                var patient            = _packager.GenerateExtracts(id);
                var artMessageBag      = ArtMessageBag.Create(patient);
                var baselineMessageBag = BaselineMessageBag.Create(patient);
                var labMessageBag      = LabMessageBag.Create(patient);
                var pharmacyMessageBag = PharmacyMessageBag.Create(patient);
                var statusMessageBag   = StatusMessageBag.Create(patient);
                var visitsMessageBag   = VisitsMessageBag.Create(patient);


                foreach (var message in artMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{artMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientArt, message.ArtExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientArt, message.ArtExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in baselineMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{baselineMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientBaseline, message.BaselinesExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientBaseline, message.BaselinesExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in labMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{labMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientLab, message.LaboratoryExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientLab, message.LaboratoryExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in pharmacyMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{pharmacyMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientPharmacy, message.PharmacyExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientPharmacy, message.PharmacyExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        Log.Error(e, $"Send Error");
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in statusMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{statusMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientStatus, message.StatusExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, $"Send Error");
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientStatus, message.StatusExtracts.Select(x => x.Id).ToList(), SendStatus.Failed, e.Message));
                        PrintMessage(message);
                        throw;
                    }
                }

                foreach (var message in visitsMessageBag.Messages.Where(x => x.HasContents))
                {
                    try
                    {
                        var msg      = JsonConvert.SerializeObject(message);
                        var response = await client.PostAsJsonAsync(sendTo.GetUrl($"{_endPoint.HasToEndsWith("/")}{visitsMessageBag.EndPoint}"), message);

                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();

                            responses.Add(content);
                            DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientVisit, message.VisitExtracts.Select(x => x.Id).ToList(), SendStatus.Sent));
                        }
                        else
                        {
                            var error = await response.Content.ReadAsStringAsync();

                            throw new Exception(error);
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(e, $"Send Error");
                        DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.PatientVisit, message.VisitExtracts.Select(x => x.Id).ToList(), SendStatus.Sent, e.Message));
                        PrintMessage(message);
                        throw;
                    }
                }


                DomainEvents.Dispatch(new DwhSendNotification(new SendProgress(nameof(PatientExtract), Common.GetProgress(count, total))));

                DomainEvents.Dispatch(new DwhExtractSentEvent(ExtractType.Patient, new List <Guid> {
                    id
                }, SendStatus.Sent));
            }

            return(responses);
        }