Skip to content

Commit 3ada9ef

Browse files
Only reset NR objects created before a team's cutoff date
Because we weren't able to reset the piloting national reporting teams before they onboarded, we must now tweak this CLI tool to ensure that we don't affect information which the teams have uploaded since they onboarded for real, on their `national_reporting_cut_off_date`. Jira-Issue: MAV-7080
1 parent 7f67ab8 commit 3ada9ef

2 files changed

Lines changed: 138 additions & 9 deletions

File tree

app/lib/mavis_cli/teams/reset_national_reporting.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def call(workgroup: nil, force: false, **)
2929
puts "Found #{teams.count} national reporting team(s) to reset:"
3030
teams.each do |team|
3131
puts " - #{team.name} (#{team.workgroup})"
32-
puts " - Immunisation imports: #{ImmunisationImport.where(team:).count}"
32+
puts " - Cut-off date: #{team.national_reporting_cut_off_date}"
33+
puts " - Immunisation imports (before cut-off): #{find_immunisation_imports_for_team(team).count}"
3334
puts " - Total patients: #{find_patients_for_team(team).count}"
3435

3536
vaccination_records = find_vaccination_records_for_team(team)
@@ -92,8 +93,9 @@ def reset_team(team)
9293
patient_ids_to_update = Set.new
9394

9495
ActiveRecord::Base.transaction do
95-
immunisation_imports = ImmunisationImport.where(team:)
96-
puts " - Found #{immunisation_imports.count} immunisation import(s)"
96+
immunisation_imports = find_immunisation_imports_for_team(team)
97+
puts " - Found #{immunisation_imports.count} immunisation import(s) created before team's cut off date: " \
98+
"#{team.national_reporting_cut_off_date}"
9799

98100
patient_ids = find_patients_for_team(team).ids
99101
puts " - Found #{patient_ids.count} patient(s) in this team"
@@ -169,14 +171,21 @@ def reset_team(team)
169171
PatientStatusUpdaterJob.perform_bulk(patient_ids_to_update.zip)
170172
end
171173

174+
def find_immunisation_imports_for_team(team)
175+
ImmunisationImport.where(team:).where(
176+
"immunisation_imports.created_at < ?",
177+
team.national_reporting_cut_off_date
178+
)
179+
end
180+
172181
def find_patients_for_team(team)
173182
Patient.joins(:patient_teams).where(patient_teams: { team: }).distinct
174183
end
175184

176185
def find_vaccination_records_for_team(team)
177186
VaccinationRecord.joins(:immunisation_imports).where(
178187
immunisation_imports: {
179-
team:
188+
id: find_immunisation_imports_for_team(team)
180189
}
181190
)
182191
end

spec/features/cli_teams_reset_national_reporting_spec.rb

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
given_a_national_reporting_team_exists
2929
and_i_upload_some_vaccination_records
3030
and_i_view_a_patient_record
31+
and_the_cut_off_date_is_set_to_tomorrow
3132

3233
when_i_run_the_command_for_single_team
3334

@@ -109,6 +110,18 @@
109110
and_the_other_archive_reasons_are_deleted
110111
and_the_other_patients_are_deleted
111112
end
113+
114+
it "only operates on imports created before the cut-off date" do
115+
given_a_national_reporting_team_exists
116+
and_the_national_reporting_team_has_immunisation_imports_both_before_and_after_the_cut_off_date
117+
118+
when_i_run_the_command_for_single_team
119+
120+
then_the_imports_before_the_cut_off_date_are_deleted
121+
and_the_vaccination_records_from_imports_before_the_cut_off_date_are_deleted
122+
and_the_imports_on_or_after_the_cut_off_date_are_not_deleted
123+
and_the_vaccination_records_from_imports_on_or_after_the_cut_off_date_are_not_deleted
124+
end
112125
end
113126

114127
context "when resetting all national_reporting teams" do
@@ -250,8 +263,22 @@ def and_i_view_the_patient_that_is_associated_with_both_teams
250263
end
251264

252265
def and_the_national_reporting_team_has_immunisation_imports_with_vaccination_records
253-
@import1 = create(:immunisation_import, team: @national_reporting_team)
254-
@import2 = create(:immunisation_import, team: @national_reporting_team)
266+
cut_off_date =
267+
@national_reporting_team.national_reporting_cut_off_date ||
268+
Time.zone.today
269+
270+
@import1 =
271+
create(
272+
:immunisation_import,
273+
team: @national_reporting_team,
274+
created_at: cut_off_date - 2.days
275+
)
276+
@import2 =
277+
create(
278+
:immunisation_import,
279+
team: @national_reporting_team,
280+
created_at: cut_off_date - 1.day
281+
)
255282

