Skip to content

Commit e9e1016

Browse files
authored
Merge pull request #3984 from nhsuk/custom-year-group-eligibility-school
Allow custom programme year groups per school
2 parents f997932 + f8d5e92 commit e9e1016

78 files changed

Lines changed: 762 additions & 248 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/components/app_patient_session_search_result_card_component.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ def initialize(patient_session, context:, programmes: [])
6868
@context = context
6969

7070
@programmes =
71-
programmes
72-
.select { it.year_groups.include?(patient.year_group) }
73-
.presence || patient_session.programmes
71+
if programmes.present?
72+
patient_session.programmes.select { it.in?(programmes) }
73+
else
74+
patient_session.programmes
75+
end
7476
end
7577

7678
private

app/controllers/programmes/cohorts_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ class Programmes::CohortsController < Programmes::BaseController
44
include Pagy::Backend
55

66
def index
7-
birth_academic_years = @programme.birth_academic_years
7+
birth_academic_years =
8+
policy_scope(Location::ProgrammeYearGroup)
9+
.where(programme: @programme)
10+
.pluck_year_groups
11+
.map(&:to_birth_academic_year)
812

913
@patient_count_by_birth_academic_year =
1014
patients_in_organisation

app/controllers/programmes/patients_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class Programmes::PatientsController < Programmes::BaseController
77
before_action :set_search_form
88

