Skip to content

Commit 52b6bfb

Browse files
committed
Add CSVImportable#record!
This extracts the `record!` method from `ImmunisationImport` to the common concern used by it and `CohortImport` to ensure that the behaviour is the same across both import models.
1 parent 9913f07 commit 52b6bfb

File tree

6 files changed

+45
-38
lines changed

6 files changed

+45
-38
lines changed

app/controllers/cohort_lists_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def create
2727

2828
@cohort_import.process!
2929

30+
@cohort_import.record!
31+
3032
session[:last_cohort_upload_count] = @cohort_import.rows.count
3133

3234
redirect_to action: :success

app/models/cohort_import.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,8 @@ def process_row(row)
7171

7272
:new_record_count
7373
end
74+
75+
def record_rows
76+
# TODO: implement this when we create draft patients
77+
end
7478
end

app/models/concerns/csv_importable.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module CSVImportable
66
extend ActiveSupport::Concern
77

8+
include Recordable
9+
810
included do
911
attr_accessor :csv_is_malformed, :data, :rows
1012

@@ -86,6 +88,18 @@ def process!
8688
end
8789
end
8890

91+
def record!
92+
return if recorded?
93+
94+
process! unless processed?
95+
return if invalid?
96+
97+
ActiveRecord::Base.transaction do
98+
record_rows
99+
update!(recorded_at: Time.zone.now)
100+
end
101+
end
102+
89103
def remove!
90104
return if csv_removed?
91105
update!(csv_data: nil, csv_removed_at: Time.zone.now)

app/models/immunisation_import.rb

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#
3131
class ImmunisationImport < ApplicationRecord
3232
include CSVImportable
33-
include Recordable
3433

3534
belongs_to :programme
3635

@@ -42,31 +41,6 @@ class ImmunisationImport < ApplicationRecord
4241
has_many :patients
4342
end
4443

45-
def record!
46-
return if recorded?
47-
48-
process! unless processed?
49-
return if invalid?
50-
51-
recorded_at = Time.zone.now
52-
53-
ActiveRecord::Base.transaction do
54-
vaccination_records.draft.each do |vaccination_record|
55-
if (patient_session = vaccination_record.patient_session).draft?
56-
patient_session.update!(active: true)
57-
end
58-
59-
if (session = vaccination_record.session).draft?
60-
session.update!(active: true)
61-
end
62-
63-
vaccination_record.update!(recorded_at:)
64-
end
65-
66-
update!(recorded_at:)
67-
end
68-
end
69-
7044
private
7145

7246
def required_headers
@@ -116,4 +90,18 @@ def process_row(row)
11690
:not_administered_record_count
11791
end
11892
end
93+
94+
def record_rows
95+
vaccination_records.draft.each do |vaccination_record|
96+
if (patient_session = vaccination_record.patient_session).draft?
97+
patient_session.update!(active: true)
98+
end
99+
100+
if (session = vaccination_record.session).draft?
101+
session.update!(active: true)
102+
end
103+
104+
vaccination_record.update!(recorded_at: Time.zone.now)
105+
end
106+
end
119107
end

spec/models/immunisation_import_spec.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,6 @@
279279
let(:programme) { create(:programme, :flu_all_vaccines, academic_year:) }
280280
let(:file) { "valid_flu.csv" }
281281

282-
it "sets the recorded at time" do
283-
expect { record! }.to change(immunisation_import, :recorded_at).from(
284-
nil
285-
)
286-
end
287-
288282
it "records the vaccination records" do
289283
expect { record! }.to change(VaccinationRecord.recorded, :count).from(
290284
0
@@ -302,12 +296,6 @@
302296
let(:programme) { create(:programme, :hpv_all_vaccines, academic_year:) }
303297
let(:file) { "valid_hpv.csv" }
304298

305-
it "sets the recorded at time" do
306-
expect { record! }.to change(immunisation_import, :recorded_at).from(
307-
nil
308-
)
309-
end
310-
311299
it "records the vaccination records" do
312300
expect { record! }.to change(VaccinationRecord.recorded, :count).from(
313301
0

spec/support/a_csv_importable_model.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@
5757
end
5858
end
5959

60+
describe "#record!" do
61+
let(:today) { Time.zone.local(2025, 1, 1) }
62+
63+
it "sets recorded_at" do
64+
expect { travel_to(today) { subject.record! } }.to change(
65+
subject,
66+
:recorded_at
67+
).from(nil).to(today)
68+
end
69+
end
70+
6071
describe "#remove!" do
6172
let(:today) { Time.zone.local(2020, 1, 1) }
6273

0 commit comments

Comments
 (0)