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

Added dynamic user details #22

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ Example of Pull Request Title: #1023 Add user authentication functionality
- [ ] The changes requires a change to the documentation.
- [ ] I have updated the documentation based on the my changes.
- [ ] I have added tests to cover my changes (if not applicable, please state why)
- [ ] All new and existing tests are passing.\
- [ ] All new and existing tests are passing.
46 changes: 29 additions & 17 deletions api/services/user_creation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,42 @@


class UserCreationService:
def __init__(self):
self.class_model = models.Users

def create_new_user(self, contact_data):
try:
user_phone = contact_data["phone"]
formatted_user_phone = int(user_phone[10:])
user = models.Users.query.get_by_phone(formatted_user_phone)
if not user:
glific_user_id = contact_data["id"]
name = contact_data["name"]

user = models.Users(
glific_user_id=glific_user_id,
phone=formatted_user_phone,
name=name,
location=None,
formatted_user_phone = int(user_phone[-10:])
user = self.class_model.query.get_by_phone(formatted_user_phone)
if user:
logger.info(
f"Skipped user creation for user {user.phone}. "
"Reason: User already exists."
)
return user

db_utils.save(user)
logger.info(f"Created a new user entry with phone number {user.phone}.")
logger.info(
f"Skipped user creation for user {user.phone}. "
"Reason: User already exists."
)
user = self.create_user(contact_data, formatted_user_phone)

logger.info(f"Created a new user entry with phone number {user.phone}.")

return user
except Exception as e:
logger.error(
f"Error while creating new user. Contact data: {contact_data}."
f"Error message: {e}"
)

def create_user(self, contact_data, formatted_user_phone):
glific_user_id = contact_data["id"]
name = contact_data["name"]

user = self.class_model(
glific_user_id=glific_user_id,
phone=formatted_user_phone,
name=name,
location=None,
)

db_utils.save(user)
return user
8 changes: 4 additions & 4 deletions api/services/user_indicator_response_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@


class UserIndicatorResponseService:
def __init__(self, user_id, user_phone, user_flow_id):
def __init__(self, user, user_flow_id):
self.key = "indicator_question" # Prefix for the indicators key in payload
self.user_id = user_id
self.user_phone = user_phone
self.user_id = user.id
self.user_phone = user.phone
self.user_flow_id = user_flow_id
self.class_model = models.UserIndicatorResponses

Expand All @@ -18,7 +18,7 @@ def process_user_indicator_responses(self, data):
response_key = f"{indicator_key}_response"
response_value = data.get(response_key)
if response_value is not None:
user_response = self.class_model.UserIndicatorResponses(
user_response = self.class_model(
user_id=self.user_id,
user_phone=self.user_phone,
user_flow_id=self.user_flow_id,
Expand Down
17 changes: 7 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,23 @@ def handle_webhook(json_data):

contact_data = json_data["contact"]
if contact_data:
handle_contact_field_data(contact_data)

user_id = 1 # Placeholder for user id. Will make is dynamic.
process_user_indicators(user_id, json_data)
user = handle_contact_field_data(contact_data)
process_user_indicators(user, json_data)


def handle_contact_field_data(contact_data):
user_creation_service = UserCreationService()
user_creation_service.create_new_user(contact_data)
return user_creation_service.create_new_user(contact_data)


def process_user_indicators(
user_id,
user,
json_data,
):
user_phone = json_data.get("phone")

user_flow_id = (
1 # Placeholder for user_flow_id, should be obtained from user_flow details.
)
user_indicator_res_service = UserIndicatorResponseService(
user_id, user_phone, user_flow_id
)
user_indicator_res_service = UserIndicatorResponseService(user, user_flow_id)
user_indicator_res_service.process_user_indicator_responses(json_data)
return True