Skip to content

Commit

Permalink
import api: fix patient name extraction (#5309)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tfidfwastaken authored Oct 19, 2023
1 parent 8b60577 commit 1162488
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/services/bulk_api_import/fhir_patient_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
30 changes: 30 additions & 0 deletions spec/services/bulk_api_import/fhir_patient_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1162488

Please sign in to comment.