2023-10-16 17:11:34 +03:00
|
|
|
from datetime import datetime, timedelta
|
2023-10-05 10:47:29 +03:00
|
|
|
|
|
|
|
from fastapi import HTTPException
|
2023-11-28 16:27:39 +03:00
|
|
|
from models.settings import get_supabase_client
|
2023-11-24 12:25:02 +03:00
|
|
|
from modules.onboarding.entity.onboarding import OnboardingStates
|
2023-10-05 10:47:29 +03:00
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
from .onboardings_interface import OnboardingInterface
|
2023-10-05 10:47:29 +03:00
|
|
|
|
2023-10-05 12:31:26 +03:00
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
class Onboarding(OnboardingInterface):
|
2023-11-28 16:27:39 +03:00
|
|
|
def __init__(self):
|
|
|
|
supabase_client = get_supabase_client()
|
2023-10-05 10:47:29 +03:00
|
|
|
self.db = supabase_client
|
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
def get_user_onboarding(self, user_id):
|
2023-10-05 10:47:29 +03:00
|
|
|
"""
|
|
|
|
Get user onboarding information by user_id
|
|
|
|
"""
|
|
|
|
onboarding_data = (
|
2023-10-06 13:37:12 +03:00
|
|
|
self.db.from_("onboardings")
|
|
|
|
.select(
|
|
|
|
"onboarding_a",
|
|
|
|
"onboarding_b1",
|
|
|
|
"onboarding_b2",
|
|
|
|
"onboarding_b3",
|
|
|
|
)
|
2023-10-05 10:47:29 +03:00
|
|
|
.filter("user_id", "eq", user_id)
|
|
|
|
.limit(1)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
2023-10-05 12:31:26 +03:00
|
|
|
if onboarding_data == []:
|
|
|
|
return None
|
2023-10-05 10:47:29 +03:00
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
return OnboardingStates(**onboarding_data[0])
|
2023-10-05 10:47:29 +03:00
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
def update_user_onboarding(self, user_id, onboarding):
|
2023-10-05 10:47:29 +03:00
|
|
|
"""Update user onboarding information by user_id"""
|
2023-10-06 13:37:12 +03:00
|
|
|
update_data = {
|
|
|
|
key: value for key, value in onboarding.dict().items() if value is not None
|
|
|
|
}
|
|
|
|
|
2023-10-05 10:47:29 +03:00
|
|
|
response = (
|
2023-10-06 13:37:12 +03:00
|
|
|
self.db.from_("onboardings")
|
|
|
|
.update(update_data)
|
2023-10-05 12:31:26 +03:00
|
|
|
.match({"user_id": user_id})
|
2023-10-05 10:47:29 +03:00
|
|
|
.execute()
|
|
|
|
.data
|
|
|
|
)
|
|
|
|
|
|
|
|
if not response:
|
|
|
|
raise HTTPException(404, "User onboarding not updated")
|
2023-10-05 12:31:26 +03:00
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
return OnboardingStates(**response[0])
|
2023-10-11 10:46:57 +03:00
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
def remove_user_onboarding(self, user_id):
|
2023-10-11 10:46:57 +03:00
|
|
|
"""
|
|
|
|
Remove user onboarding information by user_id
|
|
|
|
"""
|
|
|
|
onboarding_data = (
|
|
|
|
self.db.from_("onboardings")
|
|
|
|
.delete()
|
|
|
|
.match({"user_id": str(user_id)})
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
if onboarding_data == []:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return OnboardingStates(**onboarding_data[0])
|
2023-10-16 17:11:34 +03:00
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
def create_user_onboarding(self, user_id):
|
2023-11-13 21:02:10 +03:00
|
|
|
"""
|
|
|
|
Create user onboarding information by user_id
|
|
|
|
"""
|
|
|
|
onboarding_data = (
|
|
|
|
self.db.from_("onboardings")
|
|
|
|
.insert(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"user_id": str(user_id),
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
return OnboardingStates(**onboarding_data[0])
|
|
|
|
|
2023-11-24 12:25:02 +03:00
|
|
|
def remove_onboarding_more_than_x_days(self, days):
|
2023-10-16 17:11:34 +03:00
|
|
|
"""
|
|
|
|
Remove onboarding if it is older than x days
|
|
|
|
"""
|
|
|
|
onboarding_data = (
|
|
|
|
self.db.from_("onboardings")
|
|
|
|
.delete()
|
|
|
|
.lt(
|
|
|
|
"creation_time",
|
|
|
|
(datetime.now() - timedelta(days=days)).strftime(
|
|
|
|
"%Y-%m-%d %H:%M:%S.%f"
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
).data
|
|
|
|
|
|
|
|
return onboarding_data
|