Skip to content

Commit ff9b3ec

Browse files
authored
Merge pull request #4008 from nhsuk/clinic-cli-tools
Add CLI tools for clinics
2 parents e9e1016 + ec39149 commit ff9b3ec

11 files changed

Lines changed: 224 additions & 63 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ See the [releasing documentation](docs/releasing.md) for more information.
339339

340340
- `access_log:for_patient[id]`
341341
- `access_log:for_user[id]`
342-
- `clinics:create[name,address,town,postcode,ods_code,organisation_ods_code]`
343342
- `teams:create[ods_code,name,email,phone]`
344343
- `users:create[email,password,given_name,family_name,organisation_ods_code]`
345344
- `vaccines:seed[type]`

app/lib/mavis_cli.rb

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

21+
require_relative "mavis_cli/clinics/add_to_organisation"
22+
require_relative "mavis_cli/clinics/create"
2123
require_relative "mavis_cli/generate/cohort_imports"
2224
require_relative "mavis_cli/generate/consents"
2325
require_relative "mavis_cli/generate/vaccination_records"
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module Clinics
5+
class AddToOrganisation < Dry::CLI::Command
6+
desc "Add an existing clinic to an organisation"
7+
8+
argument :organisation_ods_code,
9+
required: true,
10+
desc: "The ODS code of the organisation"
11+
argument :team, required: true, desc: "The team of the organisation"
12+
argument :clinic_ods_codes,
13+
type: :array,
14+
required: true,
15+
desc: "The ODS codes of the clinics"
16+
17+
def call(organisation_ods_code:, team:, clinic_ods_codes:, **)
18+
organisation = Organisation.find_by(ods_code: organisation_ods_code)
19+
20+
if organisation.nil?
21+
warn "Could not find organisation."
22+
return
23+
end
24+
25+
team = organisation.teams.find_by(name: team)
26+
27+
if team.nil?
28+
warn "Could not find team."
29+
return
30+
end
31+
32+
ActiveRecord::Base.transaction do
33+
clinic_ods_codes.each do |ods_code|
34+
location = Location.clinic.find_by(ods_code:)
35+
36+
if location.nil?
37+
warn "Could not find location: #{ods_code}"
38+
next
39+
end
40+
41+
if !location.team_id.nil? && location.team_id != team.id
42+
warn "#{ods_code} previously belonged to #{location.team.name}"
43+
end
44+
45+
location.update!(team:)
46+
end
47+
end
48+
end
49+
end
50+
end
51+
52+
register "clinics" do |prefix|
53+
prefix.register "add-to-organisation", Clinics::AddToOrganisation
54+
end
55+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
module MavisCLI
4+
module Clinics
5+
class Create < Dry::CLI::Command
6+
desc "Create a new clinic location"
7+
8+
argument :ods_code, required: true, desc: "The ODS code of the clinic"
9+
argument :name, required: true, desc: "The name of the clinic"
10+
argument :address_line_1,
11+
required: true,
12+
desc: "The line 1 of the address"
13+
argument :address_town, required: true, desc: "The town of the address"
14+
argument :address_postcode,
15+
required: true,
16+
desc: "The postcode of the address"
17+
18+
def call(
19+
ods_code:,
20+
name:,
21+
address_line_1:,
22+
address_town:,
23+
address_postcode:,
24+
**
25+
)
26+
Location.create!(
27+
type: :community_clinic,
28+
name:,
29+
address_line_1:,
30+
address_town:,
31+
address_postcode:,
32+
ods_code:
33+
)
34+
end
35+
end
36+
end
37+
38+
register "clinics" do |prefix|
39+
prefix.register "create", Clinics::Create
40+
end
41+
end

app/models/location.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,13 @@ class Location < ApplicationRecord
6464
validates :url, url: true, allow_nil: true
6565
validates :urn, uniqueness: true, allow_nil: true
6666

67-
with_options if: :clinic? do
68-
validates :team, presence: true
69-
end
70-
7167
with_options if: :community_clinic? do
7268
validates :ods_code, exclusion: { in: :organisation_ods_code }
7369
end
7470

7571
with_options if: :generic_clinic? do
7672
validates :ods_code, inclusion: { in: :organisation_ods_code }
73+
validates :team, presence: true
7774
end
7875

7976
with_options if: :gp_practice? do
@@ -86,7 +83,7 @@ class Location < ApplicationRecord
8683
validates :urn, presence: true
8784
end
8885

89-
normalizes :urn, with: -> { _1.blank? ? nil : _1.strip }
86+
normalizes :urn, with: -> { it.blank? ? nil : it.strip }
9087

9188
delegate :fhir_reference, to: :fhir_mapper
9289

@@ -121,9 +118,9 @@ def create_default_programme_year_groups!(programmes)
121118

122119
private
123120

124-
def organisation_ods_code
125-
[team&.organisation&.ods_code]
126-
end
121+
def organisation_ods_code = [team&.organisation&.ods_code].compact
127122

