Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: schedule weekly reflection when not automatically scheduled #201

Merged
merged 6 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion scheduler/state_machine/controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import logging
from celery import Celery
from datetime import date, datetime, timedelta
from state_machine.state_machine_utils import (create_new_date, get_dialog_completion_state,
from state_machine.state_machine_utils import (compute_previous_day,
create_new_date, get_dialog_completion_state,
get_execution_week, get_intervention_component,
get_activity_completion_state,
get_all_scheduled_occurrence,
Expand Down Expand Up @@ -608,6 +609,26 @@ def on_new_day(self, current_date: date):
# number of weeks doesn't increase
update_execution_week(self.user_id, week_number + 1)

# if the preferred day is passed, and the weekly reflection has not been
# completed nor planned, send it now.

# get the date of the previous preferred day
last_preferred_day = compute_previous_day(self.user_id, current_date)

# make sure that today is not the preferred day
if last_preferred_day != current_date:
component = get_intervention_component(Components.WEEKLY_REFLECTION)
# get the general activity dialog that have been scheduled after the
# last preferred date
next_scheduled = get_next_scheduled_occurrence(self.user_id,
component.intervention_component_id,
last_preferred_day)
# if none have been scheduled, trigger one
if not next_scheduled:
plan_and_store(user_id=self.user_id,
dialog=Components.WEEKLY_REFLECTION,
phase_id=2)

def run(self):
logging.info("Running state %s", self.state)
save_fsm_state_in_db(self.user_id, self.state)
Expand Down
25 changes: 25 additions & 0 deletions scheduler/state_machine/state_machine_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ def compute_next_day(selectable_days: list, current_date: datetime) -> date:
return next_date


def compute_previous_day(user_id: int, current_date: datetime) -> date:
"""
Gets the date of the last preferred day for a user.

Args:
user_id: the id of the user
current_date: the date to start from

Returns:
The date of the last preferred day for a user.
"""

date_time = get_preferred_date_time(user_id=user_id)

# first element of the tuple is the list of days
pref_day = date_time[0][0]
wbaccinelli marked this conversation as resolved.
Show resolved Hide resolved

days_to_pref_day = (current_date.isoweekday() - pref_day) % 7
days_ago = (days_to_pref_day + 7) % 7

pref_day_before_today = current_date - timedelta(days=days_ago)

return pref_day_before_today


def create_new_date(start_date: date,
time_delta: int = 0,
hour: int = 10,
Expand Down