Skip to content

Commit

Permalink
Merge pull request #212 from PerfectFit-project/492-change-quit-date-…
Browse files Browse the repository at this point in the history
…state-machine

Check if user changed quit date
  • Loading branch information
wbaccinelli authored Jan 18, 2024
2 parents 7a3cab5 + d43a732 commit 8ee3089
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
58 changes: 23 additions & 35 deletions scheduler/state_machine/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
get_quit_date, get_pa_group, get_start_date,
is_new_week, plan_and_store, plan_every_day_range,
reschedule_dialog,
retrieve_tracking_day, revoke_execution,
retrieve_tracking_day,
run_uncompleted_dialog, run_option_menu,
save_fsm_state_in_db,
schedule_next_execution, store_completed_dialog,
store_scheduled_dialog, update_execution_week,
update_fsm_dialog_running_status,
dialogs_to_be_completed, get_component_id)
dialogs_to_be_completed, get_component_id,
reschedule_weekly_reflection,
plan_new_date_notifications)
from state_machine.const import (ACTIVITY_C2_9_DAY_TRIGGER, FUTURE_SELF_INTRO, GOAL_SETTING,
TRACKING_DURATION, TIMEZONE, PREPARATION_GA, PAUSE_AND_TRIGGER,
MAX_PREPARATION_DURATION, HIGH_PA_GROUP,
Expand Down Expand Up @@ -528,6 +530,23 @@ def on_dialog_completed(self, dialog):
elif dialog == Components.WEEKLY_REFLECTION:
logging.info('Weekly reflection completed')

quit_date = get_quit_date(self.user_id)
current_date = date.today()

# if the quit date is in the future, it has been reset
# during the weekly reflection dialog
if quit_date > current_date:
# if a new quit date has been set, the weekly reflection might be rescheduled,
# a notification on the day before and on the new date are planned.
# Then we go back to the buffer state
reschedule_weekly_reflection(self.user_id, quit_date)
plan_new_date_notifications(self.user_id, quit_date)
self.set_new_state(BufferState(self.user_id))
store_completed_dialog(user_id=self.user_id,
dialog=Components.RELAPSE_DIALOG,
phase_id=3)
return

week = get_execution_week(user_id=self.user_id)

# on the first week, the execution of the general activity dialog
Expand Down Expand Up @@ -790,8 +809,8 @@ def on_dialog_completed(self, dialog):
# if a new quit date has been set, the weekly reflection might be rescheduled,
# a notification on the day before and on the new date are planned.
# Then we go back to the buffer state
self.reschedule_weekly_reflection(quit_date)
self.plan_new_date_notifications(quit_date)
reschedule_weekly_reflection(self.user_id, quit_date)
plan_new_date_notifications(self.user_id, quit_date)
self.set_new_state(BufferState(self.user_id))

else:
Expand Down Expand Up @@ -839,37 +858,6 @@ def on_user_trigger(self, dialog: str):
dialog=dialog,
phase_id=3)

def plan_new_date_notifications(self, quit_date: date):
# plan the notification for the day before the quit date

quit_datetime = create_new_date(quit_date)

plan_and_store(user_id=self.user_id,
dialog=Notifications.BEFORE_QUIT_NOTIFICATION,
planned_date=quit_datetime - timedelta(days=1),
phase_id=3)

def reschedule_weekly_reflection(self, quit_date: date):

component = get_intervention_component(Components.WEEKLY_REFLECTION)

next_occurrence = get_next_scheduled_occurrence(
user_id=self.user_id,
intervention_component_id=component.intervention_component_id,
current_date=datetime.now()
)

quit_datetime = create_new_date(quit_date)

if next_occurrence is not None and next_occurrence.next_planned_date < quit_datetime:
# revoke the planned task
revoke_execution(next_occurrence.task_uuid)
# plan a new one
schedule_next_execution(user_id=self.user_id,
dialog=Components.WEEKLY_REFLECTION,
current_date=quit_datetime,
phase_id=2)


class ClosingState(State):

Expand Down
47 changes: 46 additions & 1 deletion scheduler/state_machine/state_machine_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
InterventionActivitiesPerformed,
InterventionComponents, InterventionPhases, Users,
UserStateMachine, UserInterventionState)
from virtual_coach_db.helper.definitions import Components, ComponentsTriggers, DialogQuestionsEnum
from virtual_coach_db.helper.definitions import (Components, ComponentsTriggers,
DialogQuestionsEnum, Notifications)
from virtual_coach_db.helper.helper_functions import get_db_session

celery = Celery(broker=REDIS_URL)
Expand Down Expand Up @@ -1243,3 +1244,47 @@ def schedule_next_execution(user_id: int, dialog: str, phase_id: int, current_da
dialog=dialog,
planned_date=planned_date,
phase_id=phase_id)

def plan_new_date_notifications(user_id: int, quit_date: date):
"""
Plan the user notification for the day before the quit date
Args:
user_id: id of the user
quit_date: new quit date selected by user
"""
quit_datetime = create_new_date(quit_date)

plan_and_store(user_id=user_id,
dialog=Notifications.BEFORE_QUIT_NOTIFICATION,
planned_date=quit_datetime - timedelta(days=1),
phase_id=3)

def reschedule_weekly_reflection(user_id: int, quit_date: date):
"""
Reschedule the weekly reflection dialog to after the
new quit date
Args:
user_id: id of the user
quit_date: new quit date selected by user
"""

component = get_intervention_component(Components.WEEKLY_REFLECTION)

next_occurrence = get_next_scheduled_occurrence(
user_id=user_id,
intervention_component_id=component.intervention_component_id,
current_date=datetime.now()
)

quit_datetime = create_new_date(quit_date)

if next_occurrence is not None and next_occurrence.next_planned_date < quit_datetime:
# revoke the planned task
revoke_execution(next_occurrence.task_uuid)
# plan a new one
schedule_next_execution(user_id=user_id,
dialog=Components.WEEKLY_REFLECTION,
current_date=quit_datetime,
phase_id=2)

0 comments on commit 8ee3089

Please sign in to comment.