/// <inheritdoc />
        public async System.Threading.Tasks.Task <RequestResult <PatientModel> > GetDemographicsByPHNAsync(string phn)
        {
            using (Source.StartActivity("GetDemographicsByPHNAsync"))
            {
                // Create request object
                HCIM_IN_GetDemographicsRequest request = CreateRequest(OIDType.PHN, phn);
                try
                {
                    // Perform the request
                    HCIM_IN_GetDemographicsResponse1 reply = await this.clientRegistriesClient.HCIM_IN_GetDemographicsAsync(request).ConfigureAwait(true);

                    return(this.ParseResponse(reply));
                }
                catch (CommunicationException e)
                {
                    this.logger.LogError(e.ToString());
                    return(new RequestResult <PatientModel>()
                    {
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = "Communication Exception when trying to retrieve the patient information from PHN", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.ClientRegistries)
                        },
                    });
                }
            }
        }
        /// <inheritdoc />
        public async System.Threading.Tasks.Task <HCIM_IN_GetDemographicsResponse1> GetDemographicsAsync(HCIM_IN_GetDemographicsRequest request)
        {
            // Perform the request
            HCIM_IN_GetDemographicsResponse1 reply = await this.clientRegistriesClient.HCIM_IN_GetDemographicsAsync(request).ConfigureAwait(true);

            return(reply);
        }
        /// <summary>
        /// Gets the patient record.
        /// </summary>
        /// <param name="hdid">The patient id.</param>
        /// <returns>The patient model.</returns>
        public async System.Threading.Tasks.Task <Patient> GetPatient(string hdid)
        {
            // Create request
            HCIM_IN_GetDemographics request = this.CreateRequest(hdid);

            // Perform the request
            HCIM_IN_GetDemographicsResponse1 reply = await this.getDemographicsClient.HCIM_IN_GetDemographicsAsync(request).ConfigureAwait(true);

            // Verify that the reply contains a result
            string responseCode = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.queryAck.queryResponseCode.code;

            if (responseCode.Contains("BCHCIM.GD.0.0013", System.StringComparison.InvariantCulture))
            {
                HCIM_IN_GetDemographicsResponseIdentifiedPerson retrievedPerson = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.subject[0].target;

                // Extract the subject names
                List <string> givenNameList = new List <string>();
                List <string> lastNameList  = new List <string>();
                for (int i = 0; i < retrievedPerson.identifiedPerson.name[0].Items.Length; i++)
                {
                    ENXP name = retrievedPerson.identifiedPerson.name[0].Items[i];

                    if (name.GetType() == typeof(engiven))
                    {
                        givenNameList.Add(name.Text[0]);
                    }
                    else if (name.GetType() == typeof(enfamily))
                    {
                        lastNameList.Add(name.Text[0]);
                    }
                }

                string delimiter  = " ";
                string givenNames = givenNameList.Aggregate((i, j) => i + delimiter + j);
                string lastNames  = lastNameList.Aggregate((i, j) => i + delimiter + j);
                string phn        = ((II)retrievedPerson.identifiedPerson.id.GetValue(0)).extension;
                // For now, add the return message to the reply
                return(new Patient(hdid, phn, givenNames, lastNames));
            }
            else
            {
                this.logger.LogInformation("Client Registry did not return a person. Returned message code: " + responseCode);
                return(new Patient());
            }
        }
