Skip to content

Commit a9148b3

Browse files
Automatically accept incoming changes which only differ in case
1 parent bb2b211 commit a9148b3

File tree

6 files changed

+239
-7
lines changed

6 files changed

+239
-7
lines changed

app/models/concerns/pending_changes_concern.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,33 @@ module PendingChangesConcern
66
included { attribute :pending_changes, :jsonb, default: {} }
77

88
def stage_changes(attributes)
9+
need_save = false
10+
911
new_pending_changes =
1012
attributes.each_with_object({}) do |(attr, new_value), staged_changes|
1113
current_value = public_send(attr)
12-
staged_changes[attr.to_s] = new_value if new_value != current_value
14+
15+
if normalise_for_comparison(new_value) ==
16+
normalise_for_comparison(current_value)
17+
if new_value != current_value
18+
public_send("#{attr}=", new_value)
19+
pending_changes.delete(attr.to_s)
20+
need_save = true
21+
end
22+
else
23+
staged_changes[attr.to_s] = new_value
24+
end
1325
end
1426

15-
if new_pending_changes.any?
27+
if new_pending_changes.any? || need_save
1628
update!(pending_changes: pending_changes.merge(new_pending_changes))
1729
end
1830
end
1931

32+
def normalise_for_comparison(value)
33+
value.respond_to?(:downcase) ? value.downcase : value
34+
end
35+
2036
def with_pending_changes
2137
return self if pending_changes.blank?
2238

spec/models/class_import_row_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@
177177
expect(parents.first.full_name).to eq("John Smith")
178178
end
179179
end
180+
181+
context "when uploading different caps name" do
182+
let!(:existing_parent) do
183+
create(:parent, full_name: "JENNY SMITH", email: "jenny@example.com")
184+
end
185+
186+
let(:capitalised_parent_2_data) do
187+
{
188+
"PARENT_2_EMAIL" => "jenny@example.com",
189+
"PARENT_2_NAME" => "Jenny Smith"
190+
}
191+
end
192+
let(:data) { valid_data.merge(capitalised_parent_2_data) }
193+
194+
it { should include(existing_parent) }
195+
end
180196
end
181197

182198
describe "#to_patient" do
@@ -511,6 +527,59 @@
511527
expect(patient.pending_changes).to be_empty
512528
end
513529
end
530+
531+
context "with an existing patient with different capitalisation" do
532+
let(:data) do
533+
{
534+
"CHILD_ADDRESS_LINE_1" => "10 Downing Street",
535+
"CHILD_PREFERRED_FIRST_NAME" => "Jim",
536+
"CHILD_DATE_OF_BIRTH" => "2010-01-01",
537+
"CHILD_FIRST_NAME" => "Jimmy",
538+
"CHILD_GENDER" => "Male",
539+
"CHILD_LAST_NAME" => "Smith",
540+
"CHILD_PREFERRED_LAST_NAME" => "Smithy",
541+
"CHILD_NHS_NUMBER" => "9990000018",
542+
"CHILD_POSTCODE" => "sw1a 1aa",
543+
"CHILD_TOWN" => "London"
544+
}
545+
end
546+
547+
let!(:existing_patient) do
548+
create(
549+
:patient,
550+
address_postcode: "SW1A 1AA",
551+
family_name: "SMITH",
552+
gender_code: "male",
553+
given_name: "JIMMY",
554+
nhs_number: "9990000018",
555+
address_line_1: "10 DOWNING STREET",
556+
preferred_given_name: "JIM",
557+
preferred_family_name: "SMITHY",
558+
date_of_birth: Date.new(2010, 1, 1),
559+
address_town: "LONDON"
560+
)
561+
end
562+
563+
it { should eq(existing_patient) }
564+
565+
it "saves the incoming values" do
566+
expect(patient).to have_attributes(
567+
address_postcode: "SW1A 1AA",
568+
family_name: "Smith",
569+
gender_code: "male",
570+
given_name: "Jimmy",
571+
nhs_number: "9990000018",
572+
address_line_1: "10 Downing Street",
573+
preferred_given_name: "Jim",
574+
preferred_family_name: "Smithy",
575+
address_town: "London"
576+
)
577+
end
578+
579+
it "doesn't stage the capitalisation differences" do
580+
expect(patient.pending_changes).to be_empty
581+
end
582+
end
514583
end
515584

