2023-07-11 19:20:31 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
from logger import get_logger
|
2023-08-03 21:24:42 +03:00
|
|
|
from models.settings import get_supabase_client
|
2023-07-11 19:20:31 +03:00
|
|
|
from pydantic import BaseModel
|
2023-08-03 21:24:42 +03:00
|
|
|
from supabase.client import Client
|
2023-07-20 16:15:43 +03:00
|
|
|
|
2023-07-11 19:20:31 +03:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BrainSubscription(BaseModel):
|
2023-07-18 10:47:59 +03:00
|
|
|
brain_id: UUID
|
|
|
|
email: str
|
|
|
|
rights: str = "Viewer"
|
2023-07-11 19:20:31 +03:00
|
|
|
|
|
|
|
class Config:
|
|
|
|
arbitrary_types_allowed = True
|
|
|
|
|
|
|
|
@property
|
2023-08-03 21:24:42 +03:00
|
|
|
def supabase_client(self) -> Client:
|
|
|
|
return get_supabase_client()
|
2023-07-11 19:20:31 +03:00
|
|
|
|
|
|
|
def create_subscription_invitation(self):
|
|
|
|
logger.info("Creating subscription invitation")
|
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-17 09:57:27 +03:00
|
|
|
.insert(
|
|
|
|
{
|
|
|
|
"brain_id": str(self.brain_id),
|
|
|
|
"email": self.email,
|
|
|
|
"rights": self.rights,
|
|
|
|
}
|
|
|
|
)
|
2023-07-11 19:20:31 +03:00
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
def update_subscription_invitation(self):
|
2023-07-17 09:57:27 +03:00
|
|
|
logger.info("Updating subscription invitation")
|
2023-07-11 19:20:31 +03:00
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-11 19:20:31 +03:00
|
|
|
.update({"rights": self.rights})
|
|
|
|
.eq("brain_id", str(self.brain_id))
|
|
|
|
.eq("email", self.email)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
def create_or_update_subscription_invitation(self):
|
2023-07-17 09:57:27 +03:00
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-17 09:57:27 +03:00
|
|
|
.select("*")
|
|
|
|
.eq("brain_id", str(self.brain_id))
|
|
|
|
.eq("email", self.email)
|
|
|
|
.execute()
|
|
|
|
)
|
2023-07-11 19:20:31 +03:00
|
|
|
|
|
|
|
if response.data:
|
|
|
|
response = self.update_subscription_invitation()
|
|
|
|
else:
|
2023-07-17 09:57:27 +03:00
|
|
|
response = self.create_subscription_invitation()
|
2023-07-11 19:20:31 +03:00
|
|
|
|
2023-07-20 16:15:43 +03:00
|
|
|
return response
|