@@ -7,25 +7,41 @@ def initialize(patient)
77 @patient = patient
88 end
99
10- def send_clinic_invitation ( programme_types :, team :, academic_year :, sent_by :)
10+ ##
11+ # Send a clinic initiation email and SMS to the parents of this patient.
12+ #
13+ # This determines the correct type of invitation to use (either an initial
14+ # invitation or a subsequent invitation) based on the previous invitations
15+ # which have been sent:
16+ #
17+ # +include_vaccinated_programmes+ allows for the sending of invitations for
18+ # programmes where the patient has already been vaccinated.
19+ #
20+ # +include_already_invited_programmes+ allows for the sending of invitations
21+ # for programmes where the patient has already been invited for in the past.
22+ #
23+ def send_clinic_invitation (
24+ programmes ,
25+ team :,
26+ academic_year :,
27+ sent_by :,
28+ include_vaccinated_programmes : false ,
29+ include_already_invited_programmes : true
30+ )
1131 return unless send_notification? ( team :)
1232
13- programme_types . reject! do |programme_type |
14- patient . programme_status (
15- Programme . find ( programme_type ) ,
16- academic_year :
17- ) . vaccinated_fully?
18- end
33+ programme_types =
34+ programme_types_to_send_for (
35+ programmes ,
36+ team :,
37+ academic_year :,
38+ include_vaccinated_programmes :,
39+ include_already_invited_programmes :
40+ )
1941
2042 return if programme_types . empty?
2143
22- already_sent =
23- patient . clinic_notifications . any? do
24- it . team_id == team . id && it . academic_year == academic_year &&
25- ( programme_types - it . programme_types ) . empty? # is subset
26- end
27-
28- type = already_sent ? :subsequent_invitation : :initial_invitation
44+ type = clinic_invitation_type ( programme_types , team :, academic_year :)
2945
3046 # We create a record in the database first to avoid sending duplicate emails/texts.
3147 # If a problem occurs while the emails/texts are sent, they will be in the job
@@ -41,7 +57,7 @@ def send_clinic_invitation(programme_types:, team:, academic_year:, sent_by:)
4157 sent_by :
4258 )
4359
44- template_name = determine_template_name ( type , team :)
60+ template_name = find_template_name ( type , team :)
4561
4662 params = { academic_year :, patient :, programme_types :, sent_by :, team : }
4763
@@ -64,7 +80,56 @@ def parents
6480 @parents ||= patient . parents . select ( &:contactable? ) . uniq
6581 end
6682
67- def determine_template_name ( type , team :)
83+ def programme_types_to_send_for (
84+ programmes ,
85+ team :,
86+ academic_year :,
87+ include_vaccinated_programmes : false ,
88+ include_already_invited_programmes : true
89+ )
90+ programmes_to_send_for =
91+ programmes . select do |programme |
92+ unless include_vaccinated_programmes
93+ is_vaccinated =
94+ patient . programme_status ( programme , academic_year :) . vaccinated?
95+
96+ next false if is_vaccinated
97+ end
98+
99+ unless include_already_invited_programmes
100+ already_invited =
101+ patient . clinic_notifications . any? do
102+ it . team_id == team . id && it . academic_year == academic_year &&
103+ it . programme_types . include? ( programme . type )
104+ end
105+
106+ next false if already_invited
107+ end
108+
109+ true
110+ end
111+
112+ programmes_to_send_for . map ( &:type )
113+ end
114+
115+ def clinic_invitation_type ( programme_types , team :, academic_year :)
116+ already_sent_initial_invitation_to_all_programmes =
117+ programme_types . all? do |programme_type |
118+ patient . clinic_notifications . any? do
119+ it . team_id == team . id && it . academic_year == academic_year &&
120+ it . initial_invitation? &&
121+ it . programme_types . include? ( programme_type )
122+ end
123+ end
124+
125+ if already_sent_initial_invitation_to_all_programmes
126+ :subsequent_invitation
127+ else
128+ :initial_invitation
129+ end
130+ end
131+
132+ def find_template_name ( type , team :)
68133 template_names = [
69134 :"clinic_#{ type } _#{ team . organisation . ods_code . downcase } " ,
70135 :"clinic_#{ type } "
0 commit comments