Skip to content

Commit 0634f35

Browse files
authored
fix(OpenSRP Deduplicator): Multiple fixes (#5681)
**Story card:** [sc-16598](https://app.shortcut.com/simpledotorg/story/16598/fix-import-api-duplications) ## Because We noticed some bugs while testing this ## This addresses - **do nothing if the old_entity is nil i.e. already merged** - **only merge medical histories when both are present** - **fix associated deduplicator calls** - **discard patients** ## Test instructions n/a
1 parent f5a30b6 commit 0634f35

File tree

5 files changed

+20
-7
lines changed

5 files changed

+20
-7
lines changed

app/services/one_off/opensrp/deduplicator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def call!
7474

7575
def deprecate_old_patients
7676
@old_patients = @duplicates.map(&:first)
77-
Patient.where(id: @old_patients).discard!
77+
Patient.where(id: @old_patients).map(&:discard!)
7878
end
7979

8080
private

app/services/one_off/opensrp/deduplicators/for_entity.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ module OneOff
22
module Opensrp
33
module Deduplicators
44
class ForEntity
5-
def self.call! old_id, new_id
6-
new(old_id, new_id).call
5+
def self.call! old_id, new_id, assoc = nil
6+
if assoc.nil?
7+
new(old_id, new_id).call!
8+
else
9+
new(old_id, new_id, assoc).call!
10+
end
711
end
812

913
def initialize old_id, new_id

app/services/one_off/opensrp/deduplicators/for_immutable_entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize old_id, new_id, assoc = nil
99
end
1010

1111
def merge
12-
old_patient.send(assoc).each do |associated|
12+
old_patient.send(@association).each do |associated|
1313
associated.patient = new_patient
1414
associated.save!
1515
end

app/services/one_off/opensrp/deduplicators/for_medical_history.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@ class ForMedicalHistory < ForMutableEntity
2323
].freeze
2424

2525
def merge
26-
new_patient.medical_history.tap do |new_medical_history|
27-
merge_old(new_medical_history, old_patient.medical_history, CHOOSING_OLD)
28-
merge_new(new_medical_history, old_patient.medical_history, CHOOSING_NEW)
26+
if new_patient.medical_history.nil?
27+
new_patient.medical_history = old_patient.medical_history
28+
new_patient.save!
29+
old_patient.medical_history.discard!
30+
else
31+
new_patient.medical_history.tap do |new_medical_history|
32+
merge_old(new_medical_history, old_patient.medical_history, CHOOSING_OLD)
33+
merge_new(new_medical_history, old_patient.medical_history, CHOOSING_NEW)
34+
end
2935
end
36+
37+
new_patient.medical_history
3038
end
3139
end
3240
end

app/services/one_off/opensrp/deduplicators/for_mutable_entity.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def merge_new(entity, old_patient, attributes)
2222
end
2323

2424
def merge_old(entity, old_entity, attributes)
25+
return if old_entity.nil?
2526
attributes.each do |attr|
2627
entity.send("#{attr}=", old_entity.send(attr))
2728
end

0 commit comments

Comments
 (0)