@@ -153,45 +153,50 @@ def patient_statuses_to_import
153153 . pluck ( :patient_id , :"patients.birth_academic_year" )
154154 . uniq
155155 . flat_map do |patient_id , birth_academic_year |
156- programme_ids_per_birth_academic_year
157- . fetch ( birth_academic_year , [ ] )
156+ programme_ids_per_year_group
157+ . fetch ( birth_academic_year . to_year_group , [ ] )
158158 . map { [ patient_id , it ] }
159159 end
160160 end
161161
162162 def patient_session_statuses_to_import
163163 @patient_session_statuses_to_import ||=
164164 patient_sessions
165- . joins ( :patient )
166- . pluck ( :id , :"patients.birth_academic_year" )
167- . flat_map do |patient_session_id , birth_academic_year |
168- programme_ids_per_birth_academic_year
169- . fetch ( birth_academic_year , [ ] )
165+ . joins ( :patient , :session )
166+ . pluck ( :id , :"session.location_id" , :"patients.birth_academic_year" )
167+ . flat_map do |patient_session_id , location_id , birth_academic_year |
168+ programme_ids_per_location_id_and_year_group
169+ . fetch ( location_id , { } )
170+ . fetch ( birth_academic_year . to_year_group , [ ] )
170171 . map { [ patient_session_id , it ] }
171172 end
172173 end
173174
174175 def registration_statuses_to_import
175176 @registration_statuses_to_import ||=
176- patient_sessions
177- . joins ( :patient )
178- . pluck ( :id , :"patients.birth_academic_year" )
179- . filter_map do |patient_session_id , birth_academic_year |
180- if programme_ids_per_birth_academic_year . key? ( birth_academic_year )
181- [ patient_session_id ]
182- end
177+ patient_session_statuses_to_import . map { [ it . first ] } . uniq
178+ end
179+
180+ def programme_ids_per_year_group
181+ @programme_ids_per_year_group ||=
182+ Location ::ProgrammeYearGroup
183+ . distinct
184+ . pluck ( :programme_id , :year_group )
185+ . each_with_object ( { } ) do |( programme_id , year_group ) , hash |
186+ hash [ year_group ] ||= [ ]
187+ hash [ year_group ] << programme_id
183188 end
184189 end
185190
186- def programme_ids_per_birth_academic_year
187- @programme_ids_per_birth_academic_year ||=
188- Programme
189- . all
190- . each_with_object ( { } ) do | programme , hash |
191- programme . birth_academic_years . each do |year_group |
192- hash [ year_group ] ||= [ ]
193- hash [ year_group ] << programme . id
194- end
191+ def programme_ids_per_location_id_and_year_group
192+ @programme_ids_per_location_id_and_year_group ||=
193+ Location :: ProgrammeYearGroup
194+ . distinct
195+ . pluck ( :location_id , :programme_id , :year_group )
196+ . each_with_object ( { } ) do |( location_id , programme_id , year_group ) , hash |
197+ hash [ location_id ] ||= { }
198+ hash [ location_id ] [ year_group ] ||= [ ]
199+ hash [ location_id ] [ year_group ] << programme_id
195200 end
196201 end
197202end
0 commit comments