@@ -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
6391end
0 commit comments