Skip to content

Commit 869eeb9

Browse files
authored
fix(OpenSRP Exporter): Handle missing patient addresses (#5599)
**Story card:** [sc-15562](https://app.shortcut.com/simpledotorg/story/15562/fix-address-issue-in-opensrp-exporter-then-re-dump-katugahahena) ## Because Patients without addresses break the export process ## This addresses Handling null cases for `patient.address` ## Test instructions suite tests
1 parent 328638c commit 869eeb9

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

app/services/one_off/opensrp/patient_exporter.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,7 @@ def export
3636
use: "mobile"
3737
)
3838
end,
39-
address: FHIR::Address.new(
40-
line: [patient.address.street_address],
41-
city: patient.address.village_or_colony,
42-
district: patient.address.district,
43-
state: patient.address.state,
44-
country: patient.address.country,
45-
postalCode: patient.address.pin,
46-
use: "home",
47-
type: "physical",
48-
text: address_text
49-
),
39+
address: fhir_patient_address,
5040
generalPractitioner: [
5141
FHIR::Reference.new(reference: "Practitioner/#{opensrp_ids[:practitioner_id]}")
5242
],
@@ -66,7 +56,27 @@ def export
6656
)
6757
end
6858

59+
def fhir_patient_address
60+
return {} if patient.address.nil?
61+
62+
if patient.address
63+
FHIR::Address.new(
64+
line: [patient.address.street_address],
65+
city: patient.address.village_or_colony,
66+
district: patient.address.district,
67+
state: patient.address.state,
68+
country: patient.address.country,
69+
postalCode: patient.address.pin,
70+
use: "home",
71+
type: "physical",
72+
text: address_text
73+
)
74+
end
75+
end
76+
6977
def address_text
78+
return "" if patient.address.nil?
79+
7080
"#{patient.address.street_address} (#{patient.address.village_or_colony}), [No GND Registered]"
7181
end
7282

lib/tasks/opensrp.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ namespace :opensrp do
187187
patient_name: patient.full_name,
188188
patient_gender: patient.gender,
189189
patient_date_of_birth: patient.date_of_birth || patient.age_updated_at - patient.age.years,
190-
patient_address: patient.address.street_address,
190+
patient_address: patient.address ? patient.address.street_address : "",
191191
patient_telephone: patient.phone_numbers.pluck(:number).join(";"),
192192
patient_facility: facilities[patient.assigned_facility_id][:name],
193193
patient_preferred_language: "Sinhala",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "rails_helper"
2+
3+
RSpec.describe OneOff::Opensrp::PatientExporter do
4+
describe "#fhir_patient_address" do
5+
it "is {} when the patient has no address" do
6+
patient = create(:patient, address: nil)
7+
8+
expect(patient.address).to be_nil
9+
10+
exporter = OneOff::Opensrp::PatientExporter.new(patient, {})
11+
12+
expect(exporter.fhir_patient_address).to eq({})
13+
end
14+
end
15+
16+
describe "#address_text" do
17+
it "is empty string when patient has no address" do
18+
patient = create(:patient, address: nil)
19+
20+
expect(patient.address).to be_nil
21+
22+
exporter = OneOff::Opensrp::PatientExporter.new(patient, {})
23+
24+
expect(exporter.address_text).to eq("")
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)