Skip to content

Commit b66fb90

Browse files
committed
Add programmes option to add-to-organisation
When adding schools to an organisation we will sometimes want that school to only administer a subset of programmes. This commit makes that possible by adding a `--programmes` option to the CLI tool. Jira-Issue: MAV-1293
1 parent e9e1016 commit b66fb90

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

app/lib/mavis_cli/schools/add_to_organisation.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ class AddToOrganisation < Dry::CLI::Command
1414
required: true,
1515
desc: "The URN of the school"
1616

17-
def call(ods_code:, team:, urns:, **)
17+
option :programmes,
18+
type: :array,
19+
desc: "The programmes administered at the school"
20+
21+
def call(ods_code:, team:, urns:, programmes: [], **)
1822
organisation = Organisation.find_by(ods_code:)
1923

2024
if organisation.nil?
@@ -29,6 +33,13 @@ def call(ods_code:, team:, urns:, **)
2933
return
3034
end
3135

36+
programmes =
37+
if programmes.empty?
38+
organisation.programmes
39+
else
40+
Programme.where(type: programmes)
41+
end
42+
3243
ActiveRecord::Base.transaction do
3344
urns.each do |urn|
3445
location = Location.school.find_by(urn:)
@@ -43,9 +54,7 @@ def call(ods_code:, team:, urns:, **)
4354
end
4455

4556
location.update!(team:)
46-
location.create_default_programme_year_groups!(
47-
organisation.programmes
48-
)
57+
location.create_default_programme_year_groups!(programmes)
4958
end
5059
end
5160

app/models/location.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Location < ApplicationRecord
4949
has_many :sessions
5050

5151
has_one :organisation, through: :team
52+
has_many :programmes, -> { distinct }, through: :programme_year_groups
5253

5354
# This is based on the school statuses from the DfE GIAS data.
5455
enum :status,

spec/features/cli_schools_add_to_organisation_spec.rb

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
end
4141
end
4242

43+
context "when customising the programmes" do
44+
it "runs successfully" do
45+
given_the_organisation_exists
46+
and_the_team_exists
47+
and_the_school_exists
48+
49+
when_i_run_the_command_with_flu_only
50+
then_the_school_is_added_to_the_organisation_with_flu_only
51+
end
52+
end
53+
4354
private
4455

4556
def command
@@ -48,22 +59,42 @@ def command
4859
)
4960
end
5061

62+
def command_with_flu_only
63+
Dry::CLI.new(MavisCLI).call(
64+
arguments: %w[
65+
schools
66+
add-to-organisation
67+
ABC
68+
Team
69+
123456
70+
--programmes
71+
flu
72+
]
73+
)
74+
end
75+
5176
def given_the_organisation_exists
52-
@organisation = create(:organisation, ods_code: "ABC")
77+
@programmes = [create(:programme, :flu), create(:programme, :hpv)]
78+
@organisation =
79+
create(:organisation, ods_code: "ABC", programmes: @programmes)
5380
end
5481

5582
def and_the_team_exists
5683
@team = create(:team, name: "Team", organisation: @organisation)
5784
end
5885

5986
def and_the_school_exists
60-
@school = create(:school, name: "School", urn: "123456", team: @team)
87+
@school = create(:school, name: "School", urn: "123456")
6188
end
6289

6390
def when_i_run_the_command
6491
@output = capture_error { command }
6592
end
6693

94+
def when_i_run_the_command_with_flu_only
95+
@output = capture_error { command_with_flu_only }
96+
end
97+
6798
def when_i_run_the_command_expecting_an_error
6899
@output = capture_error { command }
69100
end
@@ -82,5 +113,11 @@ def then_a_school_not_found_error_message_is_displayed
82113

83114
def then_the_school_is_added_to_the_organisation
84115
expect(@organisation.schools).to include(@school)
116+
expect(@school.programmes).to eq(@programmes)
117+
end
118+
119+
def then_the_school_is_added_to_the_organisation_with_flu_only
120+
expect(@organisation.schools).to include(@school)
121+
expect(@school.programmes).to contain_exactly(@programmes.first)
85122
end
86123
end

0 commit comments

Comments
 (0)