habulaj commited on
Commit
977d398
·
verified ·
1 Parent(s): 49851a0

Update routes/stylist.py

Browse files
Files changed (1) hide show
  1. routes/stylist.py +28 -28
routes/stylist.py CHANGED
@@ -3,7 +3,7 @@ import stripe
3
  import requests
4
  import logging
5
  import pytz
6
- from fastapi import APIRouter, HTTPException, Header
7
  from datetime import datetime, timedelta
8
  from dateutil.relativedelta import relativedelta
9
  from typing import List, Dict, Any
@@ -44,6 +44,20 @@ def verify_token(user_token: str) -> str:
44
  else:
45
  raise HTTPException(status_code=401, detail="Invalid or expired token")
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
48
  ny_timezone = pytz.timezone('America/New_York')
49
  now_ny = datetime.now(ny_timezone)
@@ -103,22 +117,10 @@ def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
103
  logger.error(f"❌ Error getting monthly revenue: {str(e)}")
104
  return list(monthly_data.values())
105
 
106
- def get_account_balance(account_id: str) -> Dict[str, Any]:
107
- try:
108
- balance = stripe.Balance.retrieve(stripe_account=account_id)
109
- available_balance = next((b.amount for b in balance.available if b.currency.upper() == "BRL"), 0)
110
- pending_balance = next((b.amount for b in balance.pending if b.currency.upper() == "BRL"), 0)
111
- return {
112
- "available_balance": available_balance,
113
- "pending_balance": pending_balance,
114
- "currency": "BRL"
115
- }
116
- except Exception as e:
117
- logger.error(f"❌ Error getting account balance: {str(e)}")
118
- return {"available_balance": 0, "pending_balance": 0, "currency": "BRL"}
119
-
120
- def get_active_subscribers(user_id: str) -> List[Dict[str, Any]]:
121
- url = f"{SUPABASE_URL}/rest/v1/Subscriptions?stylist_id=eq.{user_id}&active=eq.true&limit=30"
122
  response = requests.get(url, headers=SUPABASE_HEADERS)
123
  if response.status_code == 200:
124
  subscribers = response.json()
@@ -135,28 +137,26 @@ def get_active_subscribers(user_id: str) -> List[Dict[str, Any]]:
135
  "avatar": user_info.get("avatar"),
136
  "blurhash": user_info.get("blurhash")
137
  })
138
- return subscriber_list
139
- return []
 
 
 
140
 
141
  @router.get("/dashboard")
142
- def get_dashboard(user_token: str = Header(None, alias="User-key")):
143
  try:
144
- if not user_token:
145
- raise HTTPException(status_code=401, detail="Missing User-key header")
146
  user_id = verify_token(user_token)
147
  user_data_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
148
  response = requests.get(user_data_url, headers=SUPABASE_HEADERS)
149
- if response.status_code != 200 or not response.json():
150
- raise HTTPException(status_code=404, detail="User not found")
151
  user_data = response.json()[0]
152
  stripe_id = user_data.get("stripe_id")
 
153
  return {
154
  "stripe_id": stripe_id,
155
- "available_balance": get_account_balance(stripe_id)["available_balance"],
156
- "pending_balance": get_account_balance(stripe_id)["pending_balance"],
157
- "currency": get_account_balance(stripe_id)["currency"],
158
  "monthly_revenue": get_monthly_revenue(stripe_id),
159
- "subscribers": get_active_subscribers(user_id)
160
  }
161
  except Exception as e:
162
  logger.error(f"❌ Error: {str(e)}")
 
3
  import requests
4
  import logging
5
  import pytz
6
+ from fastapi import APIRouter, HTTPException, Header, Query
7
  from datetime import datetime, timedelta
8
  from dateutil.relativedelta import relativedelta
9
  from typing import List, Dict, Any
 
44
  else:
45
  raise HTTPException(status_code=401, detail="Invalid or expired token")
46
 
47
+ def get_account_balance(account_id: str) -> Dict[str, Any]:
48
+ try:
49
+ balance = stripe.Balance.retrieve(stripe_account=account_id)
50
+ available_balance = next((b.amount for b in balance.available if b.currency.upper() == "BRL"), 0)
51
+ pending_balance = next((b.amount for b in balance.pending if b.currency.upper() == "BRL"), 0)
52
+ return {
53
+ "available_balance": available_balance,
54
+ "pending_balance": pending_balance,
55
+ "currency": "BRL"
56
+ }
57
+ except Exception as e:
58
+ logger.error(f"❌ Error getting account balance: {str(e)}")
59
+ return {"available_balance": 0, "pending_balance": 0, "currency": "BRL"}
60
+
61
  def get_monthly_revenue(account_id: str) -> List[Dict[str, Any]]:
62
  ny_timezone = pytz.timezone('America/New_York')
63
  now_ny = datetime.now(ny_timezone)
 
117
  logger.error(f"❌ Error getting monthly revenue: {str(e)}")
118
  return list(monthly_data.values())
119
 
120
+ def get_active_subscribers(user_id: str, page: int) -> Dict[str, Any]:
121
+ limit = 30
122
+ offset = page * limit
123
+ url = f"{SUPABASE_URL}/rest/v1/Subscriptions?stylist_id=eq.{user_id}&active=eq.true&limit={limit}&offset={offset}"
 
 
 
 
 
 
 
 
 
 
 
 
124
  response = requests.get(url, headers=SUPABASE_HEADERS)
125
  if response.status_code == 200:
126
  subscribers = response.json()
 
137
  "avatar": user_info.get("avatar"),
138
  "blurhash": user_info.get("blurhash")
139
  })
140
+
141
+ has_next_page = len(subscribers) == limit
142
+ return {"subscribers": subscriber_list, "has_next_page": has_next_page}
143
+
144
+ return {"subscribers": [], "has_next_page": False}
145
 
146
  @router.get("/dashboard")
147
+ def get_dashboard(user_token: str = Header(None, alias="User-key"), page: int = Query(0, ge=0)):
148
  try:
 
 
149
  user_id = verify_token(user_token)
150
  user_data_url = f"{SUPABASE_URL}/rest/v1/User?id=eq.{user_id}"
151
  response = requests.get(user_data_url, headers=SUPABASE_HEADERS)
 
 
152
  user_data = response.json()[0]
153
  stripe_id = user_data.get("stripe_id")
154
+
155
  return {
156
  "stripe_id": stripe_id,
157
+ "available_balance": get_account_balance(stripe_id),
 
 
158
  "monthly_revenue": get_monthly_revenue(stripe_id),
159
+ **get_active_subscribers(user_id, page)
160
  }
161
  except Exception as e:
162
  logger.error(f"❌ Error: {str(e)}")