-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from DostEducation/feature/15-create-flow-run-…
…log-service Feature/15 create flow run log service
- Loading branch information
Showing
6 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .flow_run_log_service import * | ||
from .user_creation_service import * | ||
from .user_indicator_response_service import * | ||
from .webhook_transaction_log_service import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from api import models | ||
from api.utils import db_utils | ||
from api.utils.loggingutils import logger | ||
|
||
|
||
class FlowRunLogService: | ||
def __init__(self, user): | ||
self.user = user | ||
self.class_model = models.UserFlows | ||
|
||
def create_user_flow_log(self, json_data): | ||
try: | ||
flow_uuid = json_data.get("flow_uuid") | ||
flow_name = json_data.get("flow_name") | ||
flow_type = json_data.get("flow_type") | ||
flow_completed = json_data.get("flow_completed") | ||
|
||
today_flow_log = self.class_model.query.get_by_flow_uuid_and_phone( | ||
flow_uuid, self.user.phone | ||
) | ||
|
||
user_flow_log = None | ||
|
||
if not today_flow_log: | ||
user_flow_log = self.create_log(flow_uuid, flow_name, flow_type) | ||
elif today_flow_log and flow_completed: | ||
user_flow_log = self.update_log(today_flow_log) | ||
|
||
return user_flow_log | ||
except Exception as e: | ||
logger.error( | ||
f"Error while creating new user flow log. json data: {json_data}." | ||
f"Error message: {e}" | ||
) | ||
raise | ||
|
||
def create_log(self, flow_uuid, flow_name, flow_type): | ||
user_flow_log = self.class_model( | ||
user_id=self.user.id, | ||
user_phone=self.user.phone, | ||
flow_uuid=flow_uuid, | ||
flow_name=flow_name, | ||
flow_type=flow_type, | ||
flow_run_status=self.class_model.FlowRunStatus.IN_PROGRESS, | ||
flow_start_time=datetime.utcnow() + timedelta(minutes=330), | ||
is_active=True, | ||
) | ||
|
||
db_utils.save(user_flow_log) | ||
logger.info(f"Created a user flow log for phone number {self.user.phone}.") | ||
return user_flow_log | ||
|
||
def update_log(self, today_flow_log): | ||
today_flow_log.flow_run_status = self.class_model.FlowRunStatus.COMPLETED | ||
today_flow_log.flow_end_time = datetime.utcnow() + timedelta(minutes=330) | ||
today_flow_log.is_active = False | ||
|
||
db_utils.save(today_flow_log) | ||
logger.info(f"Updated user flow log for phone number {self.user.phone}.") | ||
return today_flow_log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
migrations/versions/cf56b46971fa_adding_index_to_the_flou_uuid_cloumn_in_.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Adding index to the flou uuid cloumn in user flow table. | ||
Revision ID: cf56b46971fa | ||
Revises: 06b59f5dd95e | ||
Create Date: 2024-05-03 15:19:29.399203 | ||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "cf56b46971fa" | ||
down_revision = "06b59f5dd95e" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("user_flows", schema=None) as batch_op: | ||
batch_op.create_index( | ||
batch_op.f("ix_user_flows_flow_uuid"), ["flow_uuid"], unique=False | ||
) | ||
|
||
with op.batch_alter_table("user_indicator_responses", schema=None) as batch_op: | ||
batch_op.alter_column( | ||
"id", | ||
existing_type=sa.INTEGER(), | ||
type_=sa.BigInteger(), | ||
existing_nullable=False, | ||
autoincrement=True, | ||
) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
with op.batch_alter_table("user_indicator_responses", schema=None) as batch_op: | ||
batch_op.alter_column( | ||
"id", | ||
existing_type=sa.BigInteger(), | ||
type_=sa.INTEGER(), | ||
existing_nullable=False, | ||
autoincrement=True, | ||
) | ||
|
||
with op.batch_alter_table("user_flows", schema=None) as batch_op: | ||
batch_op.drop_index(batch_op.f("ix_user_flows_flow_uuid")) | ||
|
||
# ### end Alembic commands ### |