99
def index
10+
@year_groups =
11+
policy_scope(Location::ProgrammeYearGroup).where(
12+
programme: @programme
13+
).pluck_year_groups
14+
1015
scope =
1116
policy_scope(Patient).includes(:vaccination_statuses).in_programmes(
1217
[@programme]

app/lib/govuk_notify_personalisation.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,13 @@ def batch_name
9393
end
9494

9595
def catch_up
96-
return nil if patient.nil? || programmes.empty?
97-
if patient.year_group == programmes.flat_map(&:year_groups).sort.uniq.first
98-
"no"
99-
else
100-
"yes"
101-
end
96+
return nil if patient.nil? || administered_year_groups.empty?
97+
patient.year_group == administered_year_groups.first ? "no" : "yes"
10298
end
10399

104100
def not_catch_up
105-
return nil if patient.nil? || programmes.empty?
106-
if patient.year_group == programmes.flat_map(&:year_groups).sort.uniq.first
107-
"yes"
108-
else
109-
"no"
110-
end
101+
return nil if patient.nil? || administered_year_groups.empty?
102+
patient.year_group == administered_year_groups.first ? "yes" : "no"
111103
end
112104

113105
def consent_deadline
@@ -400,6 +392,10 @@ def vaccine_side_effects
400392

401393
private
402394

395+
def administered_year_groups
396+
session&.year_groups || programmes.flat_map(&:default_year_groups).sort.uniq
397+
end
398+
403399
def programme_names
404400
@programme_names ||= programmes.map(&:name)
405401
end

app/lib/mavis_cli.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ def self.progress_bar(total)
2424
require_relative "mavis_cli/gias/check_import"
2525
require_relative "mavis_cli/gias/download"
2626
require_relative "mavis_cli/gias/import"
27+
require_relative "mavis_cli/schools/add_programme_year_group"
2728
require_relative "mavis_cli/schools/add_to_organisation"
29+
require_relative "mavis_cli/schools/remove_programme_year_group"
2830
require_relative "mavis_cli/vaccination_records/generate_fhir"
2931
require_relative "mavis_cli/vaccination_records/sync"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module Schools
5+
class AddProgrammeYearGroup < Dry::CLI::Command
6+
desc "Add programme year groups to a school"
7+
8+
argument :urn, required: true, desc: "The URN of the school"
9+
argument :programme_type,
10+
required: true,
11+
desc: "The programme to add year groups to"
12+
argument :year_groups,
13+
type: :array,
14+
required: true,
15+
desc: "The year groups to add"
16+
17+
def call(urn:, programme_type:, year_groups:, **)
18+
location = Location.school.find_by(urn:)
19+
20+
if location.nil?
21+
warn "Could not find school."
22+
return
23+
end
24+
25+
programme = Programme.find_by(type: programme_type)
26+
27+
if programme.nil?
28+
warn "Could not find programme."
29+
return
30+
end
31+
32+
ActiveRecord::Base.transaction do
33+
year_groups.each do |year_group|
34+
location.programme_year_groups.create!(programme:, year_group:)
35+
end
36+
end
37+
end
38+
end
39+
end
40+
41+
register "schools" do |prefix|
42+
prefix.register "add-programme-year-group", Schools::AddProgrammeYearGroup
43+
end
44+
end

app/lib/mavis_cli/schools/add_to_organisation.rb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module MavisCLI
44
module Schools
55
class AddToOrganisation < Dry::CLI::Command
66
desc "Add an existing school to an organisation"
7+
78
argument :ods_code,
89
required: true,
910
desc: "The ODS code of the organisation"
@@ -13,7 +14,7 @@ class AddToOrganisation < Dry::CLI::Command
1314
required: true,
1415
desc: "The URN of the school"
1516

16-
def call(ods_code:, team:, urns:)
17+
def call(ods_code:, team:, urns:, **)
1718
organisation = Organisation.find_by(ods_code:)
1819

1920
if organisation.nil?
@@ -28,19 +29,24 @@ def call(ods_code:, team:, urns:)
2829
return
2930
end
3031

31-
urns.each do |urn|
32-
location = Location.school.find_by(urn:)
32+
ActiveRecord::Base.transaction do
33+
urns.each do |urn|
34+
location = Location.school.find_by(urn:)
3335

34-
if location.nil?
35-
warn "Could not find location: #{urn}"
36-
next
37-
end
36+
if location.nil?
37+
warn "Could not find location: #{urn}"
38+
next
39+
end
3840

39-
if !location.team_id.nil? && location.team_id != team.id
40-
warn "#{urn} previously belonged to #{location.team.name}"
41-
end
41+
if !location.team_id.nil? && location.team_id != team.id
42+
warn "#{urn} previously belonged to #{location.team.name}"
43+
end
4244

43-
location.update!(team:)
45+
location.update!(team:)
46+
location.create_default_programme_year_groups!(
47+
organisation.programmes
48+
)
49+
end
4450
end
4551

4652
UnscheduledSessionsFactory.new.call
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module Schools
5+
class RemoveProgrammeYearGroup < Dry::CLI::Command
6+
desc "Remove programme year groups from a school"
7+
8+
argument :urn, required: true, desc: "The URN of the school"
9+
argument :programme_type,
10+
required: true,
11+
desc: "The programme to remove year groups from"
12+
argument :year_groups,
13+
type: :array,
14+
required: true,
15+
desc: "The year groups to remove"
16+
17+
def call(urn:, programme_type:, year_groups:, **)
18+
location = Location.school.find_by(urn:)
19+
20+
if location.nil?
21+
warn "Could not find school."
22+
return
23+
end
24+
25+
programme = Programme.find_by(type: programme_type)
26+
27+
if programme.nil?
28+
warn "Could not find programme."
29+
return
30+
end
31+
32+
ActiveRecord::Base.transaction do
33+
year_groups.each do |year_group|
34+
location
35+
.programme_year_groups
36+
.find_by(programme:, year_group:)
37+
&.destroy!
38+
end
39+
end
40+
end
41+
end
42+
end
43+
44+
register "schools" do |prefix|
45+
prefix.register "remove-programme-year-group",
46+
Schools::RemoveProgrammeYearGroup
47+
end
48+
end

app/lib/reports/offline_session_exporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def add_vaccinations_sheet(package)
4242
cached_styles = CachedStyles.new(workbook)
4343

4444
workbook.add_worksheet(name: "Vaccinations") do |sheet|
45-
sheet.add_row(columns.map { _1.to_s.upcase })
45+
sheet.add_row(columns.map { it.to_s.upcase })
4646

4747
patient_sessions.find_each do |patient_session|
4848
rows(patient_session:).each { |row| row.add_to(sheet:, cached_styles:) }

app/lib/status_updater.rb

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,45 +153,50 @@ def patient_statuses_to_import
153153
.pluck(:patient_id, :"patients.birth_academic_year")
154154
.uniq
155155
.flat_map do |patient_id, birth_academic_year|
156-
programme_ids_per_birth_academic_year
157-
.fetch(birth_academic_year, [])
156+
programme_ids_per_year_group
157+
.fetch(birth_academic_year.to_year_group, [])
158158
.map { [patient_id, it] }
159159
end
160160
end
161161

162162
def patient_session_statuses_to_import
163163
@patient_session_statuses_to_import ||=
164164
patient_sessions
165-
.joins(:patient)
166-
.pluck(:id, :"patients.birth_academic_year")
167-
.flat_map do |patient_session_id, birth_academic_year|
168-
programme_ids_per_birth_academic_year
169-
.fetch(birth_academic_year, [])
165+
.joins(:patient, :session)
166+
.pluck(:id, :"session.location_id", :"patients.birth_academic_year")
167+
.flat_map do |patient_session_id, location_id, birth_academic_year|
168+
programme_ids_per_location_id_and_year_group
169+
.fetch(location_id, {})
170+
.fetch(birth_academic_year.to_year_group, [])
170171
.map { [patient_session_id, it] }
171172
end
172173
end
173174

174175
def registration_statuses_to_import
175176
@registration_statuses_to_import ||=
176-
patient_sessions
177-
.joins(:patient)
178-
.pluck(:id, :"patients.birth_academic_year")
179-
.filter_map do |patient_session_id, birth_academic_year|
180-
if programme_ids_per_birth_academic_year.key?(birth_academic_year)
181-
[patient_session_id]
182-
end
177+
patient_session_statuses_to_import.map { [it.first] }.uniq
178+
end
179+
180+
def programme_ids_per_year_group
181+
@programme_ids_per_year_group ||=
182+
Location::ProgrammeYearGroup
183+
.distinct
184+
.pluck(:programme_id, :year_group)
185+
.each_with_object({}) do |(programme_id, year_group), hash|
186+
hash[year_group] ||= []
187+
hash[year_group] << programme_id
183188
end
184189
end
185190

186-
def programme_ids_per_birth_academic_year
187-
@programme_ids_per_birth_academic_year ||=
188-
Programme
189-
.all
190-
.each_with_object({}) do |programme, hash|
191-
programme.birth_academic_years.each do |year_group|
192-
hash[year_group] ||= []
193-
hash[year_group] << programme.id
194-
end
191+
def programme_ids_per_location_id_and_year_group
192+
@programme_ids_per_location_id_and_year_group ||=
193+
Location::ProgrammeYearGroup
194+
.distinct
195+
.pluck(:location_id, :programme_id, :year_group)
196+
.each_with_object({}) do |(location_id, programme_id, year_group), hash|
197+
hash[location_id] ||= {}
198+
hash[location_id][year_group] ||= []
199+
hash[location_id][year_group] << programme_id
195200
end
196201
end
197202
end

0 commit comments

Comments
 (0)