Skip to content

Commit 27a2a23

Browse files
authored
Merge pull request #6478 from NHSDigital/record-careplus-exports-in-db
Record careplus exports in database
2 parents d00d8da + 7bd4b6c commit 27a2a23

9 files changed

Lines changed: 561 additions & 214 deletions

app/lib/mavis_cli/reports/export_automated_careplus.rb

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,57 @@ def call(
9696

9797
academic_year_value = academic_year&.to_i || AcademicYear.current
9898

99-
csv =
100-
::Reports::AutomatedCareplusExporter.call(
99+
records =
100+
::Reports::AutomatedCareplusExporter.vaccination_records_scope(
101101
team:,
102102
academic_year: academic_year_value,
103103
start_date: parsed_start_date,
104104
end_date: parsed_end_date
105105
)
106106

107+
csv =
108+
::Reports::AutomatedCareplusExporter.from_records(
109+
team:,
110+
academic_year: academic_year_value,
111+
vaccination_records: records
112+
)
113+
114+
now = Time.current
115+
116+
# we'll create the export with status "sent" for now
117+
# in the future we'll change this to "pending" or
118+
# prevent this tool from creating database entries at all
119+
ActiveRecord::Base.transaction do
120+
careplus_export =
121+
CareplusExport.create!(
122+
team:,
123+
academic_year: academic_year_value,
124+
date_from: parsed_start_date,
125+
date_to: parsed_end_date,
126+
programme_types: team.programme_types,
127+
scheduled_at: now,
128+
sent_at: now,
129+
status: :sent,
130+
csv_filename: File.basename(output),
131+
csv_data: csv
132+
)
133+
134+
if records.any?
135+
now_iso = now.iso8601(6)
136+
CareplusExportVaccinationRecord.insert_all!(
137+
records.map do |record|
138+
{
139+
careplus_export_id: careplus_export.id,
140+
vaccination_record_id: record.id,
141+
change_type: 0,
142+
created_at: now_iso,
143+
updated_at: now_iso
144+
}
145+
end
146+
)
147+
end
148+
end
149+
107150
File.write(output, csv)
108151
puts "Exported to #{output}"
109152
end
Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,61 @@
11
# frozen_string_literal: true
22

33
class Reports::AutomatedCareplusExporter
4+
VACCINE_COLUMNS = %i[
5+
vaccine
6+
dose
7+
reason_not_given
8+
site
9+
manufacturer
10+
batch_number
11+
].freeze
12+
413
def self.call(team:, academic_year:, start_date:, end_date:)
514
Reports::CareplusExporter.call(
15+
**shared_args(team:, academic_year:),
16+
start_date:,
17+
end_date:,
18+
include_missing_nhs_number: false
19+
)
20+
end
21+
22+
def self.from_records(vaccination_records:, team:, academic_year:)
23+
Reports::CareplusExporter.from_records(
24+
**shared_args(team:, academic_year:),
25+
vaccination_records:
26+
vaccination_records.includes(
27+
:patient,
28+
:vaccine,
29+
session: %i[location team_location]
30+
)
31+
)
32+
end
33+
34+
def self.vaccination_records_scope(
35+
team:,
36+
academic_year:,
37+
start_date:,
38+
end_date:
39+
)
40+
Reports::CareplusExporter.vaccination_records_scope(
641
team:,
742
programmes: team.programmes,
843
academic_year:,
944
start_date:,
1045
end_date:,
11-
include_gender: false,
12-
include_missing_nhs_number: false,
13-
vaccine_columns: %i[
14-
vaccine
15-
dose
16-
reason_not_given
17-
site
18-
manufacturer
19-
batch_number
20-
]
46+
include_missing_nhs_number: false
2147
)
2248
end
2349

24-
private_class_method :new
50+
def self.shared_args(team:, academic_year:)
51+
{
52+
team:,
53+
programmes: team.programmes,
54+
academic_year:,
55+
include_gender: false,
56+
vaccine_columns: VACCINE_COLUMNS
57+
}
58+
end
59+
60+
private_class_method :new, :shared_args
2561
end

app/lib/reports/careplus_exporter.rb

Lines changed: 76 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,15 @@ def initialize(
3030
team:,
3131
programmes:,
3232
academic_year:,
33-
start_date:,
34-
end_date:,
33+
vaccination_records:,
3534
include_gender:,
36-
include_missing_nhs_number:,
3735
vaccine_columns:
3836
)
3937
@team = team
4038
@programmes = programmes
4139
@academic_year = academic_year
42-
@start_date = start_date
43-
@end_date = end_date
40+
@vaccination_records = vaccination_records
4441
@include_gender = include_gender
45-
@include_missing_nhs_number = include_missing_nhs_number
4642
@vaccine_columns = vaccine_columns
4743
end
4844

@@ -57,7 +53,79 @@ def call
5753
end
5854
end
5955

60-
def self.call(...) = new(...).call
56+
def self.call(
57+
team:,
58+
programmes:,
59+
academic_year:,
60+
start_date:,
61+
end_date:,
62+
include_gender:,
63+
include_missing_nhs_number:,
64+
vaccine_columns:
65+
)
66+
vaccination_records =
67+
vaccination_records_scope(
68+
team:,
69+
programmes:,
70+
academic_year:,
71+
start_date:,
72+
end_date:,
73+
include_missing_nhs_number:
74+
).includes(:patient, :vaccine, session: %i[location team_location])
75+
76+
from_records(
77+
vaccination_records:,
78+
team:,
79+
programmes:,
80+
academic_year:,
81+
include_gender:,
82+
vaccine_columns:
83+
)
84+
end
85+
86+
def self.from_records(
87+
vaccination_records:,
88+
team:,
89+
programmes:,
90+
academic_year:,
91+
include_gender:,
92+
vaccine_columns:
93+
)
94+
new(
95+
vaccination_records:,
96+
team:,
97+
programmes:,
98+
academic_year:,
99+
include_gender:,
100+
vaccine_columns:
101+
).call
102+
end
103+
104+
def self.vaccination_records_scope(
105+
team:,
106+
programmes:,
107+
academic_year:,
108+
start_date:,
109+
end_date:,
110+
include_missing_nhs_number:
111+
)
112+
scope =
113+
team
114+
.vaccination_records
115+
.sourced_from_service
116+
.for_programmes(programmes)
117+
.for_academic_year(academic_year)
118+
.administered
119+
.order_by_performed_at
120+
.created_or_updated_between(start_date, end_date)
121+
122+
scope =
123+
scope.joins(:patient).merge(
124+
Patient.with_nhs_number
125+
) unless include_missing_nhs_number
126+
127+
scope
128+
end
61129

62130
private_class_method :new
63131

@@ -73,10 +141,8 @@ def self.call(...) = new(...).call
73141
attr_reader :team,
74142
:programmes,
75143
:academic_year,
76-
:start_date,
77-
:end_date,
144+
:vaccination_records,
78145
:include_gender,
79-
:include_missing_nhs_number,
80146
:vaccine_columns
81147

82148
def headers
@@ -120,52 +186,6 @@ def gender_row_value(patient)
120186
include_gender ? [GENDER_CODE_MAPPINGS[patient.gender_code]] : []
121187
end
122188

123-
def vaccination_records
124-
scope =
125-
VaccinationRecord
126-
.kept
127-
.sourced_from_service
128-
.for_programmes(programmes)
129-
.where(team_location: { team_id: team.id })
130-
.for_academic_year(academic_year)
131-
.administered
132-
.order_by_performed_at
133-
.includes(:patient, :vaccine, session: %i[location team_location])
134-
135-
if start_date.present?
136-
scope =
137-
scope.where(
138-
"vaccination_records.created_at >= ?",
139-
start_date.beginning_of_day
140-
).or(
141-
scope.where(
142-
"vaccination_records.updated_at >= ?",
143-
start_date.beginning_of_day
144-
)
145-
)
146-
end
147-
148-
if end_date.present?
149-
scope =
150-
scope.where(
151-
"vaccination_records.created_at <= ?",
152-
end_date.end_of_day
153-
).or(
154-
scope.where(
155-
"vaccination_records.updated_at <= ?",
156-
end_date.end_of_day
157-
)
158-
)
159-
end
160-
161-
scope =
162-
scope.joins(:patient).merge(
163-
Patient.with_nhs_number
164-
) unless include_missing_nhs_number
165-
166-
scope
167-
end
168-
169189
def consents
170190
@consents ||=
171191
Consent

app/lib/reports/programme_vaccinations_exporter.rb

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -88,48 +88,20 @@ def headers
8888
end
8989

9090
def vaccination_records
91-
scope =
92-
team
93-
.vaccination_records
94-
.sourced_from_service
95-
.for_programme(programme)
96-
.for_academic_year(academic_year)
97-
.includes(
98-
:location,
99-
:performed_by_user,
100-
:session,
101-
:supplied_by,
102-
:vaccine,
103-
patient: %i[programme_statuses gp_practice school]
104-
)
105-
106-
if start_date.present?
107-
scope =
108-
scope.where(
109-
"vaccination_records.created_at >= ?",
110-
start_date.beginning_of_day
111-
).or(
112-
scope.where(
113-
"vaccination_records.updated_at >= ?",
114-
start_date.beginning_of_day
115-
)
116-
)
117-
end
118-
119-
if end_date.present?
120-
scope =
121-
scope.where(
122-
"vaccination_records.created_at <= ?",
123-
end_date.end_of_day
124-
).or(
125-
scope.where(
126-
"vaccination_records.updated_at <= ?",
127-
end_date.end_of_day
128-
)
129-
)
130-
end
131-
132-
scope
91+
team
92+
.vaccination_records
93+
.sourced_from_service
94+
.for_programme(programme)
95+
.for_academic_year(academic_year)
96+
.created_or_updated_between(start_date, end_date)
97+
.includes(
98+
:location,
99+
:performed_by_user,
100+
:session,
101+
:supplied_by,
102+
:vaccine,
103+
patient: %i[programme_statuses gp_practice school]
104+
)
133105
end
134106

135107
def consents

0 commit comments

Comments
 (0)