dde_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DdeService
4
+ ##
5
+ # Matches local and remote (Dde) people.
6
+ #
7
+ # TODO: Move module to own file
8
+ module Matcher
9
+ class << self
10
+ def find_differences(local_person, remote_person)
11
+ FIELDS_TO_MATCH
12
+ .map { |field| [field, diff_field(field, local_person, remote_person)] }
13
+ .reject { |_field, diff| diff.nil? }
14
+ .each_with_object({}) { |sub_diff, diff| diff[sub_diff[0]] = sub_diff[1] }
15
+ end
16
+
17
+ private
18
+
19
+ FIELDS_TO_MATCH = %w[given_name family_name birthdate birthdate_estimated gender
20
+ current_village current_traditional_authority current_district
21
+ home_village home_traditional_authority home_district
22
+ npid].freeze
23
+
24
+ LOCATION_FIELDS = %w[current_village current_traditional_authority current_district
25
+ home_village home_traditional_authority home_district].freeze
26
+
27
+ def diff_field(field, local_person, remote_person)
28
+ return diff_location(field, local_person, remote_person) if LOCATION_FIELDS.include?(field)
29
+
30
+ send("diff_#{field}", local_person, remote_person)
31
+ end
32
+
33
+ def diff_given_name(local_person, remote_person)
34
+ local_name = PersonName.find_by_person_id(local_person.person_id)
35
+
36
+ return nil if local_name&.given_name&.casecmp?(remote_person['given_name'])
37
+
38
+ { local: local_name.given_name, remote: remote_person['given_name'] }
39
+ end
40
+
41
+ def diff_family_name(local_person, remote_person)
42
+ local_name = PersonName.find_by_person_id(local_person.person_id)
43
+
44
+ return nil if local_name&.family_name&.casecmp?(remote_person['family_name'])
45
+
46
+ { local: local_name.family_name, remote: remote_person['family_name'] }
47
+ end
48
+
49
+ def diff_birthdate(local_person, remote_person)
50
+ return nil if local_person.birthdate&.to_date == remote_person['birthdate'].to_date
51
+
52
+ { local: local_person.birthdate, remote: remote_person['birthdate'] }
53
+ end
54
+
55
+ def diff_birthdate_estimated(local_person, remote_person)
56
+ birthdate_estimated = local_person.birthdate_estimated.positive?
57
+
58
+ return nil if birthdate_estimated == remote_person['birthdate_estimated']
59
+
60
+ { local: birthdate_estimated, remote: remote_person['birthdate_estimated'] }
61
+ end
62
+
63
+ def diff_gender(local_person, remote_person)
64
+ return nil if local_person.gender.first.casecmp?(remote_person['gender'].first)
65
+
66
+ { local: local_person.gender, remote: remote_person['gender'] }
67
+ end
68
+
69
+ def diff_npid(local_person, remote_person)
70
+ npid_type = PatientIdentifierType.where(name: 'National id')
71
+ local_npid = PatientIdentifier.find_by(patient_id: local_person.person_id, identifier_type: npid_type)&.identifier
72
+
73
+ return nil if local_npid&.casecmp?(remote_person['npid'])
74
+
75
+ { local: local_npid, remote: remote_person['npid'] }
76
+ end
77
+
78
+ def diff_location(field, local_person, remote_person)
79
+ local_address = PersonAddress.find_by_person_id(local_person.person_id)
80
+ remote_address = remote_person['attributes']
81
+
82
+ return nil if local_address.send(field).casecmp?(remote_address[field])
83
+ { local: local_address.send(field), remote: remote_address[field] }
84
+ end
85
+
86
+ def current_location_id
87
+ Location.current_health_center.location_id
88
+ end
89
+ end
90
+ end
91
+
92
+ end