Skip to content

Commit 225222e

Browse files
Handle the case when NHS number is nil
If a patient's NHS number is `nil`, then there needs to be no API vaccination records associated with them. Previously they would've been left, but now they're removed.
1 parent 551311a commit 225222e

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

app/jobs/search_vaccination_records_in_nhs_job.rb

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,32 @@ def perform(patient)
1414
Sentry.set_tags(tx_id:, job_id: provider_job_id || job_id)
1515

1616
return unless Flipper.enabled?(:imms_api_search_job)
17-
return if patient.nhs_number.nil?
1817

1918
programmes = Programme.can_search_in_immunisations_api
2019

21-
fhir_bundle =
22-
NHS::ImmunisationsAPI.search_immunisations(patient, programmes:)
20+
if patient.nhs_number.nil?
21+
incoming_vaccination_records = []
22+
else
23+
fhir_bundle =
24+
NHS::ImmunisationsAPI.search_immunisations(patient, programmes:)
2325

24-
incoming_vaccination_records =
25-
extract_vaccination_records(fhir_bundle).map do |fhir_record|
26-
if FHIRMapper::VaccinationRecord::MAVIS_SYSTEM_NAME.in?(
27-
fhir_record.identifier.map(&:system)
28-
)
29-
next
26+
incoming_vaccination_records =
27+
extract_vaccination_records(fhir_bundle).map do |fhir_record|
28+
if FHIRMapper::VaccinationRecord::MAVIS_SYSTEM_NAME.in?(
29+
fhir_record.identifier.map(&:system)
30+
)
31+
next
32+
end
33+
34+
FHIRMapper::VaccinationRecord.from_fhir_record(
35+
fhir_record,
36+
patient:,
37+
team: patient.school.team
38+
)
3039
end
40+
incoming_vaccination_records = incoming_vaccination_records.compact
41+
end
3142

32-
FHIRMapper::VaccinationRecord.from_fhir_record(
33-
fhir_record,
34-
patient:,
35-
team: patient.school.team
36-
)
37-
end
38-
incoming_vaccination_records = incoming_vaccination_records.compact
3943
existing_vaccination_records =
4044
patient
4145
.vaccination_records

spec/jobs/search_vaccination_records_in_nhs_job_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
let(:team) { create(:team) }
77
let(:school) { create(:school, team:) }
8-
let(:patient) { create(:patient, team:, school:, nhs_number: "9449308357") }
8+
let(:patient) { create(:patient, team:, school:, nhs_number:) }
9+
let(:nhs_number) { "9449308357" }
910
let!(:programme) { create(:programme, :flu) }
1011

1112
before do
@@ -163,5 +164,22 @@
163164
)
164165
end
165166
end
167+
168+
context "with no NHS number" do
169+
let(:nhs_number) { nil }
170+
171+
let(:existing_bundle) do
172+
FHIR.from_contents(
173+
file_fixture("fhir/search_response_2_results.json").read
174+
)
175+
end
176+
177+
it "deletes all the API records and does not create any new ones" do
178+
expect { perform_job }.to change {
179+
patient.vaccination_records.count
180+
}.by(-2)
181+
expect(patient.vaccination_records.count).to eq(0)
182+
end
183+
end
166184
end
167185
end

0 commit comments

Comments
 (0)