Ejemplo n.º 1
0
        public Hl7.Fhir.Model.Bundle GetPatientsByMatch(int dni,
                                                        string primerApellido,
                                                        string primerNombre,
                                                        string otrosNombres,
                                                        Common.Constants.Sexo sexo,
                                                        DateTime fechaNacimiento)
        {
            if (dni < 10000)
            {
                throw new Exception($"Nro. de documento no válido:{dni} debe ser mayor a 10.000");
            }

            if (string.IsNullOrWhiteSpace(primerApellido))
            {
                throw new ArgumentException("message", nameof(primerApellido));
            }

            if (string.IsNullOrWhiteSpace(primerNombre))
            {
                throw new ArgumentException("message", nameof(primerNombre));
            }

            var patientIdentifier = new Hl7.Fhir.Model.Identifier();

            patientIdentifier.Use    = Hl7.Fhir.Model.Identifier.IdentifierUse.Usual;
            patientIdentifier.System = Common.Constants.DomainName.RenaperDniDomain.Value;
            patientIdentifier.Value  = dni.ToString();

            var patientName = new Hl7.Fhir.Model.HumanName();

            patientName.Use    = Hl7.Fhir.Model.HumanName.NameUse.Official;
            patientName.Text   = $"{primerNombre} {primerApellido}";
            patientName.Family = $"{primerApellido}";
            patientName.Given  = new string[] { primerNombre, otrosNombres };
            patientName.FamilyElement.Extension.Add(
                new Hl7.Fhir.Model.Extension
            {
                Url   = "http://hl7.org/fhir/StructureDefinition/humanname-fathers-family",
                Value = new Hl7.Fhir.Model.FhirString($"{primerApellido}")
            });

            var paramPatient = new Hl7.Fhir.Model.Patient();

            paramPatient.Name = new List <Hl7.Fhir.Model.HumanName> {
                patientName
            };
            paramPatient.Identifier = new List <Hl7.Fhir.Model.Identifier> {
                patientIdentifier
            };
            paramPatient.BirthDate = fechaNacimiento.ToString("yyyy-MM-dd");
            paramPatient.Gender    = (sexo == Common.Constants.Sexo.Femenido) ? Hl7.Fhir.Model.AdministrativeGender.Female : Hl7.Fhir.Model.AdministrativeGender.Male;

            var parameters = new Hl7.Fhir.Model.Parameters();

            parameters.Id = "mymatch";

            parameters.Add("resource", paramPatient);
            parameters.Add("count", new Hl7.Fhir.Model.Integer(5));

            var serviceUrl = _integrationServicesConfiguration.GetConfigurationService(IntegrationServicesConfiguration.ConfigurationServicesName.BUS);
            var patientUrl = serviceUrl.GetEndPoint(IntegrationService.ConfigurationEndPointName.PATIENT_POST_MATCH);

            var activity = new ActivityLog
            {
                ActivityTypeDescriptorId = (int)Entities.Activity.ActivityType.GET_PACIENTE_EN_BUS_MATCH
            };

            var client = new FhirClient(serviceUrl.BaseURL)
            {
                PreferredFormat = ResourceFormat.Json
            };

            client.OnBeforeRequest += (object sender, BeforeRequestEventArgs e) =>
            {
                if (e.Body != null)
                {
                    var requestAdderss = e.RawRequest.Address.ToString();
                    var requestBody    = Encoding.UTF8.GetString(e.Body, 0, e.Body.Length);

                    //Prettify !!!
                    requestBody = JToken.Parse(requestBody).ToString();

                    activity.RequestIsJson       = true;
                    activity.ActivityRequestUI   = requestAdderss;
                    activity.ActivityRequestBody = requestBody;

                    _logger.LogInformation($"Send Request Address:{requestAdderss}");
                    _logger.LogInformation($"Send Request Body:{requestBody}");
                }
            };

            client.OnAfterResponse += (object sender, AfterResponseEventArgs e) =>
            {
                if (e.Body != null)
                {
                    var responseBody = Encoding.UTF8.GetString(e.Body, 0, e.Body.Length);

                    //Prettify !!!
                    responseBody = JToken.Parse(responseBody).ToString();

                    activity.ResponseIsJson       = true;
                    activity.ActivityResponse     = $"Status: {e.RawResponse.StatusCode}";
                    activity.ActivityResponseBody = responseBody;

                    _logger.LogInformation($"Received response with status: {e.RawResponse.StatusCode}");
                    _logger.LogInformation($"Received response with body: {responseBody}");
                }
            };

            var resp = client.Operation(new Uri(patientUrl.URL), parameters, false);

            var ret = (Hl7.Fhir.Model.Bundle)resp;

            _currentContext.RegisterActivityLog(activity);

            return(ret);
        }