-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This refactors the seeds to make the data more realistic and integrate with the other seeds.
- Loading branch information
1 parent
62fe06b
commit e8f572b
Showing
3 changed files
with
110 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "[email protected]") || | ||
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? | ||
create_list(:location, 30, :school) | ||
else | ||
Rake::Task["schools:import"].execute | ||
end | ||
end | ||
|
||
def create_user_and_team | ||
user = | ||
User.find_by(email: "[email protected]") || | ||
FactoryBot.create( | ||
:user, | ||
family_name: "Joy", | ||
given_name: "Nurse", | ||
email: "[email protected]", | ||
password: "[email protected]" | ||
) | ||
|
||
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: "[email protected]", | ||
password: "[email protected]" | ||
: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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters