Skip to content

Commit

Permalink
Refactor seeds
Browse files Browse the repository at this point in the history
This refactors the seeds to make the data more realistic and integrate
with the other seeds.
  • Loading branch information
thomasleese committed Oct 1, 2024
1 parent edbe27f commit 251457d
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 221 deletions.
133 changes: 109 additions & 24 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,126 @@
# frozen_string_literal: true

FEATURE_FLAGS = %i[dev_tools mesh_jobs cis2].freeze

FEATURE_FLAGS.each { |flag| Flipper.add(flag) unless Flipper.exist?(flag) }

if Settings.disallow_database_seeding
Rails.logger.info "Database seeding is disabled"
exit
end

Faker::Config.locale = "en-GB"

user =
User.find_by(email: "nurse.joy@example.com") ||
def set_feature_flags
%i[dev_tools mesh_jobs cis2].each do |feature_flag|
Flipper.add(feature_flag) unless Flipper.exist?(feature_flag)
end
end

def seed_vaccines
Rake::Task["vaccines:seed"].execute
end

def import_schools
if Rails.env.test?
FactoryBot.create_list(:location, 30, :school)
else
Rake::Task["schools:import"].execute
end
end

def create_user_and_team
user =
User.find_by(email: "nurse.joy@example.com") ||
FactoryBot.create(
:user,
family_name: "Joy",
given_name: "Nurse",
email: "nurse.joy@example.com",
password: "nurse.joy@example.com"
)

team = user.teams.first

Programme.all.find_each do |programme|
FactoryBot.create(:team_programme, team:, programme:)
end

[user, team]
end

def attach_locations_to(team)
Location.order("RANDOM()").limit(50).update_all(team_id: team.id)
end

def create_session(user, team)
programme = Programme.find_by(type: "hpv")

FactoryBot.create_list(:batch, 4, vaccine: programme.vaccines.active.first)

session =
FactoryBot.create(
:user,
family_name: "Joy",
given_name: "Nurse",
email: "nurse.joy@example.com",
password: "nurse.joy@example.com"
:session,
team:,
programme:,
location: team.locations.for_year_groups(programme.year_groups).sample
)

session.dates.create!(value: Date.yesterday)
session.dates.create!(value: Date.tomorrow)

patients_without_consent =
FactoryBot.create_list(
:patient_session,
4,
programme:,
session:,
created_by: user
)
Audited
.audit_class
.as_user(user) do
unmatched_patients = patients_without_consent.sample(2).map(&:patient)
unmatched_patients.each do |patient|
FactoryBot.create(
:example_programme,
:in_progress,
:in_past,
:in_future,
:hpv,
user:
:consent_form,
:recorded,
programme:,
first_name: patient.first_name,
last_name: patient.last_name,
session:
)
end

%i[
consent_given_triage_not_needed
consent_given_triage_needed
triaged_ready_to_vaccinate
consent_refused
consent_conflicting
vaccinated
delay_vaccination
unable_to_vaccinate
].each do |trait|
FactoryBot.create_list(
:patient_session,
3,
trait,
programme:,
session:,
created_by: user
)
end
end

def create_patients(team)
team.schools.each do |school|
FactoryBot.create_list(:patient, 5, team:, school:)
end
end

set_feature_flags

seed_vaccines
import_schools

user, team = create_user_and_team

Team.find_by(ods_code: "Y51") ||
FactoryBot.create(:team, name: "NMEPFIT SAIS Team", ods_code: "Y51")
attach_locations_to(team)

Rake::Task["vaccines:seed"].execute
Audited.audit_class.as_user(user) { create_session(user, team) }

Rake::Task["schools:import"].execute unless Rails.env.test?
create_patients(team)
196 changes: 0 additions & 196 deletions spec/factories/example_programmes.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/factories/vaccination_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
recorded_at { "2023-06-09" }
delivery_site { "left_arm_upper_position" }
delivery_method { "intramuscular" }
vaccine { programme.vaccines.first }
vaccine { programme.vaccines.active.first }
batch { vaccine.batches.first }

performed_by
Expand Down

0 comments on commit 251457d

Please sign in to comment.