@@ -42,46 +42,6 @@ class ImmunisationImport < ApplicationRecord
42
42
has_many :patients
43
43
end
44
44
45
- before_save :ensure_processed_with_count_statistics
46
-
47
- COUNT_COLUMNS = %i[
48
- exact_duplicate_record_count
49
- new_record_count
50
- not_administered_record_count
51
- ] . freeze
52
-
53
- def processed?
54
- processed_at != nil
55
- end
56
-
57
- def process!
58
- return if processed?
59
-
60
- parse_rows! if rows . nil?
61
- return if invalid?
62
-
63
- stats = COUNT_COLUMNS . index_with { |_column | 0 }
64
-
65
- ActiveRecord ::Base . transaction do
66
- save!
67
-
68
- rows . each do |row |
69
- if ( vaccination_record = row . to_vaccination_record )
70
- if vaccination_record . new_record?
71
- vaccination_record . save!
72
- stats [ :new_record_count ] += 1
73
- else
74
- stats [ :exact_duplicate_record_count ] += 1
75
- end
76
- else
77
- stats [ :not_administered_record_count ] += 1
78
- end
79
- end
80
-
81
- update! ( processed_at : Time . zone . now , **stats )
82
- end
83
- end
84
-
85
45
def record!
86
46
return if recorded?
87
47
@@ -127,6 +87,14 @@ def required_headers
127
87
]
128
88
end
129
89
90
+ def count_columns
91
+ %i[
92
+ exact_duplicate_record_count
93
+ new_record_count
94
+ not_administered_record_count
95
+ ]
96
+ end
97
+
130
98
def parse_row ( row_data )
131
99
ImmunisationImportRow . new (
132
100
data : row_data ,
@@ -136,11 +104,16 @@ def parse_row(row_data)
136
104
)
137
105
end
138
106
139
- def ensure_processed_with_count_statistics
140
- if processed? && COUNT_COLUMNS . any? { |column | send ( column ) . nil? }
141
- raise "Count statistics must be set for a processed import."
142
- elsif !processed? && COUNT_COLUMNS . any? { |column | !send ( column ) . nil? }
143
- raise "Count statistics must not be set for a non-processed import."
107
+ def process_row ( row )
108
+ if ( vaccination_record = row . to_vaccination_record )
109
+ if vaccination_record . new_record?
110
+ vaccination_record . save!
111
+ :new_record_count
112
+ else
113
+ :exact_duplicate_record_count
114
+ end
115
+ else
116
+ :not_administered_record_count
144
117
end
145
118
end
146
119
end
0 commit comments