256283
@vaccination_record1 =
257284
create(
@@ -279,7 +306,13 @@ def and_the_national_reporting_team_has_immunisation_imports_with_vaccination_re
279306
end
280307

281308
def and_a_patient_is_associated_with_both_teams
282-
@import = create(:immunisation_import, team: @national_reporting_team)
309+
cut_off_date = @national_reporting_team.national_reporting_cut_off_date
310+
@import =
311+
create(
312+
:immunisation_import,
313+
team: @national_reporting_team,
314+
created_at: cut_off_date - 1.day
315+
)
283316
@shared_patient = create(:patient)
284317
@national_reporting_vaccination_record =
285318
create(
@@ -319,8 +352,19 @@ def given_multiple_national_reporting_teams_exist
319352
end
320353

321354
def and_each_team_has_immunisation_imports
322-
@import1 = create(:immunisation_import, team: @national_reporting_team1)
323-
@import2 = create(:immunisation_import, team: @national_reporting_team2)
355+
cut_off_date = @national_reporting_team1.national_reporting_cut_off_date
356+
@import1 =
357+
create(
358+
:immunisation_import,
359+
team: @national_reporting_team1,
360+
created_at: cut_off_date - 1.day
361+
)
362+
@import2 =
363+
create(
364+
:immunisation_import,
365+
team: @national_reporting_team2,
366+
created_at: cut_off_date - 1.day
367+
)
324368

325369
@vaccination_record1 =
326370
create(
@@ -536,7 +580,83 @@ def and_the_status_updater_job_is_enqueued
536580
)
537581
end
538582

583+
def and_the_cut_off_date_is_set_to_tomorrow
584+
@national_reporting_team.update!(
585+
national_reporting_cut_off_date: Time.zone.tomorrow
586+
)
587+
end
588+
539589
def and_the_access_log_entry_is_not_deleted
540590
expect(AccessLogEntry.where(patient: @shared_patient)).to exist
541591
end
592+
593+
def and_the_national_reporting_team_has_immunisation_imports_both_before_and_after_the_cut_off_date
594+
cut_off_date = @national_reporting_team.national_reporting_cut_off_date
595+
596+
@import_before =
597+
create(
598+
:immunisation_import,
599+
team: @national_reporting_team,
600+
created_at: cut_off_date - 1.day
601+
)
602+
@import_on_cut_off =
603+
create(
604+
:immunisation_import,
605+
team: @national_reporting_team,
606+
created_at: cut_off_date
607+
)
608+
@import_after =
609+
create(
610+
:immunisation_import,
611+
team: @national_reporting_team,
612+
created_at: cut_off_date + 1.day
613+
)
614+
615+
@vaccination_record_before =
616+
create(
617+
:vaccination_record,
618+
:sourced_from_national_reporting,
619+
immunisation_import: @import_before,
620+
team: @national_reporting_team,
621+
performed_at: cut_off_date - 2.days
622+
)
623+
@vaccination_record_on_cut_off =
624+
create(
625+
:vaccination_record,
626+
:sourced_from_national_reporting,
627+
immunisation_import: @import_on_cut_off,
628+
team: @national_reporting_team,
629+
performed_at: cut_off_date
630+
)
631+
@vaccination_record_after =
632+
create(
633+
:vaccination_record,
634+
:sourced_from_national_reporting,
635+
immunisation_import: @import_after,
636+
team: @national_reporting_team,
637+
performed_at: cut_off_date + 2.days
638+
)
639+
end
640+
641+
def then_the_imports_before_the_cut_off_date_are_deleted
642+
expect(ImmunisationImport.where(id: @import_before.id)).to be_empty
643+
end
644+
645+
def and_the_vaccination_records_from_imports_before_the_cut_off_date_are_deleted
646+
expect(
647+
VaccinationRecord.where(id: @vaccination_record_before.id)
648+
).to be_empty
649+
end
650+
651+
def and_the_imports_on_or_after_the_cut_off_date_are_not_deleted
652+
expect(ImmunisationImport.where(id: @import_on_cut_off.id)).to exist
653+
expect(ImmunisationImport.where(id: @import_after.id)).to exist
654+
end
655+
656+
def and_the_vaccination_records_from_imports_on_or_after_the_cut_off_date_are_not_deleted
657+
expect(
658+
VaccinationRecord.where(id: @vaccination_record_on_cut_off.id)
659+
).to exist
660+
expect(VaccinationRecord.where(id: @vaccination_record_after.id)).to exist
661+
end
542662
end

0 commit comments

Comments
 (0)