Skip to content

Commit f2310f6

Browse files
Alistair Davidsonthomasleese
authored andcommitted
Add method & CLI task to populate ReportingAPI::VaccinationEvents for VaccinationRecords
linter-compliant version using Time.zone.parse after the .load_rails call
1 parent de304d6 commit f2310f6

File tree

5 files changed

+407
-6
lines changed

5 files changed

+407
-6
lines changed

app/lib/mavis_cli.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,16 @@ def self.progress_bar(total)
2929
require_relative "mavis_cli/gias/download"
3030
require_relative "mavis_cli/gias/import"
3131
require_relative "mavis_cli/gp_practices/import"
32-
require_relative "mavis_cli/nhs_api/access_token"
33-
require_relative "mavis_cli/pds/get"
34-
require_relative "mavis_cli/pds/search"
3532
require_relative "mavis_cli/local_authorities/download"
3633
require_relative "mavis_cli/local_authorities/download_gias_codes"
34+
require_relative "mavis_cli/local_authorities/download_postcode_mappings"
3735
require_relative "mavis_cli/local_authorities/import"
3836
require_relative "mavis_cli/local_authorities/import_gias_codes"
39-
require_relative "mavis_cli/local_authorities/download_postcode_mappings"
4037
require_relative "mavis_cli/local_authorities/import_postcode_mappings"
38+
require_relative "mavis_cli/nhs_api/access_token"
39+
require_relative "mavis_cli/pds/get"
40+
require_relative "mavis_cli/pds/search"
41+
require_relative "mavis_cli/reporting_api/update_vaccination_events"
4142
require_relative "mavis_cli/schools/add_programme_year_group"
4243
require_relative "mavis_cli/schools/add_to_team"
4344
require_relative "mavis_cli/schools/create"
@@ -53,7 +54,7 @@ def self.progress_bar(total)
5354
require_relative "mavis_cli/teams/list"
5455
require_relative "mavis_cli/teams/onboard"
5556
require_relative "mavis_cli/users/create"
56-
require_relative "mavis_cli/vaccination_records/generate_fhir"
57-
require_relative "mavis_cli/vaccination_records/sync"
5857
require_relative "mavis_cli/vaccination_records/create_from_fhir"
58+
require_relative "mavis_cli/vaccination_records/generate_fhir"
5959
require_relative "mavis_cli/vaccination_records/search_imms_api"
60+
require_relative "mavis_cli/vaccination_records/sync"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module ReportingAPI
5+
class UpdateVaccinationEvents < Dry::CLI::Command
6+
desc <<-DESC
7+
Update ReportingAPI Vaccination Events for all VaccinationRecords created after a given DateTime.
8+
DateTimes can be given in any format parsable by Time.parse.
9+
Defaults to one year ago.
10+
DESC
11+
12+
option :from,
13+
desc: "Only consider vaccination records created after this time",
14+
type: :string,
15+
optional: true,
16+
aliases: %w[--from],
17+
default: nil
18+
19+
def call(from: nil, **)
20+
MavisCLI.load_rails
21+
22+
min_datetime = from ? Time.zone.parse(from) : (Time.zone.now - 1.year)
23+
24+
vaccination_records =
25+
VaccinationRecord.where("created_at > ?", min_datetime)
26+
puts "#{records.count} VaccinationRecords created since #{min_datetime.iso8601}"
27+
if vaccination_records.exists?
28+
puts "Updating VaccinationEvents"
29+
progress_bar = MavisCLI.progress_bar(vaccination_records.count)
30+
31+
vaccination_records.find_each do |vaccination_record|
32+
vaccination_record.create_or_update_reporting_api_vaccination_event!
33+
progress_bar.increment
34+
end
35+
else
36+
puts "Nothing to do"
37+
end
38+
end
39+
end
40+
end
41+
42+
register "reporting-api" do |prefix|
43+
prefix.register "update-vaccination-events",
44+
ReportingAPI::UpdateVaccinationEvents
45+
end
46+
end

app/models/vaccination_record.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,34 @@ def snomed_procedure_code = vaccine&.snomed_procedure_code(dose_sequence:)
230230

231231
delegate :snomed_procedure_term, to: :vaccine, allow_nil: true
232232

233+
def create_or_update_reporting_api_vaccination_event!
234+
re =
235+
ReportingAPI::VaccinationEvent.find_or_initialize_by(
236+
source_id: id,
237+
source_type: self.class.name
238+
)
239+
re.event_timestamp = performed_at
240+
re.event_type = outcome
241+
242+
re.copy_attributes_from_references(
243+
patient: patient.reload,
244+
patient_local_authority_from_postcode:
245+
patient.local_authority_from_postcode,
246+
patient_school: patient.school,
247+
patient_school_local_authority: patient.school&.local_authority,
248+
location:,
249+
location_local_authority: location&.local_authority,
250+
vaccination_record: self,
251+
vaccine:,
252+
team:,
253+
organisation: team.organisation,
254+
programme:
255+
)
256+
257+
re.save!
258+
re
259+
end
260+
233261
private
234262

