Skip to content

Commit 7538713

Browse files
authored
Merge pull request #4012 from nhsuk/generic-clinic-factory
Refactor how generic clinic locations are created
2 parents 8c70361 + 0f69628 commit 7538713

26 files changed

Lines changed: 220 additions & 115 deletions

app/lib/generic_clinic_factory.rb

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
class GenericClinicFactory
4+
def initialize(organisation:)
5+
@organisation = organisation
6+
end
7+
8+
def call
9+
ActiveRecord::Base.transaction do
10+
location.update!(year_groups:)
11+
location.create_default_programme_year_groups!(programmes)
12+
location
13+
end
14+
end
15+
16+
def self.call(...) = new(...).call
17+
18+
private_class_method :new
19+
20+
private
21+
22+
attr_reader :organisation
23+
24+
delegate :programmes, to: :organisation
25+
26+
def team
27+
organisation
28+
.teams
29+
.create_with(
30+
email: organisation.email,
31+
phone: organisation.phone,
32+
phone_instructions: organisation.phone_instructions
33+
)
34+
.find_or_create_by!(name: organisation.name)
35+
end
36+
37+
def location
38+
organisation.locations.find_by(
39+
ods_code: organisation.ods_code,
40+
type: :generic_clinic
41+
) ||
42+
Location.create!(
43+
name: "Community clinics",
44+
ods_code: organisation.ods_code,
45+
team:,
46+
type: :generic_clinic
47+
)
48+
end
49+
50+
def year_groups
51+
programmes.flat_map(&:default_year_groups).uniq.sort
52+
end
53+
end

app/models/onboarding.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ def errors
117117
def save!
118118
ActiveRecord::Base.transaction do
119119
models.each(&:save!)
120-
organisation.generic_clinic.create_default_programme_year_groups!(
121-
programmes.map(&:programme)
122-
)
120+
121+
# Reload to ensure the programmes are loaded.
122+
GenericClinicFactory.call(organisation: organisation.reload)
123+
123124
@users.each { |user| user.organisations << organisation }
124125
UnscheduledSessionsFactory.new.call
125126
end

app/models/organisation.rb

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,9 @@ def year_groups
8080
@year_groups ||= location_programme_year_groups.pluck_year_groups
8181
end
8282

83-
def generic_team
84-
teams.create_with(email:, phone:, phone_instructions:).find_or_create_by!(
85-
name:
86-
)
87-
end
88-
89-
def generic_clinic
90-
locations.find_by(ods_code:, type: :generic_clinic) ||
91-
ActiveRecord::Base.transaction do
92-
Location
93-
.create!(
94-
name: "Community clinics",
95-
team: generic_team,
96-
ods_code:,
97-
type: :generic_clinic,
98-
year_groups: programmes.flat_map(&:default_year_groups).uniq.sort
99-
)
100-
.tap { it.create_default_programme_year_groups!(programmes) }
101-
end
102-
end
103-
10483
def generic_clinic_session
10584
academic_year = AcademicYear.current
106-
location = generic_clinic
85+
location = locations.generic_clinic.first
10786

10887
sessions
10988
.includes(:location, :programmes, :session_dates)

db/seeds.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ def attach_sample_of_schools_to(organisation)
6565
.where(team_id: nil)
6666
.order("RANDOM()")
6767
.limit(50)
68-
.update_all(team_id: organisation.generic_team.id)
68+
.update_all(team_id: organisation.teams.first.id)
6969
end
7070

7171
def attach_specific_school_to_organisation_if_present(organisation:, urn:)
72-
Location.where(urn:).update_all(team_id: organisation.generic_team.id)
72+
Location.where(urn:).update_all(team_id: organisation.teams.first.id)
7373
end
7474

7575
def create_session(

lib/tasks/clinics.rake

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

lib/tasks/create_default_programme_year_groups.rake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ task create_default_programme_year_groups: :environment do
77
.find_each do |organisation|
88
year_groups =
99
organisation.programmes.flat_map(&:default_year_groups).uniq.sort
10-
organisation.generic_clinic.update!(year_groups:)
1110

12-
organisation.generic_clinic.create_default_programme_year_groups!(
13-
organisation.programmes
14-
)
11+
organisation.locations.generic_clinic.find_each do |generic_clinic|
12+
generic_clinic.update_all(year_groups:)
13+
generic_clinic.create_default_programme_year_groups!(
14+
organisation.programmes
15+
)
16+
end
1517

1618
organisation.schools.find_each do |school|
1719
school.create_default_programme_year_groups!(organisation.programmes)

lib/tasks/organisations.rake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ namespace :organisations do
77
programme = Programme.find_by!(type: args[:type])
88

99
OrganisationProgramme.find_or_create_by!(organisation:, programme:)
10+
11+
GenericClinicFactory.call(organisation: organisation.reload)
1012
end
1113
end

spec/controllers/api/organisations_controller_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
describe "DELETE" do
88
let(:programmes) { [create(:programme, :hpv_all_vaccines)] }
99

10-
let(:organisation) { create(:organisation, ods_code: "R1L", programmes:) }
10+
let(:organisation) do
11+
create(:organisation, :with_generic_clinic, ods_code: "R1L", programmes:)
12+
end
1113

1214
let(:cohort_import) do
1315
create(
@@ -66,7 +68,7 @@
6668
expect { delete :destroy, params: { ods_code: "r1l" } }.to(
6769
change(Organisation, :count)
6870
.by(-1)
69-
.and(change(Team, :count).by(-2))
71+
.and(change(Team, :count).by(-1))
7072
.and(change(Session, :count).by(-1))
7173
.and(change(CohortImport, :count).by(-1))
7274
.and(change(ImmunisationImport, :count).by(-1))

spec/controllers/concerns/consent_form_mailer_concern_spec.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@
9292

9393
context "when there are no upcoming sessions" do
9494
let(:programmes) { [create(:programme)] }
95-
let(:organisation) { create(:organisation, programmes:) }
95+
let(:organisation) do
96+
create(:organisation, :with_generic_clinic, programmes:)
97+
end
9698
let(:consent_form) do
9799
create(
98100
:consent_form,

spec/factories/organisations.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@
4646
end
4747

4848
trait :with_generic_clinic do
49-
after(:create) do |organisation|
50-
create(:generic_clinic, team: organisation.generic_team)
51-
end
49+
after(:create) { |organisation| GenericClinicFactory.call(organisation:) }
5250
end
5351
end
5452
end

0 commit comments

Comments
 (0)