|
| 1 | +import os |
| 2 | +from typing import Optional |
| 3 | +from uuid import UUID |
| 4 | + |
| 5 | +import resend |
| 6 | +from logger import get_logger |
| 7 | +from models.settings import CommonsDep, common_dependencies |
| 8 | +from pydantic import BaseModel |
| 9 | + |
| 10 | +logger = get_logger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class BrainSubscription(BaseModel): |
| 14 | + brain_id: Optional[UUID] = None |
| 15 | + inviter_email: Optional[str] |
| 16 | + email: Optional[str] |
| 17 | + rights: Optional[str] |
| 18 | + |
| 19 | + class Config: |
| 20 | + arbitrary_types_allowed = True |
| 21 | + |
| 22 | + @property |
| 23 | + def commons(self) -> CommonsDep: |
| 24 | + return common_dependencies() |
| 25 | + |
| 26 | + def create_subscription_invitation(self): |
| 27 | + logger.info("Creating subscription invitation") |
| 28 | + response = ( |
| 29 | + self.commons["supabase"] |
| 30 | + .table("brain_subscription_invitations") |
| 31 | + .insert({"brain_id": str(self.brain_id), "email": self.email, "rights": self.rights}) |
| 32 | + .execute() |
| 33 | + ) |
| 34 | + return response.data |
| 35 | + |
| 36 | + def update_subscription_invitation(self): |
| 37 | + logger.info('Updating subscription invitation') |
| 38 | + response = ( |
| 39 | + self.commons["supabase"] |
| 40 | + .table("brain_subscription_invitations") |
| 41 | + .update({"rights": self.rights}) |
| 42 | + .eq("brain_id", str(self.brain_id)) |
| 43 | + .eq("email", self.email) |
| 44 | + .execute() |
| 45 | + ) |
| 46 | + return response.data |
| 47 | + |
| 48 | + def create_or_update_subscription_invitation(self): |
| 49 | + response = self.commons["supabase"].table("brain_subscription_invitations").select("*").eq("brain_id", str(self.brain_id)).eq("email", self.email).execute() |
| 50 | + |
| 51 | + if response.data: |
| 52 | + response = self.update_subscription_invitation() |
| 53 | + else: |
| 54 | + response = self.create_subscription_invitation() |
| 55 | + |
| 56 | + return response |
| 57 | + |
| 58 | + def get_brain_url(self) -> str: |
| 59 | + """Generates the brain URL based on the brain_id.""" |
| 60 | + base_url = "https://www.quivr.app/chat" |
| 61 | + return f"{base_url}?brain_subscription_invitation={self.brain_id}" |
| 62 | + |
| 63 | + def resend_invitation_email(self): |
| 64 | + resend.api_key = os.getenv("RESEND_API_KEY") |
| 65 | + |
| 66 | + brain_url = self.get_brain_url() |
| 67 | + |
| 68 | + html_body = f""" |
| 69 | + <p>This brain has been shared with you by {self.inviter_email}.</p> |
| 70 | + <p><a href='{brain_url}'>Click here</a> to access your brain.</p> |
| 71 | + """ |
| 72 | + |
| 73 | + try: |
| 74 | + r = resend.Emails.send({ |
| 75 | + |
| 76 | + "to": self.email, |
| 77 | + "subject": "Quivr - Brain Shared With You", |
| 78 | + "html": html_body |
| 79 | + }) |
| 80 | + print('Resend response', r) |
| 81 | + except Exception as e: |
| 82 | + logger.error(f"Error sending email: {e}") |
| 83 | + return |
| 84 | + |
| 85 | + return r |
0 commit comments