128-
def fhir_mapper = @fhir_mapper ||= FHIRMapper::Location.new(self)
123+
def fhir_mapper
124+
@fhir_mapper ||= FHIRMapper::Location.new(self)
125+
end
129126
end

docs/rake-tasks.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,6 @@ Displays the access log for a particular patient identified by their ID.
1010

1111
Displays the access log for a particular user identified by an email address.
1212

13-
## Clinics
14-
15-
### `clinics:create[name,address,town,postcode,ods_code,organisation_ods_code]`
16-
17-
- `name` - The name of the clinic.
18-
- `address` - The first line of the address.
19-
- `town` - The town of the clinic.
20-
- `postcode` - The postcode of the clinic.
21-
- `ods_code` - The ODS code of the clinic.
22-
- `organisation_ods_code` - The ODS code of the organisation.
23-
24-
If none of the arguments are provided (`rake clinics:create`), the user will be prompted for responses.
25-
26-
This creates a new clinic location and attaches it to a organisation.
27-
2813
## GP Practices
2914

3015
### `gp_practices:smoke`

spec/factories/locations.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@
6565
name { "#{Faker::University.name} Clinic" }
6666

6767
sequence(:ods_code, 100) { "CL#{_1}" }
68-
69-
organisation
7068
end
7169

7270
factory :generic_clinic do
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../app/lib/mavis_cli"
4+
5+
describe "mavis clinics add-to-organisation" do
6+
context "when the organisation doesn't exist" do
7+
it "displays an error message" do
8+
when_i_run_the_command_expecting_an_error
9+
then_an_organisation_not_found_error_message_is_displayed
10+
end
11+
end
12+
13+
context "when the team doesn't exist" do
14+
it "displays an error message" do
15+
given_the_organisation_exists
16+
17+
when_i_run_the_command_expecting_an_error
18+
then_a_team_not_found_error_message_is_displayed
19+
end
20+
end
21+
22+
context "when the clinic doesn't exist" do
23+
it "displays an error message" do
24+
given_the_organisation_exists
25+
and_the_team_exists
26+
27+
when_i_run_the_command_expecting_an_error
28+
then_a_clinic_not_found_error_message_is_displayed
29+
end
30+
end
31+
32+
context "when the clinic exists" do
33+
it "runs successfully" do
34+
given_the_organisation_exists
35+
and_the_team_exists
36+
and_the_clinic_exists
37+
38+
when_i_run_the_command
39+
then_the_clinic_is_added_to_the_organisation
40+
end
41+
end
42+
43+
private
44+
45+
def command
46+
Dry::CLI.new(MavisCLI).call(
47+
arguments: %w[clinics add-to-organisation ABC Team 123456]
48+
)
49+
end
50+
51+
def given_the_organisation_exists
52+
@organisation = create(:organisation, ods_code: "ABC")
53+
end
54+
55+
def and_the_team_exists
56+
@team = create(:team, name: "Team", organisation: @organisation)
57+
end
58+
59+
def and_the_clinic_exists
60+
@clinic = create(:community_clinic, name: "Clinic", ods_code: "123456")
61+
end
62+
63+
def when_i_run_the_command
64+
@output = capture_output { command }
65+
end
66+
67+
def when_i_run_the_command_expecting_an_error
68+
@output = capture_error { command }
69+
end
70+
71+
def then_an_organisation_not_found_error_message_is_displayed
72+
expect(@output).to include("Could not find organisation.")
73+
end
74+
75+
def then_a_team_not_found_error_message_is_displayed
76+
expect(@output).to include("Could not find team.")
77+
end
78+
79+
def then_a_clinic_not_found_error_message_is_displayed
80+
expect(@output).to include("Could not find location: 123456")
81+
end
82+
83+
def then_the_clinic_is_added_to_the_organisation
84+
expect(@organisation.community_clinics).to include(@clinic)
85+
end
86+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "../../app/lib/mavis_cli"
4+
5+
describe "mavis clinics create" do
6+
context "when valid arguments" do
7+
it "runs successfully" do
8+
when_i_run_the_command
9+
then_the_clinic_is_created
10+
end
11+
end
12+
13+
private
14+
15+
def command
16+
Dry::CLI.new(MavisCLI).call(
17+
arguments: %w[clinics create 123456 Clinic Line Town SW1A1AA]
18+
)
19+
end
20+
21+
def when_i_run_the_command
22+
@output = capture_output { command }
23+
end
24+
25+
def then_the_clinic_is_created
26+
clinic = Location.community_clinic.find_by!(ods_code: "123456")
27+
28+
expect(clinic.name).to eq("Clinic")
29+
expect(clinic.address_line_1).to eq("Line")
30+
expect(clinic.address_town).to eq("Town")
31+
expect(clinic.address_postcode).to eq("SW1A 1AA")
32+
end
33+
end

spec/lib/tasks/clinics_rake_spec.rb

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)