File size: 865 Bytes
bf2bf0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from typing import List, Dict

from core.constants import Tables
from core.interface.supabase_client import supabase_config, async_supabase_client
from core.schemas.chat import ChatModel


class ChatDataSource:
    @staticmethod
    async def fetch_previous_chats(user_id: str) -> List[Dict]:
        supabase = await async_supabase_client(supabase_config)
        response = await supabase.table(Tables.AI_CHAT_TABLE) \
            .select('*') \
            .eq('user_id', user_id) \
            .order('datetime', desc=True) \
            .execute()
        return response.data

    @staticmethod
    async def send_new_chat(chat: ChatModel):
        supabase = await async_supabase_client(supabase_config)
        response = await supabase.table(Tables.AI_CHAT_TABLE) \
            .insert(chat.to_json()) \
            .execute()
        return response.data