Skip to content

Commit 6f3d36a

Browse files
committed
Ensure nhs_number_first_added is always set
Jira-Issue: MAV-7091
1 parent 9ecacb6 commit 6f3d36a

6 files changed

Lines changed: 105 additions & 11 deletions

File tree

app/models/concerns/patient_import_concern.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def import_patients_and_parents(changesets, import)
1212
.uniq { [_1.parent, _1.patient] }
1313

1414
deduplicate_patients!(patients, relationships)
15+
patients.each(&:ensure_nhs_number_first_added_at)
1516

1617
patients_with_nhs_number_changes =
1718
patients.select(&:nhs_number_previously_changed?)

app/models/patient.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,7 @@ class Patient < ApplicationRecord
497497
it.blank? ? nil : it.normalise_whitespace.gsub(/\s/, "")
498498
end
499499

500-
before_validation :set_nhs_number_first_added_at,
501-
if: :will_save_change_to_nhs_number?
500+
before_validation :ensure_nhs_number_first_added_at
502501
after_update :sync_vaccinations_to_nhs_immunisations_api
503502
after_commit :generate_important_notice_if_needed, on: :update
504503
after_commit :search_vaccinations_from_nhs_immunisations_api, on: :update
@@ -816,6 +815,15 @@ def pds_lookup_match?
816815

817816
def notifier = Notifier::Patient.new(self)
818817

818+
def ensure_nhs_number_first_added_at
819+
return unless will_save_change_to_nhs_number?
820+
821+
old_nhs_number, new_nhs_number = nhs_number_change_to_be_saved
822+
return unless old_nhs_number.blank? && new_nhs_number.present?
823+
824+
self.nhs_number_first_added_at ||= Time.current
825+
end
826+
819827
private
820828

821829
def locations_are_correct_type
@@ -839,14 +847,6 @@ def destroy_childless_parents
839847
end
840848
end
841849

842-
def set_nhs_number_first_added_at
843-
old_nhs_number, new_nhs_number = nhs_number_change_to_be_saved
844-
845-
return unless old_nhs_number.blank? && new_nhs_number.present?
846-
847-
self.nhs_number_first_added_at ||= Time.current
848-
end
849-
850850
def archive_due_to_deceased!
851851
archive_reasons =
852852
teams.map do |team|

app/models/patient_changeset.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ def set_child_attribute_if_valid(
383383
end
384384

385385
if in_pending_changes && !in_existing_patient
386-
existing_patient[attribute] = child_attributes[attribute.to_s]
386+
existing_patient.public_send(
387+
"#{attribute}=",
388+
child_attributes[attribute.to_s]
389+
)
387390
end
388391
end
389392

spec/jobs/commit_patient_changesets_job_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@
130130
expect(gae.parents).not_to be_empty
131131
end
132132

133+
it "sets nhs_number_first_added_at for newly imported patients with NHS numbers" do
134+
freeze_time do
135+
perform_job
136+
137+
timestamps =
138+
Patient.where(nhs_number: %w[9990000018 9990000026 9990000034]).pluck(
139+
:nhs_number_first_added_at
140+
)
141+
142+
expect(timestamps).to all(eq(Time.current))
143+
end
144+
end
145+
133146
it "stores statistics on the import" do
134147
# stree-ignore
135148
expect {
@@ -199,6 +212,38 @@
199212
end
200213
end
201214

215+
context "with an existing patient matching the import except for the NHS number" do
216+
let!(:patient) do
217+
create(
218+
:patient,
219+
given_name: "Jimmy",
220+
preferred_given_name: "Jim",
221+
family_name: "Smith",
222+
date_of_birth: Date.new(2010, 1, 2),
223+
nhs_number: nil,
224+
nhs_number_first_added_at: nil,
225+
address_line_1: "10 Downing Street",
226+
address_town: "London",
227+
address_postcode: "SW1A 1AA",
228+
registration: "ABC",
229+
school: location,
230+
parents: []
231+
)
232+
end
233+
234+
before { PatientChangeset.all.map(&:calculate_review_data!) }
235+
236+
it "sets nhs_number_first_added_at when an NHS number is auto-accepted onto an existing patient" do
237+
freeze_time do
238+
expect { perform_job }.to change {
239+
patient.reload.nhs_number_first_added_at
240+
}.from(nil).to(Time.current)
241+
end
242+
243+
expect(patient.reload.nhs_number).to eq("9990000026")
244+
end
245+
end
246+
202247
context "with an existing patient matching the name but a different case" do
203248
before do
204249
create(

spec/models/immunisation_import_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,19 @@
261261
.and change(immunisation_import, :new_record_count).to(11)
262262
end
263263

264+
it "sets nhs_number_first_added_at for imported patients with NHS numbers" do
265+
immunisation_import.process!
266+
267+
timestamps =
268+
immunisation_import
269+
.patients
270+
.where.not(nhs_number: nil)
271+
.pluck(:nhs_number_first_added_at)
272+
273+
expect(timestamps).not_to be_empty
274+
expect(timestamps).to all(eq(Time.current))
275+
end
276+
264277
it "ignores and counts duplicate records" do
265278
duplicate_import.parse_rows!
266279
duplicate_import.process!

spec/models/patient_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,20 @@
11031103
end
11041104
end
11051105

1106+
it "sets nhs_number_first_added_at when the NHS number was assigned before save" do
1107+
patient =
1108+
create(:patient, nhs_number: nil, nhs_number_first_added_at: nil)
1109+
patient.nhs_number = "9449310475"
1110+
pds_patient = PDS::Patient.new(nhs_number: "9449310475")
1111+
1112+
freeze_time do
1113+
expect { patient.update_from_pds!(pds_patient) }.to change(
1114+
patient,
1115+
:nhs_number_first_added_at
1116+
).from(nil).to(Time.current)
1117+
end
1118+
end
1119+
11061120
context "when the NHS number doesn't match" do
11071121
let(:pds_patient) { PDS::Patient.new(nhs_number: "abc") }
11081122

@@ -1414,6 +1428,24 @@
14141428
end
14151429
end
14161430

1431+
describe "#apply_pending_changes!" do
1432+
let(:patient) do
1433+
create(:patient, nhs_number: nil, nhs_number_first_added_at: nil)
1434+
end
1435+
1436+
before do
1437+
patient.update!(pending_changes: { "nhs_number" => "9449310475" })
1438+
end
1439+
1440+
it "sets nhs_number_first_added_at when a pending NHS number is applied" do
1441+
freeze_time do
1442+
expect { patient.apply_pending_changes! }.to change {
1443+
patient.reload.nhs_number_first_added_at
1444+
}.from(nil).to(Time.current)
1445+
end
1446+
end
1447+
end
1448+
14171449
describe "#dup_for_pending_changes" do
14181450
subject(:new_patient) { old_patient.dup_for_pending_changes }
14191451

0 commit comments

Comments
 (0)