Skip to content

Commit

Permalink
Merge pull request #46 from DostEducation/bugfix/correcting-logic-in-…
Browse files Browse the repository at this point in the history
…user-flow-and-activities

Bugfix/correcting logic in user flow and activities
  • Loading branch information
Sachinbisht27 authored May 23, 2024
2 parents 75709a8 + 6236c09 commit d43df23
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
13 changes: 11 additions & 2 deletions api/helpers/common_helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from datetime import datetime, timezone
from typing import Any


def get_current_utc_timestamp() -> datetime:
return datetime.now(timezone.utc)


def check_activity_key(activity_key: str, keyword: str, status: str):
return activity_key.startswith(keyword) and activity_key.endswith(status)
def check_activity_key(
activity_key: str, activity_value: dict[str, Any], keyword: str, status: str
):
return (
True
if activity_key.startswith(keyword)
and activity_key.endswith(status)
and activity_value["value"] == "yes"
else False
)
2 changes: 1 addition & 1 deletion api/models/user_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class UserFlows(TimestampMixin, db.Model):
query_class = UserFlowsQuery

class FlowRunStatus:
STARTED = "started"
SENT = "sent"
COMPLETED = "completed"
ENDED = "ended"

Expand Down
14 changes: 6 additions & 8 deletions api/services/flow_run_log_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ def create_user_flow_log(self, json_data: dict[str, Any]) -> models.UserFlows:
)
)

if flow_status == self.class_model.FlowRunStatus.STARTED:
if flow_status == self.class_model.FlowRunStatus.SENT:
user_flow_log = self.create_log(flow_uuid, flow_name, flow_type)
elif flow_status == self.class_model.FlowRunStatus.COMPLETED:
user_flow_log = self.update_log(latest_flow_log)
else:
logger.error(
f"Got unexpected flow status {flow_status}. Flow name {flow_name}."
)

except Exception as e:
logger.error(
Expand All @@ -43,7 +39,9 @@ def create_user_flow_log(self, json_data: dict[str, Any]) -> models.UserFlows:
raise

if user_flow_log is None:
raise ValueError("Failed to create or update user flow log.")
return models.UserFlows.query.get_todays_latest_user_flow(
flow_uuid, self.user.phone
)

return user_flow_log

Expand All @@ -58,8 +56,8 @@ def create_log(
user_phone=self.user.phone,
flow_uuid=flow_uuid,
flow_name=flow_name,
flow_type=flow_type,
flow_run_status=self.class_model.FlowRunStatus.STARTED,
flow_type=flow_type if flow_type else "activity",
flow_run_status=self.class_model.FlowRunStatus.SENT,
flow_start_time=common_helper.get_current_utc_timestamp(),
is_active=True,
)
Expand Down
21 changes: 10 additions & 11 deletions api/services/user_activities_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ def handle_user_activities(self, json_data: dict[str, Any]):
current_time = common_helper.get_current_utc_timestamp()
contact_activities = json_data.get("contact", {}).get("fields", {})

for activity_key, _ in contact_activities.items():
if activity_key.strip() in contact_activities:
user_activity = self.create_or_update_user_activity(
activity_key, current_time
)
if user_activity:
db.session.add(user_activity)
for activity_key, activity_value in contact_activities.items():
user_activity = self.create_or_update_user_activity(
activity_key, activity_value, current_time
)
if user_activity:
db.session.add(user_activity)

db.session.commit()
logger.info(f"Captured user activity for {self.user_phone}.")
Expand All @@ -35,16 +34,16 @@ def handle_user_activities(self, json_data: dict[str, Any]):
)

def create_or_update_user_activity(
self, activity_key: str, current_time: datetime
self, activity_key: str, activity_value: dict[str, Any], current_time: datetime
) -> Optional[models.UserActivities]:
is_started = common_helper.check_activity_key(
activity_key, "activity_", "_started"
activity_key, activity_value, "activity_", "_started"
)
is_succeeded = common_helper.check_activity_key(
activity_key, "activity_", "_success"
activity_key, activity_value, "activity_", "_success"
)
is_completed = common_helper.check_activity_key(
activity_key, "activity_", "_completed"
activity_key, activity_value, "activity_", "_completed"
)

if is_started:
Expand Down
4 changes: 0 additions & 4 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,3 @@
SQLALCHEMY_DATABASE_URI = os.environ.get("SQLALCHEMY_DATABASE_URI")

LOGGING_LEVEL = os.environ.get("LOGGING_LEVEL", "DEBUG")
if LOGGING_LEVEL == "DEBUG":
# Print all SQL queries for debugging.
# Not recommended for production env
SQLALCHEMY_ECHO = True

0 comments on commit d43df23

Please sign in to comment.