Skip to content

Commit 9958f88

Browse files
Automatically accept preferred names (#3509)
If a patient has either `preferred_given_name` or `preferred_family_name` set to `nil` in Mavis, but a name (eg "Benny") is uploaded for a matching record (in either a class upload, or cohort upload), this is now automatically accepted, rather than prompting the user to review. [MAV-1100](https://nhsd-jira.digital.nhs.uk/browse/MAV-1100)
2 parents 324e32f + 8c7430f commit 9958f88

File tree

3 files changed

+183
-8
lines changed

3 files changed

+183
-8
lines changed

app/models/patient_import_row.rb

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,26 @@ def to_patient
4949
existing_patient.registration = attributes.delete(:registration)
5050
end
5151

52-
if attributes[:gender_code].in?(%w[male female not_specified]) &&
53-
!existing_patient.gender_code.in?(%w[male female not_specified]) &&
54-
attributes[:gender_code] != existing_patient.gender_code
55-
existing_patient.gender_code = attributes.delete(:gender_code)
56-
end
52+
auto_accept_attribute(
53+
existing_patient,
54+
attributes,
55+
:gender_code,
56+
:in?,
57+
%w[male female not_specified]
58+
)
59+
60+
auto_accept_attribute(
61+
existing_patient,
62+
attributes,
63+
:preferred_given_name,
64+
:present?
65+
)
66+
auto_accept_attribute(
67+
existing_patient,
68+
attributes,
69+
:preferred_family_name,
70+
:present?
71+
)
5772

5873
if address_postcode.present? &&
5974
address_postcode.to_postcode != existing_patient.address_postcode
@@ -207,6 +222,22 @@ def nhs_number_value
207222

208223
private
209224

225+
def auto_accept_attribute(
226+
existing_patient,
227+
attributes,
228+
attribute_name,
229+
condition_function,
230+
*condition_params
231+
)
232+
if attributes[attribute_name].send(condition_function, *condition_params) &&
233+
!existing_patient[attribute_name].send(
234+
condition_function,
235+
*condition_params
236+
)
237+
existing_patient[attribute_name] = attributes.delete(attribute_name)
238+
end
239+
end
240+
210241
def auto_overwrite_address?(existing_patient)
211242
existing_patient.address_postcode == address_postcode&.to_postcode &&
212243
[address_line_1, address_line_2, address_town].any?(&:present?)

spec/models/class_import_row_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,81 @@
304304
end
305305
end
306306

307+
context "with an existing patient without preferred names" do
308+
let(:data) do
309+
valid_data.merge(
310+
"CHILD_PREFERRED_FIRST_NAME" => "Jim",
311+
"CHILD_PREFERRED_LAST_NAME" => "Smithy"
312+
)
313+
end
314+
315+
let!(:existing_patient) do
316+
create(
317+
:patient,
318+
address_postcode: "SW1A 1AA",
319+
family_name: "Smith",
320+
given_name: "Jimmy",
321+
date_of_birth: Date.new(2010, 1, 1)
322+
)
323+
end
324+
325+
it { should eq(existing_patient) }
326+
327+
it "saves the incoming preferred names" do
328+
expect(patient).to have_attributes(
329+
preferred_given_name: "Jim",
330+
preferred_family_name: "Smithy"
331+
)
332+
end
333+
334+
it "doesn't stage the preferred names differences" do
335+
expect(patient.pending_changes).to be_empty
336+
end
337+
end
338+
339+
context "with an existing patient already with preferred names" do
340+
let(:data) do
341+
valid_data.merge(
342+
"CHILD_PREFERRED_FIRST_NAME" => "Jim",
343+
"CHILD_PREFERRED_LAST_NAME" => "Smithy"
344+
)
345+
end
346+
347+
let!(:existing_patient) do
348+
create(
349+
:patient,
350+
address_postcode: "SW1A 1AA",
351+
family_name: "Smith",
352+
given_name: "Jimmy",
353+
preferred_given_name: "Jimothy",
354+
preferred_family_name: "Smithers",
355+
nhs_number: "9990000018",
356+
address_line_1: "10 Downing Street",
357+
address_line_2: "",
358+
address_town: "London",
359+
birth_academic_year: 2009,
360+
date_of_birth: Date.new(2010, 1, 1),
361+
registration: "8AB"
362+
)
363+
end
364+
365+
it { should eq(existing_patient) }
366+
367+
it "does not save the incoming gender" do
368+
expect(patient).to have_attributes(
369+
preferred_given_name: "Jimothy",
370+
preferred_family_name: "Smithers"
371+
)
372+
end
373+
374+
it "does stage the gender differences" do
375+
expect(patient.pending_changes).to include(
376+
"preferred_given_name" => "Jim",
377+
"preferred_family_name" => "Smithy"
378+
)
379+
end
380+
end
381+
307382
context "with an existing patient without address" do
308383
let(:data) do
309384
valid_data.merge(

spec/models/cohort_import_row_spec.rb

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
"CHILD_ADDRESS_LINE_2" => "",
2828
"CHILD_TOWN" => "London",
2929
"CHILD_POSTCODE" => "SW1A 1AA",
30-
"CHILD_PREFERRED_GIVEN_NAME" => "Jim",
31-
"CHILD_DATE_OF_BIRTH" => "2010-01-01",
3230
"CHILD_FIRST_NAME" => "Jimmy",
33-
"CHILD_GENDER" => "Male",
3431
"CHILD_LAST_NAME" => "Smith",
32+
"CHILD_PREFERRED_FIRST_NAME" => "Jim",
33+
"CHILD_PREFERRED_LAST_NAME" => "Smithy",
34+
"CHILD_DATE_OF_BIRTH" => "2010-01-01",
35+
"CHILD_GENDER" => "Male",
3536
"CHILD_NHS_NUMBER" => "9990000018",
3637
"CHILD_REGISTRATION" => "8AB",
3738
"CHILD_SCHOOL_URN" => school_urn
@@ -291,6 +292,74 @@
291292
end
292293
end
293294

295+
context "with an existing patient without preferred names" do
296+
let!(:existing_patient) do
297+
create(
298+
:patient,
299+
address_postcode: "SW1A 1AA",
300+
family_name: "Smith",
301+
gender_code: "male",
302+
given_name: "Jimmy",
303+
nhs_number: "9990000018",
304+
address_line_1: "10 Downing Street",
305+
address_line_2: "",
306+
address_town: "London",
307+
birth_academic_year: 2009,
308+
date_of_birth: Date.new(2010, 1, 1),
309+
registration: "8AB"
310+
)
311+
end
312+
313+
it { should eq(existing_patient) }
314+
315+
it "saves the incoming preferred names" do
316+
expect(patient).to have_attributes(
317+
preferred_given_name: "Jim",
318+
preferred_family_name: "Smithy"
319+
)
320+
end
321+
322+
it "doesn't stage the preferred names differences" do
323+
expect(patient.pending_changes).to be_empty
324+
end
325+
end
326+
327+
context "with an existing patient already with preferred names" do
328+
let!(:existing_patient) do
329+
create(
330+
:patient,
331+
address_postcode: "SW1A 1AA",
332+
given_name: "Jimmy",
333+
family_name: "Smith",
334+
preferred_given_name: "Jimothy",
335+
preferred_family_name: "Smithers",
336+
nhs_number: "9990000018",
337+
address_line_1: "10 Downing Street",
338+
address_line_2: "",
339+
address_town: "London",
340+
birth_academic_year: 2009,
341+
date_of_birth: Date.new(2010, 1, 1),
342+
registration: "8AB"
343+
)
344+
end
345+
346+
it { should eq(existing_patient) }
347+
348+
it "does not save the incoming gender" do
349+
expect(patient).to have_attributes(
350+
preferred_given_name: "Jimothy",
351+
preferred_family_name: "Smithers"
352+
)
353+
end
354+
355+
it "does stage the gender differences" do
356+
expect(patient.pending_changes).to include(
357+
"preferred_given_name" => "Jim",
358+
"preferred_family_name" => "Smithy"
359+
)
360+
end
361+
end
362+
294363
context "with an existing patient without address (ex. postcode)" do
295364
let!(:existing_patient) do
296365
create(

0 commit comments

Comments
 (0)