@@ -245,30 +245,27 @@ defmodule Ask.Runtime.Session do
245245 end
246246 end
247247
248- def contact_respondent ( % { current_mode: % SMSMode { } } = session ) do
248+ def contact_respondent ( % { schedule: schedule , current_mode: % SMSMode { } } = session ) do
249249 token = Ecto.UUID . generate ( )
250250
251251 respondent = session . respondent
252252 { :ok , _flow , reply } = Flow . retry ( session . flow , TextVisitor . new ( "sms" ) , respondent . disposition )
253253 channel = session . current_mode . channel
254254 log_prompts ( reply , channel , session . flow . mode , respondent )
255255
256- ChannelBroker . ask ( channel . id , channel . type , session . respondent , token , reply )
256+ { not_before , not_after } = acceptable_contact_time_window ( schedule )
257+
258+ ChannelBroker . ask ( channel . id , channel . type , session . respondent , token , reply , not_before , not_after )
257259
260+ # TODO: what happens with this when contact attempt falls outside acceptable window
258261 respondent = Respondent . update_stats ( respondent . id , reply )
259262 % { session | token: token , respondent: respondent }
260263 end
261264
262265 def contact_respondent ( % { schedule: schedule , current_mode: % IVRMode { } } = session ) do
263266 token = Ecto.UUID . generate ( )
264267
265- next_available_date_time =
266- schedule
267- |> Schedule . next_available_date_time ( )
268-
269- today_end_time =
270- schedule
271- |> Schedule . at_end_time ( next_available_date_time )
268+ { not_before , not_after } = acceptable_contact_time_window ( schedule )
272269
273270 channel = session . current_mode . channel
274271
@@ -277,8 +274,8 @@ defmodule Ask.Runtime.Session do
277274 channel . type ,
278275 session . respondent ,
279276 token ,
280- next_available_date_time ,
281- today_end_time
277+ not_before ,
278+ not_after
282279 )
283280
284281 % { session | token: token }
@@ -388,9 +385,13 @@ defmodule Ask.Runtime.Session do
388385 flow: flow ,
389386 respondent: respondent ,
390387 token: token ,
391- current_mode: % SMSMode { channel: channel }
388+ current_mode: % SMSMode { channel: channel } ,
389+ schedule: schedule
392390 } = session
393391 ) do
392+
393+ { not_before , not_after } = acceptable_contact_time_window ( schedule )
394+
394395 case flow
395396 |> Flow . step (
396397 session . current_mode |> SessionMode . visitor ( ) ,
@@ -401,7 +402,7 @@ defmodule Ask.Runtime.Session do
401402 if Reply . prompts ( reply ) != [ ] do
402403 log_prompts ( reply , channel , flow . mode , respondent , true )
403404
404- ChannelBroker . ask ( channel . id , channel . type , respondent , token , reply )
405+ ChannelBroker . ask ( channel . id , channel . type , respondent , token , reply , not_before , not_after )
405406
406407 respondent = Respondent . update_stats ( respondent . id , reply )
407408 { :end , reply , respondent }
@@ -422,7 +423,7 @@ defmodule Ask.Runtime.Session do
422423
423424 log_prompts ( reply , channel , flow . mode , respondent )
424425
425- ChannelBroker . ask ( channel . id , channel . type , respondent , token , reply )
426+ ChannelBroker . ask ( channel . id , channel . type , respondent , token , reply , not_before , not_after )
426427
427428 respondent = Respondent . update_stats ( respondent . id , reply )
428429 { :ok , % { session | flow: flow , respondent: respondent } , reply , current_timeout ( session ) }
@@ -472,21 +473,16 @@ defmodule Ask.Runtime.Session do
472473 schedule: schedule
473474 } = session
474475 ) do
475- next_available_date_time =
476- schedule
477- |> Schedule . next_available_date_time ( )
478476
479- today_end_time =
480- schedule
481- |> Schedule . at_end_time ( next_available_date_time )
477+ { not_before , not_after } = acceptable_contact_time_window ( schedule )
482478
483479 ChannelBroker . setup (
484480 channel . id ,
485481 channel . type ,
486482 respondent ,
487483 token ,
488- next_available_date_time ,
489- today_end_time
484+ not_before ,
485+ not_after
490486 )
491487
492488 { :ok , % { session | respondent: respondent } , % Reply { } , current_timeout ( session ) }
@@ -1043,4 +1039,16 @@ defmodule Ask.Runtime.Session do
10431039 until = Schedule . next_available_date_time ( session . schedule )
10441040 Interval . new ( from: from , until: until ) |> Interval . duration ( :minutes )
10451041 end
1042+
1043+ defp acceptable_contact_time_window ( schedule ) do
1044+ not_before =
1045+ schedule
1046+ |> Schedule . next_available_date_time ( )
1047+
1048+ not_after =
1049+ schedule
1050+ |> Schedule . at_end_time ( not_before )
1051+
1052+ { not_before , not_after }
1053+ end
10461054end
0 commit comments