sharktide commited on
Commit
828bf6e
·
verified ·
1 Parent(s): ba68bd5

Update subscriptions.py

Browse files
Files changed (1) hide show
  1. subscriptions.py +27 -4
subscriptions.py CHANGED
@@ -5,6 +5,21 @@ 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(jwt: str):
9
  auth_res = supabase.auth.get_user(jwt)
10
  if auth_res.user is None:
@@ -24,7 +39,8 @@ async def fetch_subscription(jwt: str):
24
  return {
25
  "email": email,
26
  "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
27
- "subscription": None
 
28
  }
29
 
30
  customer = cust_res.data[0]
@@ -42,10 +58,12 @@ async def fetch_subscription(jwt: str):
42
  return {
43
  "email": email,
44
  "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
45
- "subscription": None
 
46
  }
47
 
48
  subscriptions = []
 
49
 
50
  for s in sub_res.data:
51
  price_id = (
@@ -80,16 +98,21 @@ async def fetch_subscription(jwt: str):
80
  if product_res.data:
81
  product_name = product_res.data[0].get("name")
82
 
 
83
  subscriptions.append({
84
  "status": s["status"],
85
  "subscription_id": s["id"],
86
  "current_period_end": s["current_period_end"],
87
  "product_name": product_name,
88
- "nickname": nickname
 
89
  })
 
 
90
 
91
  return {
92
  "email": email,
93
  "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
94
- "subscription": subscriptions
 
95
  }
 
5
  SUPABASE_KEY = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
6
  supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
7
 
8
+
9
+ def normalize_plan_key(plan_name: str | None) -> str:
10
+ if not plan_name:
11
+ return "free"
12
+ normalized = "".join(ch for ch in str(plan_name).lower() if ch.isalpha())
13
+ if "professional" in normalized:
14
+ return "professional"
15
+ if "creator" in normalized:
16
+ return "creator"
17
+ if "pro" in normalized:
18
+ return "pro"
19
+ if "light" in normalized:
20
+ return "light"
21
+ return "free"
22
+
23
  async def fetch_subscription(jwt: str):
24
  auth_res = supabase.auth.get_user(jwt)
25
  if auth_res.user is None:
 
39
  return {
40
  "email": email,
41
  "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
42
+ "subscription": None,
43
+ "plan_key": "free",
44
  }
45
 
46
  customer = cust_res.data[0]
 
58
  return {
59
  "email": email,
60
  "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
61
+ "subscription": None,
62
+ "plan_key": "free",
63
  }
64
 
65
  subscriptions = []
66
+ preferred_plan_key = "free"
67
 
68
  for s in sub_res.data:
69
  price_id = (
 
98
  if product_res.data:
99
  product_name = product_res.data[0].get("name")
100
 
101
+ plan_key = normalize_plan_key(product_name or nickname)
102
  subscriptions.append({
103
  "status": s["status"],
104
  "subscription_id": s["id"],
105
  "current_period_end": s["current_period_end"],
106
  "product_name": product_name,
107
+ "nickname": nickname,
108
+ "plan_key": plan_key,
109
  })
110
+ if s.get("status") in ("active", "trialing"):
111
+ preferred_plan_key = plan_key
112
 
113
  return {
114
  "email": email,
115
  "signed_up": user.created_at.isoformat() if hasattr(user.created_at, "isoformat") else user.created_at,
116
+ "subscription": subscriptions,
117
+ "plan_key": preferred_plan_key,
118
  }