sharktide commited on
Commit
8f0180b
·
verified ·
1 Parent(s): 4b2e1ca

Update subscriptions.py

Browse files
Files changed (1) hide show
  1. subscriptions.py +35 -13
subscriptions.py CHANGED
@@ -5,14 +5,22 @@ SUPABASE_URL = os.getenv("SUPABASE_URL")
5
  SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
6
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
7
 
 
8
  async def fetch_subscription(email: str):
9
- user_res = supabase.table("auth.users").select("*").eq("email", email).execute()
10
  if not user_res.data:
11
  return None
12
 
13
  user = user_res.data[0]
14
 
15
- cust_res = supabase.table("stripe.customers").select("*").eq("email", email).execute()
 
 
 
 
 
 
 
16
  if not cust_res.data:
17
  return {
18
  "email": email,
@@ -22,7 +30,7 @@ async def fetch_subscription(email: str):
22
 
23
  customer = cust_res.data[0]
24
 
25
- # Fetch subscriptions
26
  sub_res = (
27
  supabase.table("stripe.subscriptions")
28
  .select("*")
@@ -38,31 +46,45 @@ async def fetch_subscription(email: str):
38
  "subscription": None
39
  }
40
 
41
- subs = []
 
42
  for s in sub_res.data:
 
43
  price_id = (
44
  s["items"]["data"][0]["price"]["id"]
45
  if s.get("items") and s["items"].get("data")
46
  else None
47
  )
48
 
49
- product_info = get_product_for_price(price_id) if price_id else None
50
- product_name = (
51
- product_info["product"]["name"]
52
- if product_info and product_info.get("product")
53
- else None
54
- )
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- subs.append({
57
  "status": s["status"],
58
  "subscription_id": s["id"],
59
  "current_period_end": s["current_period_end"],
60
  "product_name": product_name,
61
- "nickname": product_info["nickname"] if product_info else None
62
  })
63
 
64
  return {
65
  "email": email,
66
  "signed_up": user["created_at"],
67
- "subscription": subs
68
  }
 
5
  SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
6
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
7
 
8
+
9
  async def fetch_subscription(email: str):
10
+ user_res = supabase.rpc("get_user_info", {"email_input": email}).execute()
11
  if not user_res.data:
12
  return None
13
 
14
  user = user_res.data[0]
15
 
16
+ # 2. Fetch Stripe customer
17
+ cust_res = (
18
+ supabase.table("stripe.customers")
19
+ .select("*")
20
+ .eq("email", email)
21
+ .execute()
22
+ )
23
+
24
  if not cust_res.data:
25
  return {
26
  "email": email,
 
30
 
31
  customer = cust_res.data[0]
32
 
33
+ # 3. Fetch subscriptions
34
  sub_res = (
35
  supabase.table("stripe.subscriptions")
36
  .select("*")
 
46
  "subscription": None
47
  }
48
 
49
+ subscriptions = []
50
+
51
  for s in sub_res.data:
52
+ # Extract price_id from subscription item
53
  price_id = (
54
  s["items"]["data"][0]["price"]["id"]
55
  if s.get("items") and s["items"].get("data")
56
  else None
57
  )
58
 
59
+ # 4. Fetch product info for this price
60
+ product_info = None
61
+ product_name = None
62
+ nickname = None
63
+
64
+ if price_id:
65
+ price_res = (
66
+ supabase.table("stripe.prices")
67
+ .select("id, nickname, product:product_id(name, description)")
68
+ .eq("id", price_id)
69
+ .execute()
70
+ )
71
+
72
+ if price_res.data:
73
+ product_info = price_res.data[0]
74
+ nickname = product_info.get("nickname")
75
+ if product_info.get("product"):
76
+ product_name = product_info["product"].get("name")
77
 
78
+ subscriptions.append({
79
  "status": s["status"],
80
  "subscription_id": s["id"],
81
  "current_period_end": s["current_period_end"],
82
  "product_name": product_name,
83
+ "nickname": nickname
84
  })
85
 
86
  return {
87
  "email": email,
88
  "signed_up": user["created_at"],
89
+ "subscription": subscriptions
90
  }