@@ -348,16 +348,22 @@ def actual_upcoming_session
348
348
Session . upcoming . has_programme ( programme ) . where ( organisation :)
349
349
350
350
if location . clinic?
351
- # If they've been booked in to a clinic we don't move them to a school
352
- # session as it's likely they're in a clinic for a reason.
351
+ # If they've been booked in to a clinic we don't move them to a
352
+ # school session as it's likely they're in a clinic for a reason.
353
353
session_scope . find_by ( location :)
354
354
elsif school
355
355
session_scope . find_by ( location : school )
356
356
elsif education_setting_home?
357
357
session_scope . find_by ( location : organisation . generic_clinic )
358
358
else
359
359
session_scope . find_by ( location :)
360
- end
360
+ end => upcoming_session
361
+
362
+ # There are no upcoming sessions available for their chosen location,
363
+ # either the original session or a different school or a clinic if
364
+ # home educated. This can happen if the parent fills out the form
365
+ # late.
366
+ upcoming_session || organisation . generic_clinic_session
361
367
end
362
368
end
363
369
@@ -424,24 +430,14 @@ def match_with_patient!(patient)
424
430
if patient . changed?
425
431
patient . save!
426
432
427
- unless patient . deceased? || patient . invalidated?
428
- move_patient_to_session =
429
- if actual_upcoming_session . nil?
430
- # There are no upcoming sessions available for their chosen location,
431
- # either the original session or a different school or a clinic if
432
- # home educated. This can happen if the parent fills out the form
433
- # late.
434
- organisation . generic_clinic_session
435
- elsif original_session != actual_upcoming_session
436
- actual_upcoming_session
437
- end
433
+ patient_can_move =
434
+ !patient . deceased? && !patient . invalidated? && actual_upcoming_session
438
435
439
- if move_patient_to_session
440
- patient . move_to_session! (
441
- move_patient_to_session ,
442
- from : original_session
443
- )
444
- end
436
+ if patient_can_move
437
+ patient . move_to_session! (
438
+ actual_upcoming_session ,
439
+ from : original_session
440
+ )
445
441
end
446
442
end
447
443
0 commit comments