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
|
|
|
|
|
|
|
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(
|
|
|
|
self, brain_subscription: BrainSubscription
|
|
|
|
):
|
|
|
|
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:
|
|
|
|
response = self.update_subscription_invitation(brain_subscription)
|
|
|
|
else:
|
2023-07-18 19:28:44 +03:00
|
|
|
response = self.create_subscription_invitation(brain_subscription)
|
2023-07-18 10:47:59 +03:00
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
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
|