2023-10-16 17:11:34 +03:00
|
|
|
from datetime import datetime, timedelta
|
2023-10-05 10:47:29 +03:00
|
|
|
from typing import Optional
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
2023-11-07 16:03:50 +03:00
|
|
|
from models.databases.repository import Repository
|
2023-10-05 10:47:29 +03:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class OnboardingUpdatableProperties(BaseModel):
|
2023-10-05 12:31:26 +03:00
|
|
|
|
2023-10-05 10:47:29 +03:00
|
|
|
"""Properties that can be received on onboarding update"""
|
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
onboarding_a: Optional[bool]
|
2023-10-05 10:47:29 +03:00
|
|
|
onboarding_b1: Optional[bool]
|
|
|
|
onboarding_b2: Optional[bool]
|
|
|
|
onboarding_b3: Optional[bool]
|
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
class Config:
|
|
|
|
extra = "forbid"
|
2023-10-05 10:47:29 +03:00
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
|
|
|
|
class OnboardingStates(BaseModel):
|
2023-10-05 12:31:26 +03:00
|
|
|
"""Response when getting onboarding"""
|
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
onboarding_a: bool
|
2023-10-05 12:31:26 +03:00
|
|
|
onboarding_b1: bool
|
|
|
|
onboarding_b2: bool
|
|
|
|
onboarding_b3: bool
|
|
|
|
|
|
|
|
|
2023-10-05 10:47:29 +03:00
|
|
|
class Onboarding(Repository):
|
|
|
|
def __init__(self, supabase_client):
|
|
|
|
self.db = supabase_client
|
|
|
|
|
2023-10-06 13:37:12 +03:00
|
|
|
def get_user_onboarding(self, user_id: UUID) -> OnboardingStates | None:
|
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
|
|
|
|
|
|
|
def update_user_onboarding(
|
|
|
|
self, user_id: UUID, onboarding: OnboardingUpdatableProperties
|
2023-10-06 13:37:12 +03:00
|
|
|
) -> OnboardingStates:
|
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
|
|
|
|
|
|
|
def remove_user_onboarding(self, user_id: UUID) -> OnboardingStates | None:
|
|
|
|
"""
|
|
|
|
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-13 21:02:10 +03:00
|
|
|
def create_user_onboarding(self, user_id: UUID) -> OnboardingStates:
|
|
|
|
"""
|
|
|
|
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-10-16 17:11:34 +03:00
|
|
|
def remove_onboarding_more_than_x_days(self, days: int):
|
|
|
|
"""
|
|
|
|
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
|