|
import os |
|
import datetime as DT |
|
from typing import TypedDict, List |
|
from supabase import create_client, Client |
|
from zoneinfo import ZoneInfo |
|
|
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
url: str = os.environ.get("SUPABASE_URL") |
|
key: str = os.environ.get("SUPABASE_KEY") |
|
supabase: Client = create_client(url, key) |
|
|
|
Activity = TypedDict("Activity", { |
|
"id": str, |
|
"email": str, |
|
"chat_history": list, |
|
"messages": list, |
|
"buttons": list, |
|
"created_at": str, |
|
"updated_at": str, |
|
}) |
|
|
|
|
|
def getUserActivities(emailId: str, id=None) -> List[Activity]: |
|
filterQuery = supabase.table("user_activities") \ |
|
.select("*") \ |
|
.eq("email", emailId) |
|
|
|
if id: |
|
filterQuery = filterQuery.eq("id", id) |
|
|
|
response = filterQuery \ |
|
.order("updated_at", desc=True) \ |
|
.execute() |
|
|
|
activities = response.data |
|
return activities or [] |
|
|
|
|
|
def createUserActivity(emailId: str, chatHistory: list, messages: list, buttons: list) -> str: |
|
data, count = supabase.table('user_activities').insert({ |
|
"email": emailId, |
|
"chat_history": chatHistory, |
|
"messages": messages, |
|
"buttons": buttons, |
|
}).execute() |
|
|
|
if data: |
|
print(f"{data=}") |
|
activityId = data[1][0]["id"] |
|
return activityId |
|
|
|
|
|
def updateUserActivityById(id: str, chatHistory: list, messages: list, buttons: list): |
|
data, count = supabase.table('user_activities').update({ |
|
"chat_history": chatHistory, |
|
"messages": messages, |
|
"buttons": buttons, |
|
"updated_at": DT.datetime.now(ZoneInfo("UTC")).isoformat() |
|
}).eq("id", id).execute() |
|
|