2023-10-05 10:47:29 +03:00
|
|
|
from typing import Optional
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from models.databases.repository import (
|
|
|
|
Repository, # Assuming you have a repository class
|
|
|
|
)
|
|
|
|
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])
|