Skip to content

Commit 96f596f

Browse files
authored
Merge pull request #5624 from nhsuk/fix-cli-schools-show
Fix mavis cli to show schools with programmes
2 parents df766ce + 16aaab1 commit 96f596f

5 files changed

Lines changed: 221 additions & 8 deletions

File tree

app/lib/mavis_cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def self.progress_bar(total)
1818
end
1919
end
2020

21+
require_relative "mavis_cli_helpers"
2122
require_relative "mavis_cli/access_log"
2223
require_relative "mavis_cli/clinics/add_to_team"
2324
require_relative "mavis_cli/clinics/create"

app/lib/mavis_cli/schools/create.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module MavisCLI
44
module Schools
55
class Create < Dry::CLI::Command
6-
desc "Create a new school"
6+
desc "Create a new school or site"
77

88
argument :urn, required: true, desc: "School URN"
99
argument :name, required: true, desc: "Name of the school"
@@ -17,7 +17,7 @@ class Create < Dry::CLI::Command
1717
required: true,
1818
desc: "GIAS phase (e.g. primary or secondary)"
1919

20-
option :site, desc: "Additional site "
20+
option :site, desc: "Additional site"
2121
option :status, desc: "Status of the school", default: "open"
2222
option :systm_one_code, desc: "SystmOne code of the school"
2323
option :url, desc: "URL of the school"
@@ -29,7 +29,7 @@ class Create < Dry::CLI::Command
2929

3030
option :gias_year_groups,
3131
type: :array,
32-
desc: "Year groups taught at the school"
32+
desc: "Year groups taught at the school (e.g. \"0,1,2,3,4,5,6\")"
3333

