88#
99# Usage from the Rails console:
1010#
11- # # By default it uses existing locations in the db.
12- # Generate::PatientImports.call
11+ # Create a cohort import of 1000 children for all the school sessions for the
12+ # org A9A5A in the local db:
13+ #
14+ # Generate::CohortImports.call(patient_count: 1000)
15+ #
16+ # You can also generate a cohort import for sessions not in the local db.
17+ #
18+ # Generate::CohortImports.call(
19+ # patient_count: 1000,
20+ # urns: ["123456", "987654"],
21+ # school_year_groups: {
22+ # "123456" => [-2, -1, 0, 1, 2, 3, 4, 5, 6],
23+ # "987654" => [9, 10, 11, 12, 13]
24+ # }
25+ # )
26+ #
27+ # You can pull out the year groups with the following:
28+ #
29+ # org = Organisation.find_by(ods_code: "A9A5A")
30+ # org.locations.school.pluck(:urn, :year_groups) .to_h
1331#
1432module Generate
15- class PatientImports
16- attr_reader :ods_code , :organisation , :programme , :urns , :patient_count
33+ class CohortImports
34+ attr_reader :ods_code ,
35+ :organisation ,
36+ :programme ,
37+ :urns ,
38+ :patient_count ,
39+ :school_year_groups
1740
1841 def initialize (
1942 ods_code : "A9A5A" ,
2043 programme : "hpv" ,
2144 urns : nil ,
45+ school_year_groups : nil ,
2246 patient_count : 10
2347 )
2448 @organisation = Organisation . find_by ( ods_code :)
@@ -30,19 +54,19 @@ def initialize(
3054 . select { it . urn . present? }
3155 . sample ( 3 )
3256 . pluck ( :urn )
57+ @school_year_groups = school_year_groups
3358 @patient_count = patient_count
59+ @nhs_numbers = Set . new
3460 end
3561
3662 def self . call ( ...) = new ( ...) . call
3763
3864 def call
39- generate
4065 write_cohort_import_csv
41- write_class_import_csv
4266 end
4367
4468 def patients
45- @patients = patient_count . times . map { build_patient }
69+ patient_count . times . lazy . map { build_patient }
4670 end
4771
4872 private
@@ -53,12 +77,6 @@ def cohort_import_csv_filepath
5377 )
5478 end
5579
56- def class_import_csv_filepath ( school :)
57- Rails . root . join (
58- "tmp/perf-test-class-import-#{ school . name } -#{ school . sessions . first . slug } .csv"
59- )
60- end
61-
6280 def write_cohort_import_csv
6381 CSV . open ( cohort_import_csv_filepath , "w" ) do |csv |
6482 csv << %w[
@@ -105,40 +123,7 @@ def write_cohort_import_csv
105123 ]
106124 end
107125 end
108- end
109-
110- def write_class_import_csv
111- patients
112- . group_by ( &:school )
113- . each do |school , school_patients |
114- next if school . nil?
115-
116- CSV . open ( class_import_csv_filepath ( school :) , "w" ) do |csv |
117- csv << %w[
118- CHILD_POSTCODE
119- CHILD_DATE_OF_BIRTH
120- CHILD_FIRST_NAME
121- CHILD_LAST_NAME
122- PARENT_1_EMAIL
123- PARENT_1_PHONE
124- PARENT_2_EMAIL
125- PARENT_2_PHONE
126- ]
127-
128- school_patients . each do |patient |
129- csv << [
130- patient . address_postcode ,
131- patient . date_of_birth ,
132- patient . given_name ,
133- patient . family_name ,
134- patient . parents . first &.email ,
135- patient . parents . first &.phone ,
136- patient . parents . second &.email ,
137- patient . parents . second &.phone
138- ]
139- end
140- end
141- end
126+ cohort_import_csv_filepath . to_s
142127 end
143128
144129 def programme_year_groups
@@ -147,28 +132,46 @@ def programme_year_groups
147132
148133 def schools_with_year_groups
149134 @schools_with_year_groups ||=
150- organisation
151- . locations
152- . includes ( :organisation , :sessions )
153- . select { ( it . year_groups & programme_year_groups ) . any? }
135+ begin
136+ locations =
137+ if school_year_groups . present?
138+ urns . map do |urn |
139+ Location . new ( urn :, year_groups : school_year_groups [ urn ] )
140+ end
141+ else
142+ organisation
143+ . locations
144+ . where ( urn : urns )
145+ . includes ( :organisation , :sessions )
146+ end
147+ locations . select { ( it . year_groups & programme_year_groups ) . any? }
148+ end
154149 end
155150
156- def build_patient ( year_group : nil )
151+ def build_patient
157152 school = schools_with_year_groups . sample
158153 year_group ||= ( school . year_groups & programme_year_groups ) . sample
154+ nhs_number = nil
155+ loop do
156+ nhs_number = Faker ::NationalHealthService . british_number . gsub ( " " , "" )
157+ break unless nhs_number . in? @nhs_numbers
158+ end
159+ @nhs_numbers << nhs_number
159160
160161 FactoryBot
161162 . build (
162163 :patient ,
163164 school :,
164- date_of_birth : date_of_birth_for_year ( year_group )
165+ organisation :,
166+ date_of_birth : date_of_birth_for_year ( year_group ) ,
167+ nhs_number :
165168 )
166169 . tap do |patient |
167170 patient . parents =
168171 FactoryBot . build_list ( :parent , 2 , family_name : patient . family_name )
169172 patient . parent_relationships =
170173 patient . parents . map do
171- FactoryBot . build ( :parent_relationship , parent : it )
174+ FactoryBot . build ( :parent_relationship , parent : it , patient : )
172175 end
173176 end
174177 end
0 commit comments