Skip to content

Commit 5bba295

Browse files
committed
Add schools remove-from-team command for batch team removal
Add a CLI command that removes one or more schools from a specified team/subteam for a given academic year (defaults to pending). Includes validation to prevent removal if the school has associated records. Usage: `bin/mavis schools remove-from-team <Workgroup> <SubteamName> <URN...> [--academic-year YEAR]` MAV-2717
1 parent d4a9b83 commit 5bba295

4 files changed

Lines changed: 143 additions & 0 deletions

File tree

app/lib/mavis_cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def self.progress_bar(total)
4444
require_relative "mavis_cli/schools/add_to_team"
4545
require_relative "mavis_cli/schools/create"
4646
require_relative "mavis_cli/schools/move_patients"
47+
require_relative "mavis_cli/schools/remove_from_team"
4748
require_relative "mavis_cli/schools/remove_programme_year_group"
4849
require_relative "mavis_cli/schools/show"
4950
require_relative "mavis_cli/sessions/configure"
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module Schools
5+
class RemoveFromTeam < Dry::CLI::Command
6+
desc "Remove an existing school from a team"
7+
8+
argument :team_workgroup,
9+
required: true,
10+
desc: "The workgroup of the team"
11+
argument :subteam_name, required: true, desc: "The name of the subteam"
12+
argument :urns,
13+
type: :array,
14+
required: true,
15+
desc: "The URN of the school (including site, if applicable)"
16+
17+
option :academic_year,
18+
type: :integer,
19+
desc:
20+
"The academic year to remove the school from (defaults to pending academic year)"
21+
22+
def call(team_workgroup:, subteam_name:, urns:, academic_year: nil, **)
23+
MavisCLI.load_rails
24+
25+
team = Team.find_by(workgroup: team_workgroup)
26+
27+
if team.nil?
28+
warn "Could not find team with workgroup #{team_workgroup}."
29+
return
30+
end
31+
32+
subteam = team.subteams.find_by(name: subteam_name)
33+
34+
if subteam.nil?
35+
warn "Could not find subteam with name #{subteam_name}."
36+
return
37+
end
38+
39+
academic_year ||= AcademicYear.pending
40+
41+
ActiveRecord::Base.transaction do
42+
urns.each do |urn|
43+
location = Location.school.find_by_urn_and_site(urn)
44+
45+
if location.nil?
46+
warn "Could not find location with URN #{urn}"
47+
next
48+
end
49+
50+
team_location =
51+
TeamLocation
52+
.includes(:team)
53+
.where(team:, academic_year:, subteam:, location:)
54+
.sole
55+
56+
unless team_location.safe_to_destroy?
57+
warn "Location #{location.id} (URN: #{urn}) cannot be removed as it has associated records."
58+
next
59+
end
60+
61+
team_location.destroy!
62+
63+
puts "Location #{location.id} (URN: #{urn}) has been removed from subteam #{subteam_name} " \
64+
"for academic year #{academic_year}."
65+
end
66+
end
67+
end
68+
end
69+
end
70+
71+
register "schools" do |prefix|
72+
prefix.register "remove-from-team", Schools::RemoveFromTeam
73+
end
74+
end

app/models/team_location.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def phone = subteam&.phone || team&.phone
6262
def phone_instructions =
6363
subteam&.phone_instructions || team&.phone_instructions
6464

65+
def safe_to_destroy?
66+
!sessions.exists? && !consent_forms.exists? && !patient_locations.exists?
67+
end
68+
6569
private
6670

6771
def subteam_belongs_to_team
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../app/lib/mavis_cli"
4+
5+
describe "mavis schools remove-from-team" do
6+
context "with valid arguments" do
7+
it "runs successfully" do
8+
given_schools_exist
9+
when_i_run_the_command
10+
then_the_schools_are_removed_from_the_team
11+
end
12+
end
13+
14+
private
15+
16+
def given_schools_exist
17+
team = create(:team, workgroup: "TeamA")
18+
subteam = create(:subteam, team:, name: "SubteamA")
19+
@school_a =
20+
create(
21+
:school,
22+
urn: "123456",
23+
name: "MainSchool",
24+
site: nil,
25+
team:,
26+
subteam:
27+
)
28+
@school_b =
29+
create(
30+
:school,
31+
urn: "654321",
32+
name: "OtherSchool",
33+
site: nil,
34+
team:,
35+
subteam:
36+
)
37+
expect(@school_a.teams.count).to eq(1)
38+
expect(@school_b.teams.count).to eq(1)
39+
end
40+
41+
def command
42+
Dry::CLI.new(MavisCLI).call(
43+
arguments: %w[
44+
schools
45+
remove-from-team
46+
TeamA
47+
SubteamA
48+
123456
49+
654321
50+
--academic-year
51+
2025
52+
]
53+
)
54+
end
55+
56+
def when_i_run_the_command
57+
@output = capture_output { command }
58+
end
59+
60+
def then_the_schools_are_removed_from_the_team
61+
expect(@school_a.teams.count).to eq(0)
62+
expect(@school_b.teams.count).to eq(0)
63+
end
64+
end

0 commit comments

Comments
 (0)