-
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 #48 from DostEducation/feature/18-user-attribute-s…
…ervice Feature/18 user attribute service
- Loading branch information
Showing
4 changed files
with
66 additions
and
2 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,5 +1,6 @@ | ||
from .flow_run_log_service import * | ||
from .user_activities_service import * | ||
from .user_attribute_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,45 @@ | ||
from typing import Any | ||
|
||
from api import db, models | ||
from api.utils.loggingutils import logger | ||
|
||
|
||
class UserAttributeService: | ||
def __init__(self, user): | ||
self.user_id = user.id | ||
self.user_phone = user.phone | ||
self.class_model = models.UserAttributes | ||
|
||
def handle_contact_fields_data(self, contact_data: dict[str, Any]): | ||
try: | ||
contact_fields_data = contact_data.get("fields", {}) | ||
existing_attributes = self.class_model.query.get_existing_user_attributes( | ||
self.user_id | ||
) | ||
for field_key, field_value in contact_fields_data.items(): | ||
value = field_value.get("value") | ||
user_attribute = existing_attributes.get(field_key) | ||
|
||
if user_attribute: | ||
if user_attribute.field_value != value: | ||
user_attribute.field_value = value | ||
else: | ||
user_attribute = self.create_user_attribute(field_key, value) | ||
db.session.add(user_attribute) | ||
|
||
db.session.commit() | ||
except Exception as e: | ||
logger.error( | ||
f"Error occured while handling contact fields data. Error: {e}" | ||
) | ||
|
||
def create_user_attribute( | ||
self, field_key: str, value: str | ||
) -> models.UserAttributes: | ||
user_attribute = self.class_model( | ||
user_id=self.user_id, | ||
user_phone=self.user_phone, | ||
field_name=field_key, | ||
field_value=value, | ||
) | ||
return user_attribute |
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