Exemple #4
0
        public async Task ShouldGetDemographics()
        {
            HCIM_IN_GetDemographicsResponse1 expected = new HCIM_IN_GetDemographicsResponse1();
            IConfiguration config = new ConfigurationBuilder()
                                    .AddJsonFile("UnitTest.json").Build();

            Mock <QUPA_AR101102_PortType> clientMock = new Mock <QUPA_AR101102_PortType>();

            clientMock.Setup(x => x.HCIM_IN_GetDemographicsAsync(It.IsAny <HCIM_IN_GetDemographicsRequest>())).ReturnsAsync(expected);
            HCIM_IN_GetDemographicsRequest request = new HCIM_IN_GetDemographicsRequest();

            IClientRegistriesDelegate service = new ClientRegistriesDelegate(
                clientMock.Object
                );

            // Act
            HCIM_IN_GetDemographicsResponse1 actual = await service.GetDemographicsAsync(request);

            // Verify
            Assert.Equal(expected, actual);
        }
        private RequestResult <PatientModel> ParseResponse(HCIM_IN_GetDemographicsResponse1 reply)
        {
            using (Source.StartActivity("ParsePatientResponse"))
            {
                this.logger.LogDebug($"Parsing patient response... {JsonSerializer.Serialize(reply)}");

                // Verify that the reply contains a result
                string responseCode = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.queryAck.queryResponseCode.code;
                if (!responseCode.Contains("BCHCIM.GD.0.0013", StringComparison.InvariantCulture))
                {
                    PatientModel emptyPatient = new PatientModel();
                    this.logger.LogWarning($"Client Registry did not return a person. Returned message code: {responseCode}");
                    this.logger.LogDebug($"Finished getting patient. {JsonSerializer.Serialize(emptyPatient)}");
                    return(new RequestResult <PatientModel>()
                    {
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = "Client Registry did not return a person", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.ClientRegistries)
                        },
                    });
                }

                HCIM_IN_GetDemographicsResponseIdentifiedPerson retrievedPerson = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.subject[0].target;

                // If the deceased indicator is set and true, return an empty person.
                bool deceasedInd = retrievedPerson.identifiedPerson.deceasedInd?.value ?? false;
                if (deceasedInd)
                {
                    PatientModel emptyPatient = new PatientModel();
                    this.logger.LogWarning($"Client Registry returned a person with the deceasedIndicator set to true. No PHN was populated. {deceasedInd}");
                    this.logger.LogDebug($"Finished getting patient. {JsonSerializer.Serialize(emptyPatient)}");
                    return(new RequestResult <PatientModel>()
                    {
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = "Client Registry returned a person with the deceasedIndicator set to true", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.ClientRegistries)
                        },
                    });
                }

                PN?nameSection = retrievedPerson.identifiedPerson.name.Where(x => x.use.Any(u => u == cs_EntityNameUse.C)).FirstOrDefault();

                if (nameSection == null)
                {
                    this.logger.LogWarning($"Client Registry returned a person with an invalid name.");
                    return(new RequestResult <PatientModel>()
                    {
                        ResultStatus = ResultType.ActionRequired,
                        ResultError = ErrorTranslator.ActionRequired(ErrorMessages.InvalidServicesCard, ActionType.InvalidName),
                    });
                }

                // Extract the subject names
                List <string> givenNameList = new List <string>();
                List <string> lastNameList  = new List <string>();
                for (int i = 0; i < nameSection.Items.Length; i++)
                {
                    ENXP name = nameSection.Items[i];

                    if (name.GetType() == typeof(engiven) && (name.qualifier == null || !name.qualifier.Contains(cs_EntityNamePartQualifier.CL)))
                    {
                        givenNameList.Add(name.Text[0]);
                    }
                    else if (name.GetType() == typeof(enfamily) && (name.qualifier == null || !name.qualifier.Contains(cs_EntityNamePartQualifier.CL)))
                    {
                        lastNameList.Add(name.Text[0]);
                    }
                }

                string   delimiter  = " ";
                string   givenNames = givenNameList.Aggregate((i, j) => i + delimiter + j);
                string   lastNames  = lastNameList.Aggregate((i, j) => i + delimiter + j);
                string?  dobStr     = ((TS)retrievedPerson.identifiedPerson.birthTime).value; // yyyyMMdd
                DateTime dob        = DateTime.ParseExact(dobStr, "yyyyMMdd", CultureInfo.InvariantCulture);
                string   genderCode = retrievedPerson.identifiedPerson.administrativeGenderCode.code;
                string   gender     = "NotSpecified";
                if (genderCode == "F")
                {
                    gender = "Female";
                }
                else if (genderCode == "M")
                {
                    gender = "Male";
                }

                PatientModel patient = new PatientModel()
                {
                    FirstName = givenNames, LastName = lastNames, Birthdate = dob, Gender = gender, EmailAddress = string.Empty
                };

                II?    identifiedPersonId   = (II?)retrievedPerson.identifiedPerson.id.GetValue(0);
                string?personIdentifierType = identifiedPersonId?.root;
                string personIdentifier     = identifiedPersonId?.extension ?? string.Empty;
                if (personIdentifierType == OIDType.HDID.ToString())
                {
                    patient.HdId = personIdentifier;
                }
                else if (personIdentifierType == OIDType.PHN.ToString())
                {
                    patient.PersonalHealthNumber = personIdentifier;
                }
                else
                {
                    this.logger.LogWarning($"Client Registry returned a person with a person identifier not recognized. No PHN or HDID was populated.");
                    return(new RequestResult <PatientModel>()
                    {
                        ResultStatus = ResultType.ActionRequired,
                        ResultError = ErrorTranslator.ActionRequired(ErrorMessages.InvalidServicesCard, ActionType.NoHdId),
                    });
                }

                II?    subjectId             = (II?)retrievedPerson.id.GetValue(0);
                string?subjectIdentifierType = subjectId?.root;
                string subjectIdentifier     = subjectId?.extension ?? string.Empty;
                if (subjectIdentifierType == OIDType.HDID.ToString())
                {
                    patient.HdId = subjectIdentifier;
                }
                else if (personIdentifierType == OIDType.PHN.ToString())
                {
                    patient.PersonalHealthNumber = subjectIdentifier;
                }
                else
                {
                    this.logger.LogWarning($"Client Registry returned a person with a subject identifier not recognized. No PHN or HDID was populated.");
                    return(new RequestResult <PatientModel>()
                    {
                        ResultStatus = ResultType.ActionRequired,
                        ResultError = ErrorTranslator.ActionRequired(ErrorMessages.InvalidServicesCard, ActionType.NoHdId),
                    });
                }

                return(new RequestResult <PatientModel>()
                {
                    ResultStatus = ResultType.Success,
                    ResourcePayload = patient,
                });
            }
        }
