@@ -7,14 +7,18 @@ def initialize(
77 patient :,
88 consents :,
99 vaccination_records :,
10- parents :
10+ parents :,
11+ sessions :,
12+ consent_notifications :
1113 )
1214 @programme_type = programme_type
1315 @academic_year = academic_year
1416 @patient = patient
1517 @consents = consents
1618 @vaccination_records = vaccination_records
1719 @parents = parents
20+ @sessions = sessions
21+ @consent_notifications = consent_notifications
1822 end
1923
2024 def programme
@@ -32,6 +36,10 @@ def status
3236 :conflicts
3337 elsif status_should_be_no_contact_details?
3438 :no_contact_details
39+ elsif status_should_be_request_scheduled?
40+ :request_scheduled
41+ elsif status_should_be_request_not_scheduled?
42+ :request_not_scheduled
3543 elsif status_should_be_no_response?
3644 :no_response
3745 else
@@ -62,7 +70,9 @@ def disease_types
6270 :patient ,
6371 :consents ,
6472 :vaccination_records ,
65- :parents
73+ :parents ,
74+ :sessions ,
75+ :consent_notifications
6676
6777 def vaccinated?
6878 return @vaccinated if defined? ( @vaccinated )
@@ -131,6 +141,20 @@ def status_should_be_no_contact_details?
131141 parents . none? ( &:contactable? )
132142 end
133143
144+ def status_should_be_request_scheduled?
145+ return false if vaccinated?
146+
147+ parents_contactable? && consent_notifications . empty? &&
148+ sessions . any? { consent_request_scheduled_in_future? ( it ) }
149+ end
150+
151+ def status_should_be_request_not_scheduled?
152+ return false if vaccinated?
153+
154+ parents_contactable? && consent_notifications . empty? &&
155+ ( sessions . empty? || sessions . any? { consent_request_not_scheduled? ( it ) } )
156+ end
157+
134158 def agreed_vaccine_methods
135159 @agreed_vaccine_methods ||=
136160 consents_for_status . map ( &:vaccine_methods ) . inject ( &:intersection )
@@ -162,4 +186,21 @@ def latest_consents
162186 @latest_consents ||=
163187 ConsentGrouper . call ( consents , programme_type :, academic_year :)
164188 end
189+
190+ def parents_contactable? = parents . any? ( &:contactable? )
191+
192+ def consent_request_scheduled_in_future? ( session )
193+ send_at = session . send_consent_requests_at
194+
195+ # Not using future? because it doesn't work with Timecop
196+ send_at . present? && send_at > Time . current
197+ end
198+
199+ # Treat consent requests as not scheduled when send_consent_requests_at is
200+ # missing or has already passed, such as for sessions activated on the
201+ # same day they are created.
202+ def consent_request_not_scheduled? ( session )
203+ send_at = session . send_consent_requests_at
204+ send_at . nil? || send_at < Time . current
205+ end
165206end
0 commit comments