3
3
class Sessions ::InviteToClinicController < ApplicationController
4
4
before_action :set_session
5
5
before_action :set_generic_clinic_session
6
+ before_action :set_patient_sessions_to_invite
6
7
before_action :set_invitations_to_send
7
8
8
9
skip_after_action :verify_policy_scoped
@@ -13,6 +14,11 @@ def edit
13
14
14
15
def update
15
16
if @session . school?
17
+ PatientSession . import! (
18
+ @patient_sessions_to_invite ,
19
+ on_duplicate_key_ignore : true
20
+ )
21
+
16
22
SendClinicInitialInvitationsJob . perform_later (
17
23
@generic_clinic_session ,
18
24
school : @session . location ,
@@ -44,36 +50,51 @@ def set_session
44
50
45
51
def set_generic_clinic_session
46
52
@generic_clinic_session =
47
- (
48
- if @session . clinic?
49
- @session
50
- else
51
- @session . team . generic_clinic_session (
52
- academic_year : @session . academic_year
53
- )
54
- end
55
- )
53
+ if @session . clinic?
54
+ @session
55
+ else
56
+ @session . team . generic_clinic_session (
57
+ academic_year : @session . academic_year
58
+ )
59
+ end
56
60
end
57
61
58
- def set_invitations_to_send
62
+ def set_patient_sessions_to_invite
59
63
session_date = @generic_clinic_session . next_date ( include_today : true )
60
64
61
- @invitations_to_send =
62
- if @session . school?
63
- SendClinicInitialInvitationsJob
64
- . new
65
- . patient_sessions (
66
- @generic_clinic_session ,
67
- school : @session . location ,
68
- programmes : @session . programmes . to_a ,
69
- session_date :
65
+ if @session . school?
66
+ patient_sessions_in_school = @session . patient_sessions . includes ( :patient )
67
+
68
+ patient_sessions_in_clinic =
69
+ patient_sessions_in_school . map do |patient_session |
70
+ PatientSession . find_or_initialize_by (
71
+ patient : patient_session . patient ,
72
+ session : @generic_clinic_session
70
73
)
71
- . length
72
- else
73
- SendClinicSubsequentInvitationsJob
74
- . new
75
- . patient_sessions ( @session , session_date :)
76
- . length
77
- end
74
+ end
75
+
76
+ programmes = @session . programmes . to_a
77
+
78
+ @patient_sessions_to_invite =
79
+ patient_sessions_in_clinic
80
+ . reject { it . session_notifications . any? }
81
+ . select do |patient_session |
82
+ SendClinicInitialInvitationsJob . new . should_send_notification? (
83
+ patient_session :,
84
+ programmes :,
85
+ session_date :
86
+ )
87
+ end
88
+ else
89
+ @patient_sessions_to_invite =
90
+ SendClinicSubsequentInvitationsJob . new . patient_sessions (
91
+ @session ,
92
+ session_date :
93
+ )
94
+ end
95
+ end
96
+
97
+ def set_invitations_to_send
98
+ @invitations_to_send = @patient_sessions_to_invite . length
78
99
end
79
100
end
0 commit comments