Skip to content

Commit ae36b3d

Browse files
authored
feat(OpenSRP): Deduplicator (#5679)
…continuing #5677
1 parent 5897d63 commit ae36b3d

File tree

5 files changed

+44
-60
lines changed

5 files changed

+44
-60
lines changed

app/services/one_off/opensrp/deduplicator.rb

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,50 @@ def query
2121
end
2222

2323
class Deduplicator
24-
AFFECTED_ENTITIES = %w[
25-
Appointment
26-
BloodPressure
27-
BloodSugar
24+
# The order is important here. Patient must be last.
25+
MUTABLE_ENTITIES = %w[
2826
MedicalHistory
29-
PrescriptionDrug
3027
Patient
3128
].freeze
32-
# The order is important here. Patient must be last.
3329

34-
def self.call!
35-
new.call!
30+
# These are the immutable associations to the patient record, for which
31+
# our merge strategy is to point the old record to the new patient.
32+
# i.e. take the old record and set the patient_id to the new record
33+
IMMUTABLE_PATIENT_ASSOCIATIONS = %i[
34+
appointments
35+
blood_pressures
36+
blood_sugars
37+
call_results
38+
encounters
39+
notifications
40+
phone_numbers
41+
prescription_drugs
42+
teleconsultations
43+
treatment_group_memberships
44+
]
45+
46+
def self.call! duplicates = nil
47+
new(duplicates).call!
3648
end
3749

38-
def initialize
39-
@duplicates = ImportedDuplicatesQuery.call
50+
def initialize duplicates
51+
@duplicates = if duplicates.nil?
52+
ImportedDuplicatesQuery.call
53+
else
54+
duplicates
55+
end
4056
end
4157

4258
def call!
4359
@duplicates.each do |old_id, new_id|
44-
AFFECTED_ENTITIES.each do |entity|
45-
deduplicator = [
46-
Module.nesting[1],
47-
"Deduplicators",
48-
"For#{entity}"
49-
].join("::").constantize
60+
IMMUTABLE_PATIENT_ASSOCIATIONS.each do |association|
61+
entity = "ImmutableEntity"
62+
deduplicator = deduplicator_class(entity)
63+
deduplicator.call! old_id, new_id, association
64+
end
65+
66+
MUTABLE_ENTITIES.each do |entity|
67+
deduplicator = deduplicator_class(entity)
5068
deduplicator.call! old_id, new_id
5169
end
5270
end
@@ -58,6 +76,16 @@ def deprecate_old_patients
5876
@old_patients = @duplicates.map(&:first)
5977
Patient.where(id: @old_patients).discard!
6078
end
79+
80+
private
81+
82+
def deduplicator_class entity
83+
[
84+
Module.nesting[1],
85+
"Deduplicators",
86+
"For#{entity}"
87+
].join("::").constantize
88+
end
6189
end
6290
end
6391
end

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

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

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)