3434
def call(
3535
urn:,

app/lib/mavis_cli/schools/show.rb

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module MavisCLI
44
module Schools
55
class Show < Dry::CLI::Command
6+
include ::MavisCLIHelpers
7+
68
desc "Show school information"
79

810
argument :urn_or_ids,
@@ -18,8 +20,19 @@ class Show < Dry::CLI::Command
1820
type: :boolean,
1921
default: false,
2022
desc: "Find any site when searching with URN"
23+
option :show_patients,
24+
aliases: %w[-p],
25+
type: :boolean,
26+
default: false,
27+
desc: "Show patient info"
2128

22-
def call(urn_or_ids:, id: false, any_site: false, **)
29+
def call(
30+
urn_or_ids:,
31+
id: false,
32+
any_site: false,
33+
show_patients: false,
34+
**
35+
)
2336
MavisCLI.load_rails
2437

2538
if id && any_site
@@ -38,6 +51,8 @@ def call(urn_or_ids:, id: false, any_site: false, **)
3851
academic_year = AcademicYear.current
3952

4053
locations.each do |location|
54+
puts(Rainbow("-" * 64).bright) if locations.count > 1
55+
4156
location
4257
.attributes
4358
.symbolize_keys
@@ -47,6 +62,9 @@ def call(urn_or_ids:, id: false, any_site: false, **)
4762
:urn,
4863
:site,
4964
:status,
65+
:gias_phase,
66+
:gias_establishment_number,
67+
:gias_local_authority_code,
5068
:address_line_1,
5169
:address_line_2,
5270
:address_postcode,
@@ -72,15 +90,50 @@ def call(urn_or_ids:, id: false, any_site: false, **)
7290
end
7391
end
7492
else
75-
puts "#{Rainbow("team:").bright}: No team assigned"
93+
puts "#{Rainbow("team").bright}: No team assigned"
94+
end
95+
96+
puts ""
97+
print_attributes total_patients: location.patient_locations.count
98+
if show_patients
99+
patient_locations =
100+
location
101+
.patient_locations
102+
.preload(
103+
:attendance_records,
104+
:gillick_assessments,
105+
:pre_screenings,
106+
:vaccination_records
107+
)
108+
.current
109+
110+
attendance_records =
111+
location.attendance_records.for_academic_year(academic_year)
112+
gillick_assessments =
113+
location.gillick_assessments.for_academic_year(academic_year)
114+
115+
print_attributes(
116+
_indent: 1,
117+
in_current_academic_year: {
118+
_value: patient_locations.count,
119+
with_attendance_records: attendance_records.count,
120+
with_gillick_assessments: gillick_assessments.count
121+
},
122+
safe_to_destroy: patient_locations.count(&:safe_to_destroy?)
123+
)
76124
end
77125

78-
puts Rainbow("programmes:").bright
126+
puts "", Rainbow("programmes:").bright
79127
location.programmes.each do |programme|
80128
year_groups =
81129
location
82130
.location_programme_year_groups
83-
.where(programme:, academic_year:)
131+
.where(
132+
programme_type: programme.type,
133+
location_year_group: {
134+
academic_year:
135+
}
136+
)
84137
.pluck_year_groups
85138

86139
puts " #{Rainbow(programme.type).bright}:"

app/lib/mavis_cli_helpers.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require "rainbow/refinement"
4+
5+
module MavisCLIHelpers
6+
using Rainbow
7+
8+
# rubocop:disable Lint/UnderscorePrefixedVariableName
9+
# rubocop:disable Rails/Output
10+
def print_attributes(_indent: 0, **attributes)
11+
attributes.each do |key, value|
12+
if value.is_a?(Hash)
13+
nested_attributes = value
14+
value = nested_attributes.delete(:_value)
15+
end
16+
label = key.to_s.humanize(capitalize: false)
17+
puts "#{" " * _indent * 2}#{Rainbow(label).bright}: #{value}"
18+
19+
if nested_attributes
20+
print_attributes(_indent: _indent + 1, **nested_attributes)
21+
end
22+
end
23+
end
24+
# rubocop:enable Rails/Output
25+
# rubocop:enable Lint/UnderscorePrefixedVariableName
26+
end

spec/features/cli_schools_show_spec.rb

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
require_relative "../../app/lib/mavis_cli"
44

55
describe "mavis schools show" do
6+
before do
7+
# To ensure Rainbow doesn't insert escape codes into the output
8+
Rainbow.enabled = false
9+
end
10+
611
context "with just a URN" do
712
it "displays the school details" do
813
given_a_school_exists
914
when_i_run_the_command
1015
then_the_school_details_are_displayed
16+
and_the_programme_year_groups_are_displayed
1117
end
1218
end
1319

@@ -27,8 +33,111 @@
2733
end
2834
end
2935

36+
context "with a team that has patients in a variety of states" do
37+
it "displays the school details" do
38+
given_a_school_exists
39+
and_the_school_has_patients_across_academic_years
40+
when_i_run_the_command_with_the_show_patients_option
41+
then_the_correct_patient_counts_are_displayed
42+
end
43+
end
44+
3045
def given_a_school_exists
31-
@school = create(:school, name: "Test School", urn: "123456")
46+
team = create(:team, programme_types: %w[flu hpv])
47+
@school =
48+
create(
49+
:school,
50+
name: "Test School",
51+
urn: "123456",
52+
team:
53+
).tap do |location|
54+
location.import_year_groups_from_gias!(
55+
academic_year: AcademicYear.previous
56+
)
57+
58+
location.import_default_programme_year_groups!(
59+
[Programme.flu],
60+
academic_year: AcademicYear.previous
61+
)
62+
end
63+
end
64+
65+
def and_the_school_has_patients_across_academic_years
66+
location = school = @school
67+
session =
68+
create(
69+
:session,
70+
location:,
71+
team: @school.teams.first,
72+
programmes: [Programme.flu]
73+
)
74+
session_last_year =
75+
create(
76+
:session,
77+
location:,
78+
team: @school.teams.first,
79+
date: Date.new(AcademicYear.previous, 11, 1),
80+
programmes: [Programme.flu]
81+
)
82+
83+
# patient in current academic years
84+
create(:patient, school: @school, location: @school)
85+
86+
# patient in previous academic years
87+
create(:patient, academic_year: AcademicYear.previous, school:, location:)
88+
89+
# patient in current and previous academic years
90+
create(
91+
:patient,
92+
:in_attendance,
93+
school:,
94+
location:,
95+
session:
96+
).tap do |patient|
97+
create(
98+
:patient_location,
99+
patient:,
100+
location:,
101+
academic_year: AcademicYear.previous
102+
)
103+
end
104+
105+
# patient with attendance record in previous academic year
106+
create(
107+
:patient,
108+
academic_year: AcademicYear.previous,
109+
school:,
110+
location:,
111+
session: session_last_year
112+
).tap do |patient|
113+
create(
114+
:attendance_record,
115+
:present,
116+
patient:,
117+
session: session_last_year,
118+
date: session_last_year.dates.first
119+
)
120+
end
121+
122+
# patient with gillick assessment in current academic year
123+
create(:patient, school:, location:, session:).tap do |patient|
124+
create(:gillick_assessment, :competent, patient:, session:)
125+
end
126+
127+
# patient with gillick assessment record in previous academic year
128+
create(
129+
:patient,
130+
school:,
131+
location:,
132+
session: session_last_year
133+
).tap do |patient|
134+
create(
135+
:gillick_assessment,
136+
:competent,
137+
patient:,
138+
session: session_last_year
139+
)
140+
end
32141
end
33142

34143
def given_a_school_with_sites_exists
@@ -61,6 +170,15 @@ def when_i_run_the_command_with_the_any_site_option
61170
end
62171
end
63172

173+
def when_i_run_the_command_with_the_show_patients_option
174+
@output =
175+
capture_output do
176+
Dry::CLI.new(MavisCLI).call(
177+
arguments: %w[schools show --show-patients] + [@school.urn]
178+
)
179+
end
180+
end
181+
64182
def then_the_school_details_are_displayed
65183
expect(@output).to match(/name.*Test School/)
66184
expect(@output).to match(/urn.*123456/)
@@ -71,4 +189,19 @@ def then_the_school_details_with_sites_are_displayed
71189
expect(@output).to match(/urn.*123456/)
72190
expect(@output).to match(/site.*B/)
73191
end
192+
193+
def and_the_programme_year_groups_are_displayed
194+
expect(@output).to match(
195+
/flu:\s*year groups: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11/
196+
)
197+
expect(@output).to match(/hpv:\s*year groups: 8, 9, 10, 11/)
198+
end
199+
200+
def then_the_correct_patient_counts_are_displayed
201+
expect(@output).to match(/^total patients: 7/)
202+
expect(@output).to match(/^ in current academic year: 3/)
203+
expect(@output).to match(/^ with attendance records: 1/)
204+
expect(@output).to match(/^ with gillick assessments: 1/)
205+
expect(@output).to match(/^ safe to destroy: 1/)
206+
end
74207
end

0 commit comments

Comments
 (0)