Skip to content

Commit 280d3fe

Browse files
authored
Merge pull request #6316 from NHSDigital/run-patient-status-updater-for-patient-imports
Run Patient Status Updater for patient imports
2 parents 011a484 + 958a46d commit 280d3fe

4 files changed

Lines changed: 68 additions & 12 deletions

File tree

app/forms/import_duplicate_form.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def apply_pending_changes!
5555
object.patient.apply_pending_changes! if object.respond_to?(:patient)
5656

5757
object.apply_pending_changes!
58+
59+
PatientStatusUpdater.call(patient: object) if object.is_a?(Patient)
5860
end
5961

6062
def discard_pending_changes!
@@ -67,9 +69,11 @@ def keep_both_changes!
6769
return unless can_keep_both?
6870
return unless can_apply?
6971

70-
object.apply_pending_changes_to_new_record!(
71-
changeset: changeset_for_keep_both
72-
)
72+
new_record =
73+
object.apply_pending_changes_to_new_record!(
74+
changeset: changeset_for_keep_both
75+
)
76+
PatientStatusUpdater.call(patient: new_record) if new_record.is_a?(Patient)
7377
end
7478

7579
def changeset_for_keep_both

app/jobs/commit_patient_changesets_job.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def perform(patient_changeset_ids)
4646
to_skip.each(&:processed!)
4747

4848
PatientTeamUpdater.call(patient_scope: import.patients)
49+
PatientStatusUpdater.call(patient_scope: import.patients)
4950
end
5051

5152
if finished_committing_changesets?(import)

spec/forms/import_duplicate_form_spec.rb

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,55 @@
44
let(:programme) { Programme.sample }
55

66
describe "#save" do
7-
context "resolving a vaccination record" do
8-
subject do
9-
described_class.new(
10-
apply_changes: "apply",
11-
object: vaccination_record,
12-
current_team: team
13-
).save
14-
end
7+
subject(:save) { form.save }
8+
9+
let(:form) do
10+
described_class.new(apply_changes:, object:, current_team: team)
11+
end
1512

13+
context "resolving a vaccination record" do
14+
let(:apply_changes) { "apply" }
15+
let(:object) { vaccination_record }
1616
let(:team) { create(:team, programmes: [programme]) }
1717
let(:vaccination_record) do
1818
create(:vaccination_record, programme:, team:)
1919
end
2020

2121
it_behaves_like "a method that updates team cached counts"
22+
23+
it "does not call PatientStatusUpdater" do
24+
expect(PatientStatusUpdater).not_to receive(:call)
25+
expect(save).to be(true)
26+
end
2227
end
2328

2429
context "resolving a patient record" do
30+
let(:team) { create(:team, programmes: [programme]) }
31+
let(:patient) do
32+
create(:patient, pending_changes: { "given_name" => "Updated" })
33+
end
34+
35+
context "when applying pending changes" do
36+
let(:apply_changes) { "apply" }
37+
let(:object) { patient }
38+
39+
it "calls PatientStatusUpdater" do
40+
expect(PatientStatusUpdater).to receive(:call).with(patient:)
41+
expect(save).to be(true)
42+
end
43+
end
44+
45+
context "when discarding changes" do
46+
let(:apply_changes) { "discard" }
47+
let(:object) { patient }
48+
49+
it "does not call PatientStatusUpdater" do
50+
expect(PatientStatusUpdater).not_to receive(:call)
51+
expect(save).to be(true)
52+
end
53+
end
54+
2555
context "when a patient import issue includes parent relationships" do
26-
let(:team) { create(:team, programmes: [programme]) }
2756
let(:session) { create(:session, team:, programmes: [programme]) }
2857
let(:class_import) { create(:class_import, session:) }
2958
let(:existing_patient) { create(:patient) }
@@ -93,6 +122,20 @@
93122
expect(class_import.patients.reload).to contain_exactly(new_patient)
94123
end
95124

125+
it "calls PatientStatusUpdater with the new patient when keeping both" do
126+
form =
127+
described_class.new(
128+
apply_changes: "keep_both",
129+
object: existing_patient,
130+
current_team: team
131+
)
132+
133+
expect(PatientStatusUpdater).to receive(:call).with(
134+
patient: instance_of(Patient)
135+
)
136+
expect(form.save).to be(true)
137+
end
138+
96139
it "keeps imported parent relationships when discarding changes" do
97140
form =
98141
described_class.new(

spec/jobs/commit_patient_changesets_job_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@
148148
).with(location.id).once
149149
end
150150

151+
it "calls PatientStatusUpdater for imported patients" do
152+
allow(PatientStatusUpdater).to receive(:call)
153+
perform_job
154+
expect(PatientStatusUpdater).to have_received(:call).with(
155+
patient_scope: a_kind_of(ActiveRecord::Relation)
156+
)
157+
end
158+
151159
it "imports PDS search results when present" do
152160
changeset = import.changesets.first
153161
changeset.data["search_results"] = [

0 commit comments

Comments
 (0)