235263
def requires_location_name? = location.nil?
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../app/lib/mavis_cli"
4+
5+
describe "mavis reporting-api update-vaccination-events" do
6+
it "generates Reporting API Vaccination Event records" do
7+
given_a_team_exists
8+
and_there_is_a_patient_in_a_session
9+
and_there_are_some_vaccination_records
10+
when_i_run_the_reporting_api_update_vaccination_events_command
11+
then_reporting_api_vaccination_events_are_created
12+
end
13+
14+
it "updates existing Reporting API Vaccination Event records" do
15+
given_a_team_exists
16+
and_there_is_a_patient_in_a_session
17+
and_there_are_some_vaccination_records
18+
and_the_vaccination_records_already_have_reporting_api_vaccination_events
19+
when_i_run_the_reporting_api_update_vaccination_events_command
20+
then_it_does_not_generate_new_vaccination_event_records
21+
and_it_updates_the_existing_vaccination_event_records
22+
end
23+
24+
it "only affects Vaccination Records created after the given from_datetime" do
25+
given_a_team_exists
26+
and_there_is_a_patient_in_a_session
27+
and_there_are_some_vaccination_records
28+
when_i_run_the_reporting_api_update_vaccination_events_command_with_a_from_datetime
29+
then_event_records_are_only_created_for_vaccination_records_created_after_the_given_from_datetime
30+
end
31+
32+
def given_a_team_exists
33+
@programme =
34+
Programme.find_by(type: "hpv") || create(:programme, type: "hpv")
35+
@team = create(:team, programmes: [@programme])
36+
end
37+
38+
def nurse
39+
User.find_by(fallback_role: :nurse) || create(:nurse)
40+
end
41+
42+
def and_there_is_a_patient_in_a_session
43+
subteam = create(:subteam, team: @team)
44+
location = create(:generic_clinic, subteam:)
45+
@session =
46+
create(:session, team: @team, programmes: [@programme], location:)
47+
parent = create(:parent)
48+
@patient =
49+
create(
50+
:patient,
51+
:consent_given_triage_not_needed,
52+
team: @team,
53+
session: @session,
54+
programmes: [@programme],
55+
parents: [parent]
56+
)
57+
@other_patient =
58+
create(
59+
:patient,
60+
:consent_given_triage_not_needed,
61+
team: @team,
62+
session: @session,
63+
programmes: [@programme],
64+
parents: [parent]
65+
)
66+
end
67+
68+
def and_there_are_some_vaccination_records
69+
@vr1 =
70+
create(
71+
:vaccination_record,
72+
patient: @patient,
73+
session: @session,
74+
location: @session.location,
75+
performed_by: nurse,
76+
programme: @programme,
77+
created_at: Time.current - 1.day,
78+
updated_at: Time.current - 1.day
79+
)
80+
@vr2 =
81+
create(
82+
:vaccination_record,
83+
patient: @other_patient,
84+
session: @session,
85+
location: @session.location,
86+
performed_by: nurse,
87+
programme: @programme,
88+
created_at: Time.current - 4.hours,
89+
updated_at: Time.current - 4.hours
90+
)
91+
end
92+
93+
def and_the_vaccination_records_already_have_reporting_api_vaccination_events
94+
VaccinationRecord.all.find_each(
95+
&:create_or_update_reporting_api_vaccination_event!
96+
)
97+
end
98+
99+
def when_i_run_the_reporting_api_update_vaccination_events_command
100+
@vaccination_events_count_before = ReportingAPI::VaccinationEvent.count
101+
102+
Dry::CLI.new(MavisCLI).call(
103+
arguments: %w[reporting-api update-vaccination-events]
104+
)
105+
end
106+
107+
def when_i_run_the_reporting_api_update_vaccination_events_command_with_a_from_datetime
108+
@vaccination_events_count_before = ReportingAPI::VaccinationEvent.count
109+
110+
Dry::CLI.new(MavisCLI).call(
111+
arguments: [
112+
"reporting-api",
113+
"update-vaccination-events",
114+
"--from",
115+
(Time.current - 8.hours).iso8601.to_s
116+
]
117+
)
118+
end
119+
120+
def then_reporting_api_vaccination_events_are_created
121+
expect(ReportingAPI::VaccinationEvent.count).to be >
122+
@vaccination_events_count_before
123+
end
124+
125+
def then_event_records_are_only_created_for_vaccination_records_created_after_the_given_from_datetime
126+
expect(ReportingAPI::VaccinationEvent.count).to eq(1)
127+
expect(ReportingAPI::VaccinationEvent.last.source_id).to eq(@vr2.id)
128+
end
129+
130+
def then_it_does_not_generate_new_vaccination_event_records
131+
expect(
132+
ReportingAPI::VaccinationEvent.count
133+
).to eq @vaccination_events_count_before
134+
end
135+
136+
def and_it_updates_the_existing_vaccination_event_records
137+
expect(ReportingAPI::VaccinationEvent.pluck(:updated_at)).to all be_within(
138+
1.second
139+
).of(Time.current)
140+
end
141+
end

0 commit comments

Comments
 (0)