2023-07-18 10:47:59 +03:00
|
|
|
from logger import get_logger
|
2023-08-21 13:25:16 +03:00
|
|
|
from models import BrainSubscription, get_supabase_client
|
2023-07-18 10:47:59 +03:00
|
|
|
|
2023-08-30 16:55:06 +03:00
|
|
|
from repository.brain import get_brain_for_user
|
|
|
|
from repository.user.get_user_id_by_user_email import get_user_id_by_user_email
|
|
|
|
|
2023-07-18 10:47:59 +03:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class SubscriptionInvitationService:
|
2023-08-03 21:24:42 +03:00
|
|
|
def __init__(self):
|
|
|
|
self.supabase_client = get_supabase_client()
|
2023-07-18 10:47:59 +03:00
|
|
|
|
|
|
|
def create_subscription_invitation(self, brain_subscription: BrainSubscription):
|
|
|
|
logger.info("Creating subscription invitation")
|
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-18 19:28:44 +03:00
|
|
|
.insert(
|
|
|
|
{
|
|
|
|
"brain_id": str(brain_subscription.brain_id),
|
|
|
|
"email": brain_subscription.email,
|
|
|
|
"rights": brain_subscription.rights,
|
|
|
|
}
|
|
|
|
)
|
2023-07-18 10:47:59 +03:00
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response.data
|
|
|
|
|
|
|
|
def update_subscription_invitation(self, brain_subscription: BrainSubscription):
|
2023-07-18 19:28:44 +03:00
|
|
|
logger.info("Updating subscription invitation")
|
2023-07-18 10:47:59 +03:00
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-18 10:47:59 +03:00
|
|
|
.update({"rights": brain_subscription.rights})
|
|
|
|
.eq("brain_id", str(brain_subscription.brain_id))
|
|
|
|
.eq("email", brain_subscription.email)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
return response.data
|
|
|
|
|
2023-07-18 19:28:44 +03:00
|
|
|
def create_or_update_subscription_invitation(
|
2023-08-30 16:55:06 +03:00
|
|
|
self,
|
|
|
|
brain_subscription: BrainSubscription,
|
|
|
|
) -> bool:
|
|
|
|
"""
|
|
|
|
Creates a subscription invitation if it does not exist, otherwise updates it.
|
|
|
|
Returns True if the invitation was created or updated and False if user already has access.
|
|
|
|
"""
|
2023-07-18 19:28:44 +03:00
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-18 19:28:44 +03:00
|
|
|
.select("*")
|
|
|
|
.eq("brain_id", str(brain_subscription.brain_id))
|
|
|
|
.eq("email", brain_subscription.email)
|
|
|
|
.execute()
|
|
|
|
)
|
2023-07-18 10:47:59 +03:00
|
|
|
|
|
|
|
if response.data:
|
2023-08-30 16:55:06 +03:00
|
|
|
self.update_subscription_invitation(brain_subscription)
|
|
|
|
return True
|
2023-07-18 10:47:59 +03:00
|
|
|
else:
|
2023-08-30 16:55:06 +03:00
|
|
|
user_id = get_user_id_by_user_email(brain_subscription.email)
|
|
|
|
brain_id = brain_subscription.brain_id
|
|
|
|
brain_user = get_brain_for_user(user_id, brain_id)
|
|
|
|
|
|
|
|
if brain_user is None:
|
|
|
|
self.create_subscription_invitation(brain_subscription)
|
|
|
|
return True
|
2023-07-18 10:47:59 +03:00
|
|
|
|
2023-08-30 16:55:06 +03:00
|
|
|
return False
|
2023-07-18 10:47:59 +03:00
|
|
|
|
|
|
|
def fetch_invitation(self, subscription: BrainSubscription):
|
|
|
|
logger.info("Fetching subscription invitation")
|
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-18 10:47:59 +03:00
|
|
|
.select("*")
|
|
|
|
.eq("brain_id", str(subscription.brain_id))
|
|
|
|
.eq("email", subscription.email)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
if response.data:
|
|
|
|
return response.data[0] # return the first matching invitation
|
|
|
|
else:
|
|
|
|
return None
|
2023-07-18 19:28:44 +03:00
|
|
|
|
2023-07-18 10:47:59 +03:00
|
|
|
def remove_invitation(self, subscription: BrainSubscription):
|
2023-07-18 19:28:44 +03:00
|
|
|
logger.info(
|
|
|
|
f"Removing subscription invitation for email {subscription.email} and brain {subscription.brain_id}"
|
|
|
|
)
|
2023-07-18 10:47:59 +03:00
|
|
|
response = (
|
2023-08-03 21:24:42 +03:00
|
|
|
self.supabase_client.table("brain_subscription_invitations")
|
2023-07-18 10:47:59 +03:00
|
|
|
.delete()
|
|
|
|
.eq("brain_id", str(subscription.brain_id))
|
|
|
|
.eq("email", subscription.email)
|
|
|
|
.execute()
|
|
|
|
)
|
2023-07-18 19:28:44 +03:00
|
|
|
logger.info(
|
|
|
|
f"Removed subscription invitation for email {subscription.email} and brain {subscription.brain_id}"
|
|
|
|
)
|
2023-07-18 10:47:59 +03:00
|
|
|
logger.info(response)
|
2023-07-18 19:28:44 +03:00
|
|
|
return response.data
|