Exemple #6
0
        /// <summary>
        /// Gets the patient record.
        /// </summary>
        /// <param name="hdid">The patient id.</param>
        /// <returns>The patient model.</returns>
        public async System.Threading.Tasks.Task <RequestResult <Patient> > GetPatient(string hdid)
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();
            this.logger.LogTrace($"Getting patient... {hdid}");
            Patient patient;

            // Create request
            HCIM_IN_GetDemographicsRequest request = this.CreateRequest(hdid);

            // Perform the request
            try
            {
                HCIM_IN_GetDemographicsResponse1 reply = await this.clientRegistriesDelegate.GetDemographicsAsync(request).ConfigureAwait(true);

                // Verify that the reply contains a result
                string responseCode = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.queryAck.queryResponseCode.code;
                if (!responseCode.Contains("BCHCIM.GD.0.0013", StringComparison.InvariantCulture))
                {
                    patient = new Patient();
                    this.logger.LogWarning($"Client Registry did not return a person. Returned message code: {responseCode}");
                    this.logger.LogDebug($"Finished getting patient. {JsonSerializer.Serialize(patient)}");
                    return(new RequestResult <Patient>()
                    {
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = "Client Registry did not return a person", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.ClientRegistries)
                        },
                    });
                }

                HCIM_IN_GetDemographicsResponseIdentifiedPerson retrievedPerson = reply.HCIM_IN_GetDemographicsResponse.controlActProcess.subject[0].target;

                // If the deceased indicator is set and true, return an empty person.
                bool deceasedInd = retrievedPerson.identifiedPerson.deceasedInd?.value == true;
                if (deceasedInd)
                {
                    patient = new Patient();
                    this.logger.LogWarning($"Client Registry returned a person with the deceasedIndicator set to true. No PHN was populated. {deceasedInd}");
                    this.logger.LogDebug($"Finished getting patient. {JsonSerializer.Serialize(patient)}");
                    return(new RequestResult <Patient>()
                    {
                        ResultStatus = ResultType.Error,
                        ResultError = new RequestResultError()
                        {
                            ResultMessage = "Client Registry returned a person with the deceasedIndicator set to true", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.ClientRegistries)
                        },
                    });
                }

                // Extract the subject names
                List <string> givenNameList = new List <string>();
                List <string> lastNameList  = new List <string>();
                for (int i = 0; i < retrievedPerson.identifiedPerson.name[0].Items.Length; i++)
                {
                    ENXP name = retrievedPerson.identifiedPerson.name[0].Items[i];

                    if (name.GetType() == typeof(engiven))
                    {
                        givenNameList.Add(name.Text[0]);
                    }
                    else if (name.GetType() == typeof(enfamily))
                    {
                        lastNameList.Add(name.Text[0]);
                    }
                }

                string   delimiter  = " ";
                string   givenNames = givenNameList.Aggregate((i, j) => i + delimiter + j);
                string   lastNames  = lastNameList.Aggregate((i, j) => i + delimiter + j);
                string   phn        = ((II)retrievedPerson.identifiedPerson.id.GetValue(0) !).extension;
                string?  dobStr     = ((TS)retrievedPerson.identifiedPerson.birthTime).value; // yyyyMMdd
                DateTime dob        = DateTime.ParseExact(dobStr, "yyyyMMdd", CultureInfo.InvariantCulture);
                patient = new Patient(hdid, phn, givenNames, lastNames, dob, string.Empty);

                timer.Stop();
                this.logger.LogDebug($"Finished getting patient. {JsonSerializer.Serialize(patient)} Time Elapsed: {timer.Elapsed}");
                return(new RequestResult <Patient>()
                {
                    ResultStatus = ResultType.Success,
                    ResourcePayload = patient,
                });
            }
            catch (CommunicationException e)
            {
                this.logger.LogError(e.ToString());
                return(new RequestResult <Patient>()
                {
                    ResultStatus = ResultType.Error,
                    ResultError = new RequestResultError()
                    {
                        ResultMessage = "Communication Exception when trying to retrieve the PHN", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.ClientRegistries)
                    },
                });
            }
        }