516585
describe "#to_parent_relationships" do

spec/models/class_import_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,21 @@
332332
end
333333
end
334334

335+
context "with an existing parent matching the name but a different case" do
336+
let!(:existing_parent) do
337+
create(:parent, full_name: "JOHN smith", email: "john@example.com")
338+
end
339+
340+
it "doesn't create an additional parent" do
341+
expect { process! }.to change(Parent, :count).by(4)
342+
end
343+
344+
it "changes the parent's name to the incoming version" do
345+
process!
346+
expect(existing_parent.reload.full_name).to eq("John Smith")
347+
end
348+
end
349+
335350
context "with an existing patient in a different school" do
336351
let(:patient) do
337352
create(:patient, nhs_number: "9990000018", school: create(:school))

spec/models/cohort_import_row_spec.rb

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,22 @@
178178
)
179179
end
180180
end
181+
182+
context "when uploading different caps name" do
183+
let(:capitalised_parent_2_data) do
184+
{
185+
"PARENT_2_EMAIL" => "jenny@example.com",
186+
"PARENT_2_NAME" => "Jenny Smith"
187+
}
188+
end
189+
let(:data) { valid_data.merge(capitalised_parent_2_data) }
190+
191+
let!(:existing_parent) do
192+
create(:parent, full_name: "JENNY SMITH", email: "jenny@example.com")
193+
end
194+
195+
it { should eq([existing_parent]) }
196+
end
181197
end
182198

183199
describe "#to_patient" do
@@ -242,6 +258,7 @@
242258
family_name: "Smith",
243259
gender_code: "not_known",
244260
given_name: "Jimmy",
261+
preferred_given_name: "Jim",
245262
nhs_number: "9990000018",
246263
address_line_1: "10 Downing Street",
247264
address_line_2: "",
@@ -267,14 +284,15 @@
267284
let!(:existing_patient) do
268285
create(
269286
:patient,
287+
address_line_1: "10 Downing Street",
288+
address_line_2: "",
289+
address_town: "London",
270290
address_postcode: "SW1A 1AA",
291+
given_name: "Jimmy",
271292
family_name: "Smith",
293+
preferred_given_name: "Jim",
272294
gender_code: "female",
273-
given_name: "Jimmy",
274295
nhs_number: "9990000018",
275-
address_line_1: "10 Downing Street",
276-
address_line_2: "",
277-
address_town: "London",
278296
birth_academic_year: 2009,
279297
date_of_birth: Date.new(2010, 1, 1),
280298
registration: "8AB"
@@ -366,6 +384,7 @@
366384
:patient,
367385
family_name: "Smith",
368386
given_name: "Jimmy",
387+
preferred_given_name: "Jim",
369388
gender_code: "male",
370389
nhs_number: "9990000018",
371390
birth_academic_year: 2009,
@@ -400,6 +419,7 @@
400419
:patient,
401420
family_name: "Smith",
402421
given_name: "Jimmy",
422+
preferred_given_name: "Jim",
403423
gender_code: "male",
404424
nhs_number: "9990000018",
405425
birth_academic_year: 2009,
@@ -465,6 +485,60 @@
465485
)
466486
end
467487
end
488+
489+
context "with an existing patient with different capitalisation" do
490+
let(:data) do
491+
{
492+
"CHILD_ADDRESS_LINE_1" => "10 Downing Street",
493+
"CHILD_PREFERRED_FIRST_NAME" => "Jim",
494+
"CHILD_DATE_OF_BIRTH" => "2010-01-01",
495+
"CHILD_FIRST_NAME" => "Jimmy",
496+
"CHILD_GENDER" => "Male",
497+
"CHILD_LAST_NAME" => "Smith",
498+
"CHILD_PREFERRED_LAST_NAME" => "Smithy",
499+
"CHILD_NHS_NUMBER" => "9990000018",
500+
"CHILD_POSTCODE" => "sw1a 1aa",
501+
"CHILD_SCHOOL_URN" => school_urn,
502+
"CHILD_TOWN" => "London"
503+
}
504+
end
505+
506+
let!(:existing_patient) do
507+
create(
508+
:patient,
509+
address_postcode: "SW1A 1AA",
510+
family_name: "SMITH",
511+
gender_code: "male",
512+
given_name: "JIMMY",
513+
nhs_number: "9990000018",
514+
address_line_1: "10 DOWNING STREET",
515+
preferred_given_name: "JIM",
516+
preferred_family_name: "SMITHY",
517+
date_of_birth: Date.new(2010, 1, 1),
518+
address_town: "LONDON"
519+
)
520+
end
521+
522+
it { should eq(existing_patient) }
523+
524+
it "saves the incoming values" do
525+
expect(patient).to have_attributes(
526+
address_postcode: "SW1A 1AA",
527+
family_name: "Smith",
528+
gender_code: "male",
529+
given_name: "Jimmy",
530+
nhs_number: "9990000018",
531+
address_line_1: "10 Downing Street",
532+
preferred_given_name: "Jim",
533+
preferred_family_name: "Smithy",
534+
address_town: "London"
535+
)
536+
end
537+
538+
it "doesn't stage the capitalisation differences" do
539+
expect(patient.pending_changes).to be_empty
540+
end
541+
end
468542
end
469543

