From 1162488426c0bd358f121c83e7366430e5ca433b Mon Sep 17 00:00:00 2001 From: Atharva Raykar <24277692+tfidfwastaken@users.noreply.github.com> Date: Thu, 19 Oct 2023 14:40:34 +0530 Subject: [PATCH] import api: fix patient name extraction (#5309) There was a goof-up with name extraction. Staring at the deeply nested semi-uniform patterns of the FHIR spec has caused semantic satiation in this author which has introduced this bug in the patient importer, where instead of trying to extract `patient.name.text` for the name (as per the FHIR spec), the code is trying to extract `patient.name.value`, which always returns `nil`. This is promptly fixed, with some unit tests that should have been there to catch this slip in the first place. --- .../bulk_api_import/fhir_patient_importer.rb | 2 +- .../fhir_patient_importer_spec.rb | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/services/bulk_api_import/fhir_patient_importer.rb b/app/services/bulk_api_import/fhir_patient_importer.rb index ba91cf05d2..77fdb2f048 100644 --- a/app/services/bulk_api_import/fhir_patient_importer.rb +++ b/app/services/bulk_api_import/fhir_patient_importer.rb @@ -28,7 +28,7 @@ def identifier def build_attributes { id: translate_id(identifier), - full_name: @resource.dig(:name, :value) || "Anonymous " + Faker::Name.first_name, + full_name: @resource.dig(:name, :text) || "Anonymous " + Faker::Name.first_name, gender: gender, status: status, date_of_birth: @resource[:birthDate], diff --git a/spec/services/bulk_api_import/fhir_patient_importer_spec.rb b/spec/services/bulk_api_import/fhir_patient_importer_spec.rb index 06f80a9a2d..f40b8525e6 100644 --- a/spec/services/bulk_api_import/fhir_patient_importer_spec.rb +++ b/spec/services/bulk_api_import/fhir_patient_importer_spec.rb @@ -60,6 +60,36 @@ expect(Api::V3::PatientPayloadValidator.new(attributes)).to be_valid end end + + it "correctly extracts gender" do + [ + {input: "male", expected: "male"}, + {input: "female", expected: "female"}, + {input: "other", expected: "transgender"} + ].map do |input:, expected:| + patient_resource = build_patient_import_resource + .merge(managingOrganization: [{value: facility_identifier.identifier}], gender: input) + .except(:registrationOrganization) + + expect(described_class.new(patient_resource).build_attributes[:gender]).to eq(expected) + end + end + + it "correctly extracts name when present" do + patient_resource = build_patient_import_resource + .merge(managingOrganization: [{value: facility_identifier.identifier}], name: {text: "naem"}) + .except(:registrationOrganization) + + expect(described_class.new(patient_resource).build_attributes[:full_name]).to eq("naem") + end + + it "generates a name when not present in the resource" do + patient_resource = build_patient_import_resource + .merge(managingOrganization: [{value: facility_identifier.identifier}]) + .except(:registrationOrganization, :name) + + expect(described_class.new(patient_resource).build_attributes[:full_name]).to match(/Anonymous \w/) + end end describe "#status" do