Skip to content

Commit 0f69628

Browse files
committed
Add GenericClinicFactory
This adds a new class which handles the creation of the generic clinic factory and no longer creates these on the fly through the removal of the `Organisation#generic_clinic` method. I think this makes the code more explicit at describing how these locations get created.
1 parent bfe07bd commit 0f69628

11 files changed

Lines changed: 123 additions & 39 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/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/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

spec/features/td_ipv_already_had_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def given_a_td_ipv_programme_with_a_session(clinic:)
6565
@programme = create(:programme, :td_ipv)
6666
programmes = [@programme]
6767

68-
organisation = create(:organisation, :with_generic_clinic, programmes:)
68+
organisation = create(:organisation, programmes:)
6969
@nurse = create(:nurse, organisations: [organisation])
7070

7171
location =
7272
(
7373
if clinic
74-
organisation.generic_clinic
74+
create(:generic_clinic, organisation:)
7575
else
7676
create(:school, :secondary, urn: 123_456, organisation:)
7777
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# frozen_string_literal: true
2+
3+
describe GenericClinicFactory do
4+
describe "#call" do
5+
subject(:call) { described_class.call(organisation:) }
6+
7+
let(:programmes) { [create(:programme, :hpv), create(:programme, :flu)] }
8+
9+
context "with a new organisation" do
10+
let(:organisation) { create(:organisation, programmes:) }
11+
12+
it "creates a generic clinic location" do
13+
expect { call }.to change(Location.generic_clinic, :count).by(1)
14+
15+
location = Location.generic_clinic.first
16+
expect(location.organisation).to eq(organisation)
17+
expect(location.year_groups).to contain_exactly(
18+
0,
19+
1,
20+
2,
21+
3,
22+
4,
23+
5,
24+
6,
25+
7,
26+
8,
27+
9,
28+
10,
29+
11
30+
)
31+
end
32+
end
33+
34+
context "with an existing organisation" do
35+
let(:organisation) do
36+
create(:organisation, :with_generic_clinic, programmes:)
37+
end
38+
39+
it "doesn't create a generic clinic location" do
40+
organisation
41+
42+
expect { call }.not_to change(Location.generic_clinic, :count)
43+
end
44+
end
45+
end
46+
end

spec/lib/reports/offline_session_exporter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def validation_formula(worksheet:, column_name:, row: 1)
651651
context "a clinic session" do
652652
subject(:workbook) { RubyXL::Parser.parse_buffer(call) }
653653

654-
let(:location) { organisation.generic_clinic }
654+
let(:location) { organisation.locations.generic_clinic.first }
655655

656656
it { should_not be_blank }
657657

0 commit comments

Comments
 (0)