Skip to content

Commit 220d534

Browse files
authored
Merge pull request #6363 from NHSDigital/do-not-consume-placeholder-imms-api-values
Do not consume placeholder imms api values
2 parents 7af3795 + 4488f88 commit 220d534

3 files changed

Lines changed: 93 additions & 8 deletions

File tree

app/lib/fhir_mapper/location.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ def initialize(location)
88
@location = location
99
end
1010

11+
UNKNOWN_IDENTIFIER = "X99999"
12+
1113
class UnknownValueError < StandardError
1214
end
1315

1416
def fhir_reference
1517
if school?
16-
value = urn || "X99999"
18+
value = urn || UNKNOWN_IDENTIFIER
1719
system = "https://fhir.hl7.org.uk/Id/urn-school-number"
1820
elsif clinic?
19-
value = ods_code || "X99999"
21+
value = ods_code || UNKNOWN_IDENTIFIER
2022
system = "https://fhir.nhs.uk/Id/ods-organization-code"
2123
else
2224
raise UnknownValueError, "Unsupported location type: #{type}"

app/lib/fhir_mapper/vaccination_record.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class VaccinationRecord
1111

1212
MILLILITER_SUB_STRINGS = %w[ml millilitre milliliter].freeze
1313

14+
BATCH_EXPIRY_MIN = Date.new(Date.current.year - 100, 1, 1)
15+
BATCH_EXPIRY_MAX = Date.new(Date.current.year + 100, 1, 1)
16+
1417
def initialize(vaccination_record)
1518
@vaccination_record = vaccination_record
1619
end
@@ -84,7 +87,7 @@ def self.from_fhir_record(fhir_record, patient:)
8487

8588
location_system = fhir_record.location.identifier.system
8689
location_value = fhir_record.location.identifier.value
87-
unless location_value == "X99999"
90+
unless location_value == FHIRMapper::Location::UNKNOWN_IDENTIFIER
8891
case location_system
8992
when "https://fhir.hl7.org.uk/Id/urn-school-number"
9093
attrs[:location] = ::Location.find_by(urn: location_value)
@@ -94,10 +97,19 @@ def self.from_fhir_record(fhir_record, patient:)
9497
end
9598

9699
if attrs[:location].nil?
97-
attrs[:location_name] = fhir_record.location.identifier.value
100+
attrs[:location_name] = (
101+
if location_value == FHIRMapper::Location::UNKNOWN_IDENTIFIER
102+
"Unknown"
103+
else
104+
location_value
105+
end
106+
)
98107
end
99108

100-
attrs[:performed_ods_code] = org_performer_ods_code_from_fhir(fhir_record)
109+
performer_ods_code = org_performer_ods_code_from_fhir(fhir_record)
110+
unless performer_ods_code == FHIRMapper::Location::UNKNOWN_IDENTIFIER
111+
attrs[:performed_ods_code] = performer_ods_code
112+
end
101113

102114
user_performer_name = user_performer_name_from_fhir(fhir_record)
103115
attrs[:performed_by_given_name] = user_performer_name&.given&.first
@@ -116,7 +128,11 @@ def self.from_fhir_record(fhir_record, patient:)
116128

117129
attrs[:vaccine] = Vaccine.from_fhir_record(fhir_record)
118130
attrs[:batch_number] = fhir_record.lotNumber&.to_s
119-
attrs[:batch_expiry] = fhir_record.expirationDate&.to_date
131+
132+
batch_expiry = fhir_record.expirationDate&.to_date
133+
attrs[:batch_expiry] = batch_expiry if (
134+
BATCH_EXPIRY_MIN...BATCH_EXPIRY_MAX
135+
).cover?(batch_expiry)
120136

121137
if attrs[:vaccine]
122138
attrs[:disease_types] = attrs[:vaccine].disease_types

spec/lib/fhir_mapper/vaccination_record_spec.rb

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,70 @@
546546
end
547547
end
548548

549+
describe "the parsed performed_ods_code value" do
550+
subject { record.performed_ods_code }
551+
552+
before do
553+
allow(
554+
fhir_immunization
555+
.performer
556+
.find { it.actor&.type == "Organization" }
557+
.actor
558+
.identifier
559+
).to receive(:value).and_return(ods_code)
560+
end
561+
562+
let(:fixture_file_name) { "fhir/flu/fhir_record_full.json" }
563+
564+
context "when the org performer ODS code is a real code" do
565+
let(:ods_code) { "B0C4P" }
566+
567+
it { should eq "B0C4P" }
568+
end
569+
570+
context "when the org performer ODS code is a placeholder" do
571+
let(:ods_code) { FHIRMapper::Location::UNKNOWN_IDENTIFIER }
572+
573+
it { should be_nil }
574+
end
575+
end
576+
577+
describe "the parsed batch_expiry value" do
578+
subject { record.batch_expiry }
579+
580+
let(:fixture_file_name) { "fhir/flu/fhir_record_full.json" }
581+
582+
before do
583+
allow(fhir_immunization).to receive(:expirationDate).and_return(
584+
expiration_date
585+
)
586+
end
587+
588+
context "when the expiry date is realistic" do
589+
let(:expiration_date) { "2026-07-02" }
590+
591+
it { should eq Date.new(2026, 7, 2) }
592+
end
593+
594+
context "when the expiry date is after 2100" do
595+
let(:expiration_date) { "9999-12-31" }
596+
597+
it { should be_nil }
598+
end
599+
600+
context "when the expiry date is before 1900" do
601+
let(:expiration_date) { "1899-12-31" }
602+
603+
it { should be_nil }
604+
end
605+
606+
context "when the expiry date is nil" do
607+
let(:expiration_date) { nil }
608+
609+
it { should be_nil }
610+
end
611+
end
612+
549613
context "with a full fhir record" do
550614
let(:fixture_file_name) { "fhir/flu/fhir_record_full.json" }
551615

@@ -740,7 +804,9 @@
740804
its(:delivery_method) { should eq "intramuscular" }
741805
its(:delivery_site) { should eq "left_arm_upper_position" }
742806
its(:full_dose) { should be true }
743-
its(:location_name) { should eq "X99999" }
807+
808+
its(:location_name) { should eq "Unknown" }
809+
744810
its(:outcome) { should eq "administered" }
745811
its(:performed_ods_code) { should eq "B0C4P" }
746812
its(:nhs_immunisations_api_primary_source) { should be true }
@@ -907,7 +973,8 @@
907973
its(:nhs_immunisations_api_primary_source) { should be true }
908974

909975
its(:location) { should be_nil }
910-
its(:location_name) { should eq "X99999" }
976+
977+
its(:location_name) { should eq "Unknown" }
911978

912979
its(:notes) { should be_nil }
913980
end

0 commit comments

Comments
 (0)