File size: 1,840 Bytes
2542be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from pydantic import BaseModel
from dotenv import load_dotenv
from supabase import create_client
from functools import lru_cache
import os
load_dotenv()
@lru_cache(maxsize=1)
class Supabase_Settings(BaseModel):

    SUPABASE_URL: str = os.getenv("SUPABASE_URL")
    SUPABASE_ANON_KEY: str = os.getenv("SUPABASE_ANON_KEY")



@lru_cache(maxsize=1)
class Supabase_Clients:
    values = Supabase_Settings()
    anon_supabase = create_client(values.SUPABASE_URL, values.SUPABASE_ANON_KEY)

def fetch_data(user_id:str):
    supabase_clients=Supabase_Clients()
    supabase = supabase_clients.anon_supabase
    response_0 = (
    supabase.table("receipt_radar_structured_data")
    .select("location")
    .eq("user_id",f"{user_id}")
    .eq("brand_category","Travel and Leisure")
    .execute()
    )
    print("Printing fetched data")
    print(response_0)
    return response_0.data

def authenticate_user(apitoken: str):
    supabase_clients = Supabase_Clients()
    supabase = supabase_clients.anon_supabase
    try:
        user = supabase.auth.get_user(apitoken)
        return str(user.user.id)
    except Exception as e:
        print("Exceptional error")
        return "Exceptional error"

def fetch_cards_data(user_id:str):
    supabase_clients=Supabase_Clients()
    supabase = supabase_clients.anon_supabase
    response_0 = (
    supabase.table("receipt_radar_structured_data")
    .select("brand")
    .eq("user_id",f"{user_id}")
    .execute()
    ).data
    response_1 =  (
    supabase.table("card_market")
    .select("brand_name")
    .execute()
    ).data
    brands_response_1_set = set(item['brand_name'].lower() for item in response_1)
    common_brands = [item['brand'] for item in response_0 if item['brand'].lower() in brands_response_1_set]
    print("common_brands")
    print(common_brands)
    return common_brands