470544
describe "#to_school_move" do

spec/models/cohort_import_spec.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@
298298
end
299299

300300
context "with an existing patient matching the name but a different case" do
301-
before do
301+
let!(:existing_patient) do
302302
create(
303303
:patient,
304304
given_name: "JIMMY",
@@ -312,6 +312,37 @@
312312
it "doesn't create an additional patient" do
313313
expect { process! }.to change(Patient, :count).by(2)
314314
end
315+
316+
it "doesn't stage the changes to the names" do
317+
process!
318+
expect(existing_patient.reload.pending_changes).not_to have_key(
319+
"given_name"
320+
)
321+
expect(existing_patient.reload.pending_changes).not_to have_key(
322+
"family_name"
323+
)
324+
end
325+
326+
it "automatically accepts the incoming case differences" do
327+
process!
328+
expect(existing_patient.reload.given_name).to eq("Jimmy")
329+
expect(existing_patient.reload.family_name).to eq("Smith")
330+
end
331+
end
332+
333+
context "with an existing parent matching the name but a different case" do
334+
let!(:existing_parent) do
335+
create(:parent, full_name: "JOHN smith", email: "john@example.com")
336+
end
337+
338+
it "doesn't create an additional parent" do
339+
expect { process! }.to change(Parent, :count).by(2)
340+
end
341+
342+
it "changes the parent's name to the incoming version" do
343+
process!
344+
expect(existing_parent.reload.full_name).to eq("John Smith")
345+
end
315346
end
316347

317348
context "with an existing patient that was previously removed from cohort" do

spec/models/immunisation_import_row_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,33 @@
14871487
end
14881488
end
14891489

1490+
context "with an existing matching patient but mismatching capitalisation, without NHS number" do
1491+
let(:data) do
1492+
valid_data.except("NHS_NUMBER").merge(
1493+
{
1494+
"PERSON_FORENAME" => "RON",
1495+
"PERSON_SURNAME" => "WEASLEY",
1496+
"PERSON_POSTCODE" => "sw1a 1aa"
1497+
}
1498+
)
1499+
end
1500+
1501+
let!(:existing_patient) do
1502+
create(
1503+
:patient,
1504+
given_name: "Ron",
1505+
family_name: "Weasley",
1506+
date_of_birth: Date.parse(date_of_birth),
1507+
address_postcode:,
1508+
nhs_number: "9990000018"
1509+
)
1510+
end
1511+
1512+
it "still matches to a patient" do
1513+
expect(patient).to eq(existing_patient)
1514+
end
1515+
end
1516+
14901517
describe "#address_postcode" do
14911518
subject { patient.address_postcode }
14921519

0 commit comments

Comments
 (0)