/// <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()); } }
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, }); } }
/// <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) }, }); } }