From 1818404a8c3aa1da046c81f5d4249dcd5d7f0e71 Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Tue, 12 Aug 2025 15:23:39 +0100 Subject: [PATCH] Ensure aged out patients are added to community clinic This fixes a bug where patients who have aged out of their school aren't added to the community clinic when the nightly job runs, meaning they're left in a state where they exist but don't belong to any sessions, resulting in the patient being in a strange state. --- app/jobs/patients_aged_out_of_school_job.rb | 8 +++++++- spec/jobs/patients_aged_out_of_school_job_spec.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/jobs/patients_aged_out_of_school_job.rb b/app/jobs/patients_aged_out_of_school_job.rb index cd7db181da..05487a7c2d 100644 --- a/app/jobs/patients_aged_out_of_school_job.rb +++ b/app/jobs/patients_aged_out_of_school_job.rb @@ -16,7 +16,13 @@ def perform next if school.year_groups.include?(year_group) team = school.team - SchoolMove.new(patient:, home_educated: false, team:).confirm! + + SchoolMove.new( + patient:, + home_educated: false, + team:, + academic_year: + ).confirm! end end end diff --git a/spec/jobs/patients_aged_out_of_school_job_spec.rb b/spec/jobs/patients_aged_out_of_school_job_spec.rb index 73c5501a86..465daf8ad3 100644 --- a/spec/jobs/patients_aged_out_of_school_job_spec.rb +++ b/spec/jobs/patients_aged_out_of_school_job_spec.rb @@ -13,6 +13,9 @@ let(:date_of_birth) { Date.new(2008, 1, 1) } let!(:patient) { create(:patient, school:, date_of_birth:) } + let!(:clinic_session) do + team.generic_clinic_session(academic_year: AcademicYear.pending) + end context "on the last day of July" do let(:today) { Date.new(2024, 7, 31) } @@ -28,6 +31,10 @@ it "doesn't move the patient to unknown school" do expect { perform }.not_to(change { patient.reload.school }) end + + it "doesn't add the patient to the clinic session" do + expect { perform }.not_to(change { patient.sessions.count }) + end end context "on the first day of August" do @@ -49,5 +56,10 @@ it "moves the patient to unknown school" do expect { perform }.to change { patient.reload.school }.to(nil) end + + it "adds the patient to the clinic session" do + expect { perform }.to change { patient.sessions.count }.by(1) + expect(patient.sessions).to include(clinic_session) + end end end