2023-07-11 19:20:31 +03:00
|
|
|
from typing import Optional
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
import resend
|
|
|
|
from logger import get_logger
|
2023-07-17 16:45:31 +03:00
|
|
|
from models.settings import BrainSettings, CommonsDep, common_dependencies
|
2023-07-11 19:20:31 +03:00
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class BrainSubscription(BaseModel):
|
|
|
|
brain_id: Optional[UUID] = None
|
|
|
|
inviter_email: Optional[str]
|
|
|
|
email: Optional[str]
|
|
|
|
rights: Optional[str]
|
|
|
|
|
|
|
|
class Config:
|
|
|
|
arbitrary_types_allowed = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def commons(self) -> CommonsDep:
|
|
|
|
return common_dependencies()
|
|
|
|
|
|
|
|
def create_subscription_invitation(self):
|
|
|
|
logger.info("Creating subscription invitation")
|
|
|
|
response = (
|
|
|
|
self.commons["supabase"]
|
|
|
|
.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 = (
|
|
|
|
self.commons["supabase"]
|
|
|
|
.table("brain_subscription_invitations")
|
|
|
|
.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 = (
|
|
|
|
self.commons["supabase"]
|
|
|
|
.table("brain_subscription_invitations")
|
|
|
|
.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
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_brain_url(self) -> str:
|
|
|
|
"""Generates the brain URL based on the brain_id."""
|
|
|
|
base_url = "https://www.quivr.app/chat"
|
|
|
|
return f"{base_url}?brain_subscription_invitation={self.brain_id}"
|
|
|
|
|
|
|
|
def resend_invitation_email(self):
|
2023-07-17 16:45:31 +03:00
|
|
|
brains_settings = BrainSettings() # pyright: ignore reportPrivateUsage=none
|
|
|
|
resend.api_key = brains_settings.resend_api_key
|
2023-07-11 19:20:31 +03:00
|
|
|
|
|
|
|
brain_url = self.get_brain_url()
|
|
|
|
|
|
|
|
html_body = f"""
|
|
|
|
<p>This brain has been shared with you by {self.inviter_email}.</p>
|
|
|
|
<p><a href='{brain_url}'>Click here</a> to access your brain.</p>
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
2023-07-17 09:57:27 +03:00
|
|
|
r = resend.Emails.send(
|
|
|
|
{
|
2023-07-17 16:45:31 +03:00
|
|
|
"from": brains_settings.resend_email_address,
|
2023-07-17 09:57:27 +03:00
|
|
|
"to": self.email,
|
|
|
|
"subject": "Quivr - Brain Shared With You",
|
|
|
|
"html": html_body,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
print("Resend response", r)
|
2023-07-11 19:20:31 +03:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error sending email: {e}")
|
|
|
|
return
|
|
|
|
|
|
|
|
return r
|